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 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 19 | import android.animation.LayoutTransition; |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 20 | import android.annotation.IdRes; |
Alan Viverette | 922e1c6 | 2015-05-05 17:18:27 -0700 | [diff] [blame] | 21 | import android.annotation.NonNull; |
Tor Norbye | 83c6896 | 2015-03-10 20:55:31 -0700 | [diff] [blame] | 22 | import android.annotation.UiThread; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | import android.content.Context; |
Clara Bayarri | d5bf3ed | 2015-03-27 17:32:45 +0000 | [diff] [blame] | 24 | import android.content.Intent; |
Adam Powell | ff0d298 | 2014-07-10 20:34:14 -0700 | [diff] [blame] | 25 | import android.content.pm.PackageManager; |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 26 | import android.content.res.Configuration; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | import android.content.res.TypedArray; |
| 28 | import android.graphics.Bitmap; |
| 29 | import android.graphics.Canvas; |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 30 | import android.graphics.Color; |
| 31 | import android.graphics.Insets; |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 32 | import android.graphics.Matrix; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | import android.graphics.Paint; |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 34 | import android.graphics.PointF; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | import android.graphics.Rect; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | import android.graphics.RectF; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 37 | import android.graphics.Region; |
Jeff Brown | 995e774 | 2010-12-22 16:59:36 -0800 | [diff] [blame] | 38 | import android.os.Build; |
Adam Powell | b6ab098 | 2015-01-07 17:00:12 -0800 | [diff] [blame] | 39 | import android.os.Bundle; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | import android.os.Parcelable; |
| 41 | import android.os.SystemClock; |
| 42 | import android.util.AttributeSet; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | import android.util.Log; |
Svetoslav Ganov | be922dc | 2012-11-30 16:46:26 -0800 | [diff] [blame] | 44 | import android.util.Pools.SynchronizedPool; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | import android.util.SparseArray; |
Dianne Hackborn | 6ff55fc | 2015-08-05 18:07:31 -0700 | [diff] [blame] | 46 | import android.util.SparseBooleanArray; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 47 | import android.view.accessibility.AccessibilityEvent; |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 48 | import android.view.accessibility.AccessibilityNodeInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | import android.view.animation.Animation; |
| 50 | import android.view.animation.AnimationUtils; |
| 51 | import android.view.animation.LayoutAnimationController; |
| 52 | import android.view.animation.Transformation; |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 53 | |
Romain Guy | 0211a0a | 2011-02-14 16:34:59 -0800 | [diff] [blame] | 54 | import com.android.internal.R; |
| 55 | import com.android.internal.util.Predicate; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | |
| 57 | import java.util.ArrayList; |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 58 | import java.util.Collections; |
Christopher Tate | 86cab1b | 2011-01-13 20:28:55 -0800 | [diff] [blame] | 59 | import java.util.HashSet; |
George Mount | e180337 | 2014-02-26 19:00:52 +0000 | [diff] [blame] | 60 | import java.util.List; |
| 61 | import java.util.Map; |
Keisuke Kuroyanagi | d85bc50 | 2016-01-21 14:50:38 +0900 | [diff] [blame] | 62 | |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 63 | import static android.os.Build.VERSION_CODES.JELLY_BEAN_MR1; |
| 64 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | /** |
| 66 | * <p> |
| 67 | * A <code>ViewGroup</code> is a special view that can contain other views |
| 68 | * (called children.) The view group is the base class for layouts and views |
| 69 | * containers. This class also defines the |
| 70 | * {@link android.view.ViewGroup.LayoutParams} class which serves as the base |
| 71 | * class for layouts parameters. |
| 72 | * </p> |
| 73 | * |
| 74 | * <p> |
| 75 | * Also see {@link LayoutParams} for layout attributes. |
| 76 | * </p> |
Romain Guy | d6a463a | 2009-05-21 23:10:10 -0700 | [diff] [blame] | 77 | * |
Joe Fernandez | 558459f | 2011-10-13 16:47:36 -0700 | [diff] [blame] | 78 | * <div class="special reference"> |
| 79 | * <h3>Developer Guides</h3> |
| 80 | * <p>For more information about creating user interface layouts, read the |
| 81 | * <a href="{@docRoot}guide/topics/ui/declaring-layout.html">XML Layouts</a> developer |
| 82 | * guide.</p></div> |
| 83 | * |
Dianne Hackborn | 7caab0f | 2013-03-06 13:47:04 -0800 | [diff] [blame] | 84 | * <p>Here is a complete implementation of a custom ViewGroup that implements |
| 85 | * a simple {@link android.widget.FrameLayout} along with the ability to stack |
| 86 | * children in left and right gutters.</p> |
| 87 | * |
| 88 | * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/CustomLayout.java |
| 89 | * Complete} |
| 90 | * |
| 91 | * <p>If you are implementing XML layout attributes as shown in the example, this is the |
| 92 | * corresponding definition for them that would go in <code>res/values/attrs.xml</code>:</p> |
| 93 | * |
| 94 | * {@sample development/samples/ApiDemos/res/values/attrs.xml CustomLayout} |
| 95 | * |
| 96 | * <p>Finally the layout manager can be used in an XML layout like so:</p> |
| 97 | * |
| 98 | * {@sample development/samples/ApiDemos/res/layout/custom_layout.xml Complete} |
| 99 | * |
Romain Guy | d6a463a | 2009-05-21 23:10:10 -0700 | [diff] [blame] | 100 | * @attr ref android.R.styleable#ViewGroup_clipChildren |
| 101 | * @attr ref android.R.styleable#ViewGroup_clipToPadding |
| 102 | * @attr ref android.R.styleable#ViewGroup_layoutAnimation |
| 103 | * @attr ref android.R.styleable#ViewGroup_animationCache |
| 104 | * @attr ref android.R.styleable#ViewGroup_persistentDrawingCache |
| 105 | * @attr ref android.R.styleable#ViewGroup_alwaysDrawnWithCache |
| 106 | * @attr ref android.R.styleable#ViewGroup_addStatesFromChildren |
| 107 | * @attr ref android.R.styleable#ViewGroup_descendantFocusability |
Chet Haase | 13cc120 | 2010-09-03 15:39:20 -0700 | [diff] [blame] | 108 | * @attr ref android.R.styleable#ViewGroup_animateLayoutChanges |
Scott Main | 27a8508 | 2013-06-10 10:39:48 -0700 | [diff] [blame] | 109 | * @attr ref android.R.styleable#ViewGroup_splitMotionEvents |
| 110 | * @attr ref android.R.styleable#ViewGroup_layoutMode |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 111 | */ |
Tor Norbye | 83c6896 | 2015-03-10 20:55:31 -0700 | [diff] [blame] | 112 | @UiThread |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 113 | public abstract class ViewGroup extends View implements ViewParent, ViewManager { |
Adam Powell | 539ee87 | 2012-02-03 19:00:49 -0800 | [diff] [blame] | 114 | private static final String TAG = "ViewGroup"; |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 115 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 116 | private static final boolean DBG = false; |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 117 | /** @hide */ |
| 118 | public static boolean DEBUG_DRAW = false; |
Gilles Debunne | cea4513 | 2011-11-24 02:19:27 +0100 | [diff] [blame] | 119 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 120 | /** |
| 121 | * Views which have been hidden or removed which need to be animated on |
| 122 | * their way out. |
| 123 | * This field should be made private, so it is hidden from the SDK. |
| 124 | * {@hide} |
| 125 | */ |
| 126 | protected ArrayList<View> mDisappearingChildren; |
| 127 | |
| 128 | /** |
| 129 | * Listener used to propagate events indicating when children are added |
| 130 | * and/or removed from a view group. |
| 131 | * This field should be made private, so it is hidden from the SDK. |
| 132 | * {@hide} |
| 133 | */ |
| 134 | protected OnHierarchyChangeListener mOnHierarchyChangeListener; |
| 135 | |
| 136 | // The view contained within this ViewGroup that has or contains focus. |
| 137 | private View mFocused; |
| 138 | |
Chet Haase | 4846032 | 2010-06-11 14:22:25 -0700 | [diff] [blame] | 139 | /** |
| 140 | * A Transformation used when drawing children, to |
| 141 | * apply on the child being drawn. |
| 142 | */ |
Romain Guy | f699130 | 2013-06-05 17:19:01 -0700 | [diff] [blame] | 143 | private Transformation mChildTransformation; |
Chet Haase | 4846032 | 2010-06-11 14:22:25 -0700 | [diff] [blame] | 144 | |
| 145 | /** |
| 146 | * Used to track the current invalidation region. |
| 147 | */ |
Chet Haase | 64a48c1 | 2012-02-13 16:33:29 -0800 | [diff] [blame] | 148 | RectF mInvalidateRegion; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 149 | |
Chet Haase | 4846032 | 2010-06-11 14:22:25 -0700 | [diff] [blame] | 150 | /** |
| 151 | * A Transformation used to calculate a correct |
| 152 | * invalidation area when the application is autoscaled. |
| 153 | */ |
Chet Haase | 64a48c1 | 2012-02-13 16:33:29 -0800 | [diff] [blame] | 154 | Transformation mInvalidationTransformation; |
Chet Haase | 4846032 | 2010-06-11 14:22:25 -0700 | [diff] [blame] | 155 | |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 156 | // View currently under an ongoing drag. Can be null, a child or this window. |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 157 | private View mCurrentDragView; |
| 158 | |
Christopher Tate | 86cab1b | 2011-01-13 20:28:55 -0800 | [diff] [blame] | 159 | // Metadata about the ongoing drag |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 160 | private DragEvent mCurrentDragStartEvent; |
| 161 | private boolean mIsInterestedInDrag; |
| 162 | private HashSet<View> mChildrenInterestedInDrag; |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 163 | |
| 164 | // Used during drag dispatch |
Romain Guy | 6410c0a | 2013-06-17 11:21:58 -0700 | [diff] [blame] | 165 | private PointF mLocalPoint; |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 166 | |
Alan Viverette | b942b6f | 2014-12-08 10:37:39 -0800 | [diff] [blame] | 167 | // Lazily-created holder for point computations. |
| 168 | private float[] mTempPoint; |
| 169 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 170 | // Layout animation |
| 171 | private LayoutAnimationController mLayoutAnimationController; |
| 172 | private Animation.AnimationListener mAnimationListener; |
| 173 | |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 174 | // First touch target in the linked list of touch targets. |
| 175 | private TouchTarget mFirstTouchTarget; |
| 176 | |
Joe Onorato | 03ab0c7 | 2011-01-06 15:46:27 -0800 | [diff] [blame] | 177 | // For debugging only. You can see these in hierarchyviewer. |
Romain Guy | e95003e | 2011-01-09 13:53:06 -0800 | [diff] [blame] | 178 | @SuppressWarnings({"FieldCanBeLocal", "UnusedDeclaration"}) |
Joe Onorato | 03ab0c7 | 2011-01-06 15:46:27 -0800 | [diff] [blame] | 179 | @ViewDebug.ExportedProperty(category = "events") |
| 180 | private long mLastTouchDownTime; |
| 181 | @ViewDebug.ExportedProperty(category = "events") |
| 182 | private int mLastTouchDownIndex = -1; |
Romain Guy | e95003e | 2011-01-09 13:53:06 -0800 | [diff] [blame] | 183 | @SuppressWarnings({"FieldCanBeLocal", "UnusedDeclaration"}) |
Joe Onorato | 03ab0c7 | 2011-01-06 15:46:27 -0800 | [diff] [blame] | 184 | @ViewDebug.ExportedProperty(category = "events") |
| 185 | private float mLastTouchDownX; |
Romain Guy | e95003e | 2011-01-09 13:53:06 -0800 | [diff] [blame] | 186 | @SuppressWarnings({"FieldCanBeLocal", "UnusedDeclaration"}) |
Joe Onorato | 03ab0c7 | 2011-01-06 15:46:27 -0800 | [diff] [blame] | 187 | @ViewDebug.ExportedProperty(category = "events") |
| 188 | private float mLastTouchDownY; |
| 189 | |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 190 | // First hover target in the linked list of hover targets. |
| 191 | // The hover targets are children which have received ACTION_HOVER_ENTER. |
| 192 | // They might not have actually handled the hover event, but we will |
| 193 | // continue sending hover events to them as long as the pointer remains over |
| 194 | // their bounds and the view group does not intercept hover. |
| 195 | private HoverTarget mFirstHoverTarget; |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 196 | |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 197 | // True if the view group itself received a hover event. |
| 198 | // It might not have actually handled the hover event. |
| 199 | private boolean mHoveredSelf; |
| 200 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 201 | /** |
| 202 | * Internal flags. |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 203 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 204 | * This field should be made private, so it is hidden from the SDK. |
| 205 | * {@hide} |
| 206 | */ |
Romain Guy | 2440e67 | 2012-08-07 14:43:43 -0700 | [diff] [blame] | 207 | @ViewDebug.ExportedProperty(flagMapping = { |
| 208 | @ViewDebug.FlagToString(mask = FLAG_CLIP_CHILDREN, equals = FLAG_CLIP_CHILDREN, |
| 209 | name = "CLIP_CHILDREN"), |
| 210 | @ViewDebug.FlagToString(mask = FLAG_CLIP_TO_PADDING, equals = FLAG_CLIP_TO_PADDING, |
| 211 | name = "CLIP_TO_PADDING"), |
| 212 | @ViewDebug.FlagToString(mask = FLAG_PADDING_NOT_NULL, equals = FLAG_PADDING_NOT_NULL, |
| 213 | name = "PADDING_NOT_NULL") |
Jon Miranda | 4597e98 | 2014-07-29 07:25:49 -0700 | [diff] [blame] | 214 | }, formatToHexString = true) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 215 | protected int mGroupFlags; |
| 216 | |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 217 | /** |
| 218 | * Either {@link #LAYOUT_MODE_CLIP_BOUNDS} or {@link #LAYOUT_MODE_OPTICAL_BOUNDS}. |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 219 | */ |
Philip Milne | cfb631b | 2012-10-26 10:51:46 -0700 | [diff] [blame] | 220 | private int mLayoutMode = LAYOUT_MODE_UNDEFINED; |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 221 | |
Romain Guy | 33f6beb | 2012-02-16 19:24:51 -0800 | [diff] [blame] | 222 | /** |
| 223 | * NOTE: If you change the flags below make sure to reflect the changes |
| 224 | * the DisplayList class |
| 225 | */ |
Filip Gruszczynski | a33bdf3 | 2015-11-19 18:22:16 -0800 | [diff] [blame] | 226 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 227 | // When set, ViewGroup invalidates only the child's rectangle |
| 228 | // Set by default |
Chet Haase | 64a48c1 | 2012-02-13 16:33:29 -0800 | [diff] [blame] | 229 | static final int FLAG_CLIP_CHILDREN = 0x1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 230 | |
| 231 | // When set, ViewGroup excludes the padding area from the invalidate rectangle |
| 232 | // Set by default |
| 233 | private static final int FLAG_CLIP_TO_PADDING = 0x2; |
| 234 | |
| 235 | // When set, dispatchDraw() will invoke invalidate(); this is set by drawChild() when |
| 236 | // a child needs to be invalidated and FLAG_OPTIMIZE_INVALIDATE is set |
Chet Haase | 64a48c1 | 2012-02-13 16:33:29 -0800 | [diff] [blame] | 237 | static final int FLAG_INVALIDATE_REQUIRED = 0x4; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 238 | |
| 239 | // When set, dispatchDraw() will run the layout animation and unset the flag |
| 240 | private static final int FLAG_RUN_ANIMATION = 0x8; |
| 241 | |
| 242 | // When set, there is either no layout animation on the ViewGroup or the layout |
| 243 | // animation is over |
| 244 | // Set by default |
Chet Haase | 64a48c1 | 2012-02-13 16:33:29 -0800 | [diff] [blame] | 245 | static final int FLAG_ANIMATION_DONE = 0x10; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 246 | |
| 247 | // If set, this ViewGroup has padding; if unset there is no padding and we don't need |
| 248 | // to clip it, even if FLAG_CLIP_TO_PADDING is set |
| 249 | private static final int FLAG_PADDING_NOT_NULL = 0x20; |
| 250 | |
Chris Craik | 5a6bbae | 2015-04-10 17:41:34 -0700 | [diff] [blame] | 251 | /** @deprecated - functionality removed */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 252 | private static final int FLAG_ANIMATION_CACHE = 0x40; |
| 253 | |
| 254 | // When set, this ViewGroup converts calls to invalidate(Rect) to invalidate() during a |
| 255 | // layout animation; this avoid clobbering the hierarchy |
| 256 | // Automatically set when the layout animation starts, depending on the animation's |
| 257 | // characteristics |
Chet Haase | 64a48c1 | 2012-02-13 16:33:29 -0800 | [diff] [blame] | 258 | static final int FLAG_OPTIMIZE_INVALIDATE = 0x80; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 259 | |
| 260 | // When set, the next call to drawChild() will clear mChildTransformation's matrix |
Chet Haase | 64a48c1 | 2012-02-13 16:33:29 -0800 | [diff] [blame] | 261 | static final int FLAG_CLEAR_TRANSFORMATION = 0x100; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 262 | |
| 263 | // When set, this ViewGroup invokes mAnimationListener.onAnimationEnd() and removes |
| 264 | // the children's Bitmap caches if necessary |
| 265 | // This flag is set when the layout animation is over (after FLAG_ANIMATION_DONE is set) |
| 266 | private static final int FLAG_NOTIFY_ANIMATION_LISTENER = 0x200; |
| 267 | |
| 268 | /** |
| 269 | * When set, the drawing method will call {@link #getChildDrawingOrder(int, int)} |
| 270 | * to get the index of the child to draw for that iteration. |
Filip Gruszczynski | a33bdf3 | 2015-11-19 18:22:16 -0800 | [diff] [blame] | 271 | * |
Romain Guy | 293451e | 2009-11-04 13:59:48 -0800 | [diff] [blame] | 272 | * @hide |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 273 | */ |
| 274 | protected static final int FLAG_USE_CHILD_DRAWING_ORDER = 0x400; |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 275 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 276 | /** |
| 277 | * When set, this ViewGroup supports static transformations on children; this causes |
| 278 | * {@link #getChildStaticTransformation(View, android.view.animation.Transformation)} to be |
| 279 | * invoked when a child is drawn. |
| 280 | * |
| 281 | * Any subclass overriding |
| 282 | * {@link #getChildStaticTransformation(View, android.view.animation.Transformation)} should |
| 283 | * set this flags in {@link #mGroupFlags}. |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 284 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 285 | * {@hide} |
| 286 | */ |
| 287 | protected static final int FLAG_SUPPORT_STATIC_TRANSFORMATIONS = 0x800; |
| 288 | |
John Reck | fb5899d | 2014-08-15 18:51:27 -0700 | [diff] [blame] | 289 | // UNUSED FLAG VALUE: 0x1000; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 290 | |
| 291 | /** |
| 292 | * When set, this ViewGroup's drawable states also include those |
| 293 | * of its children. |
| 294 | */ |
| 295 | private static final int FLAG_ADD_STATES_FROM_CHILDREN = 0x2000; |
| 296 | |
Chris Craik | 5a6bbae | 2015-04-10 17:41:34 -0700 | [diff] [blame] | 297 | /** @deprecated functionality removed */ |
| 298 | 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] | 299 | |
Chris Craik | 5a6bbae | 2015-04-10 17:41:34 -0700 | [diff] [blame] | 300 | /** @deprecated functionality removed */ |
| 301 | 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] | 302 | |
| 303 | /** |
| 304 | * When set, this group will go through its list of children to notify them of |
| 305 | * any drawable state change. |
| 306 | */ |
| 307 | private static final int FLAG_NOTIFY_CHILDREN_ON_DRAWABLE_STATE_CHANGE = 0x10000; |
| 308 | |
| 309 | private static final int FLAG_MASK_FOCUSABILITY = 0x60000; |
| 310 | |
| 311 | /** |
| 312 | * This view will get focus before any of its descendants. |
| 313 | */ |
| 314 | public static final int FOCUS_BEFORE_DESCENDANTS = 0x20000; |
| 315 | |
| 316 | /** |
| 317 | * This view will get focus only if none of its descendants want it. |
| 318 | */ |
| 319 | public static final int FOCUS_AFTER_DESCENDANTS = 0x40000; |
| 320 | |
| 321 | /** |
| 322 | * This view will block any of its descendants from getting focus, even |
| 323 | * if they are focusable. |
| 324 | */ |
| 325 | public static final int FOCUS_BLOCK_DESCENDANTS = 0x60000; |
| 326 | |
| 327 | /** |
| 328 | * Used to map between enum in attrubutes and flag values. |
| 329 | */ |
| 330 | private static final int[] DESCENDANT_FOCUSABILITY_FLAGS = |
| 331 | {FOCUS_BEFORE_DESCENDANTS, FOCUS_AFTER_DESCENDANTS, |
| 332 | FOCUS_BLOCK_DESCENDANTS}; |
| 333 | |
| 334 | /** |
| 335 | * When set, this ViewGroup should not intercept touch events. |
Adam Powell | 110486f | 2010-06-22 17:14:44 -0700 | [diff] [blame] | 336 | * {@hide} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 337 | */ |
Adam Powell | 110486f | 2010-06-22 17:14:44 -0700 | [diff] [blame] | 338 | protected static final int FLAG_DISALLOW_INTERCEPT = 0x80000; |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 339 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 340 | /** |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 341 | * When set, this ViewGroup will split MotionEvents to multiple child Views when appropriate. |
| 342 | */ |
Adam Powell | f37df07 | 2010-09-17 16:22:49 -0700 | [diff] [blame] | 343 | private static final int FLAG_SPLIT_MOTION_EVENTS = 0x200000; |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 344 | |
| 345 | /** |
Adam Powell | 4b86788 | 2011-09-16 12:59:46 -0700 | [diff] [blame] | 346 | * When set, this ViewGroup will not dispatch onAttachedToWindow calls |
| 347 | * to children when adding new views. This is used to prevent multiple |
| 348 | * onAttached calls when a ViewGroup adds children in its own onAttached method. |
| 349 | */ |
| 350 | private static final int FLAG_PREVENT_DISPATCH_ATTACHED_TO_WINDOW = 0x400000; |
| 351 | |
| 352 | /** |
Philip Milne | cfb631b | 2012-10-26 10:51:46 -0700 | [diff] [blame] | 353 | * When true, indicates that a layoutMode has been explicitly set, either with |
| 354 | * an explicit call to {@link #setLayoutMode(int)} in code or from an XML resource. |
| 355 | * This distinguishes the situation in which a layout mode was inherited from |
| 356 | * one of the ViewGroup's ancestors and cached locally. |
| 357 | */ |
| 358 | private static final int FLAG_LAYOUT_MODE_WAS_EXPLICITLY_SET = 0x800000; |
| 359 | |
Chris Craik | b49f446 | 2014-03-20 12:44:20 -0700 | [diff] [blame] | 360 | static final int FLAG_IS_TRANSITION_GROUP = 0x1000000; |
Chris Craik | d863a10 | 2013-12-19 13:31:15 -0800 | [diff] [blame] | 361 | |
Chris Craik | b49f446 | 2014-03-20 12:44:20 -0700 | [diff] [blame] | 362 | static final int FLAG_IS_TRANSITION_GROUP_SET = 0x2000000; |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 363 | |
Chris Craik | d863a10 | 2013-12-19 13:31:15 -0800 | [diff] [blame] | 364 | /** |
Adam Powell | ff0d298 | 2014-07-10 20:34:14 -0700 | [diff] [blame] | 365 | * When set, focus will not be permitted to enter this group if a touchscreen is present. |
| 366 | */ |
| 367 | static final int FLAG_TOUCHSCREEN_BLOCKS_FOCUS = 0x4000000; |
| 368 | |
| 369 | /** |
Clara Bayarri | 4423d91 | 2015-03-02 19:42:48 +0000 | [diff] [blame] | 370 | * When true, indicates that a call to startActionModeForChild was made with the type parameter |
| 371 | * and should not be ignored. This helps in backwards compatibility with the existing method |
| 372 | * without a type. |
| 373 | * |
| 374 | * @see #startActionModeForChild(View, android.view.ActionMode.Callback) |
| 375 | * @see #startActionModeForChild(View, android.view.ActionMode.Callback, int) |
| 376 | */ |
| 377 | private static final int FLAG_START_ACTION_MODE_FOR_CHILD_IS_TYPED = 0x8000000; |
| 378 | |
| 379 | /** |
| 380 | * When true, indicates that a call to startActionModeForChild was made without the type |
| 381 | * parameter. This helps in backwards compatibility with the existing method |
| 382 | * without a type. |
| 383 | * |
| 384 | * @see #startActionModeForChild(View, android.view.ActionMode.Callback) |
| 385 | * @see #startActionModeForChild(View, android.view.ActionMode.Callback, int) |
| 386 | */ |
| 387 | private static final int FLAG_START_ACTION_MODE_FOR_CHILD_IS_NOT_TYPED = 0x10000000; |
| 388 | |
| 389 | /** |
Adam Powell | 2af189a | 2016-02-05 15:52:02 -0800 | [diff] [blame] | 390 | * When set, indicates that a call to showContextMenuForChild was made with explicit |
| 391 | * coordinates within the initiating child view. |
| 392 | */ |
| 393 | private static final int FLAG_SHOW_CONTEXT_MENU_WITH_COORDS = 0x20000000; |
| 394 | |
| 395 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 396 | * Indicates which types of drawing caches are to be kept in memory. |
| 397 | * This field should be made private, so it is hidden from the SDK. |
| 398 | * {@hide} |
| 399 | */ |
| 400 | protected int mPersistentDrawingCache; |
| 401 | |
| 402 | /** |
| 403 | * Used to indicate that no drawing cache should be kept in memory. |
| 404 | */ |
| 405 | public static final int PERSISTENT_NO_CACHE = 0x0; |
| 406 | |
| 407 | /** |
| 408 | * Used to indicate that the animation drawing cache should be kept in memory. |
| 409 | */ |
| 410 | public static final int PERSISTENT_ANIMATION_CACHE = 0x1; |
| 411 | |
| 412 | /** |
| 413 | * Used to indicate that the scrolling drawing cache should be kept in memory. |
| 414 | */ |
| 415 | public static final int PERSISTENT_SCROLLING_CACHE = 0x2; |
| 416 | |
| 417 | /** |
| 418 | * Used to indicate that all drawing caches should be kept in memory. |
| 419 | */ |
| 420 | public static final int PERSISTENT_ALL_CACHES = 0x3; |
| 421 | |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 422 | // Layout Modes |
| 423 | |
Philip Milne | cfb631b | 2012-10-26 10:51:46 -0700 | [diff] [blame] | 424 | private static final int LAYOUT_MODE_UNDEFINED = -1; |
| 425 | |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 426 | /** |
| 427 | * This constant is a {@link #setLayoutMode(int) layoutMode}. |
Philip Milne | 7a23b49 | 2012-04-24 22:12:36 -0700 | [diff] [blame] | 428 | * 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] | 429 | * {@link #getRight() right} and {@link #getBottom() bottom}. |
| 430 | */ |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 431 | public static final int LAYOUT_MODE_CLIP_BOUNDS = 0; |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 432 | |
| 433 | /** |
| 434 | * This constant is a {@link #setLayoutMode(int) layoutMode}. |
Philip Milne | 7a23b49 | 2012-04-24 22:12:36 -0700 | [diff] [blame] | 435 | * Optical bounds describe where a widget appears to be. They sit inside the clip |
| 436 | * bounds which need to cover a larger area to allow other effects, |
| 437 | * such as shadows and glows, to be drawn. |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 438 | */ |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 439 | public static final int LAYOUT_MODE_OPTICAL_BOUNDS = 1; |
| 440 | |
| 441 | /** @hide */ |
Philip Milne | cfb631b | 2012-10-26 10:51:46 -0700 | [diff] [blame] | 442 | public static int LAYOUT_MODE_DEFAULT = LAYOUT_MODE_CLIP_BOUNDS; |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 443 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 444 | /** |
| 445 | * We clip to padding when FLAG_CLIP_TO_PADDING and FLAG_PADDING_NOT_NULL |
| 446 | * are set at the same time. |
| 447 | */ |
| 448 | protected static final int CLIP_TO_PADDING_MASK = FLAG_CLIP_TO_PADDING | FLAG_PADDING_NOT_NULL; |
| 449 | |
| 450 | // Index of the child's left position in the mLocation array |
| 451 | private static final int CHILD_LEFT_INDEX = 0; |
| 452 | // Index of the child's top position in the mLocation array |
| 453 | private static final int CHILD_TOP_INDEX = 1; |
| 454 | |
| 455 | // Child views of this ViewGroup |
| 456 | private View[] mChildren; |
| 457 | // Number of valid children in the mChildren array, the rest should be null or not |
| 458 | // considered as children |
| 459 | private int mChildrenCount; |
| 460 | |
Chet Haase | b989502 | 2013-04-02 15:10:58 -0700 | [diff] [blame] | 461 | // Whether layout calls are currently being suppressed, controlled by calls to |
| 462 | // suppressLayout() |
| 463 | boolean mSuppressLayout = false; |
| 464 | |
| 465 | // Whether any layout calls have actually been suppressed while mSuppressLayout |
| 466 | // has been true. This tracks whether we need to issue a requestLayout() when |
| 467 | // layout is later re-enabled. |
| 468 | private boolean mLayoutCalledWhileSuppressed = false; |
| 469 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 470 | private static final int ARRAY_INITIAL_CAPACITY = 12; |
| 471 | private static final int ARRAY_CAPACITY_INCREMENT = 12; |
| 472 | |
Romain Guy | cbc6774 | 2012-04-27 16:12:57 -0700 | [diff] [blame] | 473 | private static Paint sDebugPaint; |
| 474 | private static float[] sDebugLines; |
Philip Milne | 604f440 | 2012-04-24 19:27:11 -0700 | [diff] [blame] | 475 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 476 | // Used to draw cached views |
Chet Haase | 64a48c1 | 2012-02-13 16:33:29 -0800 | [diff] [blame] | 477 | Paint mCachePaint; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 478 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 479 | // Used to animate add/remove changes in layout |
| 480 | private LayoutTransition mTransition; |
| 481 | |
| 482 | // The set of views that are currently being transitioned. This list is used to track views |
| 483 | // being removed that should not actually be removed from the parent yet because they are |
| 484 | // being animated. |
| 485 | private ArrayList<View> mTransitioningViews; |
| 486 | |
Chet Haase | 5e25c2c | 2010-09-16 11:15:56 -0700 | [diff] [blame] | 487 | // List of children changing visibility. This is used to potentially keep rendering |
| 488 | // views during a transition when they otherwise would have become gone/invisible |
| 489 | private ArrayList<View> mVisibilityChangingChildren; |
| 490 | |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 491 | // Temporary holder of presorted children, only used for |
| 492 | // input/software draw dispatch for correctly Z ordering. |
| 493 | private ArrayList<View> mPreSortedChildren; |
| 494 | |
Adam Powell | 539ee87 | 2012-02-03 19:00:49 -0800 | [diff] [blame] | 495 | // Indicates how many of this container's child subtrees contain transient state |
| 496 | @ViewDebug.ExportedProperty(category = "layout") |
| 497 | private int mChildCountWithTransientState = 0; |
| 498 | |
Adam Powell | 10ba277 | 2014-04-15 09:46:51 -0700 | [diff] [blame] | 499 | /** |
| 500 | * Currently registered axes for nested scrolling. Flag set consisting of |
| 501 | * {@link #SCROLL_AXIS_HORIZONTAL} {@link #SCROLL_AXIS_VERTICAL} or {@link #SCROLL_AXIS_NONE} |
| 502 | * for null. |
| 503 | */ |
| 504 | private int mNestedScrollAxes; |
| 505 | |
Chet Haase | c633d2f | 2015-04-07 10:29:39 -0700 | [diff] [blame] | 506 | // Used to manage the list of transient views, added by addTransientView() |
| 507 | private List<Integer> mTransientIndices = null; |
| 508 | private List<View> mTransientViews = null; |
| 509 | |
| 510 | |
Clara Bayarri | 4423d91 | 2015-03-02 19:42:48 +0000 | [diff] [blame] | 511 | /** |
| 512 | * Empty ActionMode used as a sentinel in recursive entries to startActionModeForChild. |
| 513 | * |
| 514 | * @see #startActionModeForChild(View, android.view.ActionMode.Callback) |
| 515 | * @see #startActionModeForChild(View, android.view.ActionMode.Callback, int) |
| 516 | */ |
| 517 | private static final ActionMode SENTINEL_ACTION_MODE = new ActionMode() { |
| 518 | @Override |
| 519 | public void setTitle(CharSequence title) {} |
| 520 | |
| 521 | @Override |
| 522 | public void setTitle(int resId) {} |
| 523 | |
| 524 | @Override |
| 525 | public void setSubtitle(CharSequence subtitle) {} |
| 526 | |
| 527 | @Override |
| 528 | public void setSubtitle(int resId) {} |
| 529 | |
| 530 | @Override |
| 531 | public void setCustomView(View view) {} |
| 532 | |
| 533 | @Override |
| 534 | public void invalidate() {} |
| 535 | |
| 536 | @Override |
| 537 | public void finish() {} |
| 538 | |
| 539 | @Override |
| 540 | public Menu getMenu() { |
| 541 | return null; |
| 542 | } |
| 543 | |
| 544 | @Override |
| 545 | public CharSequence getTitle() { |
| 546 | return null; |
| 547 | } |
| 548 | |
| 549 | @Override |
| 550 | public CharSequence getSubtitle() { |
| 551 | return null; |
| 552 | } |
| 553 | |
| 554 | @Override |
| 555 | public View getCustomView() { |
| 556 | return null; |
| 557 | } |
| 558 | |
| 559 | @Override |
| 560 | public MenuInflater getMenuInflater() { |
| 561 | return null; |
| 562 | } |
| 563 | }; |
| 564 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 565 | public ViewGroup(Context context) { |
Alan Viverette | d6479ec | 2013-09-10 17:03:02 -0700 | [diff] [blame] | 566 | this(context, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | public ViewGroup(Context context, AttributeSet attrs) { |
Alan Viverette | d6479ec | 2013-09-10 17:03:02 -0700 | [diff] [blame] | 570 | this(context, attrs, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 571 | } |
| 572 | |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 573 | public ViewGroup(Context context, AttributeSet attrs, int defStyleAttr) { |
Alan Viverette | d6479ec | 2013-09-10 17:03:02 -0700 | [diff] [blame] | 574 | this(context, attrs, defStyleAttr, 0); |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | public ViewGroup(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 578 | super(context, attrs, defStyleAttr, defStyleRes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 579 | initViewGroup(); |
Alan Viverette | d6479ec | 2013-09-10 17:03:02 -0700 | [diff] [blame] | 580 | initFromAttributes(context, attrs, defStyleAttr, defStyleRes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 581 | } |
| 582 | |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 583 | private boolean debugDraw() { |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 584 | return DEBUG_DRAW || mAttachInfo != null && mAttachInfo.mDebugLayout; |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 585 | } |
| 586 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 587 | private void initViewGroup() { |
| 588 | // ViewGroup doesn't draw by default |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 589 | if (!debugDraw()) { |
| 590 | setFlags(WILL_NOT_DRAW, DRAW_MASK); |
| 591 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 592 | mGroupFlags |= FLAG_CLIP_CHILDREN; |
| 593 | mGroupFlags |= FLAG_CLIP_TO_PADDING; |
| 594 | mGroupFlags |= FLAG_ANIMATION_DONE; |
Chris Craik | f6ce8fd | 2015-05-11 15:33:11 -0700 | [diff] [blame] | 595 | mGroupFlags |= FLAG_ANIMATION_CACHE; |
| 596 | mGroupFlags |= FLAG_ALWAYS_DRAWN_WITH_CACHE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 597 | |
Jeff Brown | 995e774 | 2010-12-22 16:59:36 -0800 | [diff] [blame] | 598 | if (mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.HONEYCOMB) { |
| 599 | mGroupFlags |= FLAG_SPLIT_MOTION_EVENTS; |
| 600 | } |
| 601 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 602 | setDescendantFocusability(FOCUS_BEFORE_DESCENDANTS); |
| 603 | |
| 604 | mChildren = new View[ARRAY_INITIAL_CAPACITY]; |
| 605 | mChildrenCount = 0; |
| 606 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 607 | mPersistentDrawingCache = PERSISTENT_SCROLLING_CACHE; |
| 608 | } |
| 609 | |
Alan Viverette | d6479ec | 2013-09-10 17:03:02 -0700 | [diff] [blame] | 610 | private void initFromAttributes( |
| 611 | Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
Filip Gruszczyński | b50cea0 | 2014-03-05 17:54:58 -0800 | [diff] [blame] | 612 | final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ViewGroup, defStyleAttr, |
| 613 | defStyleRes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 614 | |
| 615 | final int N = a.getIndexCount(); |
| 616 | for (int i = 0; i < N; i++) { |
| 617 | int attr = a.getIndex(i); |
| 618 | switch (attr) { |
| 619 | case R.styleable.ViewGroup_clipChildren: |
| 620 | setClipChildren(a.getBoolean(attr, true)); |
| 621 | break; |
| 622 | case R.styleable.ViewGroup_clipToPadding: |
| 623 | setClipToPadding(a.getBoolean(attr, true)); |
| 624 | break; |
| 625 | case R.styleable.ViewGroup_animationCache: |
| 626 | setAnimationCacheEnabled(a.getBoolean(attr, true)); |
| 627 | break; |
| 628 | case R.styleable.ViewGroup_persistentDrawingCache: |
| 629 | setPersistentDrawingCache(a.getInt(attr, PERSISTENT_SCROLLING_CACHE)); |
| 630 | break; |
| 631 | case R.styleable.ViewGroup_addStatesFromChildren: |
| 632 | setAddStatesFromChildren(a.getBoolean(attr, false)); |
| 633 | break; |
| 634 | case R.styleable.ViewGroup_alwaysDrawnWithCache: |
| 635 | setAlwaysDrawnWithCacheEnabled(a.getBoolean(attr, true)); |
| 636 | break; |
| 637 | case R.styleable.ViewGroup_layoutAnimation: |
| 638 | int id = a.getResourceId(attr, -1); |
| 639 | if (id > 0) { |
| 640 | setLayoutAnimation(AnimationUtils.loadLayoutAnimation(mContext, id)); |
| 641 | } |
| 642 | break; |
| 643 | case R.styleable.ViewGroup_descendantFocusability: |
| 644 | setDescendantFocusability(DESCENDANT_FOCUSABILITY_FLAGS[a.getInt(attr, 0)]); |
| 645 | break; |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 646 | case R.styleable.ViewGroup_splitMotionEvents: |
| 647 | setMotionEventSplittingEnabled(a.getBoolean(attr, false)); |
| 648 | break; |
Chet Haase | 13cc120 | 2010-09-03 15:39:20 -0700 | [diff] [blame] | 649 | case R.styleable.ViewGroup_animateLayoutChanges: |
| 650 | boolean animateLayoutChanges = a.getBoolean(attr, false); |
| 651 | if (animateLayoutChanges) { |
| 652 | setLayoutTransition(new LayoutTransition()); |
| 653 | } |
| 654 | break; |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 655 | case R.styleable.ViewGroup_layoutMode: |
Philip Milne | cfb631b | 2012-10-26 10:51:46 -0700 | [diff] [blame] | 656 | setLayoutMode(a.getInt(attr, LAYOUT_MODE_UNDEFINED)); |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 657 | break; |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 658 | case R.styleable.ViewGroup_transitionGroup: |
| 659 | setTransitionGroup(a.getBoolean(attr, false)); |
| 660 | break; |
Adam Powell | ff0d298 | 2014-07-10 20:34:14 -0700 | [diff] [blame] | 661 | case R.styleable.ViewGroup_touchscreenBlocksFocus: |
| 662 | setTouchscreenBlocksFocus(a.getBoolean(attr, false)); |
| 663 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 664 | } |
| 665 | } |
| 666 | |
| 667 | a.recycle(); |
| 668 | } |
| 669 | |
| 670 | /** |
| 671 | * Gets the descendant focusability of this view group. The descendant |
| 672 | * focusability defines the relationship between this view group and its |
| 673 | * descendants when looking for a view to take focus in |
| 674 | * {@link #requestFocus(int, android.graphics.Rect)}. |
| 675 | * |
| 676 | * @return one of {@link #FOCUS_BEFORE_DESCENDANTS}, {@link #FOCUS_AFTER_DESCENDANTS}, |
| 677 | * {@link #FOCUS_BLOCK_DESCENDANTS}. |
| 678 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 679 | @ViewDebug.ExportedProperty(category = "focus", mapping = { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 680 | @ViewDebug.IntToString(from = FOCUS_BEFORE_DESCENDANTS, to = "FOCUS_BEFORE_DESCENDANTS"), |
| 681 | @ViewDebug.IntToString(from = FOCUS_AFTER_DESCENDANTS, to = "FOCUS_AFTER_DESCENDANTS"), |
| 682 | @ViewDebug.IntToString(from = FOCUS_BLOCK_DESCENDANTS, to = "FOCUS_BLOCK_DESCENDANTS") |
| 683 | }) |
| 684 | public int getDescendantFocusability() { |
| 685 | return mGroupFlags & FLAG_MASK_FOCUSABILITY; |
| 686 | } |
| 687 | |
| 688 | /** |
| 689 | * Set the descendant focusability of this view group. This defines the relationship |
| 690 | * between this view group and its descendants when looking for a view to |
| 691 | * take focus in {@link #requestFocus(int, android.graphics.Rect)}. |
| 692 | * |
| 693 | * @param focusability one of {@link #FOCUS_BEFORE_DESCENDANTS}, {@link #FOCUS_AFTER_DESCENDANTS}, |
| 694 | * {@link #FOCUS_BLOCK_DESCENDANTS}. |
| 695 | */ |
| 696 | public void setDescendantFocusability(int focusability) { |
| 697 | switch (focusability) { |
| 698 | case FOCUS_BEFORE_DESCENDANTS: |
| 699 | case FOCUS_AFTER_DESCENDANTS: |
| 700 | case FOCUS_BLOCK_DESCENDANTS: |
| 701 | break; |
| 702 | default: |
| 703 | throw new IllegalArgumentException("must be one of FOCUS_BEFORE_DESCENDANTS, " |
| 704 | + "FOCUS_AFTER_DESCENDANTS, FOCUS_BLOCK_DESCENDANTS"); |
| 705 | } |
| 706 | mGroupFlags &= ~FLAG_MASK_FOCUSABILITY; |
| 707 | mGroupFlags |= (focusability & FLAG_MASK_FOCUSABILITY); |
| 708 | } |
| 709 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 710 | @Override |
| 711 | void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) { |
| 712 | if (mFocused != null) { |
Alan Viverette | 223622a | 2013-12-17 13:29:02 -0800 | [diff] [blame] | 713 | mFocused.unFocus(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 714 | mFocused = null; |
| 715 | } |
| 716 | super.handleFocusGainInternal(direction, previouslyFocusedRect); |
| 717 | } |
| 718 | |
Alan Viverette | be463f2 | 2016-01-21 10:50:10 -0500 | [diff] [blame] | 719 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 720 | public void requestChildFocus(View child, View focused) { |
| 721 | if (DBG) { |
| 722 | System.out.println(this + " requestChildFocus()"); |
| 723 | } |
| 724 | if (getDescendantFocusability() == FOCUS_BLOCK_DESCENDANTS) { |
| 725 | return; |
| 726 | } |
| 727 | |
| 728 | // Unfocus us, if necessary |
Alan Viverette | 223622a | 2013-12-17 13:29:02 -0800 | [diff] [blame] | 729 | super.unFocus(focused); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 730 | |
| 731 | // We had a previous notion of who had focus. Clear it. |
| 732 | if (mFocused != child) { |
| 733 | if (mFocused != null) { |
Alan Viverette | 223622a | 2013-12-17 13:29:02 -0800 | [diff] [blame] | 734 | mFocused.unFocus(focused); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | mFocused = child; |
| 738 | } |
| 739 | if (mParent != null) { |
| 740 | mParent.requestChildFocus(this, focused); |
| 741 | } |
| 742 | } |
| 743 | |
Alan Viverette | be463f2 | 2016-01-21 10:50:10 -0500 | [diff] [blame] | 744 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 745 | public void focusableViewAvailable(View v) { |
| 746 | if (mParent != null |
| 747 | // shortcut: don't report a new focusable view if we block our descendants from |
| 748 | // getting focus |
| 749 | && (getDescendantFocusability() != FOCUS_BLOCK_DESCENDANTS) |
Adam Powell | 88c1175 | 2014-07-21 17:19:16 -0700 | [diff] [blame] | 750 | && (isFocusableInTouchMode() || !shouldBlockFocusForTouchscreen()) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 751 | // shortcut: don't report a new focusable view if we already are focused |
| 752 | // (and we don't prefer our descendants) |
| 753 | // |
| 754 | // note: knowing that mFocused is non-null is not a good enough reason |
| 755 | // to break the traversal since in that case we'd actually have to find |
| 756 | // the focused view and make sure it wasn't FOCUS_AFTER_DESCENDANTS and |
Joe Onorato | c6cc0f8 | 2011-04-12 11:53:13 -0700 | [diff] [blame] | 757 | // 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] | 758 | && !(isFocused() && getDescendantFocusability() != FOCUS_AFTER_DESCENDANTS)) { |
| 759 | mParent.focusableViewAvailable(v); |
| 760 | } |
| 761 | } |
| 762 | |
Alan Viverette | be463f2 | 2016-01-21 10:50:10 -0500 | [diff] [blame] | 763 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 764 | public boolean showContextMenuForChild(View originalView) { |
Adam Powell | 2af189a | 2016-02-05 15:52:02 -0800 | [diff] [blame] | 765 | if (isShowingContextMenuWithCoords()) { |
| 766 | // We're being called for compatibility. Return false and let the version |
| 767 | // with coordinates recurse up. |
| 768 | return false; |
| 769 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 770 | return mParent != null && mParent.showContextMenuForChild(originalView); |
| 771 | } |
| 772 | |
Adam Powell | 2af189a | 2016-02-05 15:52:02 -0800 | [diff] [blame] | 773 | /** |
| 774 | * @hide used internally for compatibility with existing app code only |
| 775 | */ |
| 776 | public final boolean isShowingContextMenuWithCoords() { |
| 777 | return (mGroupFlags & FLAG_SHOW_CONTEXT_MENU_WITH_COORDS) != 0; |
| 778 | } |
| 779 | |
Oren Blasberg | ed39126 | 2015-09-01 12:12:51 -0700 | [diff] [blame] | 780 | @Override |
| 781 | public boolean showContextMenuForChild(View originalView, float x, float y) { |
Adam Powell | 2af189a | 2016-02-05 15:52:02 -0800 | [diff] [blame] | 782 | try { |
| 783 | mGroupFlags |= FLAG_SHOW_CONTEXT_MENU_WITH_COORDS; |
| 784 | if (showContextMenuForChild(originalView)) { |
| 785 | return true; |
| 786 | } |
| 787 | } finally { |
| 788 | mGroupFlags &= ~FLAG_SHOW_CONTEXT_MENU_WITH_COORDS; |
| 789 | } |
Oren Blasberg | ed39126 | 2015-09-01 12:12:51 -0700 | [diff] [blame] | 790 | return mParent != null && mParent.showContextMenuForChild(originalView, x, y); |
| 791 | } |
| 792 | |
Clara Bayarri | 4423d91 | 2015-03-02 19:42:48 +0000 | [diff] [blame] | 793 | @Override |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 794 | public ActionMode startActionModeForChild(View originalView, ActionMode.Callback callback) { |
Clara Bayarri | 4423d91 | 2015-03-02 19:42:48 +0000 | [diff] [blame] | 795 | if ((mGroupFlags & FLAG_START_ACTION_MODE_FOR_CHILD_IS_TYPED) == 0) { |
| 796 | // This is the original call. |
| 797 | try { |
| 798 | mGroupFlags |= FLAG_START_ACTION_MODE_FOR_CHILD_IS_NOT_TYPED; |
| 799 | return startActionModeForChild(originalView, callback, ActionMode.TYPE_PRIMARY); |
| 800 | } finally { |
| 801 | mGroupFlags &= ~FLAG_START_ACTION_MODE_FOR_CHILD_IS_NOT_TYPED; |
| 802 | } |
| 803 | } else { |
| 804 | // We are being called from the new method with type. |
| 805 | return SENTINEL_ACTION_MODE; |
| 806 | } |
| 807 | } |
| 808 | |
Clara Bayarri | 4423d91 | 2015-03-02 19:42:48 +0000 | [diff] [blame] | 809 | @Override |
| 810 | public ActionMode startActionModeForChild( |
| 811 | View originalView, ActionMode.Callback callback, int type) { |
Adam Powell | e9fd6d2 | 2015-06-01 11:26:32 -0700 | [diff] [blame] | 812 | if ((mGroupFlags & FLAG_START_ACTION_MODE_FOR_CHILD_IS_NOT_TYPED) == 0 |
| 813 | && type == ActionMode.TYPE_PRIMARY) { |
Clara Bayarri | 4423d91 | 2015-03-02 19:42:48 +0000 | [diff] [blame] | 814 | ActionMode mode; |
| 815 | try { |
| 816 | mGroupFlags |= FLAG_START_ACTION_MODE_FOR_CHILD_IS_TYPED; |
| 817 | mode = startActionModeForChild(originalView, callback); |
| 818 | } finally { |
| 819 | mGroupFlags &= ~FLAG_START_ACTION_MODE_FOR_CHILD_IS_TYPED; |
| 820 | } |
| 821 | if (mode != SENTINEL_ACTION_MODE) { |
| 822 | return mode; |
| 823 | } |
| 824 | } |
| 825 | if (mParent != null) { |
| 826 | try { |
| 827 | return mParent.startActionModeForChild(originalView, callback, type); |
| 828 | } catch (AbstractMethodError ame) { |
| 829 | // Custom view parents might not implement this method. |
| 830 | return mParent.startActionModeForChild(originalView, callback); |
| 831 | } |
| 832 | } |
| 833 | return null; |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | /** |
Clara Bayarri | d5bf3ed | 2015-03-27 17:32:45 +0000 | [diff] [blame] | 837 | * @hide |
| 838 | */ |
| 839 | @Override |
| 840 | public boolean dispatchActivityResult( |
| 841 | String who, int requestCode, int resultCode, Intent data) { |
| 842 | if (super.dispatchActivityResult(who, requestCode, resultCode, data)) { |
| 843 | return true; |
| 844 | } |
| 845 | int childCount = getChildCount(); |
| 846 | for (int i = 0; i < childCount; i++) { |
| 847 | View child = getChildAt(i); |
| 848 | if (child.dispatchActivityResult(who, requestCode, resultCode, data)) { |
| 849 | return true; |
| 850 | } |
| 851 | } |
| 852 | return false; |
| 853 | } |
| 854 | |
| 855 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 856 | * Find the nearest view in the specified direction that wants to take |
| 857 | * focus. |
| 858 | * |
| 859 | * @param focused The view that currently has focus |
| 860 | * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and |
| 861 | * FOCUS_RIGHT, or 0 for not applicable. |
| 862 | */ |
Alan Viverette | be463f2 | 2016-01-21 10:50:10 -0500 | [diff] [blame] | 863 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 864 | public View focusSearch(View focused, int direction) { |
Svetoslav Ganov | 27e2da7 | 2012-07-02 18:12:00 -0700 | [diff] [blame] | 865 | if (isRootNamespace()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 866 | // root namespace means we should consider ourselves the top of the |
| 867 | // tree for focus searching; otherwise we could be focus searching |
| 868 | // into other tabs. see LocalActivityManager and TabHost for more info |
| 869 | return FocusFinder.getInstance().findNextFocus(this, focused, direction); |
| 870 | } else if (mParent != null) { |
| 871 | return mParent.focusSearch(focused, direction); |
| 872 | } |
| 873 | return null; |
| 874 | } |
| 875 | |
Alan Viverette | be463f2 | 2016-01-21 10:50:10 -0500 | [diff] [blame] | 876 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 877 | public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) { |
| 878 | return false; |
| 879 | } |
| 880 | |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 881 | @Override |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 882 | public boolean requestSendAccessibilityEvent(View child, AccessibilityEvent event) { |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 883 | ViewParent parent = mParent; |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 884 | if (parent == null) { |
| 885 | return false; |
| 886 | } |
| 887 | final boolean propagate = onRequestSendAccessibilityEvent(child, event); |
| 888 | if (!propagate) { |
| 889 | return false; |
| 890 | } |
| 891 | return parent.requestSendAccessibilityEvent(this, event); |
| 892 | } |
| 893 | |
| 894 | /** |
| 895 | * Called when a child has requested sending an {@link AccessibilityEvent} and |
| 896 | * gives an opportunity to its parent to augment the event. |
Svetoslav Ganov | 031d9c1 | 2011-09-09 16:41:13 -0700 | [diff] [blame] | 897 | * <p> |
Adam Powell | 2fcbbd0 | 2011-09-28 18:56:43 -0700 | [diff] [blame] | 898 | * If an {@link android.view.View.AccessibilityDelegate} has been specified via calling |
| 899 | * {@link android.view.View#setAccessibilityDelegate(android.view.View.AccessibilityDelegate)} its |
| 900 | * {@link android.view.View.AccessibilityDelegate#onRequestSendAccessibilityEvent(ViewGroup, View, AccessibilityEvent)} |
Svetoslav Ganov | 031d9c1 | 2011-09-09 16:41:13 -0700 | [diff] [blame] | 901 | * is responsible for handling this call. |
| 902 | * </p> |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 903 | * |
| 904 | * @param child The child which requests sending the event. |
| 905 | * @param event The event to be sent. |
| 906 | * @return True if the event should be sent. |
| 907 | * |
| 908 | * @see #requestSendAccessibilityEvent(View, AccessibilityEvent) |
| 909 | */ |
| 910 | public boolean onRequestSendAccessibilityEvent(View child, AccessibilityEvent event) { |
Svetoslav Ganov | 031d9c1 | 2011-09-09 16:41:13 -0700 | [diff] [blame] | 911 | if (mAccessibilityDelegate != null) { |
| 912 | return mAccessibilityDelegate.onRequestSendAccessibilityEvent(this, child, event); |
| 913 | } else { |
| 914 | return onRequestSendAccessibilityEventInternal(child, event); |
| 915 | } |
| 916 | } |
| 917 | |
| 918 | /** |
| 919 | * @see #onRequestSendAccessibilityEvent(View, AccessibilityEvent) |
| 920 | * |
| 921 | * Note: Called from the default {@link View.AccessibilityDelegate}. |
Alan Viverette | a54956a | 2015-01-07 16:05:02 -0800 | [diff] [blame] | 922 | * |
| 923 | * @hide |
Svetoslav Ganov | 031d9c1 | 2011-09-09 16:41:13 -0700 | [diff] [blame] | 924 | */ |
Alan Viverette | a54956a | 2015-01-07 16:05:02 -0800 | [diff] [blame] | 925 | public boolean onRequestSendAccessibilityEventInternal(View child, AccessibilityEvent event) { |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 926 | return true; |
| 927 | } |
| 928 | |
| 929 | /** |
Adam Powell | 539ee87 | 2012-02-03 19:00:49 -0800 | [diff] [blame] | 930 | * 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] | 931 | */ |
Alan Viverette | be463f2 | 2016-01-21 10:50:10 -0500 | [diff] [blame] | 932 | @Override |
Adam Powell | 539ee87 | 2012-02-03 19:00:49 -0800 | [diff] [blame] | 933 | public void childHasTransientStateChanged(View child, boolean childHasTransientState) { |
| 934 | final boolean oldHasTransientState = hasTransientState(); |
| 935 | if (childHasTransientState) { |
| 936 | mChildCountWithTransientState++; |
| 937 | } else { |
| 938 | mChildCountWithTransientState--; |
| 939 | } |
| 940 | |
| 941 | final boolean newHasTransientState = hasTransientState(); |
| 942 | if (mParent != null && oldHasTransientState != newHasTransientState) { |
| 943 | try { |
| 944 | mParent.childHasTransientStateChanged(this, newHasTransientState); |
| 945 | } catch (AbstractMethodError e) { |
| 946 | Log.e(TAG, mParent.getClass().getSimpleName() + |
| 947 | " does not fully implement ViewParent", e); |
| 948 | } |
| 949 | } |
| 950 | } |
| 951 | |
Adam Powell | 539ee87 | 2012-02-03 19:00:49 -0800 | [diff] [blame] | 952 | @Override |
| 953 | public boolean hasTransientState() { |
| 954 | return mChildCountWithTransientState > 0 || super.hasTransientState(); |
| 955 | } |
| 956 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 957 | @Override |
| 958 | public boolean dispatchUnhandledMove(View focused, int direction) { |
| 959 | return mFocused != null && |
| 960 | mFocused.dispatchUnhandledMove(focused, direction); |
| 961 | } |
| 962 | |
Alan Viverette | be463f2 | 2016-01-21 10:50:10 -0500 | [diff] [blame] | 963 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 964 | public void clearChildFocus(View child) { |
| 965 | if (DBG) { |
| 966 | System.out.println(this + " clearChildFocus()"); |
| 967 | } |
| 968 | |
| 969 | mFocused = null; |
| 970 | if (mParent != null) { |
| 971 | mParent.clearChildFocus(this); |
| 972 | } |
| 973 | } |
| 974 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 975 | @Override |
| 976 | public void clearFocus() { |
Svetoslav Ganov | b36a0ac | 2012-02-14 17:46:47 -0800 | [diff] [blame] | 977 | if (DBG) { |
| 978 | System.out.println(this + " clearFocus()"); |
| 979 | } |
| 980 | if (mFocused == null) { |
| 981 | super.clearFocus(); |
| 982 | } else { |
Svetoslav Ganov | b552d89 | 2012-06-02 14:35:02 -0700 | [diff] [blame] | 983 | View focused = mFocused; |
Svetoslav Ganov | b36a0ac | 2012-02-14 17:46:47 -0800 | [diff] [blame] | 984 | mFocused = null; |
Svetoslav Ganov | b552d89 | 2012-06-02 14:35:02 -0700 | [diff] [blame] | 985 | focused.clearFocus(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 986 | } |
| 987 | } |
| 988 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 989 | @Override |
Alan Viverette | 223622a | 2013-12-17 13:29:02 -0800 | [diff] [blame] | 990 | void unFocus(View focused) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 991 | if (DBG) { |
| 992 | System.out.println(this + " unFocus()"); |
| 993 | } |
Svetoslav Ganov | b36a0ac | 2012-02-14 17:46:47 -0800 | [diff] [blame] | 994 | if (mFocused == null) { |
Alan Viverette | 223622a | 2013-12-17 13:29:02 -0800 | [diff] [blame] | 995 | super.unFocus(focused); |
Svetoslav Ganov | b36a0ac | 2012-02-14 17:46:47 -0800 | [diff] [blame] | 996 | } else { |
Alan Viverette | 223622a | 2013-12-17 13:29:02 -0800 | [diff] [blame] | 997 | mFocused.unFocus(focused); |
Svetoslav Ganov | b36a0ac | 2012-02-14 17:46:47 -0800 | [diff] [blame] | 998 | mFocused = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 999 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1000 | } |
| 1001 | |
| 1002 | /** |
| 1003 | * Returns the focused child of this view, if any. The child may have focus |
| 1004 | * or contain focus. |
| 1005 | * |
| 1006 | * @return the focused child or null. |
| 1007 | */ |
| 1008 | public View getFocusedChild() { |
| 1009 | return mFocused; |
| 1010 | } |
| 1011 | |
Adam Powell | 88c1175 | 2014-07-21 17:19:16 -0700 | [diff] [blame] | 1012 | View getDeepestFocusedChild() { |
| 1013 | View v = this; |
| 1014 | while (v != null) { |
| 1015 | if (v.isFocused()) { |
| 1016 | return v; |
| 1017 | } |
| 1018 | v = v instanceof ViewGroup ? ((ViewGroup) v).getFocusedChild() : null; |
| 1019 | } |
| 1020 | return null; |
| 1021 | } |
| 1022 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1023 | /** |
| 1024 | * Returns true if this view has or contains focus |
| 1025 | * |
| 1026 | * @return true if this view has or contains focus |
| 1027 | */ |
| 1028 | @Override |
| 1029 | public boolean hasFocus() { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 1030 | return (mPrivateFlags & PFLAG_FOCUSED) != 0 || mFocused != null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1031 | } |
| 1032 | |
| 1033 | /* |
| 1034 | * (non-Javadoc) |
| 1035 | * |
| 1036 | * @see android.view.View#findFocus() |
| 1037 | */ |
| 1038 | @Override |
| 1039 | public View findFocus() { |
| 1040 | if (DBG) { |
| 1041 | System.out.println("Find focus in " + this + ": flags=" |
| 1042 | + isFocused() + ", child=" + mFocused); |
| 1043 | } |
| 1044 | |
| 1045 | if (isFocused()) { |
| 1046 | return this; |
| 1047 | } |
| 1048 | |
| 1049 | if (mFocused != null) { |
| 1050 | return mFocused.findFocus(); |
| 1051 | } |
| 1052 | return null; |
| 1053 | } |
| 1054 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1055 | @Override |
| 1056 | public boolean hasFocusable() { |
| 1057 | if ((mViewFlags & VISIBILITY_MASK) != VISIBLE) { |
| 1058 | return false; |
| 1059 | } |
| 1060 | |
| 1061 | if (isFocusable()) { |
| 1062 | return true; |
| 1063 | } |
| 1064 | |
| 1065 | final int descendantFocusability = getDescendantFocusability(); |
Adam Powell | 88c1175 | 2014-07-21 17:19:16 -0700 | [diff] [blame] | 1066 | if (descendantFocusability != FOCUS_BLOCK_DESCENDANTS) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1067 | final int count = mChildrenCount; |
| 1068 | final View[] children = mChildren; |
| 1069 | |
| 1070 | for (int i = 0; i < count; i++) { |
| 1071 | final View child = children[i]; |
| 1072 | if (child.hasFocusable()) { |
| 1073 | return true; |
| 1074 | } |
| 1075 | } |
| 1076 | } |
| 1077 | |
| 1078 | return false; |
| 1079 | } |
| 1080 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1081 | @Override |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 1082 | 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] | 1083 | final int focusableCount = views.size(); |
| 1084 | |
| 1085 | final int descendantFocusability = getDescendantFocusability(); |
| 1086 | |
Adam Powell | 88c1175 | 2014-07-21 17:19:16 -0700 | [diff] [blame] | 1087 | if (descendantFocusability != FOCUS_BLOCK_DESCENDANTS) { |
| 1088 | if (shouldBlockFocusForTouchscreen()) { |
| 1089 | focusableMode |= FOCUSABLES_TOUCH_MODE; |
| 1090 | } |
| 1091 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1092 | final int count = mChildrenCount; |
| 1093 | final View[] children = mChildren; |
| 1094 | |
| 1095 | for (int i = 0; i < count; i++) { |
| 1096 | final View child = children[i]; |
| 1097 | if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) { |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 1098 | child.addFocusables(views, direction, focusableMode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1099 | } |
| 1100 | } |
| 1101 | } |
| 1102 | |
| 1103 | // we add ourselves (if focusable) in all cases except for when we are |
| 1104 | // FOCUS_AFTER_DESCENDANTS and there are some descendants focusable. this is |
| 1105 | // to avoid the focus search finding layouts when a more precise search |
| 1106 | // among the focusable children would be more interesting. |
Adam Powell | ff0d298 | 2014-07-10 20:34:14 -0700 | [diff] [blame] | 1107 | if ((descendantFocusability != FOCUS_AFTER_DESCENDANTS |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1108 | // No focusable descendants |
Adam Powell | 88c1175 | 2014-07-21 17:19:16 -0700 | [diff] [blame] | 1109 | || (focusableCount == views.size())) && |
| 1110 | (isFocusableInTouchMode() || !shouldBlockFocusForTouchscreen())) { |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 1111 | super.addFocusables(views, direction, focusableMode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1112 | } |
| 1113 | } |
| 1114 | |
Adam Powell | ff0d298 | 2014-07-10 20:34:14 -0700 | [diff] [blame] | 1115 | /** |
| 1116 | * Set whether this ViewGroup should ignore focus requests for itself and its children. |
| 1117 | * If this option is enabled and the ViewGroup or a descendant currently has focus, focus |
| 1118 | * will proceed forward. |
| 1119 | * |
| 1120 | * @param touchscreenBlocksFocus true to enable blocking focus in the presence of a touchscreen |
| 1121 | */ |
| 1122 | public void setTouchscreenBlocksFocus(boolean touchscreenBlocksFocus) { |
| 1123 | if (touchscreenBlocksFocus) { |
| 1124 | mGroupFlags |= FLAG_TOUCHSCREEN_BLOCKS_FOCUS; |
| 1125 | if (hasFocus()) { |
Adam Powell | 88c1175 | 2014-07-21 17:19:16 -0700 | [diff] [blame] | 1126 | final View focusedChild = getDeepestFocusedChild(); |
| 1127 | if (!focusedChild.isFocusableInTouchMode()) { |
| 1128 | final View newFocus = focusSearch(FOCUS_FORWARD); |
| 1129 | if (newFocus != null) { |
| 1130 | newFocus.requestFocus(); |
| 1131 | } |
Adam Powell | ff0d298 | 2014-07-10 20:34:14 -0700 | [diff] [blame] | 1132 | } |
| 1133 | } |
| 1134 | } else { |
| 1135 | mGroupFlags &= ~FLAG_TOUCHSCREEN_BLOCKS_FOCUS; |
| 1136 | } |
| 1137 | } |
| 1138 | |
| 1139 | /** |
| 1140 | * Check whether this ViewGroup should ignore focus requests for itself and its children. |
| 1141 | */ |
| 1142 | public boolean getTouchscreenBlocksFocus() { |
| 1143 | return (mGroupFlags & FLAG_TOUCHSCREEN_BLOCKS_FOCUS) != 0; |
| 1144 | } |
| 1145 | |
| 1146 | boolean shouldBlockFocusForTouchscreen() { |
| 1147 | return getTouchscreenBlocksFocus() && |
| 1148 | mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_TOUCHSCREEN); |
| 1149 | } |
| 1150 | |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 1151 | @Override |
Svetoslav Ganov | ea515ae | 2011-09-14 18:15:32 -0700 | [diff] [blame] | 1152 | public void findViewsWithText(ArrayList<View> outViews, CharSequence text, int flags) { |
| 1153 | super.findViewsWithText(outViews, text, flags); |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 1154 | final int childrenCount = mChildrenCount; |
| 1155 | final View[] children = mChildren; |
| 1156 | for (int i = 0; i < childrenCount; i++) { |
| 1157 | View child = children[i]; |
Svetoslav Ganov | ea515ae | 2011-09-14 18:15:32 -0700 | [diff] [blame] | 1158 | if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 1159 | && (child.mPrivateFlags & PFLAG_IS_ROOT_NAMESPACE) == 0) { |
Svetoslav Ganov | ea515ae | 2011-09-14 18:15:32 -0700 | [diff] [blame] | 1160 | child.findViewsWithText(outViews, text, flags); |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 1161 | } |
| 1162 | } |
| 1163 | } |
| 1164 | |
Svetoslav | 5b578da | 2013-05-08 14:23:32 -0700 | [diff] [blame] | 1165 | /** @hide */ |
Svetoslav Ganov | 2cdedff | 2011-10-03 14:18:42 -0700 | [diff] [blame] | 1166 | @Override |
Svetoslav | 5b578da | 2013-05-08 14:23:32 -0700 | [diff] [blame] | 1167 | public View findViewByAccessibilityIdTraversal(int accessibilityId) { |
Svetoslav Ganov | 2cdedff | 2011-10-03 14:18:42 -0700 | [diff] [blame] | 1168 | View foundView = super.findViewByAccessibilityIdTraversal(accessibilityId); |
| 1169 | if (foundView != null) { |
| 1170 | return foundView; |
| 1171 | } |
Svetoslav | 6cc4627 | 2015-06-03 11:38:30 -0700 | [diff] [blame] | 1172 | |
| 1173 | if (getAccessibilityNodeProvider() != null) { |
| 1174 | return null; |
| 1175 | } |
| 1176 | |
Svetoslav Ganov | 2cdedff | 2011-10-03 14:18:42 -0700 | [diff] [blame] | 1177 | final int childrenCount = mChildrenCount; |
| 1178 | final View[] children = mChildren; |
| 1179 | for (int i = 0; i < childrenCount; i++) { |
| 1180 | View child = children[i]; |
| 1181 | foundView = child.findViewByAccessibilityIdTraversal(accessibilityId); |
| 1182 | if (foundView != null) { |
| 1183 | return foundView; |
| 1184 | } |
| 1185 | } |
Svetoslav | 6cc4627 | 2015-06-03 11:38:30 -0700 | [diff] [blame] | 1186 | |
Svetoslav Ganov | 2cdedff | 2011-10-03 14:18:42 -0700 | [diff] [blame] | 1187 | return null; |
| 1188 | } |
| 1189 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1190 | @Override |
| 1191 | public void dispatchWindowFocusChanged(boolean hasFocus) { |
| 1192 | super.dispatchWindowFocusChanged(hasFocus); |
| 1193 | final int count = mChildrenCount; |
| 1194 | final View[] children = mChildren; |
| 1195 | for (int i = 0; i < count; i++) { |
| 1196 | children[i].dispatchWindowFocusChanged(hasFocus); |
| 1197 | } |
| 1198 | } |
| 1199 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1200 | @Override |
| 1201 | public void addTouchables(ArrayList<View> views) { |
| 1202 | super.addTouchables(views); |
| 1203 | |
| 1204 | final int count = mChildrenCount; |
| 1205 | final View[] children = mChildren; |
| 1206 | |
| 1207 | for (int i = 0; i < count; i++) { |
| 1208 | final View child = children[i]; |
| 1209 | if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) { |
| 1210 | child.addTouchables(views); |
| 1211 | } |
| 1212 | } |
| 1213 | } |
Dianne Hackborn | 3a3a6cf | 2012-03-26 10:24:04 -0700 | [diff] [blame] | 1214 | |
| 1215 | /** |
| 1216 | * @hide |
| 1217 | */ |
| 1218 | @Override |
| 1219 | public void makeOptionalFitsSystemWindows() { |
| 1220 | super.makeOptionalFitsSystemWindows(); |
| 1221 | final int count = mChildrenCount; |
| 1222 | final View[] children = mChildren; |
| 1223 | for (int i = 0; i < count; i++) { |
| 1224 | children[i].makeOptionalFitsSystemWindows(); |
| 1225 | } |
| 1226 | } |
| 1227 | |
Romain Guy | 43c9cdf | 2010-01-27 13:53:55 -0800 | [diff] [blame] | 1228 | @Override |
| 1229 | public void dispatchDisplayHint(int hint) { |
| 1230 | super.dispatchDisplayHint(hint); |
| 1231 | final int count = mChildrenCount; |
| 1232 | final View[] children = mChildren; |
| 1233 | for (int i = 0; i < count; i++) { |
| 1234 | children[i].dispatchDisplayHint(hint); |
| 1235 | } |
| 1236 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1237 | |
| 1238 | /** |
Chet Haase | 0d29936 | 2012-01-26 10:51:48 -0800 | [diff] [blame] | 1239 | * Called when a view's visibility has changed. Notify the parent to take any appropriate |
| 1240 | * action. |
| 1241 | * |
| 1242 | * @param child The view whose visibility has changed |
| 1243 | * @param oldVisibility The previous visibility value (GONE, INVISIBLE, or VISIBLE). |
| 1244 | * @param newVisibility The new visibility value (GONE, INVISIBLE, or VISIBLE). |
Chet Haase | 5e25c2c | 2010-09-16 11:15:56 -0700 | [diff] [blame] | 1245 | * @hide |
Chet Haase | 5e25c2c | 2010-09-16 11:15:56 -0700 | [diff] [blame] | 1246 | */ |
Chet Haase | 0d29936 | 2012-01-26 10:51:48 -0800 | [diff] [blame] | 1247 | protected void onChildVisibilityChanged(View child, int oldVisibility, int newVisibility) { |
Chet Haase | 5e25c2c | 2010-09-16 11:15:56 -0700 | [diff] [blame] | 1248 | if (mTransition != null) { |
Chet Haase | 0d29936 | 2012-01-26 10:51:48 -0800 | [diff] [blame] | 1249 | if (newVisibility == VISIBLE) { |
| 1250 | mTransition.showChild(this, child, oldVisibility); |
Chet Haase | 5e25c2c | 2010-09-16 11:15:56 -0700 | [diff] [blame] | 1251 | } else { |
Chet Haase | 0d29936 | 2012-01-26 10:51:48 -0800 | [diff] [blame] | 1252 | mTransition.hideChild(this, child, newVisibility); |
Chet Haase | 5e25c2c | 2010-09-16 11:15:56 -0700 | [diff] [blame] | 1253 | if (mTransitioningViews != null && mTransitioningViews.contains(child)) { |
Chet Haase | ddbb346 | 2012-06-19 13:54:29 -0700 | [diff] [blame] | 1254 | // Only track this on disappearing views - appearing views are already visible |
| 1255 | // and don't need special handling during drawChild() |
| 1256 | if (mVisibilityChangingChildren == null) { |
| 1257 | mVisibilityChangingChildren = new ArrayList<View>(); |
| 1258 | } |
| 1259 | mVisibilityChangingChildren.add(child); |
Chet Haase | 5e25c2c | 2010-09-16 11:15:56 -0700 | [diff] [blame] | 1260 | addDisappearingView(child); |
| 1261 | } |
| 1262 | } |
| 1263 | } |
Christopher Tate | 86cab1b | 2011-01-13 20:28:55 -0800 | [diff] [blame] | 1264 | |
| 1265 | // in all cases, for drags |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1266 | if (newVisibility == VISIBLE && mCurrentDragStartEvent != null) { |
| 1267 | if (!mChildrenInterestedInDrag.contains(child)) { |
| 1268 | notifyChildOfDragStart(child); |
Christopher Tate | 86cab1b | 2011-01-13 20:28:55 -0800 | [diff] [blame] | 1269 | } |
| 1270 | } |
Chet Haase | 5e25c2c | 2010-09-16 11:15:56 -0700 | [diff] [blame] | 1271 | } |
| 1272 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1273 | @Override |
Adam Powell | 326d808 | 2009-12-09 15:10:07 -0800 | [diff] [blame] | 1274 | protected void dispatchVisibilityChanged(View changedView, int visibility) { |
| 1275 | super.dispatchVisibilityChanged(changedView, visibility); |
| 1276 | final int count = mChildrenCount; |
| 1277 | final View[] children = mChildren; |
| 1278 | for (int i = 0; i < count; i++) { |
| 1279 | children[i].dispatchVisibilityChanged(changedView, visibility); |
| 1280 | } |
| 1281 | } |
| 1282 | |
Adam Powell | 326d808 | 2009-12-09 15:10:07 -0800 | [diff] [blame] | 1283 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1284 | public void dispatchWindowVisibilityChanged(int visibility) { |
| 1285 | super.dispatchWindowVisibilityChanged(visibility); |
| 1286 | final int count = mChildrenCount; |
| 1287 | final View[] children = mChildren; |
| 1288 | for (int i = 0; i < count; i++) { |
| 1289 | children[i].dispatchWindowVisibilityChanged(visibility); |
| 1290 | } |
| 1291 | } |
| 1292 | |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 1293 | @Override |
Adam Powell | 9c146bf | 2016-03-15 17:35:00 -0700 | [diff] [blame] | 1294 | boolean dispatchVisibilityAggregated(boolean isVisible) { |
| 1295 | isVisible = super.dispatchVisibilityAggregated(isVisible); |
Adam Powell | 41d9690 | 2016-03-15 14:43:19 -0700 | [diff] [blame] | 1296 | final int count = mChildrenCount; |
| 1297 | final View[] children = mChildren; |
| 1298 | for (int i = 0; i < count; i++) { |
Adam Powell | 9c146bf | 2016-03-15 17:35:00 -0700 | [diff] [blame] | 1299 | // Only dispatch to visible children. Not visible children and their subtrees already |
| 1300 | // know that they aren't visible and that's not going to change as a result of |
| 1301 | // whatever triggered this dispatch. |
| 1302 | if (children[i].getVisibility() == VISIBLE) { |
| 1303 | children[i].dispatchVisibilityAggregated(isVisible); |
Adam Powell | 41d9690 | 2016-03-15 14:43:19 -0700 | [diff] [blame] | 1304 | } |
| 1305 | } |
Adam Powell | 9c146bf | 2016-03-15 17:35:00 -0700 | [diff] [blame] | 1306 | return isVisible; |
Adam Powell | 41d9690 | 2016-03-15 14:43:19 -0700 | [diff] [blame] | 1307 | } |
| 1308 | |
| 1309 | @Override |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 1310 | public void dispatchConfigurationChanged(Configuration newConfig) { |
| 1311 | super.dispatchConfigurationChanged(newConfig); |
| 1312 | final int count = mChildrenCount; |
| 1313 | final View[] children = mChildren; |
| 1314 | for (int i = 0; i < count; i++) { |
| 1315 | children[i].dispatchConfigurationChanged(newConfig); |
| 1316 | } |
| 1317 | } |
Filip Gruszczynski | a33bdf3 | 2015-11-19 18:22:16 -0800 | [diff] [blame] | 1318 | |
Alan Viverette | be463f2 | 2016-01-21 10:50:10 -0500 | [diff] [blame] | 1319 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1320 | public void recomputeViewAttributes(View child) { |
Joe Onorato | 664644d | 2011-01-23 17:53:23 -0800 | [diff] [blame] | 1321 | if (mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) { |
| 1322 | ViewParent parent = mParent; |
| 1323 | if (parent != null) parent.recomputeViewAttributes(this); |
| 1324 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1325 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 1326 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1327 | @Override |
Dianne Hackborn | 3a3a6cf | 2012-03-26 10:24:04 -0700 | [diff] [blame] | 1328 | void dispatchCollectViewAttributes(AttachInfo attachInfo, int visibility) { |
| 1329 | if ((visibility & VISIBILITY_MASK) == VISIBLE) { |
| 1330 | super.dispatchCollectViewAttributes(attachInfo, visibility); |
| 1331 | final int count = mChildrenCount; |
| 1332 | final View[] children = mChildren; |
| 1333 | for (int i = 0; i < count; i++) { |
| 1334 | final View child = children[i]; |
| 1335 | child.dispatchCollectViewAttributes(attachInfo, |
| 1336 | visibility | (child.mViewFlags&VISIBILITY_MASK)); |
| 1337 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1338 | } |
| 1339 | } |
| 1340 | |
Alan Viverette | be463f2 | 2016-01-21 10:50:10 -0500 | [diff] [blame] | 1341 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1342 | public void bringChildToFront(View child) { |
Alan Viverette | 77bb6f1 | 2015-02-11 17:24:33 -0800 | [diff] [blame] | 1343 | final int index = indexOfChild(child); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1344 | if (index >= 0) { |
| 1345 | removeFromArray(index); |
| 1346 | addInArray(child, mChildrenCount); |
| 1347 | child.mParent = this; |
Chet Haase | cb96db8 | 2013-09-04 10:21:46 -0700 | [diff] [blame] | 1348 | requestLayout(); |
| 1349 | invalidate(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1350 | } |
| 1351 | } |
| 1352 | |
Romain Guy | 6410c0a | 2013-06-17 11:21:58 -0700 | [diff] [blame] | 1353 | private PointF getLocalPoint() { |
| 1354 | if (mLocalPoint == null) mLocalPoint = new PointF(); |
| 1355 | return mLocalPoint; |
| 1356 | } |
| 1357 | |
Steve Block | 8a7259b | 2012-03-01 11:24:41 +0000 | [diff] [blame] | 1358 | // TODO: Write real docs |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1359 | @Override |
| 1360 | public boolean dispatchDragEvent(DragEvent event) { |
| 1361 | boolean retval = false; |
| 1362 | final float tx = event.mX; |
| 1363 | final float ty = event.mY; |
| 1364 | |
Dianne Hackborn | 6dd005b | 2011-07-18 13:22:50 -0700 | [diff] [blame] | 1365 | ViewRootImpl root = getViewRootImpl(); |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1366 | |
| 1367 | // Dispatch down the view hierarchy |
Romain Guy | 6410c0a | 2013-06-17 11:21:58 -0700 | [diff] [blame] | 1368 | final PointF localPoint = getLocalPoint(); |
| 1369 | |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1370 | switch (event.mAction) { |
| 1371 | case DragEvent.ACTION_DRAG_STARTED: { |
| 1372 | // clear state to recalculate which views we drag over |
Chris Tate | 9d1ab88 | 2010-11-02 15:55:39 -0700 | [diff] [blame] | 1373 | mCurrentDragView = null; |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1374 | |
Christopher Tate | 86cab1b | 2011-01-13 20:28:55 -0800 | [diff] [blame] | 1375 | // Set up our tracking of drag-started notifications |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1376 | mCurrentDragStartEvent = DragEvent.obtain(event); |
| 1377 | if (mChildrenInterestedInDrag == null) { |
| 1378 | mChildrenInterestedInDrag = new HashSet<View>(); |
Christopher Tate | 86cab1b | 2011-01-13 20:28:55 -0800 | [diff] [blame] | 1379 | } else { |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1380 | mChildrenInterestedInDrag.clear(); |
Christopher Tate | 86cab1b | 2011-01-13 20:28:55 -0800 | [diff] [blame] | 1381 | } |
| 1382 | |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1383 | // Now dispatch down to our children, caching the responses |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1384 | final int count = mChildrenCount; |
| 1385 | final View[] children = mChildren; |
| 1386 | for (int i = 0; i < count; i++) { |
Christopher Tate | 2c095f3 | 2010-10-04 14:13:40 -0700 | [diff] [blame] | 1387 | final View child = children[i]; |
Christopher Tate | 3d4bf17 | 2011-03-28 16:16:46 -0700 | [diff] [blame] | 1388 | child.mPrivateFlags2 &= ~View.DRAG_MASK; |
Christopher Tate | 2c095f3 | 2010-10-04 14:13:40 -0700 | [diff] [blame] | 1389 | if (child.getVisibility() == VISIBLE) { |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1390 | if (notifyChildOfDragStart(children[i])) { |
| 1391 | retval = true; |
Christopher Tate | 2c095f3 | 2010-10-04 14:13:40 -0700 | [diff] [blame] | 1392 | } |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1393 | } |
| 1394 | } |
| 1395 | |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1396 | // Notify itself of the drag start. |
| 1397 | mIsInterestedInDrag = super.dispatchDragEvent(event); |
| 1398 | if (mIsInterestedInDrag) { |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1399 | retval = true; |
| 1400 | } |
Yorke Lee | 0394b21 | 2016-03-10 18:20:04 -0800 | [diff] [blame] | 1401 | |
| 1402 | if (!retval) { |
| 1403 | // Neither us nor any of our children are interested in this drag, so stop tracking |
| 1404 | // the current drag event. |
| 1405 | mCurrentDragStartEvent.recycle(); |
| 1406 | mCurrentDragStartEvent = null; |
| 1407 | } |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1408 | } break; |
| 1409 | |
| 1410 | case DragEvent.ACTION_DRAG_ENDED: { |
Christopher Tate | 86cab1b | 2011-01-13 20:28:55 -0800 | [diff] [blame] | 1411 | // Release the bookkeeping now that the drag lifecycle has ended |
Vadim Tryshev | 1517789 | 2015-12-18 08:01:00 -0800 | [diff] [blame] | 1412 | final HashSet<View> childrenInterestedInDrag = mChildrenInterestedInDrag; |
| 1413 | if (childrenInterestedInDrag != null) { |
| 1414 | for (View child : childrenInterestedInDrag) { |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1415 | // If a child was interested in the ongoing drag, it's told that it's over |
| 1416 | if (child.dispatchDragEvent(event)) { |
| 1417 | retval = true; |
| 1418 | } |
Christopher Tate | 3d4bf17 | 2011-03-28 16:16:46 -0700 | [diff] [blame] | 1419 | child.mPrivateFlags2 &= ~View.DRAG_MASK; |
| 1420 | child.refreshDrawableState(); |
Christopher Tate | 1fc014f | 2011-01-19 12:56:26 -0800 | [diff] [blame] | 1421 | } |
Vadim Tryshev | 1517789 | 2015-12-18 08:01:00 -0800 | [diff] [blame] | 1422 | childrenInterestedInDrag.clear(); |
| 1423 | } |
| 1424 | if (mCurrentDragStartEvent != null) { |
| 1425 | mCurrentDragStartEvent.recycle(); |
| 1426 | mCurrentDragStartEvent = null; |
Christopher Tate | 1fc014f | 2011-01-19 12:56:26 -0800 | [diff] [blame] | 1427 | } |
Christopher Tate | 86cab1b | 2011-01-13 20:28:55 -0800 | [diff] [blame] | 1428 | |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1429 | if (mIsInterestedInDrag) { |
| 1430 | if (super.dispatchDragEvent(event)) { |
| 1431 | retval = true; |
| 1432 | } |
| 1433 | mIsInterestedInDrag = false; |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1434 | } |
| 1435 | } break; |
| 1436 | |
| 1437 | case DragEvent.ACTION_DRAG_LOCATION: { |
| 1438 | // Find the [possibly new] drag target |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1439 | View target = findFrontmostDroppableChildAt(event.mX, event.mY, localPoint); |
| 1440 | if (target == null && mIsInterestedInDrag) { |
| 1441 | target = this; |
| 1442 | } |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1443 | |
| 1444 | // If we've changed apparent drag target, tell the view root which view |
Chris Tate | 9d1ab88 | 2010-11-02 15:55:39 -0700 | [diff] [blame] | 1445 | // we're over now [for purposes of the eventual drag-recipient-changed |
| 1446 | // notifications to the framework] and tell the new target that the drag |
| 1447 | // has entered its bounds. The root will see setDragFocus() calls all |
| 1448 | // the way down to the final leaf view that is handling the LOCATION event |
| 1449 | // before reporting the new potential recipient to the framework. |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1450 | if (mCurrentDragView != target) { |
Chris Tate | 9d1ab88 | 2010-11-02 15:55:39 -0700 | [diff] [blame] | 1451 | root.setDragFocus(target); |
| 1452 | |
| 1453 | final int action = event.mAction; |
Vladislav Kaznacheev | cc010b2 | 2016-01-21 16:24:40 -0800 | [diff] [blame] | 1454 | // Position should not be available for ACTION_DRAG_ENTERED and ACTION_DRAG_EXITED. |
| 1455 | event.mX = 0; |
| 1456 | event.mY = 0; |
| 1457 | |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1458 | // If we've dragged off of a child view or this window, send it the EXITED message |
Chris Tate | 9d1ab88 | 2010-11-02 15:55:39 -0700 | [diff] [blame] | 1459 | if (mCurrentDragView != null) { |
Christopher Tate | 3d4bf17 | 2011-03-28 16:16:46 -0700 | [diff] [blame] | 1460 | final View view = mCurrentDragView; |
Chris Tate | 9d1ab88 | 2010-11-02 15:55:39 -0700 | [diff] [blame] | 1461 | event.mAction = DragEvent.ACTION_DRAG_EXITED; |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1462 | if (view != this) { |
| 1463 | view.dispatchDragEvent(event); |
| 1464 | view.mPrivateFlags2 &= ~View.PFLAG2_DRAG_HOVERED; |
| 1465 | view.refreshDrawableState(); |
| 1466 | } else { |
| 1467 | super.dispatchDragEvent(event); |
| 1468 | } |
Chris Tate | 9d1ab88 | 2010-11-02 15:55:39 -0700 | [diff] [blame] | 1469 | } |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1470 | |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1471 | mCurrentDragView = target; |
Chris Tate | 9d1ab88 | 2010-11-02 15:55:39 -0700 | [diff] [blame] | 1472 | |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1473 | // If we've dragged over a new child view, send it the ENTERED message, otherwise |
| 1474 | // send it to this window. |
Chris Tate | 9d1ab88 | 2010-11-02 15:55:39 -0700 | [diff] [blame] | 1475 | if (target != null) { |
| 1476 | event.mAction = DragEvent.ACTION_DRAG_ENTERED; |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1477 | if (target != this) { |
| 1478 | target.dispatchDragEvent(event); |
| 1479 | target.mPrivateFlags2 |= View.PFLAG2_DRAG_HOVERED; |
| 1480 | target.refreshDrawableState(); |
| 1481 | } else { |
| 1482 | super.dispatchDragEvent(event); |
| 1483 | } |
Chris Tate | 9d1ab88 | 2010-11-02 15:55:39 -0700 | [diff] [blame] | 1484 | } |
| 1485 | event.mAction = action; // restore the event's original state |
Vladislav Kaznacheev | cc010b2 | 2016-01-21 16:24:40 -0800 | [diff] [blame] | 1486 | event.mX = tx; |
| 1487 | event.mY = ty; |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1488 | } |
Christopher Tate | 2c095f3 | 2010-10-04 14:13:40 -0700 | [diff] [blame] | 1489 | |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1490 | // Dispatch the actual drag location notice, localized into its coordinates |
| 1491 | if (target != null) { |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1492 | if (target != this) { |
| 1493 | event.mX = localPoint.x; |
| 1494 | event.mY = localPoint.y; |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1495 | |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1496 | retval = target.dispatchDragEvent(event); |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1497 | |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1498 | event.mX = tx; |
| 1499 | event.mY = ty; |
| 1500 | } else { |
| 1501 | retval = super.dispatchDragEvent(event); |
| 1502 | } |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1503 | } |
| 1504 | } break; |
| 1505 | |
Chris Tate | 9d1ab88 | 2010-11-02 15:55:39 -0700 | [diff] [blame] | 1506 | /* Entered / exited dispatch |
| 1507 | * |
| 1508 | * DRAG_ENTERED is not dispatched downwards from ViewGroup. The reason for this is |
| 1509 | * that we're about to get the corresponding LOCATION event, which we will use to |
| 1510 | * determine which of our children is the new target; at that point we will |
| 1511 | * push a DRAG_ENTERED down to the new target child [which may itself be a ViewGroup]. |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1512 | * If no suitable child is detected, dispatch to this window. |
Chris Tate | 9d1ab88 | 2010-11-02 15:55:39 -0700 | [diff] [blame] | 1513 | * |
| 1514 | * DRAG_EXITED *is* dispatched all the way down immediately: once we know the |
| 1515 | * drag has left this ViewGroup, we know by definition that every contained subview |
| 1516 | * is also no longer under the drag point. |
| 1517 | */ |
| 1518 | |
| 1519 | case DragEvent.ACTION_DRAG_EXITED: { |
| 1520 | if (mCurrentDragView != null) { |
Christopher Tate | 3d4bf17 | 2011-03-28 16:16:46 -0700 | [diff] [blame] | 1521 | final View view = mCurrentDragView; |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1522 | if (view != this) { |
| 1523 | view.dispatchDragEvent(event); |
| 1524 | view.mPrivateFlags2 &= ~View.PFLAG2_DRAG_HOVERED; |
| 1525 | view.refreshDrawableState(); |
| 1526 | } else { |
| 1527 | super.dispatchDragEvent(event); |
| 1528 | } |
Christopher Tate | 3d4bf17 | 2011-03-28 16:16:46 -0700 | [diff] [blame] | 1529 | |
Chris Tate | 9d1ab88 | 2010-11-02 15:55:39 -0700 | [diff] [blame] | 1530 | mCurrentDragView = null; |
| 1531 | } |
| 1532 | } break; |
| 1533 | |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1534 | case DragEvent.ACTION_DROP: { |
Christopher Tate | 2c095f3 | 2010-10-04 14:13:40 -0700 | [diff] [blame] | 1535 | if (ViewDebug.DEBUG_DRAG) Log.d(View.VIEW_LOG_TAG, "Drop event: " + event); |
Romain Guy | 6410c0a | 2013-06-17 11:21:58 -0700 | [diff] [blame] | 1536 | View target = findFrontmostDroppableChildAt(event.mX, event.mY, localPoint); |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1537 | if (target != null) { |
Christopher Tate | 5ada6cb | 2010-10-05 14:15:29 -0700 | [diff] [blame] | 1538 | if (ViewDebug.DEBUG_DRAG) Log.d(View.VIEW_LOG_TAG, " dispatch drop to " + target); |
Romain Guy | 6410c0a | 2013-06-17 11:21:58 -0700 | [diff] [blame] | 1539 | event.mX = localPoint.x; |
| 1540 | event.mY = localPoint.y; |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1541 | retval = target.dispatchDragEvent(event); |
| 1542 | event.mX = tx; |
| 1543 | event.mY = ty; |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1544 | } else if (mIsInterestedInDrag) { |
| 1545 | retval = super.dispatchDragEvent(event); |
Christopher Tate | 5ada6cb | 2010-10-05 14:15:29 -0700 | [diff] [blame] | 1546 | } else { |
| 1547 | if (ViewDebug.DEBUG_DRAG) { |
| 1548 | Log.d(View.VIEW_LOG_TAG, " not dropped on an accepting view"); |
| 1549 | } |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1550 | } |
| 1551 | } break; |
| 1552 | } |
| 1553 | |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1554 | return retval; |
| 1555 | } |
| 1556 | |
| 1557 | // Find the frontmost child view that lies under the given point, and calculate |
| 1558 | // the position within its own local coordinate system. |
| 1559 | View findFrontmostDroppableChildAt(float x, float y, PointF outLocalPoint) { |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1560 | final int count = mChildrenCount; |
| 1561 | final View[] children = mChildren; |
| 1562 | for (int i = count - 1; i >= 0; i--) { |
| 1563 | final View child = children[i]; |
Christopher Tate | 3d4bf17 | 2011-03-28 16:16:46 -0700 | [diff] [blame] | 1564 | if (!child.canAcceptDrag()) { |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1565 | continue; |
| 1566 | } |
| 1567 | |
Christopher Tate | 2c095f3 | 2010-10-04 14:13:40 -0700 | [diff] [blame] | 1568 | if (isTransformedTouchPointInView(x, y, child, outLocalPoint)) { |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1569 | return child; |
| 1570 | } |
| 1571 | } |
| 1572 | return null; |
| 1573 | } |
| 1574 | |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1575 | boolean notifyChildOfDragStart(View child) { |
| 1576 | // The caller guarantees that the child is not in mChildrenInterestedInDrag yet. |
| 1577 | |
Christopher Tate | 86cab1b | 2011-01-13 20:28:55 -0800 | [diff] [blame] | 1578 | if (ViewDebug.DEBUG_DRAG) { |
| 1579 | Log.d(View.VIEW_LOG_TAG, "Sending drag-started to view: " + child); |
| 1580 | } |
| 1581 | |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1582 | final boolean canAccept = child.dispatchDragEvent(mCurrentDragStartEvent); |
| 1583 | if (canAccept) { |
| 1584 | mChildrenInterestedInDrag.add(child); |
| 1585 | if (!child.canAcceptDrag()) { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 1586 | child.mPrivateFlags2 |= View.PFLAG2_DRAG_CAN_ACCEPT; |
Christopher Tate | 3d4bf17 | 2011-03-28 16:16:46 -0700 | [diff] [blame] | 1587 | child.refreshDrawableState(); |
| 1588 | } |
Christopher Tate | 86cab1b | 2011-01-13 20:28:55 -0800 | [diff] [blame] | 1589 | } |
Christopher Tate | 3d4bf17 | 2011-03-28 16:16:46 -0700 | [diff] [blame] | 1590 | return canAccept; |
Christopher Tate | 86cab1b | 2011-01-13 20:28:55 -0800 | [diff] [blame] | 1591 | } |
| 1592 | |
Joe Onorato | 664644d | 2011-01-23 17:53:23 -0800 | [diff] [blame] | 1593 | @Override |
Dianne Hackborn | 3a3a6cf | 2012-03-26 10:24:04 -0700 | [diff] [blame] | 1594 | public void dispatchWindowSystemUiVisiblityChanged(int visible) { |
| 1595 | super.dispatchWindowSystemUiVisiblityChanged(visible); |
| 1596 | |
| 1597 | final int count = mChildrenCount; |
| 1598 | final View[] children = mChildren; |
| 1599 | for (int i=0; i <count; i++) { |
| 1600 | final View child = children[i]; |
| 1601 | child.dispatchWindowSystemUiVisiblityChanged(visible); |
| 1602 | } |
| 1603 | } |
| 1604 | |
| 1605 | @Override |
Joe Onorato | 664644d | 2011-01-23 17:53:23 -0800 | [diff] [blame] | 1606 | public void dispatchSystemUiVisibilityChanged(int visible) { |
| 1607 | super.dispatchSystemUiVisibilityChanged(visible); |
| 1608 | |
| 1609 | final int count = mChildrenCount; |
| 1610 | final View[] children = mChildren; |
| 1611 | for (int i=0; i <count; i++) { |
| 1612 | final View child = children[i]; |
| 1613 | child.dispatchSystemUiVisibilityChanged(visible); |
| 1614 | } |
| 1615 | } |
| 1616 | |
Dianne Hackborn | 9a230e0 | 2011-10-06 11:51:27 -0700 | [diff] [blame] | 1617 | @Override |
Dianne Hackborn | cf67578 | 2012-05-10 15:07:24 -0700 | [diff] [blame] | 1618 | boolean updateLocalSystemUiVisibility(int localValue, int localChanges) { |
| 1619 | boolean changed = super.updateLocalSystemUiVisibility(localValue, localChanges); |
Dianne Hackborn | 9a230e0 | 2011-10-06 11:51:27 -0700 | [diff] [blame] | 1620 | |
| 1621 | final int count = mChildrenCount; |
| 1622 | final View[] children = mChildren; |
| 1623 | for (int i=0; i <count; i++) { |
| 1624 | final View child = children[i]; |
Dianne Hackborn | cf67578 | 2012-05-10 15:07:24 -0700 | [diff] [blame] | 1625 | changed |= child.updateLocalSystemUiVisibility(localValue, localChanges); |
Dianne Hackborn | 9a230e0 | 2011-10-06 11:51:27 -0700 | [diff] [blame] | 1626 | } |
Dianne Hackborn | cf67578 | 2012-05-10 15:07:24 -0700 | [diff] [blame] | 1627 | return changed; |
Dianne Hackborn | 9a230e0 | 2011-10-06 11:51:27 -0700 | [diff] [blame] | 1628 | } |
| 1629 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1630 | @Override |
| 1631 | public boolean dispatchKeyEventPreIme(KeyEvent event) { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 1632 | if ((mPrivateFlags & (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) |
| 1633 | == (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1634 | return super.dispatchKeyEventPreIme(event); |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 1635 | } else if (mFocused != null && (mFocused.mPrivateFlags & PFLAG_HAS_BOUNDS) |
| 1636 | == PFLAG_HAS_BOUNDS) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1637 | return mFocused.dispatchKeyEventPreIme(event); |
| 1638 | } |
| 1639 | return false; |
| 1640 | } |
| 1641 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1642 | @Override |
| 1643 | public boolean dispatchKeyEvent(KeyEvent event) { |
Jeff Brown | 21bc5c9 | 2011-02-28 18:27:14 -0800 | [diff] [blame] | 1644 | if (mInputEventConsistencyVerifier != null) { |
| 1645 | mInputEventConsistencyVerifier.onKeyEvent(event, 1); |
| 1646 | } |
| 1647 | |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 1648 | if ((mPrivateFlags & (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) |
| 1649 | == (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) { |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 1650 | if (super.dispatchKeyEvent(event)) { |
| 1651 | return true; |
| 1652 | } |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 1653 | } else if (mFocused != null && (mFocused.mPrivateFlags & PFLAG_HAS_BOUNDS) |
| 1654 | == PFLAG_HAS_BOUNDS) { |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 1655 | if (mFocused.dispatchKeyEvent(event)) { |
| 1656 | return true; |
| 1657 | } |
| 1658 | } |
| 1659 | |
| 1660 | if (mInputEventConsistencyVerifier != null) { |
| 1661 | mInputEventConsistencyVerifier.onUnhandledEvent(event, 1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1662 | } |
| 1663 | return false; |
| 1664 | } |
| 1665 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1666 | @Override |
| 1667 | public boolean dispatchKeyShortcutEvent(KeyEvent event) { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 1668 | if ((mPrivateFlags & (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) |
| 1669 | == (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1670 | return super.dispatchKeyShortcutEvent(event); |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 1671 | } else if (mFocused != null && (mFocused.mPrivateFlags & PFLAG_HAS_BOUNDS) |
| 1672 | == PFLAG_HAS_BOUNDS) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1673 | return mFocused.dispatchKeyShortcutEvent(event); |
| 1674 | } |
| 1675 | return false; |
| 1676 | } |
| 1677 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1678 | @Override |
| 1679 | public boolean dispatchTrackballEvent(MotionEvent event) { |
Jeff Brown | 21bc5c9 | 2011-02-28 18:27:14 -0800 | [diff] [blame] | 1680 | if (mInputEventConsistencyVerifier != null) { |
| 1681 | mInputEventConsistencyVerifier.onTrackballEvent(event, 1); |
| 1682 | } |
| 1683 | |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 1684 | if ((mPrivateFlags & (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) |
| 1685 | == (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) { |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 1686 | if (super.dispatchTrackballEvent(event)) { |
| 1687 | return true; |
| 1688 | } |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 1689 | } else if (mFocused != null && (mFocused.mPrivateFlags & PFLAG_HAS_BOUNDS) |
| 1690 | == PFLAG_HAS_BOUNDS) { |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 1691 | if (mFocused.dispatchTrackballEvent(event)) { |
| 1692 | return true; |
| 1693 | } |
| 1694 | } |
| 1695 | |
| 1696 | if (mInputEventConsistencyVerifier != null) { |
| 1697 | mInputEventConsistencyVerifier.onUnhandledEvent(event, 1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1698 | } |
| 1699 | return false; |
| 1700 | } |
| 1701 | |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 1702 | @Override |
Jun Mukai | d4eaef7 | 2015-10-30 15:54:33 -0700 | [diff] [blame] | 1703 | public PointerIcon getPointerIcon(MotionEvent event, float x, float y) { |
Keisuke Kuroyanagi | d85bc50 | 2016-01-21 14:50:38 +0900 | [diff] [blame] | 1704 | if (isOnScrollbarThumb(x, y) || isDraggingScrollBar()) { |
| 1705 | return PointerIcon.getSystemIcon(mContext, PointerIcon.STYLE_ARROW); |
| 1706 | } |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 1707 | // Check what the child under the pointer says about the pointer. |
| 1708 | final int childrenCount = mChildrenCount; |
| 1709 | if (childrenCount != 0) { |
| 1710 | final ArrayList<View> preorderedList = buildOrderedChildList(); |
| 1711 | final boolean customOrder = preorderedList == null |
| 1712 | && isChildrenDrawingOrderEnabled(); |
| 1713 | final View[] children = mChildren; |
| 1714 | for (int i = childrenCount - 1; i >= 0; i--) { |
Alan Viverette | a7b85e6 | 2016-01-22 10:14:02 -0500 | [diff] [blame] | 1715 | final int childIndex = getAndVerifyPreorderedIndex(childrenCount, i, customOrder); |
| 1716 | final View child = getAndVerifyPreorderedView(preorderedList, children, childIndex); |
| 1717 | final PointF point = getLocalPoint(); |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 1718 | if (isTransformedTouchPointInView(x, y, child, point)) { |
Jun Mukai | d4eaef7 | 2015-10-30 15:54:33 -0700 | [diff] [blame] | 1719 | final PointerIcon pointerIcon = child.getPointerIcon(event, point.x, point.y); |
| 1720 | if (pointerIcon != null) { |
| 1721 | return pointerIcon; |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 1722 | } |
| 1723 | break; |
| 1724 | } |
| 1725 | } |
| 1726 | } |
| 1727 | |
| 1728 | // The pointer is not a child or the child has no preferences, returning the default |
| 1729 | // implementation. |
Jun Mukai | d4eaef7 | 2015-10-30 15:54:33 -0700 | [diff] [blame] | 1730 | return super.getPointerIcon(event, x, y); |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 1731 | } |
| 1732 | |
Alan Viverette | a7b85e6 | 2016-01-22 10:14:02 -0500 | [diff] [blame] | 1733 | private int getAndVerifyPreorderedIndex(int childrenCount, int i, boolean customOrder) { |
| 1734 | final int childIndex; |
| 1735 | if (customOrder) { |
| 1736 | final int childIndex1 = getChildDrawingOrder(childrenCount, i); |
| 1737 | if (childIndex1 >= childrenCount) { |
| 1738 | throw new IndexOutOfBoundsException("getChildDrawingOrder() " |
| 1739 | + "returned invalid index " + childIndex1 |
| 1740 | + " (child count is " + childrenCount + ")"); |
| 1741 | } |
| 1742 | childIndex = childIndex1; |
| 1743 | } else { |
| 1744 | childIndex = i; |
| 1745 | } |
| 1746 | return childIndex; |
| 1747 | } |
| 1748 | |
Romain Guy | a948927 | 2011-06-22 20:58:11 -0700 | [diff] [blame] | 1749 | @SuppressWarnings({"ConstantConditions"}) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1750 | @Override |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 1751 | protected boolean dispatchHoverEvent(MotionEvent event) { |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 1752 | final int action = event.getAction(); |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 1753 | |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 1754 | // First check whether the view group wants to intercept the hover event. |
| 1755 | final boolean interceptHover = onInterceptHoverEvent(event); |
| 1756 | event.setAction(action); // restore action in case it was changed |
| 1757 | |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 1758 | MotionEvent eventNoHistory = event; |
| 1759 | boolean handled = false; |
| 1760 | |
| 1761 | // Send events to the hovered children and build a new list of hover targets until |
| 1762 | // one is found that handles the event. |
| 1763 | HoverTarget firstOldHoverTarget = mFirstHoverTarget; |
| 1764 | mFirstHoverTarget = null; |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 1765 | if (!interceptHover && action != MotionEvent.ACTION_HOVER_EXIT) { |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 1766 | final float x = event.getX(); |
| 1767 | final float y = event.getY(); |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 1768 | final int childrenCount = mChildrenCount; |
| 1769 | if (childrenCount != 0) { |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 1770 | final ArrayList<View> preorderedList = buildOrderedChildList(); |
| 1771 | final boolean customOrder = preorderedList == null |
| 1772 | && isChildrenDrawingOrderEnabled(); |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 1773 | final View[] children = mChildren; |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 1774 | HoverTarget lastHoverTarget = null; |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 1775 | for (int i = childrenCount - 1; i >= 0; i--) { |
Alan Viverette | a7b85e6 | 2016-01-22 10:14:02 -0500 | [diff] [blame] | 1776 | final int childIndex = getAndVerifyPreorderedIndex( |
| 1777 | childrenCount, i, customOrder); |
| 1778 | final View child = getAndVerifyPreorderedView( |
| 1779 | preorderedList, children, childIndex); |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 1780 | if (!canViewReceivePointerEvents(child) |
| 1781 | || !isTransformedTouchPointInView(x, y, child, null)) { |
| 1782 | continue; |
| 1783 | } |
| 1784 | |
| 1785 | // Obtain a hover target for this child. Dequeue it from the |
| 1786 | // old hover target list if the child was previously hovered. |
| 1787 | HoverTarget hoverTarget = firstOldHoverTarget; |
| 1788 | final boolean wasHovered; |
| 1789 | for (HoverTarget predecessor = null; ;) { |
| 1790 | if (hoverTarget == null) { |
| 1791 | hoverTarget = HoverTarget.obtain(child); |
| 1792 | wasHovered = false; |
| 1793 | break; |
| 1794 | } |
| 1795 | |
| 1796 | if (hoverTarget.child == child) { |
| 1797 | if (predecessor != null) { |
| 1798 | predecessor.next = hoverTarget.next; |
| 1799 | } else { |
| 1800 | firstOldHoverTarget = hoverTarget.next; |
| 1801 | } |
| 1802 | hoverTarget.next = null; |
| 1803 | wasHovered = true; |
| 1804 | break; |
| 1805 | } |
| 1806 | |
| 1807 | predecessor = hoverTarget; |
| 1808 | hoverTarget = hoverTarget.next; |
| 1809 | } |
| 1810 | |
| 1811 | // Enqueue the hover target onto the new hover target list. |
| 1812 | if (lastHoverTarget != null) { |
| 1813 | lastHoverTarget.next = hoverTarget; |
| 1814 | } else { |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 1815 | mFirstHoverTarget = hoverTarget; |
| 1816 | } |
Sangkyu Lee | 8725f36 | 2013-03-13 09:38:45 +0900 | [diff] [blame] | 1817 | lastHoverTarget = hoverTarget; |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 1818 | |
| 1819 | // Dispatch the event to the child. |
| 1820 | if (action == MotionEvent.ACTION_HOVER_ENTER) { |
| 1821 | if (!wasHovered) { |
| 1822 | // Send the enter as is. |
| 1823 | handled |= dispatchTransformedGenericPointerEvent( |
| 1824 | event, child); // enter |
| 1825 | } |
| 1826 | } else if (action == MotionEvent.ACTION_HOVER_MOVE) { |
| 1827 | if (!wasHovered) { |
| 1828 | // Synthesize an enter from a move. |
| 1829 | eventNoHistory = obtainMotionEventNoHistoryOrSelf(eventNoHistory); |
| 1830 | eventNoHistory.setAction(MotionEvent.ACTION_HOVER_ENTER); |
| 1831 | handled |= dispatchTransformedGenericPointerEvent( |
| 1832 | eventNoHistory, child); // enter |
| 1833 | eventNoHistory.setAction(action); |
| 1834 | |
| 1835 | handled |= dispatchTransformedGenericPointerEvent( |
| 1836 | eventNoHistory, child); // move |
| 1837 | } else { |
| 1838 | // Send the move as is. |
| 1839 | handled |= dispatchTransformedGenericPointerEvent(event, child); |
| 1840 | } |
| 1841 | } |
| 1842 | if (handled) { |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 1843 | break; |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 1844 | } |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 1845 | } |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 1846 | if (preorderedList != null) preorderedList.clear(); |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 1847 | } |
| 1848 | } |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 1849 | |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 1850 | // Send exit events to all previously hovered children that are no longer hovered. |
| 1851 | while (firstOldHoverTarget != null) { |
| 1852 | final View child = firstOldHoverTarget.child; |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 1853 | |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 1854 | // Exit the old hovered child. |
| 1855 | if (action == MotionEvent.ACTION_HOVER_EXIT) { |
| 1856 | // Send the exit as is. |
| 1857 | handled |= dispatchTransformedGenericPointerEvent( |
| 1858 | event, child); // exit |
| 1859 | } else { |
| 1860 | // Synthesize an exit from a move or enter. |
| 1861 | // Ignore the result because hover focus has moved to a different view. |
| 1862 | if (action == MotionEvent.ACTION_HOVER_MOVE) { |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 1863 | dispatchTransformedGenericPointerEvent( |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 1864 | event, child); // move |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 1865 | } |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 1866 | eventNoHistory = obtainMotionEventNoHistoryOrSelf(eventNoHistory); |
| 1867 | eventNoHistory.setAction(MotionEvent.ACTION_HOVER_EXIT); |
| 1868 | dispatchTransformedGenericPointerEvent( |
| 1869 | eventNoHistory, child); // exit |
| 1870 | eventNoHistory.setAction(action); |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 1871 | } |
| 1872 | |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 1873 | final HoverTarget nextOldHoverTarget = firstOldHoverTarget.next; |
| 1874 | firstOldHoverTarget.recycle(); |
| 1875 | firstOldHoverTarget = nextOldHoverTarget; |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 1876 | } |
| 1877 | |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 1878 | // Send events to the view group itself if no children have handled it. |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 1879 | boolean newHoveredSelf = !handled; |
| 1880 | if (newHoveredSelf == mHoveredSelf) { |
| 1881 | if (newHoveredSelf) { |
| 1882 | // Send event to the view group as before. |
| 1883 | handled |= super.dispatchHoverEvent(event); |
| 1884 | } |
| 1885 | } else { |
| 1886 | if (mHoveredSelf) { |
| 1887 | // Exit the view group. |
| 1888 | if (action == MotionEvent.ACTION_HOVER_EXIT) { |
| 1889 | // Send the exit as is. |
| 1890 | handled |= super.dispatchHoverEvent(event); // exit |
| 1891 | } else { |
| 1892 | // Synthesize an exit from a move or enter. |
| 1893 | // Ignore the result because hover focus is moving to a different view. |
| 1894 | if (action == MotionEvent.ACTION_HOVER_MOVE) { |
| 1895 | super.dispatchHoverEvent(event); // move |
| 1896 | } |
| 1897 | eventNoHistory = obtainMotionEventNoHistoryOrSelf(eventNoHistory); |
| 1898 | eventNoHistory.setAction(MotionEvent.ACTION_HOVER_EXIT); |
| 1899 | super.dispatchHoverEvent(eventNoHistory); // exit |
| 1900 | eventNoHistory.setAction(action); |
| 1901 | } |
| 1902 | mHoveredSelf = false; |
| 1903 | } |
| 1904 | |
| 1905 | if (newHoveredSelf) { |
| 1906 | // Enter the view group. |
| 1907 | if (action == MotionEvent.ACTION_HOVER_ENTER) { |
| 1908 | // Send the enter as is. |
| 1909 | handled |= super.dispatchHoverEvent(event); // enter |
| 1910 | mHoveredSelf = true; |
| 1911 | } else if (action == MotionEvent.ACTION_HOVER_MOVE) { |
| 1912 | // Synthesize an enter from a move. |
| 1913 | eventNoHistory = obtainMotionEventNoHistoryOrSelf(eventNoHistory); |
| 1914 | eventNoHistory.setAction(MotionEvent.ACTION_HOVER_ENTER); |
| 1915 | handled |= super.dispatchHoverEvent(eventNoHistory); // enter |
| 1916 | eventNoHistory.setAction(action); |
| 1917 | |
| 1918 | handled |= super.dispatchHoverEvent(eventNoHistory); // move |
| 1919 | mHoveredSelf = true; |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 1920 | } |
| 1921 | } |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 1922 | } |
| 1923 | |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 1924 | // Recycle the copy of the event that we made. |
| 1925 | if (eventNoHistory != event) { |
| 1926 | eventNoHistory.recycle(); |
| 1927 | } |
| 1928 | |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 1929 | // Done. |
| 1930 | return handled; |
| 1931 | } |
| 1932 | |
Jeff Brown | 59a422e | 2012-04-19 15:19:19 -0700 | [diff] [blame] | 1933 | private void exitHoverTargets() { |
| 1934 | if (mHoveredSelf || mFirstHoverTarget != null) { |
| 1935 | final long now = SystemClock.uptimeMillis(); |
| 1936 | MotionEvent event = MotionEvent.obtain(now, now, |
| 1937 | MotionEvent.ACTION_HOVER_EXIT, 0.0f, 0.0f, 0); |
| 1938 | event.setSource(InputDevice.SOURCE_TOUCHSCREEN); |
| 1939 | dispatchHoverEvent(event); |
| 1940 | event.recycle(); |
| 1941 | } |
| 1942 | } |
| 1943 | |
| 1944 | private void cancelHoverTarget(View view) { |
| 1945 | HoverTarget predecessor = null; |
| 1946 | HoverTarget target = mFirstHoverTarget; |
| 1947 | while (target != null) { |
| 1948 | final HoverTarget next = target.next; |
| 1949 | if (target.child == view) { |
| 1950 | if (predecessor == null) { |
| 1951 | mFirstHoverTarget = next; |
| 1952 | } else { |
| 1953 | predecessor.next = next; |
| 1954 | } |
| 1955 | target.recycle(); |
| 1956 | |
| 1957 | final long now = SystemClock.uptimeMillis(); |
| 1958 | MotionEvent event = MotionEvent.obtain(now, now, |
| 1959 | MotionEvent.ACTION_HOVER_EXIT, 0.0f, 0.0f, 0); |
| 1960 | event.setSource(InputDevice.SOURCE_TOUCHSCREEN); |
| 1961 | view.dispatchHoverEvent(event); |
| 1962 | event.recycle(); |
| 1963 | return; |
| 1964 | } |
| 1965 | predecessor = target; |
| 1966 | target = next; |
| 1967 | } |
| 1968 | } |
| 1969 | |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 1970 | /** @hide */ |
| 1971 | @Override |
| 1972 | protected boolean hasHoveredChild() { |
| 1973 | return mFirstHoverTarget != null; |
| 1974 | } |
| 1975 | |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 1976 | @Override |
Adam Powell | cf392d1 | 2015-06-25 14:48:45 -0700 | [diff] [blame] | 1977 | public void addChildrenForAccessibility(ArrayList<View> outChildren) { |
Svetoslav | 762621c | 2015-06-01 17:21:59 -0700 | [diff] [blame] | 1978 | if (getAccessibilityNodeProvider() != null) { |
| 1979 | return; |
| 1980 | } |
Svetoslav Ganov | 76f287e | 2012-04-23 11:02:36 -0700 | [diff] [blame] | 1981 | ChildListForAccessibility children = ChildListForAccessibility.obtain(this, true); |
| 1982 | try { |
| 1983 | final int childrenCount = children.getChildCount(); |
| 1984 | for (int i = 0; i < childrenCount; i++) { |
| 1985 | View child = children.getChildAt(i); |
Svetoslav Ganov | c406be9 | 2012-05-11 16:12:32 -0700 | [diff] [blame] | 1986 | if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) { |
Svetoslav Ganov | 76f287e | 2012-04-23 11:02:36 -0700 | [diff] [blame] | 1987 | if (child.includeForAccessibility()) { |
Adam Powell | cf392d1 | 2015-06-25 14:48:45 -0700 | [diff] [blame] | 1988 | outChildren.add(child); |
Svetoslav Ganov | 76f287e | 2012-04-23 11:02:36 -0700 | [diff] [blame] | 1989 | } else { |
Adam Powell | cf392d1 | 2015-06-25 14:48:45 -0700 | [diff] [blame] | 1990 | child.addChildrenForAccessibility(outChildren); |
Svetoslav Ganov | 76f287e | 2012-04-23 11:02:36 -0700 | [diff] [blame] | 1991 | } |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 1992 | } |
| 1993 | } |
Svetoslav Ganov | 76f287e | 2012-04-23 11:02:36 -0700 | [diff] [blame] | 1994 | } finally { |
| 1995 | children.recycle(); |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 1996 | } |
| 1997 | } |
| 1998 | |
| 1999 | /** |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 2000 | * Implement this method to intercept hover events before they are handled |
| 2001 | * by child views. |
| 2002 | * <p> |
| 2003 | * This method is called before dispatching a hover event to a child of |
| 2004 | * the view group or to the view group's own {@link #onHoverEvent} to allow |
| 2005 | * the view group a chance to intercept the hover event. |
| 2006 | * This method can also be used to watch all pointer motions that occur within |
| 2007 | * the bounds of the view group even when the pointer is hovering over |
| 2008 | * a child of the view group rather than over the view group itself. |
| 2009 | * </p><p> |
| 2010 | * The view group can prevent its children from receiving hover events by |
| 2011 | * implementing this method and returning <code>true</code> to indicate |
| 2012 | * that it would like to intercept hover events. The view group must |
| 2013 | * continuously return <code>true</code> from {@link #onInterceptHoverEvent} |
| 2014 | * for as long as it wishes to continue intercepting hover events from |
| 2015 | * its children. |
| 2016 | * </p><p> |
| 2017 | * Interception preserves the invariant that at most one view can be |
| 2018 | * hovered at a time by transferring hover focus from the currently hovered |
| 2019 | * child to the view group or vice-versa as needed. |
| 2020 | * </p><p> |
| 2021 | * If this method returns <code>true</code> and a child is already hovered, then the |
| 2022 | * child view will first receive a hover exit event and then the view group |
| 2023 | * itself will receive a hover enter event in {@link #onHoverEvent}. |
| 2024 | * Likewise, if this method had previously returned <code>true</code> to intercept hover |
| 2025 | * events and instead returns <code>false</code> while the pointer is hovering |
| 2026 | * within the bounds of one of a child, then the view group will first receive a |
| 2027 | * hover exit event in {@link #onHoverEvent} and then the hovered child will |
| 2028 | * receive a hover enter event. |
| 2029 | * </p><p> |
Keisuke Kuroyanagi | d85bc50 | 2016-01-21 14:50:38 +0900 | [diff] [blame] | 2030 | * The default implementation handles mouse hover on the scroll bars. |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 2031 | * </p> |
| 2032 | * |
| 2033 | * @param event The motion event that describes the hover. |
| 2034 | * @return True if the view group would like to intercept the hover event |
| 2035 | * and prevent its children from receiving it. |
| 2036 | */ |
| 2037 | public boolean onInterceptHoverEvent(MotionEvent event) { |
Keisuke Kuroyanagi | d85bc50 | 2016-01-21 14:50:38 +0900 | [diff] [blame] | 2038 | if (event.isFromSource(InputDevice.SOURCE_MOUSE)) { |
| 2039 | final int action = event.getAction(); |
| 2040 | final float x = event.getX(); |
| 2041 | final float y = event.getY(); |
| 2042 | if ((action == MotionEvent.ACTION_HOVER_MOVE |
| 2043 | || action == MotionEvent.ACTION_HOVER_ENTER) && isOnScrollbar(x, y)) { |
| 2044 | return true; |
| 2045 | } |
| 2046 | } |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 2047 | return false; |
| 2048 | } |
| 2049 | |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2050 | private static MotionEvent obtainMotionEventNoHistoryOrSelf(MotionEvent event) { |
| 2051 | if (event.getHistorySize() == 0) { |
| 2052 | return event; |
| 2053 | } |
| 2054 | return MotionEvent.obtainNoHistory(event); |
| 2055 | } |
| 2056 | |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2057 | @Override |
| 2058 | protected boolean dispatchGenericPointerEvent(MotionEvent event) { |
| 2059 | // Send the event to the child under the pointer. |
| 2060 | final int childrenCount = mChildrenCount; |
| 2061 | if (childrenCount != 0) { |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2062 | final float x = event.getX(); |
| 2063 | final float y = event.getY(); |
| 2064 | |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 2065 | final ArrayList<View> preorderedList = buildOrderedChildList(); |
| 2066 | final boolean customOrder = preorderedList == null |
| 2067 | && isChildrenDrawingOrderEnabled(); |
| 2068 | final View[] children = mChildren; |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2069 | for (int i = childrenCount - 1; i >= 0; i--) { |
Alan Viverette | a7b85e6 | 2016-01-22 10:14:02 -0500 | [diff] [blame] | 2070 | final int childIndex = getAndVerifyPreorderedIndex(childrenCount, i, customOrder); |
| 2071 | final View child = getAndVerifyPreorderedView(preorderedList, children, childIndex); |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2072 | if (!canViewReceivePointerEvents(child) |
| 2073 | || !isTransformedTouchPointInView(x, y, child, null)) { |
| 2074 | continue; |
| 2075 | } |
| 2076 | |
| 2077 | if (dispatchTransformedGenericPointerEvent(event, child)) { |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 2078 | if (preorderedList != null) preorderedList.clear(); |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2079 | return true; |
| 2080 | } |
| 2081 | } |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 2082 | if (preorderedList != null) preorderedList.clear(); |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2083 | } |
| 2084 | |
| 2085 | // No child handled the event. Send it to this view group. |
| 2086 | return super.dispatchGenericPointerEvent(event); |
| 2087 | } |
| 2088 | |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2089 | @Override |
| 2090 | protected boolean dispatchGenericFocusedEvent(MotionEvent event) { |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 2091 | // 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] | 2092 | if ((mPrivateFlags & (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) |
| 2093 | == (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) { |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2094 | return super.dispatchGenericFocusedEvent(event); |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 2095 | } else if (mFocused != null && (mFocused.mPrivateFlags & PFLAG_HAS_BOUNDS) |
| 2096 | == PFLAG_HAS_BOUNDS) { |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 2097 | return mFocused.dispatchGenericMotionEvent(event); |
| 2098 | } |
| 2099 | return false; |
| 2100 | } |
| 2101 | |
| 2102 | /** |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2103 | * Dispatches a generic pointer event to a child, taking into account |
| 2104 | * transformations that apply to the child. |
| 2105 | * |
| 2106 | * @param event The event to send. |
| 2107 | * @param child The view to send the event to. |
| 2108 | * @return {@code true} if the child handled the event. |
| 2109 | */ |
| 2110 | private boolean dispatchTransformedGenericPointerEvent(MotionEvent event, View child) { |
| 2111 | final float offsetX = mScrollX - child.mLeft; |
| 2112 | final float offsetY = mScrollY - child.mTop; |
| 2113 | |
| 2114 | boolean handled; |
| 2115 | if (!child.hasIdentityMatrix()) { |
| 2116 | MotionEvent transformedEvent = MotionEvent.obtain(event); |
| 2117 | transformedEvent.offsetLocation(offsetX, offsetY); |
| 2118 | transformedEvent.transform(child.getInverseMatrix()); |
| 2119 | handled = child.dispatchGenericMotionEvent(transformedEvent); |
| 2120 | transformedEvent.recycle(); |
| 2121 | } else { |
| 2122 | event.offsetLocation(offsetX, offsetY); |
| 2123 | handled = child.dispatchGenericMotionEvent(event); |
| 2124 | event.offsetLocation(-offsetX, -offsetY); |
| 2125 | } |
| 2126 | return handled; |
| 2127 | } |
| 2128 | |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 2129 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2130 | public boolean dispatchTouchEvent(MotionEvent ev) { |
Jeff Brown | 21bc5c9 | 2011-02-28 18:27:14 -0800 | [diff] [blame] | 2131 | if (mInputEventConsistencyVerifier != null) { |
| 2132 | mInputEventConsistencyVerifier.onTouchEvent(ev, 1); |
| 2133 | } |
| 2134 | |
Svet Ganov | 0a2ccee | 2015-02-06 10:12:32 -0800 | [diff] [blame] | 2135 | // If the event targets the accessibility focused view and this is it, start |
| 2136 | // normal event dispatch. Maybe a descendant is what will handle the click. |
Svet Ganov | f33fe1f | 2015-02-06 19:23:31 -0800 | [diff] [blame] | 2137 | if (ev.isTargetAccessibilityFocus() && isAccessibilityFocusedViewOrHost()) { |
Svet Ganov | 0a2ccee | 2015-02-06 10:12:32 -0800 | [diff] [blame] | 2138 | ev.setTargetAccessibilityFocus(false); |
| 2139 | } |
| 2140 | |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2141 | boolean handled = false; |
| 2142 | if (onFilterTouchEventForSecurity(ev)) { |
| 2143 | final int action = ev.getAction(); |
| 2144 | final int actionMasked = action & MotionEvent.ACTION_MASK; |
Jeff Brown | 85a3176 | 2010-09-01 17:01:00 -0700 | [diff] [blame] | 2145 | |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2146 | // Handle an initial down. |
| 2147 | if (actionMasked == MotionEvent.ACTION_DOWN) { |
| 2148 | // Throw away all previous state when starting a new touch gesture. |
| 2149 | // The framework may have dropped the up or cancel event for the previous gesture |
| 2150 | // due to an app switch, ANR, or some other state change. |
| 2151 | cancelAndClearTouchTargets(ev); |
| 2152 | resetTouchState(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2153 | } |
Adam Powell | b08013c | 2010-09-16 16:28:11 -0700 | [diff] [blame] | 2154 | |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2155 | // Check for interception. |
| 2156 | final boolean intercepted; |
Svet Ganov | f33fe1f | 2015-02-06 19:23:31 -0800 | [diff] [blame] | 2157 | if (actionMasked == MotionEvent.ACTION_DOWN |
| 2158 | || mFirstTouchTarget != null) { |
| 2159 | final boolean disallowIntercept = (mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0; |
| 2160 | if (!disallowIntercept) { |
| 2161 | intercepted = onInterceptTouchEvent(ev); |
| 2162 | ev.setAction(action); // restore action in case it was changed |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2163 | } else { |
Svet Ganov | f33fe1f | 2015-02-06 19:23:31 -0800 | [diff] [blame] | 2164 | intercepted = false; |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2165 | } |
| 2166 | } else { |
Svet Ganov | f33fe1f | 2015-02-06 19:23:31 -0800 | [diff] [blame] | 2167 | // There are no touch targets and this action is not an initial down |
| 2168 | // so this view group continues to intercept touches. |
| 2169 | intercepted = true; |
| 2170 | } |
| 2171 | |
| 2172 | // If intercepted, start normal event dispatch. Also if there is already |
| 2173 | // a view that is handling the gesture, do normal event dispatch. |
| 2174 | if (intercepted || mFirstTouchTarget != null) { |
| 2175 | ev.setTargetAccessibilityFocus(false); |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2176 | } |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2177 | |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2178 | // Check for cancelation. |
| 2179 | final boolean canceled = resetCancelNextUpFlag(this) |
| 2180 | || actionMasked == MotionEvent.ACTION_CANCEL; |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2181 | |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2182 | // Update list of touch targets for pointer down, if needed. |
| 2183 | final boolean split = (mGroupFlags & FLAG_SPLIT_MOTION_EVENTS) != 0; |
| 2184 | TouchTarget newTouchTarget = null; |
| 2185 | boolean alreadyDispatchedToNewTouchTarget = false; |
| 2186 | if (!canceled && !intercepted) { |
Svetoslav | c73cfa0 | 2015-02-09 17:14:28 -0800 | [diff] [blame] | 2187 | |
| 2188 | // If the event is targeting accessiiblity focus we give it to the |
| 2189 | // view that has accessibility focus and if it does not handle it |
| 2190 | // we clear the flag and dispatch the event to all children as usual. |
| 2191 | // We are looking up the accessibility focused host to avoid keeping |
| 2192 | // state since these events are very rare. |
| 2193 | View childWithAccessibilityFocus = ev.isTargetAccessibilityFocus() |
| 2194 | ? findChildWithAccessibilityFocus() : null; |
| 2195 | |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2196 | if (actionMasked == MotionEvent.ACTION_DOWN |
| 2197 | || (split && actionMasked == MotionEvent.ACTION_POINTER_DOWN) |
Svet Ganov | f33fe1f | 2015-02-06 19:23:31 -0800 | [diff] [blame] | 2198 | || actionMasked == MotionEvent.ACTION_HOVER_MOVE) { |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2199 | final int actionIndex = ev.getActionIndex(); // always 0 for down |
| 2200 | final int idBitsToAssign = split ? 1 << ev.getPointerId(actionIndex) |
| 2201 | : TouchTarget.ALL_POINTER_IDS; |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2202 | |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2203 | // Clean up earlier touch targets for this pointer id in case they |
| 2204 | // have become out of sync. |
| 2205 | removePointersFromTouchTargets(idBitsToAssign); |
| 2206 | |
| 2207 | final int childrenCount = mChildrenCount; |
Chet Haase | 9c17fe6 | 2013-03-22 17:05:55 -0700 | [diff] [blame] | 2208 | if (newTouchTarget == null && childrenCount != 0) { |
Chet Haase | edf6f4b | 2013-03-26 07:55:30 -0700 | [diff] [blame] | 2209 | final float x = ev.getX(actionIndex); |
| 2210 | final float y = ev.getY(actionIndex); |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2211 | // Find a child that can receive the event. |
| 2212 | // Scan children from front to back. |
Filip Gruszczynski | a33bdf3 | 2015-11-19 18:22:16 -0800 | [diff] [blame] | 2213 | final ArrayList<View> preorderedList = buildTouchDispatchChildList(); |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 2214 | final boolean customOrder = preorderedList == null |
| 2215 | && isChildrenDrawingOrderEnabled(); |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2216 | final View[] children = mChildren; |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2217 | for (int i = childrenCount - 1; i >= 0; i--) { |
Alan Viverette | a7b85e6 | 2016-01-22 10:14:02 -0500 | [diff] [blame] | 2218 | final int childIndex = getAndVerifyPreorderedIndex( |
| 2219 | childrenCount, i, customOrder); |
| 2220 | final View child = getAndVerifyPreorderedView( |
| 2221 | preorderedList, children, childIndex); |
Svetoslav | c73cfa0 | 2015-02-09 17:14:28 -0800 | [diff] [blame] | 2222 | |
| 2223 | // If there is a view that has accessibility focus we want it |
| 2224 | // to get the event first and if not handled we will perform a |
| 2225 | // normal dispatch. We may do a double iteration but this is |
| 2226 | // safer given the timeframe. |
| 2227 | if (childWithAccessibilityFocus != null) { |
| 2228 | if (childWithAccessibilityFocus != child) { |
| 2229 | continue; |
| 2230 | } |
| 2231 | childWithAccessibilityFocus = null; |
| 2232 | i = childrenCount - 1; |
| 2233 | } |
| 2234 | |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2235 | if (!canViewReceivePointerEvents(child) |
| 2236 | || !isTransformedTouchPointInView(x, y, child, null)) { |
Svetoslav | c73cfa0 | 2015-02-09 17:14:28 -0800 | [diff] [blame] | 2237 | ev.setTargetAccessibilityFocus(false); |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2238 | continue; |
| 2239 | } |
| 2240 | |
| 2241 | newTouchTarget = getTouchTarget(child); |
| 2242 | if (newTouchTarget != null) { |
| 2243 | // Child is already receiving touch within its bounds. |
| 2244 | // Give it the new pointer in addition to the ones it is handling. |
| 2245 | newTouchTarget.pointerIdBits |= idBitsToAssign; |
| 2246 | break; |
| 2247 | } |
| 2248 | |
| 2249 | resetCancelNextUpFlag(child); |
| 2250 | if (dispatchTransformedTouchEvent(ev, false, child, idBitsToAssign)) { |
| 2251 | // Child wants to receive touch within its bounds. |
| 2252 | mLastTouchDownTime = ev.getDownTime(); |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 2253 | if (preorderedList != null) { |
| 2254 | // childIndex points into presorted list, find original index |
| 2255 | for (int j = 0; j < childrenCount; j++) { |
| 2256 | if (children[childIndex] == mChildren[j]) { |
| 2257 | mLastTouchDownIndex = j; |
| 2258 | break; |
| 2259 | } |
| 2260 | } |
| 2261 | } else { |
| 2262 | mLastTouchDownIndex = childIndex; |
| 2263 | } |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2264 | mLastTouchDownX = ev.getX(); |
| 2265 | mLastTouchDownY = ev.getY(); |
| 2266 | newTouchTarget = addTouchTarget(child, idBitsToAssign); |
| 2267 | alreadyDispatchedToNewTouchTarget = true; |
| 2268 | break; |
| 2269 | } |
Svetoslav | c73cfa0 | 2015-02-09 17:14:28 -0800 | [diff] [blame] | 2270 | |
| 2271 | // The accessibility focus didn't handle the event, so clear |
| 2272 | // the flag and do a normal dispatch to all children. |
| 2273 | ev.setTargetAccessibilityFocus(false); |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2274 | } |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 2275 | if (preorderedList != null) preorderedList.clear(); |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2276 | } |
| 2277 | |
| 2278 | if (newTouchTarget == null && mFirstTouchTarget != null) { |
| 2279 | // Did not find a child to receive the event. |
| 2280 | // Assign the pointer to the least recently added target. |
| 2281 | newTouchTarget = mFirstTouchTarget; |
| 2282 | while (newTouchTarget.next != null) { |
| 2283 | newTouchTarget = newTouchTarget.next; |
| 2284 | } |
| 2285 | newTouchTarget.pointerIdBits |= idBitsToAssign; |
| 2286 | } |
| 2287 | } |
| 2288 | } |
| 2289 | |
| 2290 | // Dispatch to touch targets. |
| 2291 | if (mFirstTouchTarget == null) { |
| 2292 | // No touch targets so treat this as an ordinary view. |
| 2293 | handled = dispatchTransformedTouchEvent(ev, canceled, null, |
| 2294 | TouchTarget.ALL_POINTER_IDS); |
| 2295 | } else { |
| 2296 | // Dispatch to touch targets, excluding the new touch target if we already |
| 2297 | // dispatched to it. Cancel touch targets if necessary. |
| 2298 | TouchTarget predecessor = null; |
| 2299 | TouchTarget target = mFirstTouchTarget; |
| 2300 | while (target != null) { |
| 2301 | final TouchTarget next = target.next; |
| 2302 | if (alreadyDispatchedToNewTouchTarget && target == newTouchTarget) { |
| 2303 | handled = true; |
| 2304 | } else { |
| 2305 | final boolean cancelChild = resetCancelNextUpFlag(target.child) |
Chet Haase | 9c17fe6 | 2013-03-22 17:05:55 -0700 | [diff] [blame] | 2306 | || intercepted; |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2307 | if (dispatchTransformedTouchEvent(ev, cancelChild, |
| 2308 | target.child, target.pointerIdBits)) { |
| 2309 | handled = true; |
| 2310 | } |
| 2311 | if (cancelChild) { |
| 2312 | if (predecessor == null) { |
| 2313 | mFirstTouchTarget = next; |
| 2314 | } else { |
| 2315 | predecessor.next = next; |
| 2316 | } |
| 2317 | target.recycle(); |
| 2318 | target = next; |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2319 | continue; |
| 2320 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2321 | } |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2322 | predecessor = target; |
| 2323 | target = next; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2324 | } |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2325 | } |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2326 | |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2327 | // Update list of touch targets for pointer up or cancel, if needed. |
| 2328 | if (canceled |
| 2329 | || actionMasked == MotionEvent.ACTION_UP |
| 2330 | || actionMasked == MotionEvent.ACTION_HOVER_MOVE) { |
| 2331 | resetTouchState(); |
| 2332 | } else if (split && actionMasked == MotionEvent.ACTION_POINTER_UP) { |
| 2333 | final int actionIndex = ev.getActionIndex(); |
| 2334 | final int idBitsToRemove = 1 << ev.getPointerId(actionIndex); |
| 2335 | removePointersFromTouchTargets(idBitsToRemove); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2336 | } |
| 2337 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 2338 | |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2339 | if (!handled && mInputEventConsistencyVerifier != null) { |
| 2340 | mInputEventConsistencyVerifier.onUnhandledEvent(ev, 1); |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2341 | } |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2342 | return handled; |
| 2343 | } |
| 2344 | |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 2345 | /** |
Filip Gruszczynski | a33bdf3 | 2015-11-19 18:22:16 -0800 | [diff] [blame] | 2346 | * Provide custom ordering of views in which the touch will be dispatched. |
| 2347 | * |
| 2348 | * This is called within a tight loop, so you are not allowed to allocate objects, including |
| 2349 | * the return array. Instead, you should return a pre-allocated list that will be cleared |
| 2350 | * after the dispatch is finished. |
| 2351 | * @hide |
| 2352 | */ |
| 2353 | public ArrayList<View> buildTouchDispatchChildList() { |
| 2354 | return buildOrderedChildList(); |
| 2355 | } |
| 2356 | |
| 2357 | /** |
Svetoslav | c73cfa0 | 2015-02-09 17:14:28 -0800 | [diff] [blame] | 2358 | * Finds the child which has accessibility focus. |
| 2359 | * |
| 2360 | * @return The child that has focus. |
| 2361 | */ |
| 2362 | private View findChildWithAccessibilityFocus() { |
| 2363 | ViewRootImpl viewRoot = getViewRootImpl(); |
| 2364 | if (viewRoot == null) { |
| 2365 | return null; |
| 2366 | } |
| 2367 | |
| 2368 | View current = viewRoot.getAccessibilityFocusedHost(); |
| 2369 | if (current == null) { |
| 2370 | return null; |
| 2371 | } |
| 2372 | |
| 2373 | ViewParent parent = current.getParent(); |
| 2374 | while (parent instanceof View) { |
| 2375 | if (parent == this) { |
| 2376 | return current; |
| 2377 | } |
| 2378 | current = (View) parent; |
| 2379 | parent = current.getParent(); |
| 2380 | } |
| 2381 | |
| 2382 | return null; |
| 2383 | } |
| 2384 | |
| 2385 | /** |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 2386 | * Resets all touch state in preparation for a new cycle. |
| 2387 | */ |
| 2388 | private void resetTouchState() { |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2389 | clearTouchTargets(); |
| 2390 | resetCancelNextUpFlag(this); |
| 2391 | mGroupFlags &= ~FLAG_DISALLOW_INTERCEPT; |
Adam Powell | 10ba277 | 2014-04-15 09:46:51 -0700 | [diff] [blame] | 2392 | mNestedScrollAxes = SCROLL_AXIS_NONE; |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2393 | } |
| 2394 | |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 2395 | /** |
| 2396 | * Resets the cancel next up flag. |
| 2397 | * Returns true if the flag was previously set. |
| 2398 | */ |
Alan Viverette | a7b85e6 | 2016-01-22 10:14:02 -0500 | [diff] [blame] | 2399 | private static boolean resetCancelNextUpFlag(@NonNull View view) { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 2400 | if ((view.mPrivateFlags & PFLAG_CANCEL_NEXT_UP_EVENT) != 0) { |
| 2401 | view.mPrivateFlags &= ~PFLAG_CANCEL_NEXT_UP_EVENT; |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2402 | return true; |
Adam Cohen | 9b07394 | 2010-08-19 16:49:52 -0700 | [diff] [blame] | 2403 | } |
| 2404 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2405 | } |
| 2406 | |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 2407 | /** |
| 2408 | * Clears all touch targets. |
| 2409 | */ |
| 2410 | private void clearTouchTargets() { |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2411 | TouchTarget target = mFirstTouchTarget; |
| 2412 | if (target != null) { |
| 2413 | do { |
| 2414 | TouchTarget next = target.next; |
| 2415 | target.recycle(); |
| 2416 | target = next; |
| 2417 | } while (target != null); |
| 2418 | mFirstTouchTarget = null; |
| 2419 | } |
| 2420 | } |
| 2421 | |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 2422 | /** |
| 2423 | * Cancels and clears all touch targets. |
| 2424 | */ |
| 2425 | private void cancelAndClearTouchTargets(MotionEvent event) { |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2426 | if (mFirstTouchTarget != null) { |
| 2427 | boolean syntheticEvent = false; |
| 2428 | if (event == null) { |
| 2429 | final long now = SystemClock.uptimeMillis(); |
| 2430 | event = MotionEvent.obtain(now, now, |
| 2431 | MotionEvent.ACTION_CANCEL, 0.0f, 0.0f, 0); |
Jeff Brown | 2fdbc5a | 2011-06-30 12:25:54 -0700 | [diff] [blame] | 2432 | event.setSource(InputDevice.SOURCE_TOUCHSCREEN); |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2433 | syntheticEvent = true; |
| 2434 | } |
| 2435 | |
| 2436 | for (TouchTarget target = mFirstTouchTarget; target != null; target = target.next) { |
| 2437 | resetCancelNextUpFlag(target.child); |
| 2438 | dispatchTransformedTouchEvent(event, true, target.child, target.pointerIdBits); |
| 2439 | } |
| 2440 | clearTouchTargets(); |
| 2441 | |
| 2442 | if (syntheticEvent) { |
| 2443 | event.recycle(); |
| 2444 | } |
| 2445 | } |
| 2446 | } |
| 2447 | |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 2448 | /** |
| 2449 | * Gets the touch target for specified child view. |
| 2450 | * Returns null if not found. |
| 2451 | */ |
Alan Viverette | a7b85e6 | 2016-01-22 10:14:02 -0500 | [diff] [blame] | 2452 | private TouchTarget getTouchTarget(@NonNull View child) { |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2453 | for (TouchTarget target = mFirstTouchTarget; target != null; target = target.next) { |
| 2454 | if (target.child == child) { |
| 2455 | return target; |
| 2456 | } |
| 2457 | } |
| 2458 | return null; |
| 2459 | } |
| 2460 | |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 2461 | /** |
| 2462 | * Adds a touch target for specified child to the beginning of the list. |
| 2463 | * Assumes the target child is not already present. |
| 2464 | */ |
Alan Viverette | a7b85e6 | 2016-01-22 10:14:02 -0500 | [diff] [blame] | 2465 | private TouchTarget addTouchTarget(@NonNull View child, int pointerIdBits) { |
| 2466 | final TouchTarget target = TouchTarget.obtain(child, pointerIdBits); |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2467 | target.next = mFirstTouchTarget; |
| 2468 | mFirstTouchTarget = target; |
| 2469 | return target; |
| 2470 | } |
| 2471 | |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 2472 | /** |
| 2473 | * Removes the pointer ids from consideration. |
| 2474 | */ |
| 2475 | private void removePointersFromTouchTargets(int pointerIdBits) { |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2476 | TouchTarget predecessor = null; |
| 2477 | TouchTarget target = mFirstTouchTarget; |
| 2478 | while (target != null) { |
| 2479 | final TouchTarget next = target.next; |
| 2480 | if ((target.pointerIdBits & pointerIdBits) != 0) { |
| 2481 | target.pointerIdBits &= ~pointerIdBits; |
| 2482 | if (target.pointerIdBits == 0) { |
| 2483 | if (predecessor == null) { |
| 2484 | mFirstTouchTarget = next; |
| 2485 | } else { |
| 2486 | predecessor.next = next; |
| 2487 | } |
| 2488 | target.recycle(); |
| 2489 | target = next; |
| 2490 | continue; |
| 2491 | } |
| 2492 | } |
| 2493 | predecessor = target; |
| 2494 | target = next; |
| 2495 | } |
| 2496 | } |
| 2497 | |
Jeff Brown | 59a422e | 2012-04-19 15:19:19 -0700 | [diff] [blame] | 2498 | private void cancelTouchTarget(View view) { |
| 2499 | TouchTarget predecessor = null; |
| 2500 | TouchTarget target = mFirstTouchTarget; |
| 2501 | while (target != null) { |
| 2502 | final TouchTarget next = target.next; |
| 2503 | if (target.child == view) { |
| 2504 | if (predecessor == null) { |
| 2505 | mFirstTouchTarget = next; |
| 2506 | } else { |
| 2507 | predecessor.next = next; |
| 2508 | } |
| 2509 | target.recycle(); |
| 2510 | |
| 2511 | final long now = SystemClock.uptimeMillis(); |
| 2512 | MotionEvent event = MotionEvent.obtain(now, now, |
| 2513 | MotionEvent.ACTION_CANCEL, 0.0f, 0.0f, 0); |
| 2514 | event.setSource(InputDevice.SOURCE_TOUCHSCREEN); |
| 2515 | view.dispatchTouchEvent(event); |
| 2516 | event.recycle(); |
| 2517 | return; |
| 2518 | } |
| 2519 | predecessor = target; |
| 2520 | target = next; |
| 2521 | } |
| 2522 | } |
| 2523 | |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 2524 | /** |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2525 | * Returns true if a child view can receive pointer events. |
| 2526 | * @hide |
| 2527 | */ |
Alan Viverette | a7b85e6 | 2016-01-22 10:14:02 -0500 | [diff] [blame] | 2528 | private static boolean canViewReceivePointerEvents(@NonNull View child) { |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2529 | return (child.mViewFlags & VISIBILITY_MASK) == VISIBLE |
| 2530 | || child.getAnimation() != null; |
| 2531 | } |
| 2532 | |
Alan Viverette | b942b6f | 2014-12-08 10:37:39 -0800 | [diff] [blame] | 2533 | private float[] getTempPoint() { |
| 2534 | if (mTempPoint == null) { |
| 2535 | mTempPoint = new float[2]; |
| 2536 | } |
| 2537 | return mTempPoint; |
| 2538 | } |
| 2539 | |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2540 | /** |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 2541 | * Returns true if a child view contains the specified point when transformed |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2542 | * into its coordinate space. |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 2543 | * Child must not be null. |
Adam Cohen | a32edd4 | 2010-10-26 10:35:01 -0700 | [diff] [blame] | 2544 | * @hide |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 2545 | */ |
Adam Cohen | a32edd4 | 2010-10-26 10:35:01 -0700 | [diff] [blame] | 2546 | protected boolean isTransformedTouchPointInView(float x, float y, View child, |
Christopher Tate | 2c095f3 | 2010-10-04 14:13:40 -0700 | [diff] [blame] | 2547 | PointF outLocalPoint) { |
Alan Viverette | b942b6f | 2014-12-08 10:37:39 -0800 | [diff] [blame] | 2548 | final float[] point = getTempPoint(); |
| 2549 | point[0] = x; |
| 2550 | point[1] = y; |
| 2551 | transformPointToViewLocal(point, child); |
| 2552 | final boolean isInView = child.pointInView(point[0], point[1]); |
Christopher Tate | 2c095f3 | 2010-10-04 14:13:40 -0700 | [diff] [blame] | 2553 | if (isInView && outLocalPoint != null) { |
Alan Viverette | b942b6f | 2014-12-08 10:37:39 -0800 | [diff] [blame] | 2554 | outLocalPoint.set(point[0], point[1]); |
Christopher Tate | 2c095f3 | 2010-10-04 14:13:40 -0700 | [diff] [blame] | 2555 | } |
| 2556 | return isInView; |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 2557 | } |
| 2558 | |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 2559 | /** |
Alan Viverette | b942b6f | 2014-12-08 10:37:39 -0800 | [diff] [blame] | 2560 | * @hide |
| 2561 | */ |
| 2562 | public void transformPointToViewLocal(float[] point, View child) { |
| 2563 | point[0] += mScrollX - child.mLeft; |
| 2564 | point[1] += mScrollY - child.mTop; |
| 2565 | |
| 2566 | if (!child.hasIdentityMatrix()) { |
| 2567 | child.getInverseMatrix().mapPoints(point); |
| 2568 | } |
| 2569 | } |
| 2570 | |
| 2571 | /** |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 2572 | * 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] | 2573 | * filters out irrelevant pointer ids, and overrides its action if necessary. |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 2574 | * If child is null, assumes the MotionEvent will be sent to this ViewGroup instead. |
| 2575 | */ |
| 2576 | private boolean dispatchTransformedTouchEvent(MotionEvent event, boolean cancel, |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2577 | View child, int desiredPointerIdBits) { |
| 2578 | final boolean handled; |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 2579 | |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2580 | // Canceling motions is a special case. We don't need to perform any transformations |
| 2581 | // or filtering. The important part is the action, not the contents. |
| 2582 | final int oldAction = event.getAction(); |
| 2583 | if (cancel || oldAction == MotionEvent.ACTION_CANCEL) { |
| 2584 | event.setAction(MotionEvent.ACTION_CANCEL); |
| 2585 | if (child == null) { |
| 2586 | handled = super.dispatchTouchEvent(event); |
| 2587 | } else { |
| 2588 | handled = child.dispatchTouchEvent(event); |
| 2589 | } |
| 2590 | event.setAction(oldAction); |
| 2591 | return handled; |
| 2592 | } |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 2593 | |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2594 | // Calculate the number of pointers to deliver. |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 2595 | final int oldPointerIdBits = event.getPointerIdBits(); |
| 2596 | final int newPointerIdBits = oldPointerIdBits & desiredPointerIdBits; |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 2597 | |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2598 | // If for some reason we ended up in an inconsistent state where it looks like we |
| 2599 | // might produce a motion event with no pointers in it, then drop the event. |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 2600 | if (newPointerIdBits == 0) { |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2601 | return false; |
| 2602 | } |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 2603 | |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2604 | // If the number of pointers is the same and we don't need to perform any fancy |
| 2605 | // irreversible transformations, then we can reuse the motion event for this |
| 2606 | // dispatch as long as we are careful to revert any changes we make. |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 2607 | // Otherwise we need to make a copy. |
| 2608 | final MotionEvent transformedEvent; |
| 2609 | if (newPointerIdBits == oldPointerIdBits) { |
| 2610 | if (child == null || child.hasIdentityMatrix()) { |
| 2611 | if (child == null) { |
| 2612 | handled = super.dispatchTouchEvent(event); |
| 2613 | } else { |
| 2614 | final float offsetX = mScrollX - child.mLeft; |
| 2615 | final float offsetY = mScrollY - child.mTop; |
| 2616 | event.offsetLocation(offsetX, offsetY); |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 2617 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 2618 | handled = child.dispatchTouchEvent(event); |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2619 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 2620 | event.offsetLocation(-offsetX, -offsetY); |
| 2621 | } |
| 2622 | return handled; |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2623 | } |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2624 | transformedEvent = MotionEvent.obtain(event); |
| 2625 | } else { |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 2626 | transformedEvent = event.split(newPointerIdBits); |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 2627 | } |
| 2628 | |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2629 | // Perform any necessary transformations and dispatch. |
| 2630 | if (child == null) { |
| 2631 | handled = super.dispatchTouchEvent(transformedEvent); |
| 2632 | } else { |
| 2633 | final float offsetX = mScrollX - child.mLeft; |
| 2634 | final float offsetY = mScrollY - child.mTop; |
| 2635 | transformedEvent.offsetLocation(offsetX, offsetY); |
| 2636 | if (! child.hasIdentityMatrix()) { |
| 2637 | transformedEvent.transform(child.getInverseMatrix()); |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 2638 | } |
| 2639 | |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2640 | handled = child.dispatchTouchEvent(transformedEvent); |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 2641 | } |
| 2642 | |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2643 | // Done. |
| 2644 | transformedEvent.recycle(); |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 2645 | return handled; |
| 2646 | } |
| 2647 | |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 2648 | /** |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 2649 | * 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] | 2650 | * dispatch. This behavior is enabled by default for applications that target an |
| 2651 | * SDK version of {@link Build.VERSION_CODES#HONEYCOMB} or newer. |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 2652 | * |
| 2653 | * <p>When this option is enabled MotionEvents may be split and dispatched to different child |
| 2654 | * views depending on where each pointer initially went down. This allows for user interactions |
| 2655 | * such as scrolling two panes of content independently, chording of buttons, and performing |
| 2656 | * independent gestures on different pieces of content. |
| 2657 | * |
| 2658 | * @param split <code>true</code> to allow MotionEvents to be split and dispatched to multiple |
| 2659 | * child views. <code>false</code> to only allow one child view to be the target of |
| 2660 | * any MotionEvent received by this ViewGroup. |
Scott Main | 27a8508 | 2013-06-10 10:39:48 -0700 | [diff] [blame] | 2661 | * @attr ref android.R.styleable#ViewGroup_splitMotionEvents |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 2662 | */ |
| 2663 | public void setMotionEventSplittingEnabled(boolean split) { |
| 2664 | // TODO Applications really shouldn't change this setting mid-touch event, |
| 2665 | // but perhaps this should handle that case and send ACTION_CANCELs to any child views |
| 2666 | // with gestures in progress when this is changed. |
| 2667 | if (split) { |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 2668 | mGroupFlags |= FLAG_SPLIT_MOTION_EVENTS; |
| 2669 | } else { |
| 2670 | mGroupFlags &= ~FLAG_SPLIT_MOTION_EVENTS; |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 2671 | } |
| 2672 | } |
| 2673 | |
| 2674 | /** |
Jeff Brown | 995e774 | 2010-12-22 16:59:36 -0800 | [diff] [blame] | 2675 | * 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] | 2676 | * @return true if MotionEvents dispatched to this ViewGroup can be split to multiple children. |
| 2677 | */ |
| 2678 | public boolean isMotionEventSplittingEnabled() { |
| 2679 | return (mGroupFlags & FLAG_SPLIT_MOTION_EVENTS) == FLAG_SPLIT_MOTION_EVENTS; |
| 2680 | } |
| 2681 | |
| 2682 | /** |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 2683 | * Returns true if this ViewGroup should be considered as a single entity for removal |
| 2684 | * when executing an Activity transition. If this is false, child elements will move |
| 2685 | * individually during the transition. |
George Mount | 427c641 | 2014-11-05 16:45:36 -0800 | [diff] [blame] | 2686 | * |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 2687 | * @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] | 2688 | * The default value is true when there is a non-null background or if |
| 2689 | * {@link #getTransitionName()} is not null or if a |
| 2690 | * non-null {@link android.view.ViewOutlineProvider} other than |
| 2691 | * {@link android.view.ViewOutlineProvider#BACKGROUND} was given to |
| 2692 | * {@link #setOutlineProvider(ViewOutlineProvider)} and false otherwise. |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 2693 | */ |
| 2694 | public boolean isTransitionGroup() { |
| 2695 | if ((mGroupFlags & FLAG_IS_TRANSITION_GROUP_SET) != 0) { |
| 2696 | return ((mGroupFlags & FLAG_IS_TRANSITION_GROUP) != 0); |
| 2697 | } else { |
George Mount | 427c641 | 2014-11-05 16:45:36 -0800 | [diff] [blame] | 2698 | final ViewOutlineProvider outlineProvider = getOutlineProvider(); |
| 2699 | return getBackground() != null || getTransitionName() != null || |
| 2700 | (outlineProvider != null && outlineProvider != ViewOutlineProvider.BACKGROUND); |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 2701 | } |
| 2702 | } |
| 2703 | |
| 2704 | /** |
| 2705 | * 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] | 2706 | * Activity Transitions. |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 2707 | * @param isTransitionGroup Whether or not the ViewGroup should be treated as a unit |
| 2708 | * in Activity transitions. If false, the ViewGroup won't transition, |
| 2709 | * only its children. If true, the entire ViewGroup will transition |
| 2710 | * together. |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 2711 | * @see android.app.ActivityOptions#makeSceneTransitionAnimation(android.app.Activity, |
| 2712 | * android.util.Pair[]) |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 2713 | */ |
| 2714 | public void setTransitionGroup(boolean isTransitionGroup) { |
| 2715 | mGroupFlags |= FLAG_IS_TRANSITION_GROUP_SET; |
| 2716 | if (isTransitionGroup) { |
| 2717 | mGroupFlags |= FLAG_IS_TRANSITION_GROUP; |
| 2718 | } else { |
| 2719 | mGroupFlags &= ~FLAG_IS_TRANSITION_GROUP; |
| 2720 | } |
| 2721 | } |
| 2722 | |
Alan Viverette | be463f2 | 2016-01-21 10:50:10 -0500 | [diff] [blame] | 2723 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2724 | public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) { |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 2725 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2726 | if (disallowIntercept == ((mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0)) { |
| 2727 | // We're already in this state, assume our ancestors are too |
| 2728 | return; |
| 2729 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 2730 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2731 | if (disallowIntercept) { |
| 2732 | mGroupFlags |= FLAG_DISALLOW_INTERCEPT; |
| 2733 | } else { |
| 2734 | mGroupFlags &= ~FLAG_DISALLOW_INTERCEPT; |
| 2735 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 2736 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2737 | // Pass it up to our parent |
| 2738 | if (mParent != null) { |
| 2739 | mParent.requestDisallowInterceptTouchEvent(disallowIntercept); |
| 2740 | } |
| 2741 | } |
| 2742 | |
| 2743 | /** |
| 2744 | * Implement this method to intercept all touch screen motion events. This |
| 2745 | * allows you to watch events as they are dispatched to your children, and |
| 2746 | * take ownership of the current gesture at any point. |
| 2747 | * |
| 2748 | * <p>Using this function takes some care, as it has a fairly complicated |
| 2749 | * interaction with {@link View#onTouchEvent(MotionEvent) |
| 2750 | * View.onTouchEvent(MotionEvent)}, and using it requires implementing |
| 2751 | * that method as well as this one in the correct way. Events will be |
| 2752 | * received in the following order: |
| 2753 | * |
| 2754 | * <ol> |
| 2755 | * <li> You will receive the down event here. |
| 2756 | * <li> The down event will be handled either by a child of this view |
| 2757 | * group, or given to your own onTouchEvent() method to handle; this means |
| 2758 | * you should implement onTouchEvent() to return true, so you will |
| 2759 | * continue to see the rest of the gesture (instead of looking for |
| 2760 | * a parent view to handle it). Also, by returning true from |
| 2761 | * onTouchEvent(), you will not receive any following |
| 2762 | * events in onInterceptTouchEvent() and all touch processing must |
| 2763 | * happen in onTouchEvent() like normal. |
| 2764 | * <li> For as long as you return false from this function, each following |
| 2765 | * event (up to and including the final up) will be delivered first here |
| 2766 | * and then to the target's onTouchEvent(). |
| 2767 | * <li> If you return true from here, you will not receive any |
| 2768 | * following events: the target view will receive the same event but |
| 2769 | * with the action {@link MotionEvent#ACTION_CANCEL}, and all further |
| 2770 | * events will be delivered to your onTouchEvent() method and no longer |
| 2771 | * appear here. |
| 2772 | * </ol> |
| 2773 | * |
| 2774 | * @param ev The motion event being dispatched down the hierarchy. |
| 2775 | * @return Return true to steal motion events from the children and have |
| 2776 | * them dispatched to this ViewGroup through onTouchEvent(). |
| 2777 | * The current target will receive an ACTION_CANCEL event, and no further |
| 2778 | * messages will be delivered here. |
| 2779 | */ |
| 2780 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
Keisuke Kuroyanagi | d85bc50 | 2016-01-21 14:50:38 +0900 | [diff] [blame] | 2781 | if (ev.isFromSource(InputDevice.SOURCE_MOUSE) |
| 2782 | && ev.getAction() == MotionEvent.ACTION_DOWN |
| 2783 | && ev.isButtonPressed(MotionEvent.BUTTON_PRIMARY) |
| 2784 | && isOnScrollbarThumb(ev.getX(), ev.getY())) { |
| 2785 | return true; |
| 2786 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2787 | return false; |
| 2788 | } |
| 2789 | |
| 2790 | /** |
| 2791 | * {@inheritDoc} |
| 2792 | * |
| 2793 | * Looks for a view to give focus to respecting the setting specified by |
| 2794 | * {@link #getDescendantFocusability()}. |
| 2795 | * |
| 2796 | * Uses {@link #onRequestFocusInDescendants(int, android.graphics.Rect)} to |
| 2797 | * find focus within the children of this group when appropriate. |
| 2798 | * |
| 2799 | * @see #FOCUS_BEFORE_DESCENDANTS |
| 2800 | * @see #FOCUS_AFTER_DESCENDANTS |
| 2801 | * @see #FOCUS_BLOCK_DESCENDANTS |
Filip Gruszczynski | a33bdf3 | 2015-11-19 18:22:16 -0800 | [diff] [blame] | 2802 | * @see #onRequestFocusInDescendants(int, android.graphics.Rect) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2803 | */ |
| 2804 | @Override |
| 2805 | public boolean requestFocus(int direction, Rect previouslyFocusedRect) { |
| 2806 | if (DBG) { |
| 2807 | System.out.println(this + " ViewGroup.requestFocus direction=" |
| 2808 | + direction); |
| 2809 | } |
| 2810 | int descendantFocusability = getDescendantFocusability(); |
| 2811 | |
| 2812 | switch (descendantFocusability) { |
| 2813 | case FOCUS_BLOCK_DESCENDANTS: |
| 2814 | return super.requestFocus(direction, previouslyFocusedRect); |
| 2815 | case FOCUS_BEFORE_DESCENDANTS: { |
| 2816 | final boolean took = super.requestFocus(direction, previouslyFocusedRect); |
| 2817 | return took ? took : onRequestFocusInDescendants(direction, previouslyFocusedRect); |
| 2818 | } |
| 2819 | case FOCUS_AFTER_DESCENDANTS: { |
| 2820 | final boolean took = onRequestFocusInDescendants(direction, previouslyFocusedRect); |
| 2821 | return took ? took : super.requestFocus(direction, previouslyFocusedRect); |
| 2822 | } |
| 2823 | default: |
| 2824 | throw new IllegalStateException("descendant focusability must be " |
| 2825 | + "one of FOCUS_BEFORE_DESCENDANTS, FOCUS_AFTER_DESCENDANTS, FOCUS_BLOCK_DESCENDANTS " |
| 2826 | + "but is " + descendantFocusability); |
| 2827 | } |
| 2828 | } |
| 2829 | |
| 2830 | /** |
| 2831 | * Look for a descendant to call {@link View#requestFocus} on. |
| 2832 | * Called by {@link ViewGroup#requestFocus(int, android.graphics.Rect)} |
| 2833 | * when it wants to request focus within its children. Override this to |
| 2834 | * customize how your {@link ViewGroup} requests focus within its children. |
| 2835 | * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT |
| 2836 | * @param previouslyFocusedRect The rectangle (in this View's coordinate system) |
| 2837 | * to give a finer grained hint about where focus is coming from. May be null |
| 2838 | * if there is no hint. |
| 2839 | * @return Whether focus was taken. |
| 2840 | */ |
| 2841 | @SuppressWarnings({"ConstantConditions"}) |
| 2842 | protected boolean onRequestFocusInDescendants(int direction, |
| 2843 | Rect previouslyFocusedRect) { |
| 2844 | int index; |
| 2845 | int increment; |
| 2846 | int end; |
| 2847 | int count = mChildrenCount; |
| 2848 | if ((direction & FOCUS_FORWARD) != 0) { |
| 2849 | index = 0; |
| 2850 | increment = 1; |
| 2851 | end = count; |
| 2852 | } else { |
| 2853 | index = count - 1; |
| 2854 | increment = -1; |
| 2855 | end = -1; |
| 2856 | } |
| 2857 | final View[] children = mChildren; |
| 2858 | for (int i = index; i != end; i += increment) { |
| 2859 | View child = children[i]; |
| 2860 | if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) { |
| 2861 | if (child.requestFocus(direction, previouslyFocusedRect)) { |
| 2862 | return true; |
| 2863 | } |
| 2864 | } |
| 2865 | } |
| 2866 | return false; |
| 2867 | } |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 2868 | |
Romain Guy | a440b00 | 2010-02-24 15:57:54 -0800 | [diff] [blame] | 2869 | /** |
| 2870 | * {@inheritDoc} |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 2871 | * |
Romain Guy | dcc490f | 2010-02-24 17:59:35 -0800 | [diff] [blame] | 2872 | * @hide |
Romain Guy | a440b00 | 2010-02-24 15:57:54 -0800 | [diff] [blame] | 2873 | */ |
| 2874 | @Override |
| 2875 | public void dispatchStartTemporaryDetach() { |
| 2876 | super.dispatchStartTemporaryDetach(); |
| 2877 | final int count = mChildrenCount; |
| 2878 | final View[] children = mChildren; |
| 2879 | for (int i = 0; i < count; i++) { |
| 2880 | children[i].dispatchStartTemporaryDetach(); |
| 2881 | } |
| 2882 | } |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 2883 | |
Romain Guy | a440b00 | 2010-02-24 15:57:54 -0800 | [diff] [blame] | 2884 | /** |
| 2885 | * {@inheritDoc} |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 2886 | * |
Romain Guy | dcc490f | 2010-02-24 17:59:35 -0800 | [diff] [blame] | 2887 | * @hide |
Romain Guy | a440b00 | 2010-02-24 15:57:54 -0800 | [diff] [blame] | 2888 | */ |
| 2889 | @Override |
| 2890 | public void dispatchFinishTemporaryDetach() { |
| 2891 | super.dispatchFinishTemporaryDetach(); |
| 2892 | final int count = mChildrenCount; |
| 2893 | final View[] children = mChildren; |
| 2894 | for (int i = 0; i < count; i++) { |
| 2895 | children[i].dispatchFinishTemporaryDetach(); |
| 2896 | } |
| 2897 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2898 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2899 | @Override |
| 2900 | void dispatchAttachedToWindow(AttachInfo info, int visibility) { |
Adam Powell | 4b86788 | 2011-09-16 12:59:46 -0700 | [diff] [blame] | 2901 | mGroupFlags |= FLAG_PREVENT_DISPATCH_ATTACHED_TO_WINDOW; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2902 | super.dispatchAttachedToWindow(info, visibility); |
Adam Powell | 4b86788 | 2011-09-16 12:59:46 -0700 | [diff] [blame] | 2903 | mGroupFlags &= ~FLAG_PREVENT_DISPATCH_ATTACHED_TO_WINDOW; |
| 2904 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2905 | final int count = mChildrenCount; |
| 2906 | final View[] children = mChildren; |
| 2907 | for (int i = 0; i < count; i++) { |
Dianne Hackborn | 3a3a6cf | 2012-03-26 10:24:04 -0700 | [diff] [blame] | 2908 | final View child = children[i]; |
| 2909 | child.dispatchAttachedToWindow(info, |
Adam Powell | eb2b0af | 2015-05-20 18:26:35 -0700 | [diff] [blame] | 2910 | combineVisibility(visibility, child.getVisibility())); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2911 | } |
Chet Haase | c633d2f | 2015-04-07 10:29:39 -0700 | [diff] [blame] | 2912 | final int transientCount = mTransientIndices == null ? 0 : mTransientIndices.size(); |
| 2913 | for (int i = 0; i < transientCount; ++i) { |
| 2914 | View view = mTransientViews.get(i); |
Adam Powell | eb2b0af | 2015-05-20 18:26:35 -0700 | [diff] [blame] | 2915 | view.dispatchAttachedToWindow(info, |
| 2916 | combineVisibility(visibility, view.getVisibility())); |
Chet Haase | c633d2f | 2015-04-07 10:29:39 -0700 | [diff] [blame] | 2917 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2918 | } |
| 2919 | |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 2920 | @Override |
Romain Guy | bb9908b | 2012-03-08 11:14:07 -0800 | [diff] [blame] | 2921 | void dispatchScreenStateChanged(int screenState) { |
| 2922 | super.dispatchScreenStateChanged(screenState); |
| 2923 | |
| 2924 | final int count = mChildrenCount; |
| 2925 | final View[] children = mChildren; |
| 2926 | for (int i = 0; i < count; i++) { |
| 2927 | children[i].dispatchScreenStateChanged(screenState); |
| 2928 | } |
| 2929 | } |
| 2930 | |
Alan Viverette | a54956a | 2015-01-07 16:05:02 -0800 | [diff] [blame] | 2931 | /** @hide */ |
Romain Guy | bb9908b | 2012-03-08 11:14:07 -0800 | [diff] [blame] | 2932 | @Override |
Alan Viverette | a54956a | 2015-01-07 16:05:02 -0800 | [diff] [blame] | 2933 | public boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) { |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 2934 | boolean handled = false; |
| 2935 | if (includeForAccessibility()) { |
| 2936 | handled = super.dispatchPopulateAccessibilityEventInternal(event); |
| 2937 | if (handled) { |
| 2938 | return handled; |
| 2939 | } |
Svetoslav Ganov | b84b94e | 2011-09-22 19:26:56 -0700 | [diff] [blame] | 2940 | } |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 2941 | // Let our children have a shot in populating the event. |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 2942 | ChildListForAccessibility children = ChildListForAccessibility.obtain(this, true); |
Svetoslav Ganov | 76f287e | 2012-04-23 11:02:36 -0700 | [diff] [blame] | 2943 | try { |
| 2944 | final int childCount = children.getChildCount(); |
| 2945 | for (int i = 0; i < childCount; i++) { |
| 2946 | View child = children.getChildAt(i); |
| 2947 | if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) { |
| 2948 | handled = child.dispatchPopulateAccessibilityEvent(event); |
| 2949 | if (handled) { |
Svetoslav Ganov | 76f287e | 2012-04-23 11:02:36 -0700 | [diff] [blame] | 2950 | return handled; |
| 2951 | } |
Svetoslav Ganov | 6179ea3 | 2011-06-28 01:12:41 -0700 | [diff] [blame] | 2952 | } |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 2953 | } |
Svetoslav Ganov | 76f287e | 2012-04-23 11:02:36 -0700 | [diff] [blame] | 2954 | } finally { |
| 2955 | children.recycle(); |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 2956 | } |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 2957 | return false; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 2958 | } |
| 2959 | |
Dianne Hackborn | 6251f0d | 2015-04-01 16:45:03 -0700 | [diff] [blame] | 2960 | /** |
Dianne Hackborn | 49b043f | 2015-05-07 14:21:38 -0700 | [diff] [blame] | 2961 | * Dispatch creation of {@link ViewStructure} down the hierarchy. This implementation |
Dianne Hackborn | 6251f0d | 2015-04-01 16:45:03 -0700 | [diff] [blame] | 2962 | * adds in all child views of the view group, in addition to calling the default View |
| 2963 | * implementation. |
| 2964 | */ |
Alan Viverette | be463f2 | 2016-01-21 10:50:10 -0500 | [diff] [blame] | 2965 | @Override |
Dianne Hackborn | 49b043f | 2015-05-07 14:21:38 -0700 | [diff] [blame] | 2966 | public void dispatchProvideStructure(ViewStructure structure) { |
| 2967 | super.dispatchProvideStructure(structure); |
Dianne Hackborn | 799d8d0 | 2015-04-22 16:36:39 -0700 | [diff] [blame] | 2968 | if (!isAssistBlocked()) { |
| 2969 | if (structure.getChildCount() == 0) { |
| 2970 | final int childrenCount = getChildCount(); |
| 2971 | if (childrenCount > 0) { |
| 2972 | structure.setChildCount(childrenCount); |
Dianne Hackborn | 6ff55fc | 2015-08-05 18:07:31 -0700 | [diff] [blame] | 2973 | ArrayList<View> preorderedList = buildOrderedChildList(); |
| 2974 | boolean customOrder = preorderedList == null |
Dianne Hackborn | 799d8d0 | 2015-04-22 16:36:39 -0700 | [diff] [blame] | 2975 | && isChildrenDrawingOrderEnabled(); |
| 2976 | final View[] children = mChildren; |
| 2977 | for (int i=0; i<childrenCount; i++) { |
Dianne Hackborn | 6ff55fc | 2015-08-05 18:07:31 -0700 | [diff] [blame] | 2978 | int childIndex; |
| 2979 | try { |
Alan Viverette | a7b85e6 | 2016-01-22 10:14:02 -0500 | [diff] [blame] | 2980 | childIndex = getAndVerifyPreorderedIndex(childrenCount, i, customOrder); |
Dianne Hackborn | 6ff55fc | 2015-08-05 18:07:31 -0700 | [diff] [blame] | 2981 | } catch (IndexOutOfBoundsException e) { |
| 2982 | childIndex = i; |
| 2983 | if (mContext.getApplicationInfo().targetSdkVersion |
| 2984 | < Build.VERSION_CODES.M) { |
| 2985 | Log.w(TAG, "Bad getChildDrawingOrder while collecting assist @ " |
| 2986 | + i + " of " + childrenCount, e); |
| 2987 | // At least one app is failing when we call getChildDrawingOrder |
| 2988 | // at this point, so deal semi-gracefully with it by falling back |
| 2989 | // on the basic order. |
| 2990 | customOrder = false; |
| 2991 | if (i > 0) { |
| 2992 | // If we failed at the first index, there really isn't |
| 2993 | // anything to do -- we will just proceed with the simple |
| 2994 | // sequence order. |
| 2995 | // Otherwise, we failed in the middle, so need to come up |
| 2996 | // with an order for the remaining indices and use that. |
| 2997 | // Failed at the first one, easy peasy. |
| 2998 | int[] permutation = new int[childrenCount]; |
| 2999 | SparseBooleanArray usedIndices = new SparseBooleanArray(); |
| 3000 | // Go back and collected the indices we have done so far. |
| 3001 | for (int j=0; j<i; j++) { |
| 3002 | permutation[j] = getChildDrawingOrder(childrenCount, j); |
| 3003 | usedIndices.put(permutation[j], true); |
| 3004 | } |
| 3005 | // Fill in the remaining indices with indices that have not |
| 3006 | // yet been used. |
| 3007 | int nextIndex = 0; |
| 3008 | for (int j=i; j<childrenCount; j++) { |
| 3009 | while (usedIndices.get(nextIndex, false)) { |
| 3010 | nextIndex++; |
| 3011 | } |
| 3012 | permutation[j] = nextIndex; |
| 3013 | nextIndex++; |
| 3014 | } |
| 3015 | // Build the final view list. |
| 3016 | preorderedList = new ArrayList<>(childrenCount); |
| 3017 | for (int j=0; j<childrenCount; j++) { |
| 3018 | preorderedList.add(children[permutation[j]]); |
| 3019 | } |
| 3020 | } |
| 3021 | } else { |
| 3022 | throw e; |
| 3023 | } |
| 3024 | } |
Alan Viverette | a7b85e6 | 2016-01-22 10:14:02 -0500 | [diff] [blame] | 3025 | |
| 3026 | final View child = getAndVerifyPreorderedView( |
| 3027 | preorderedList, children, childIndex); |
| 3028 | final ViewStructure cstructure = structure.newChild(i); |
Dianne Hackborn | 49b043f | 2015-05-07 14:21:38 -0700 | [diff] [blame] | 3029 | child.dispatchProvideStructure(cstructure); |
Dianne Hackborn | 799d8d0 | 2015-04-22 16:36:39 -0700 | [diff] [blame] | 3030 | } |
Dianne Hackborn | 6251f0d | 2015-04-01 16:45:03 -0700 | [diff] [blame] | 3031 | } |
| 3032 | } |
| 3033 | } |
| 3034 | } |
| 3035 | |
Alan Viverette | a7b85e6 | 2016-01-22 10:14:02 -0500 | [diff] [blame] | 3036 | private static View getAndVerifyPreorderedView(ArrayList<View> preorderedList, View[] children, |
| 3037 | int childIndex) { |
| 3038 | final View child; |
| 3039 | if (preorderedList != null) { |
| 3040 | child = preorderedList.get(childIndex); |
| 3041 | if (child == null) { |
| 3042 | throw new RuntimeException("Invalid preorderedList contained null child at index " |
| 3043 | + childIndex); |
| 3044 | } |
| 3045 | } else { |
| 3046 | child = children[childIndex]; |
| 3047 | } |
| 3048 | return child; |
| 3049 | } |
| 3050 | |
Alan Viverette | a54956a | 2015-01-07 16:05:02 -0800 | [diff] [blame] | 3051 | /** @hide */ |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 3052 | @Override |
Alan Viverette | a54956a | 2015-01-07 16:05:02 -0800 | [diff] [blame] | 3053 | public void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) { |
Svetoslav Ganov | 031d9c1 | 2011-09-09 16:41:13 -0700 | [diff] [blame] | 3054 | super.onInitializeAccessibilityNodeInfoInternal(info); |
Svet Ganov | 55bdb10 | 2015-02-06 12:41:17 -0800 | [diff] [blame] | 3055 | if (getAccessibilityNodeProvider() != null) { |
| 3056 | return; |
| 3057 | } |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 3058 | if (mAttachInfo != null) { |
Alan Viverette | f0aed09 | 2013-11-06 15:33:03 -0800 | [diff] [blame] | 3059 | final ArrayList<View> childrenForAccessibility = mAttachInfo.mTempArrayList; |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 3060 | childrenForAccessibility.clear(); |
| 3061 | addChildrenForAccessibility(childrenForAccessibility); |
| 3062 | final int childrenForAccessibilityCount = childrenForAccessibility.size(); |
| 3063 | for (int i = 0; i < childrenForAccessibilityCount; i++) { |
Alan Viverette | f0aed09 | 2013-11-06 15:33:03 -0800 | [diff] [blame] | 3064 | final View child = childrenForAccessibility.get(i); |
| 3065 | info.addChildUnchecked(child); |
Svetoslav Ganov | ea1da3d | 2011-06-15 17:16:02 -0700 | [diff] [blame] | 3066 | } |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 3067 | childrenForAccessibility.clear(); |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 3068 | } |
| 3069 | } |
| 3070 | |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 3071 | @Override |
Maxim Bogatov | f399af3 | 2015-06-16 15:15:49 -0700 | [diff] [blame] | 3072 | public CharSequence getAccessibilityClassName() { |
| 3073 | return ViewGroup.class.getName(); |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 3074 | } |
| 3075 | |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 3076 | @Override |
Alan Viverette | 77e9a28 | 2013-09-12 17:16:09 -0700 | [diff] [blame] | 3077 | public void notifySubtreeAccessibilityStateChanged(View child, View source, int changeType) { |
| 3078 | // If this is a live region, we should send a subtree change event |
| 3079 | // from this view. Otherwise, we can let it propagate up. |
| 3080 | if (getAccessibilityLiveRegion() != ACCESSIBILITY_LIVE_REGION_NONE) { |
| 3081 | notifyViewAccessibilityStateChangedIfNeeded(changeType); |
| 3082 | } else if (mParent != null) { |
Adam Powell | 504a10f | 2013-07-11 15:25:59 -0700 | [diff] [blame] | 3083 | try { |
Alan Viverette | 77e9a28 | 2013-09-12 17:16:09 -0700 | [diff] [blame] | 3084 | mParent.notifySubtreeAccessibilityStateChanged(this, source, changeType); |
Adam Powell | 504a10f | 2013-07-11 15:25:59 -0700 | [diff] [blame] | 3085 | } catch (AbstractMethodError e) { |
| 3086 | Log.e(VIEW_LOG_TAG, mParent.getClass().getSimpleName() + |
| 3087 | " does not fully implement ViewParent", e); |
| 3088 | } |
Svetoslav | 6254f48 | 2013-06-04 17:22:14 -0700 | [diff] [blame] | 3089 | } |
| 3090 | } |
| 3091 | |
| 3092 | @Override |
| 3093 | void resetSubtreeAccessibilityStateChanged() { |
| 3094 | super.resetSubtreeAccessibilityStateChanged(); |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 3095 | View[] children = mChildren; |
| 3096 | final int childCount = mChildrenCount; |
| 3097 | for (int i = 0; i < childCount; i++) { |
Svetoslav | 6254f48 | 2013-06-04 17:22:14 -0700 | [diff] [blame] | 3098 | children[i].resetSubtreeAccessibilityStateChanged(); |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 3099 | } |
| 3100 | } |
| 3101 | |
| 3102 | /** |
Phil Weaver | 1f22254 | 2016-01-08 11:49:32 -0800 | [diff] [blame] | 3103 | * Counts the number of children of this View that will be sent to an accessibility service. |
| 3104 | * |
| 3105 | * @return The number of children an {@code AccessibilityNodeInfo} rooted at this View |
| 3106 | * would have. |
| 3107 | */ |
| 3108 | int getNumChildrenForAccessibility() { |
| 3109 | int numChildrenForAccessibility = 0; |
| 3110 | for (int i = 0; i < getChildCount(); i++) { |
| 3111 | View child = getChildAt(i); |
| 3112 | if (child.includeForAccessibility()) { |
| 3113 | numChildrenForAccessibility++; |
| 3114 | } else if (child instanceof ViewGroup) { |
| 3115 | numChildrenForAccessibility += ((ViewGroup) child) |
| 3116 | .getNumChildrenForAccessibility(); |
| 3117 | } |
| 3118 | } |
| 3119 | return numChildrenForAccessibility; |
| 3120 | } |
| 3121 | |
| 3122 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3123 | * {@inheritDoc} |
Adam Powell | b6ab098 | 2015-01-07 17:00:12 -0800 | [diff] [blame] | 3124 | * |
| 3125 | * <p>Subclasses should always call <code>super.onNestedPrePerformAccessibilityAction</code></p> |
| 3126 | * |
| 3127 | * @param target The target view dispatching this action |
| 3128 | * @param action Action being performed; see |
| 3129 | * {@link android.view.accessibility.AccessibilityNodeInfo} |
| 3130 | * @param args Optional action arguments |
| 3131 | * @return false by default. Subclasses should return true if they handle the event. |
| 3132 | */ |
| 3133 | @Override |
| 3134 | public boolean onNestedPrePerformAccessibilityAction(View target, int action, Bundle args) { |
| 3135 | return false; |
| 3136 | } |
| 3137 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3138 | @Override |
| 3139 | void dispatchDetachedFromWindow() { |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 3140 | // 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] | 3141 | // dispatching motion events to a child; we need to get rid of that |
| 3142 | // child to avoid dispatching events to it after the window is torn |
| 3143 | // down. To make sure we keep the child in a consistent state, we |
| 3144 | // first send it an ACTION_CANCEL motion event. |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 3145 | cancelAndClearTouchTargets(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3146 | |
Jeff Brown | 59a422e | 2012-04-19 15:19:19 -0700 | [diff] [blame] | 3147 | // Similarly, set ACTION_EXIT to all hover targets and clear them. |
| 3148 | exitHoverTargets(); |
| 3149 | |
Chet Haase | 9c08744 | 2011-01-12 16:20:16 -0800 | [diff] [blame] | 3150 | // In case view is detached while transition is running |
Chet Haase | b989502 | 2013-04-02 15:10:58 -0700 | [diff] [blame] | 3151 | mLayoutCalledWhileSuppressed = false; |
Chet Haase | 9c08744 | 2011-01-12 16:20:16 -0800 | [diff] [blame] | 3152 | |
Christopher Tate | 86cab1b | 2011-01-13 20:28:55 -0800 | [diff] [blame] | 3153 | // Tear down our drag tracking |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 3154 | mChildrenInterestedInDrag = null; |
| 3155 | mIsInterestedInDrag = false; |
| 3156 | if (mCurrentDragStartEvent != null) { |
| 3157 | mCurrentDragStartEvent.recycle(); |
| 3158 | mCurrentDragStartEvent = null; |
Christopher Tate | 86cab1b | 2011-01-13 20:28:55 -0800 | [diff] [blame] | 3159 | } |
| 3160 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3161 | final int count = mChildrenCount; |
| 3162 | final View[] children = mChildren; |
| 3163 | for (int i = 0; i < count; i++) { |
| 3164 | children[i].dispatchDetachedFromWindow(); |
| 3165 | } |
John Reck | ca7a9da | 2014-03-05 16:29:07 -0800 | [diff] [blame] | 3166 | clearDisappearingChildren(); |
Chet Haase | c633d2f | 2015-04-07 10:29:39 -0700 | [diff] [blame] | 3167 | final int transientCount = mTransientViews == null ? 0 : mTransientIndices.size(); |
| 3168 | for (int i = 0; i < transientCount; ++i) { |
| 3169 | View view = mTransientViews.get(i); |
| 3170 | view.dispatchDetachedFromWindow(); |
| 3171 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3172 | super.dispatchDetachedFromWindow(); |
| 3173 | } |
| 3174 | |
Fabrice Di Meglio | 23c89fd | 2012-08-13 12:17:42 -0700 | [diff] [blame] | 3175 | /** |
| 3176 | * @hide |
| 3177 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3178 | @Override |
Fabrice Di Meglio | 23c89fd | 2012-08-13 12:17:42 -0700 | [diff] [blame] | 3179 | protected void internalSetPadding(int left, int top, int right, int bottom) { |
Romain Guy | 2440e67 | 2012-08-07 14:43:43 -0700 | [diff] [blame] | 3180 | super.internalSetPadding(left, top, right, bottom); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3181 | |
Romain Guy | 13f35f3 | 2011-03-24 12:03:17 -0700 | [diff] [blame] | 3182 | if ((mPaddingLeft | mPaddingTop | mPaddingRight | mPaddingBottom) != 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3183 | mGroupFlags |= FLAG_PADDING_NOT_NULL; |
| 3184 | } else { |
| 3185 | mGroupFlags &= ~FLAG_PADDING_NOT_NULL; |
| 3186 | } |
| 3187 | } |
| 3188 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3189 | @Override |
| 3190 | protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) { |
| 3191 | super.dispatchSaveInstanceState(container); |
| 3192 | final int count = mChildrenCount; |
| 3193 | final View[] children = mChildren; |
| 3194 | for (int i = 0; i < count; i++) { |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 3195 | View c = children[i]; |
| 3196 | if ((c.mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED) { |
| 3197 | c.dispatchSaveInstanceState(container); |
| 3198 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3199 | } |
| 3200 | } |
| 3201 | |
| 3202 | /** |
Romain Guy | 9fc2781 | 2011-04-27 14:21:41 -0700 | [diff] [blame] | 3203 | * Perform dispatching of a {@link #saveHierarchyState(android.util.SparseArray)} freeze()} |
| 3204 | * to only this view, not to its children. For use when overriding |
| 3205 | * {@link #dispatchSaveInstanceState(android.util.SparseArray)} dispatchFreeze()} to allow |
| 3206 | * subclasses to freeze their own state but not the state of their children. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3207 | * |
| 3208 | * @param container the container |
| 3209 | */ |
| 3210 | protected void dispatchFreezeSelfOnly(SparseArray<Parcelable> container) { |
| 3211 | super.dispatchSaveInstanceState(container); |
| 3212 | } |
| 3213 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3214 | @Override |
| 3215 | protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) { |
| 3216 | super.dispatchRestoreInstanceState(container); |
| 3217 | final int count = mChildrenCount; |
| 3218 | final View[] children = mChildren; |
| 3219 | for (int i = 0; i < count; i++) { |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 3220 | View c = children[i]; |
| 3221 | if ((c.mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED) { |
| 3222 | c.dispatchRestoreInstanceState(container); |
| 3223 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3224 | } |
| 3225 | } |
| 3226 | |
| 3227 | /** |
Romain Guy | 02739a8 | 2011-05-16 11:43:18 -0700 | [diff] [blame] | 3228 | * Perform dispatching of a {@link #restoreHierarchyState(android.util.SparseArray)} |
| 3229 | * to only this view, not to its children. For use when overriding |
| 3230 | * {@link #dispatchRestoreInstanceState(android.util.SparseArray)} to allow |
| 3231 | * subclasses to thaw their own state but not the state of their children. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3232 | * |
| 3233 | * @param container the container |
| 3234 | */ |
| 3235 | protected void dispatchThawSelfOnly(SparseArray<Parcelable> container) { |
| 3236 | super.dispatchRestoreInstanceState(container); |
| 3237 | } |
| 3238 | |
| 3239 | /** |
| 3240 | * Enables or disables the drawing cache for each child of this view group. |
| 3241 | * |
| 3242 | * @param enabled true to enable the cache, false to dispose of it |
| 3243 | */ |
| 3244 | protected void setChildrenDrawingCacheEnabled(boolean enabled) { |
| 3245 | if (enabled || (mPersistentDrawingCache & PERSISTENT_ALL_CACHES) != PERSISTENT_ALL_CACHES) { |
| 3246 | final View[] children = mChildren; |
| 3247 | final int count = mChildrenCount; |
| 3248 | for (int i = 0; i < count; i++) { |
| 3249 | children[i].setDrawingCacheEnabled(enabled); |
| 3250 | } |
| 3251 | } |
| 3252 | } |
| 3253 | |
sergeyv | b37d44e | 2016-03-29 20:27:44 -0700 | [diff] [blame] | 3254 | /** |
| 3255 | * @hide |
| 3256 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3257 | @Override |
sergeyv | b37d44e | 2016-03-29 20:27:44 -0700 | [diff] [blame] | 3258 | public Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) { |
Romain Guy | 65554f2 | 2010-03-22 18:58:21 -0700 | [diff] [blame] | 3259 | int count = mChildrenCount; |
| 3260 | int[] visibilities = null; |
| 3261 | |
Romain Guy | 223ff5c | 2010-03-02 17:07:47 -0800 | [diff] [blame] | 3262 | if (skipChildren) { |
Romain Guy | 65554f2 | 2010-03-22 18:58:21 -0700 | [diff] [blame] | 3263 | visibilities = new int[count]; |
| 3264 | for (int i = 0; i < count; i++) { |
| 3265 | View child = getChildAt(i); |
| 3266 | visibilities[i] = child.getVisibility(); |
| 3267 | if (visibilities[i] == View.VISIBLE) { |
sergeyv | b37d44e | 2016-03-29 20:27:44 -0700 | [diff] [blame] | 3268 | child.mViewFlags = (child.mViewFlags & ~View.VISIBILITY_MASK) |
| 3269 | | (View.INVISIBLE & View.VISIBILITY_MASK); |
Romain Guy | 65554f2 | 2010-03-22 18:58:21 -0700 | [diff] [blame] | 3270 | } |
| 3271 | } |
Romain Guy | 223ff5c | 2010-03-02 17:07:47 -0800 | [diff] [blame] | 3272 | } |
| 3273 | |
| 3274 | Bitmap b = super.createSnapshot(quality, backgroundColor, skipChildren); |
Romain Guy | 65554f2 | 2010-03-22 18:58:21 -0700 | [diff] [blame] | 3275 | |
| 3276 | if (skipChildren) { |
| 3277 | for (int i = 0; i < count; i++) { |
sergeyv | b37d44e | 2016-03-29 20:27:44 -0700 | [diff] [blame] | 3278 | View child = getChildAt(i); |
| 3279 | child.mViewFlags = (child.mViewFlags & ~View.VISIBILITY_MASK) |
| 3280 | | (visibilities[i] & View.VISIBILITY_MASK); |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 3281 | } |
Romain Guy | 65554f2 | 2010-03-22 18:58:21 -0700 | [diff] [blame] | 3282 | } |
Romain Guy | 223ff5c | 2010-03-02 17:07:47 -0800 | [diff] [blame] | 3283 | |
| 3284 | return b; |
| 3285 | } |
| 3286 | |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 3287 | /** Return true if this ViewGroup is laying out using optical bounds. */ |
| 3288 | boolean isLayoutModeOptical() { |
| 3289 | return mLayoutMode == LAYOUT_MODE_OPTICAL_BOUNDS; |
| 3290 | } |
Romain Guy | cbc6774 | 2012-04-27 16:12:57 -0700 | [diff] [blame] | 3291 | |
Alan Viverette | be463f2 | 2016-01-21 10:50:10 -0500 | [diff] [blame] | 3292 | @Override |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 3293 | Insets computeOpticalInsets() { |
| 3294 | if (isLayoutModeOptical()) { |
| 3295 | int left = 0; |
| 3296 | int top = 0; |
| 3297 | int right = 0; |
| 3298 | int bottom = 0; |
| 3299 | for (int i = 0; i < mChildrenCount; i++) { |
| 3300 | View child = getChildAt(i); |
| 3301 | if (child.getVisibility() == VISIBLE) { |
| 3302 | Insets insets = child.getOpticalInsets(); |
| 3303 | left = Math.max(left, insets.left); |
| 3304 | top = Math.max(top, insets.top); |
| 3305 | right = Math.max(right, insets.right); |
| 3306 | bottom = Math.max(bottom, insets.bottom); |
| 3307 | } |
| 3308 | } |
| 3309 | return Insets.of(left, top, right, bottom); |
| 3310 | } else { |
| 3311 | return Insets.NONE; |
| 3312 | } |
| 3313 | } |
| 3314 | |
| 3315 | private static void fillRect(Canvas canvas, Paint paint, int x1, int y1, int x2, int y2) { |
| 3316 | if (x1 != x2 && y1 != y2) { |
| 3317 | if (x1 > x2) { |
| 3318 | int tmp = x1; x1 = x2; x2 = tmp; |
| 3319 | } |
| 3320 | if (y1 > y2) { |
| 3321 | int tmp = y1; y1 = y2; y2 = tmp; |
| 3322 | } |
| 3323 | canvas.drawRect(x1, y1, x2, y2, paint); |
| 3324 | } |
| 3325 | } |
| 3326 | |
| 3327 | private static int sign(int x) { |
| 3328 | return (x >= 0) ? 1 : -1; |
| 3329 | } |
| 3330 | |
| 3331 | private static void drawCorner(Canvas c, Paint paint, int x1, int y1, int dx, int dy, int lw) { |
| 3332 | fillRect(c, paint, x1, y1, x1 + dx, y1 + lw * sign(dy)); |
| 3333 | fillRect(c, paint, x1, y1, x1 + lw * sign(dx), y1 + dy); |
| 3334 | } |
| 3335 | |
| 3336 | private int dipsToPixels(int dips) { |
| 3337 | float scale = getContext().getResources().getDisplayMetrics().density; |
| 3338 | return (int) (dips * scale + 0.5f); |
| 3339 | } |
| 3340 | |
Romain Guy | 6410c0a | 2013-06-17 11:21:58 -0700 | [diff] [blame] | 3341 | private static void drawRectCorners(Canvas canvas, int x1, int y1, int x2, int y2, Paint paint, |
| 3342 | int lineLength, int lineWidth) { |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 3343 | drawCorner(canvas, paint, x1, y1, lineLength, lineLength, lineWidth); |
| 3344 | drawCorner(canvas, paint, x1, y2, lineLength, -lineLength, lineWidth); |
| 3345 | drawCorner(canvas, paint, x2, y1, -lineLength, lineLength, lineWidth); |
| 3346 | drawCorner(canvas, paint, x2, y2, -lineLength, -lineLength, lineWidth); |
| 3347 | } |
| 3348 | |
| 3349 | private static void fillDifference(Canvas canvas, |
| 3350 | int x2, int y2, int x3, int y3, |
| 3351 | int dx1, int dy1, int dx2, int dy2, Paint paint) { |
| 3352 | int x1 = x2 - dx1; |
| 3353 | int y1 = y2 - dy1; |
| 3354 | |
| 3355 | int x4 = x3 + dx2; |
| 3356 | int y4 = y3 + dy2; |
| 3357 | |
| 3358 | fillRect(canvas, paint, x1, y1, x4, y2); |
| 3359 | fillRect(canvas, paint, x1, y2, x2, y3); |
| 3360 | fillRect(canvas, paint, x3, y2, x4, y3); |
| 3361 | fillRect(canvas, paint, x1, y3, x4, y4); |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 3362 | } |
| 3363 | |
| 3364 | /** |
| 3365 | * @hide |
| 3366 | */ |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 3367 | protected void onDebugDrawMargins(Canvas canvas, Paint paint) { |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 3368 | for (int i = 0; i < getChildCount(); i++) { |
| 3369 | View c = getChildAt(i); |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 3370 | c.getLayoutParams().onDebugDraw(c, canvas, paint); |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 3371 | } |
| 3372 | } |
| 3373 | |
| 3374 | /** |
| 3375 | * @hide |
| 3376 | */ |
| 3377 | protected void onDebugDraw(Canvas canvas) { |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 3378 | Paint paint = getDebugPaint(); |
| 3379 | |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 3380 | // Draw optical bounds |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 3381 | { |
| 3382 | paint.setColor(Color.RED); |
| 3383 | paint.setStyle(Paint.Style.STROKE); |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 3384 | |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 3385 | for (int i = 0; i < getChildCount(); i++) { |
| 3386 | View c = getChildAt(i); |
Chris Craik | a1dab8b | 2015-06-30 13:51:25 -0700 | [diff] [blame] | 3387 | if (c.getVisibility() != View.GONE) { |
| 3388 | Insets insets = c.getOpticalInsets(); |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 3389 | |
Chris Craik | a1dab8b | 2015-06-30 13:51:25 -0700 | [diff] [blame] | 3390 | drawRect(canvas, paint, |
| 3391 | c.getLeft() + insets.left, |
| 3392 | c.getTop() + insets.top, |
| 3393 | c.getRight() - insets.right - 1, |
| 3394 | c.getBottom() - insets.bottom - 1); |
| 3395 | } |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 3396 | } |
| 3397 | } |
| 3398 | |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 3399 | // Draw margins |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 3400 | { |
| 3401 | paint.setColor(Color.argb(63, 255, 0, 255)); |
| 3402 | paint.setStyle(Paint.Style.FILL); |
Philip Milne | 604f440 | 2012-04-24 19:27:11 -0700 | [diff] [blame] | 3403 | |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 3404 | onDebugDrawMargins(canvas, paint); |
| 3405 | } |
| 3406 | |
| 3407 | // Draw clip bounds |
| 3408 | { |
| 3409 | paint.setColor(Color.rgb(63, 127, 255)); |
| 3410 | paint.setStyle(Paint.Style.FILL); |
| 3411 | |
| 3412 | int lineLength = dipsToPixels(8); |
| 3413 | int lineWidth = dipsToPixels(1); |
| 3414 | for (int i = 0; i < getChildCount(); i++) { |
| 3415 | View c = getChildAt(i); |
Chris Craik | a1dab8b | 2015-06-30 13:51:25 -0700 | [diff] [blame] | 3416 | if (c.getVisibility() != View.GONE) { |
| 3417 | drawRectCorners(canvas, c.getLeft(), c.getTop(), c.getRight(), c.getBottom(), |
| 3418 | paint, lineLength, lineWidth); |
| 3419 | } |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 3420 | } |
Philip Milne | 604f440 | 2012-04-24 19:27:11 -0700 | [diff] [blame] | 3421 | } |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 3422 | } |
| 3423 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3424 | @Override |
| 3425 | protected void dispatchDraw(Canvas canvas) { |
Chris Craik | a753f4c | 2014-07-24 12:39:17 -0700 | [diff] [blame] | 3426 | boolean usingRenderNodeProperties = canvas.isRecordingFor(mRenderNode); |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 3427 | final int childrenCount = mChildrenCount; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3428 | final View[] children = mChildren; |
| 3429 | int flags = mGroupFlags; |
| 3430 | |
| 3431 | if ((flags & FLAG_RUN_ANIMATION) != 0 && canAnimate()) { |
Romain Guy | 0d9275e | 2010-10-26 14:22:30 -0700 | [diff] [blame] | 3432 | final boolean buildCache = !isHardwareAccelerated(); |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 3433 | for (int i = 0; i < childrenCount; i++) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3434 | final View child = children[i]; |
| 3435 | if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) { |
| 3436 | final LayoutParams params = child.getLayoutParams(); |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 3437 | attachLayoutAnimationParameters(child, params, i, childrenCount); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3438 | bindLayoutAnimation(child); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3439 | } |
| 3440 | } |
| 3441 | |
| 3442 | final LayoutAnimationController controller = mLayoutAnimationController; |
| 3443 | if (controller.willOverlap()) { |
| 3444 | mGroupFlags |= FLAG_OPTIMIZE_INVALIDATE; |
| 3445 | } |
| 3446 | |
| 3447 | controller.start(); |
| 3448 | |
| 3449 | mGroupFlags &= ~FLAG_RUN_ANIMATION; |
| 3450 | mGroupFlags &= ~FLAG_ANIMATION_DONE; |
| 3451 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3452 | if (mAnimationListener != null) { |
| 3453 | mAnimationListener.onAnimationStart(controller.getAnimation()); |
| 3454 | } |
| 3455 | } |
| 3456 | |
Selim Cinek | 19cadc2 | 2014-04-16 17:27:19 +0200 | [diff] [blame] | 3457 | int clipSaveCount = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3458 | final boolean clipToPadding = (flags & CLIP_TO_PADDING_MASK) == CLIP_TO_PADDING_MASK; |
| 3459 | if (clipToPadding) { |
Chris Craik | e4cf152 | 2014-08-04 17:55:22 -0700 | [diff] [blame] | 3460 | clipSaveCount = canvas.save(); |
Romain Guy | 8f2d94f | 2009-03-25 18:04:42 -0700 | [diff] [blame] | 3461 | canvas.clipRect(mScrollX + mPaddingLeft, mScrollY + mPaddingTop, |
| 3462 | mScrollX + mRight - mLeft - mPaddingRight, |
| 3463 | mScrollY + mBottom - mTop - mPaddingBottom); |
Selim Cinek | 19cadc2 | 2014-04-16 17:27:19 +0200 | [diff] [blame] | 3464 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3465 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3466 | // We will draw our child's animation, let's reset the flag |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 3467 | mPrivateFlags &= ~PFLAG_DRAW_ANIMATION; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3468 | mGroupFlags &= ~FLAG_INVALIDATE_REQUIRED; |
| 3469 | |
| 3470 | boolean more = false; |
| 3471 | final long drawingTime = getDrawingTime(); |
| 3472 | |
Chris Craik | 8afd0f2 | 2014-08-21 17:41:57 -0700 | [diff] [blame] | 3473 | if (usingRenderNodeProperties) canvas.insertReorderBarrier(); |
Chet Haase | c633d2f | 2015-04-07 10:29:39 -0700 | [diff] [blame] | 3474 | final int transientCount = mTransientIndices == null ? 0 : mTransientIndices.size(); |
| 3475 | int transientIndex = transientCount != 0 ? 0 : -1; |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 3476 | // Only use the preordered list if not HW accelerated, since the HW pipeline will do the |
| 3477 | // draw reordering internally |
Chris Craik | a753f4c | 2014-07-24 12:39:17 -0700 | [diff] [blame] | 3478 | final ArrayList<View> preorderedList = usingRenderNodeProperties |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 3479 | ? null : buildOrderedChildList(); |
| 3480 | final boolean customOrder = preorderedList == null |
| 3481 | && isChildrenDrawingOrderEnabled(); |
| 3482 | for (int i = 0; i < childrenCount; i++) { |
Chet Haase | c633d2f | 2015-04-07 10:29:39 -0700 | [diff] [blame] | 3483 | while (transientIndex >= 0 && mTransientIndices.get(transientIndex) == i) { |
| 3484 | final View transientChild = mTransientViews.get(transientIndex); |
| 3485 | if ((transientChild.mViewFlags & VISIBILITY_MASK) == VISIBLE || |
| 3486 | transientChild.getAnimation() != null) { |
| 3487 | more |= drawChild(canvas, transientChild, drawingTime); |
| 3488 | } |
| 3489 | transientIndex++; |
| 3490 | if (transientIndex >= transientCount) { |
| 3491 | transientIndex = -1; |
| 3492 | } |
| 3493 | } |
Alan Viverette | a7b85e6 | 2016-01-22 10:14:02 -0500 | [diff] [blame] | 3494 | |
| 3495 | final int childIndex = getAndVerifyPreorderedIndex(childrenCount, i, customOrder); |
| 3496 | final View child = getAndVerifyPreorderedView(preorderedList, children, childIndex); |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 3497 | if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null) { |
| 3498 | more |= drawChild(canvas, child, drawingTime); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3499 | } |
| 3500 | } |
Chet Haase | c633d2f | 2015-04-07 10:29:39 -0700 | [diff] [blame] | 3501 | while (transientIndex >= 0) { |
| 3502 | // there may be additional transient views after the normal views |
| 3503 | final View transientChild = mTransientViews.get(transientIndex); |
| 3504 | if ((transientChild.mViewFlags & VISIBILITY_MASK) == VISIBLE || |
| 3505 | transientChild.getAnimation() != null) { |
| 3506 | more |= drawChild(canvas, transientChild, drawingTime); |
| 3507 | } |
| 3508 | transientIndex++; |
| 3509 | if (transientIndex >= transientCount) { |
| 3510 | break; |
| 3511 | } |
| 3512 | } |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 3513 | if (preorderedList != null) preorderedList.clear(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3514 | |
| 3515 | // Draw any disappearing views that have animations |
| 3516 | if (mDisappearingChildren != null) { |
| 3517 | final ArrayList<View> disappearingChildren = mDisappearingChildren; |
| 3518 | final int disappearingCount = disappearingChildren.size() - 1; |
| 3519 | // Go backwards -- we may delete as animations finish |
| 3520 | for (int i = disappearingCount; i >= 0; i--) { |
| 3521 | final View child = disappearingChildren.get(i); |
| 3522 | more |= drawChild(canvas, child, drawingTime); |
| 3523 | } |
| 3524 | } |
Chris Craik | 8afd0f2 | 2014-08-21 17:41:57 -0700 | [diff] [blame] | 3525 | if (usingRenderNodeProperties) canvas.insertInorderBarrier(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3526 | |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 3527 | if (debugDraw()) { |
| 3528 | onDebugDraw(canvas); |
| 3529 | } |
| 3530 | |
Chris Craik | e4cf152 | 2014-08-04 17:55:22 -0700 | [diff] [blame] | 3531 | if (clipToPadding) { |
Selim Cinek | 19cadc2 | 2014-04-16 17:27:19 +0200 | [diff] [blame] | 3532 | canvas.restoreToCount(clipSaveCount); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3533 | } |
| 3534 | |
| 3535 | // mGroupFlags might have been updated by drawChild() |
| 3536 | flags = mGroupFlags; |
| 3537 | |
| 3538 | if ((flags & FLAG_INVALIDATE_REQUIRED) == FLAG_INVALIDATE_REQUIRED) { |
Romain Guy | 849d0a3 | 2011-02-01 17:20:48 -0800 | [diff] [blame] | 3539 | invalidate(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3540 | } |
| 3541 | |
| 3542 | if ((flags & FLAG_ANIMATION_DONE) == 0 && (flags & FLAG_NOTIFY_ANIMATION_LISTENER) == 0 && |
| 3543 | mLayoutAnimationController.isDone() && !more) { |
| 3544 | // We want to erase the drawing cache and notify the listener after the |
| 3545 | // next frame is drawn because one extra invalidate() is caused by |
| 3546 | // drawChild() after the animation is over |
| 3547 | mGroupFlags |= FLAG_NOTIFY_ANIMATION_LISTENER; |
| 3548 | final Runnable end = new Runnable() { |
Alan Viverette | be463f2 | 2016-01-21 10:50:10 -0500 | [diff] [blame] | 3549 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3550 | public void run() { |
| 3551 | notifyAnimationListener(); |
| 3552 | } |
| 3553 | }; |
| 3554 | post(end); |
| 3555 | } |
| 3556 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 3557 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3558 | /** |
Chet Haase | edf6f4b | 2013-03-26 07:55:30 -0700 | [diff] [blame] | 3559 | * Returns the ViewGroupOverlay for this view group, creating it if it does |
| 3560 | * not yet exist. In addition to {@link ViewOverlay}'s support for drawables, |
| 3561 | * {@link ViewGroupOverlay} allows views to be added to the overlay. These |
| 3562 | * views, like overlay drawables, are visual-only; they do not receive input |
| 3563 | * events and should not be used as anything other than a temporary |
| 3564 | * representation of a view in a parent container, such as might be used |
| 3565 | * by an animation effect. |
| 3566 | * |
Chet Haase | 9539949 | 2013-04-08 14:30:31 -0700 | [diff] [blame] | 3567 | * <p>Note: Overlays do not currently work correctly with {@link |
| 3568 | * SurfaceView} or {@link TextureView}; contents in overlays for these |
| 3569 | * types of views may not display correctly.</p> |
| 3570 | * |
Chet Haase | edf6f4b | 2013-03-26 07:55:30 -0700 | [diff] [blame] | 3571 | * @return The ViewGroupOverlay object for this view. |
| 3572 | * @see ViewGroupOverlay |
| 3573 | */ |
| 3574 | @Override |
| 3575 | public ViewGroupOverlay getOverlay() { |
| 3576 | if (mOverlay == null) { |
| 3577 | mOverlay = new ViewGroupOverlay(mContext, this); |
| 3578 | } |
| 3579 | return (ViewGroupOverlay) mOverlay; |
| 3580 | } |
| 3581 | |
| 3582 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3583 | * Returns the index of the child to draw for this iteration. Override this |
| 3584 | * if you want to change the drawing order of children. By default, it |
| 3585 | * returns i. |
| 3586 | * <p> |
Romain Guy | 293451e | 2009-11-04 13:59:48 -0800 | [diff] [blame] | 3587 | * NOTE: In order for this method to be called, you must enable child ordering |
| 3588 | * first by calling {@link #setChildrenDrawingOrderEnabled(boolean)}. |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 3589 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3590 | * @param i The current iteration. |
| 3591 | * @return The index of the child to draw this iteration. |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 3592 | * |
Romain Guy | 293451e | 2009-11-04 13:59:48 -0800 | [diff] [blame] | 3593 | * @see #setChildrenDrawingOrderEnabled(boolean) |
| 3594 | * @see #isChildrenDrawingOrderEnabled() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3595 | */ |
| 3596 | protected int getChildDrawingOrder(int childCount, int i) { |
| 3597 | return i; |
| 3598 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 3599 | |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 3600 | private boolean hasChildWithZ() { |
| 3601 | for (int i = 0; i < mChildrenCount; i++) { |
| 3602 | if (mChildren[i].getZ() != 0) return true; |
| 3603 | } |
| 3604 | return false; |
| 3605 | } |
| 3606 | |
| 3607 | /** |
| 3608 | * Populates (and returns) mPreSortedChildren with a pre-ordered list of the View's children, |
Chris Craik | 57c79c8 | 2014-09-30 12:54:31 -0700 | [diff] [blame] | 3609 | * sorted first by Z, then by child drawing order (if applicable). This list must be cleared |
| 3610 | * after use to avoid leaking child Views. |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 3611 | * |
| 3612 | * Uses a stable, insertion sort which is commonly O(n) for ViewGroups with very few elevated |
| 3613 | * children. |
| 3614 | */ |
George Mount | 8120652 | 2014-09-26 21:53:39 -0700 | [diff] [blame] | 3615 | ArrayList<View> buildOrderedChildList() { |
Alan Viverette | a7b85e6 | 2016-01-22 10:14:02 -0500 | [diff] [blame] | 3616 | final int childrenCount = mChildrenCount; |
| 3617 | if (childrenCount <= 1 || !hasChildWithZ()) return null; |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 3618 | |
| 3619 | if (mPreSortedChildren == null) { |
Alan Viverette | a7b85e6 | 2016-01-22 10:14:02 -0500 | [diff] [blame] | 3620 | mPreSortedChildren = new ArrayList<>(childrenCount); |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 3621 | } else { |
Alan Viverette | a7b85e6 | 2016-01-22 10:14:02 -0500 | [diff] [blame] | 3622 | mPreSortedChildren.ensureCapacity(childrenCount); |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 3623 | } |
| 3624 | |
Alan Viverette | a7b85e6 | 2016-01-22 10:14:02 -0500 | [diff] [blame] | 3625 | final boolean customOrder = isChildrenDrawingOrderEnabled(); |
| 3626 | for (int i = 0; i < childrenCount; i++) { |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 3627 | // add next child (in child order) to end of list |
Alan Viverette | a7b85e6 | 2016-01-22 10:14:02 -0500 | [diff] [blame] | 3628 | final int childIndex = getAndVerifyPreorderedIndex(childrenCount, i, customOrder); |
| 3629 | final View nextChild = mChildren[childIndex]; |
| 3630 | final float currentZ = nextChild.getZ(); |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 3631 | |
| 3632 | // insert ahead of any Views with greater Z |
| 3633 | int insertIndex = i; |
| 3634 | while (insertIndex > 0 && mPreSortedChildren.get(insertIndex - 1).getZ() > currentZ) { |
| 3635 | insertIndex--; |
| 3636 | } |
| 3637 | mPreSortedChildren.add(insertIndex, nextChild); |
| 3638 | } |
| 3639 | return mPreSortedChildren; |
| 3640 | } |
| 3641 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3642 | private void notifyAnimationListener() { |
| 3643 | mGroupFlags &= ~FLAG_NOTIFY_ANIMATION_LISTENER; |
| 3644 | mGroupFlags |= FLAG_ANIMATION_DONE; |
| 3645 | |
| 3646 | if (mAnimationListener != null) { |
| 3647 | final Runnable end = new Runnable() { |
Alan Viverette | be463f2 | 2016-01-21 10:50:10 -0500 | [diff] [blame] | 3648 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3649 | public void run() { |
| 3650 | mAnimationListener.onAnimationEnd(mLayoutAnimationController.getAnimation()); |
| 3651 | } |
| 3652 | }; |
| 3653 | post(end); |
| 3654 | } |
| 3655 | |
Romain Guy | 849d0a3 | 2011-02-01 17:20:48 -0800 | [diff] [blame] | 3656 | invalidate(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3657 | } |
| 3658 | |
| 3659 | /** |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 3660 | * This method is used to cause children of this ViewGroup to restore or recreate their |
| 3661 | * display lists. It is called by getDisplayList() when the parent ViewGroup does not need |
| 3662 | * to recreate its own display list, which would happen if it went through the normal |
| 3663 | * draw/dispatchDraw mechanisms. |
| 3664 | * |
| 3665 | * @hide |
| 3666 | */ |
| 3667 | @Override |
| 3668 | protected void dispatchGetDisplayList() { |
| 3669 | final int count = mChildrenCount; |
| 3670 | final View[] children = mChildren; |
| 3671 | for (int i = 0; i < count; i++) { |
| 3672 | final View child = children[i]; |
John Reck | c2330f5 | 2015-04-28 13:18:52 -0700 | [diff] [blame] | 3673 | if (((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null)) { |
Chet Haase | 6c0665f | 2014-08-01 13:32:27 -0700 | [diff] [blame] | 3674 | recreateChildDisplayList(child); |
Romain Guy | 2f57ba5 | 2011-02-03 18:03:29 -0800 | [diff] [blame] | 3675 | } |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 3676 | } |
Chet Haase | 91cedf1 | 2013-03-11 07:56:30 -0700 | [diff] [blame] | 3677 | if (mOverlay != null) { |
Chet Haase | edf6f4b | 2013-03-26 07:55:30 -0700 | [diff] [blame] | 3678 | View overlayView = mOverlay.getOverlayView(); |
Chet Haase | 6c0665f | 2014-08-01 13:32:27 -0700 | [diff] [blame] | 3679 | recreateChildDisplayList(overlayView); |
Chet Haase | 91cedf1 | 2013-03-11 07:56:30 -0700 | [diff] [blame] | 3680 | } |
Chet Haase | 6c0665f | 2014-08-01 13:32:27 -0700 | [diff] [blame] | 3681 | if (mDisappearingChildren != null) { |
| 3682 | final ArrayList<View> disappearingChildren = mDisappearingChildren; |
| 3683 | final int disappearingCount = disappearingChildren.size(); |
| 3684 | for (int i = 0; i < disappearingCount; ++i) { |
| 3685 | final View child = disappearingChildren.get(i); |
| 3686 | recreateChildDisplayList(child); |
| 3687 | } |
| 3688 | } |
| 3689 | } |
| 3690 | |
| 3691 | private void recreateChildDisplayList(View child) { |
Chris Craik | 31a2d06 | 2015-05-01 14:22:47 -0700 | [diff] [blame] | 3692 | child.mRecreateDisplayList = (child.mPrivateFlags & PFLAG_INVALIDATED) != 0; |
Chet Haase | 6c0665f | 2014-08-01 13:32:27 -0700 | [diff] [blame] | 3693 | child.mPrivateFlags &= ~PFLAG_INVALIDATED; |
Chris Craik | 31a2d06 | 2015-05-01 14:22:47 -0700 | [diff] [blame] | 3694 | child.updateDisplayListIfDirty(); |
Chet Haase | 6c0665f | 2014-08-01 13:32:27 -0700 | [diff] [blame] | 3695 | child.mRecreateDisplayList = false; |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 3696 | } |
| 3697 | |
| 3698 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3699 | * Draw one child of this View Group. This method is responsible for getting |
| 3700 | * the canvas in the right state. This includes clipping, translating so |
| 3701 | * that the child's scrolled origin is at 0, 0, and applying any animation |
| 3702 | * transformations. |
| 3703 | * |
| 3704 | * @param canvas The canvas on which to draw the child |
| 3705 | * @param child Who to draw |
Chet Haase | bcca79a | 2012-02-14 08:45:14 -0800 | [diff] [blame] | 3706 | * @param drawingTime The time at which draw is occurring |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3707 | * @return True if an invalidate() was issued |
| 3708 | */ |
| 3709 | protected boolean drawChild(Canvas canvas, View child, long drawingTime) { |
Chet Haase | 64a48c1 | 2012-02-13 16:33:29 -0800 | [diff] [blame] | 3710 | return child.draw(canvas, this, drawingTime); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3711 | } |
| 3712 | |
Alan Viverette | 922e1c6 | 2015-05-05 17:18:27 -0700 | [diff] [blame] | 3713 | @Override |
| 3714 | void getScrollIndicatorBounds(@NonNull Rect out) { |
| 3715 | super.getScrollIndicatorBounds(out); |
| 3716 | |
| 3717 | // If we have padding and we're supposed to clip children to that |
| 3718 | // padding, offset the scroll indicators to match our clip bounds. |
| 3719 | final boolean clipToPadding = (mGroupFlags & CLIP_TO_PADDING_MASK) == CLIP_TO_PADDING_MASK; |
| 3720 | if (clipToPadding) { |
| 3721 | out.left += mPaddingLeft; |
| 3722 | out.right -= mPaddingRight; |
| 3723 | out.top += mPaddingTop; |
| 3724 | out.bottom -= mPaddingBottom; |
| 3725 | } |
| 3726 | } |
| 3727 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3728 | /** |
Chris Craik | d863a10 | 2013-12-19 13:31:15 -0800 | [diff] [blame] | 3729 | * 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] | 3730 | * The default value is true. |
| 3731 | * @see #setClipChildren(boolean) |
| 3732 | * |
| 3733 | * @return True if the group's children will be clipped to their bounds, |
| 3734 | * false otherwise. |
| 3735 | */ |
Chris Craik | 5c75c52 | 2014-09-05 14:08:08 -0700 | [diff] [blame] | 3736 | @ViewDebug.ExportedProperty(category = "drawing") |
Chet Haase | 430742f | 2013-04-12 11:18:36 -0700 | [diff] [blame] | 3737 | public boolean getClipChildren() { |
| 3738 | return ((mGroupFlags & FLAG_CLIP_CHILDREN) != 0); |
| 3739 | } |
| 3740 | |
| 3741 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3742 | * By default, children are clipped to their bounds before drawing. This |
| 3743 | * allows view groups to override this behavior for animations, etc. |
| 3744 | * |
| 3745 | * @param clipChildren true to clip children to their bounds, |
| 3746 | * false otherwise |
| 3747 | * @attr ref android.R.styleable#ViewGroup_clipChildren |
| 3748 | */ |
| 3749 | public void setClipChildren(boolean clipChildren) { |
Chet Haase | a1cff50 | 2012-02-21 13:43:44 -0800 | [diff] [blame] | 3750 | boolean previousValue = (mGroupFlags & FLAG_CLIP_CHILDREN) == FLAG_CLIP_CHILDREN; |
| 3751 | if (clipChildren != previousValue) { |
| 3752 | setBooleanFlag(FLAG_CLIP_CHILDREN, clipChildren); |
Chet Haase | 1271e2c | 2012-04-20 09:54:27 -0700 | [diff] [blame] | 3753 | for (int i = 0; i < mChildrenCount; ++i) { |
| 3754 | View child = getChildAt(i); |
Chris Craik | 64a12e1 | 2014-03-28 18:12:12 -0700 | [diff] [blame] | 3755 | if (child.mRenderNode != null) { |
| 3756 | child.mRenderNode.setClipToBounds(clipChildren); |
Chet Haase | a1cff50 | 2012-02-21 13:43:44 -0800 | [diff] [blame] | 3757 | } |
| 3758 | } |
John Reck | aae9f3b | 2014-07-28 09:30:36 -0700 | [diff] [blame] | 3759 | invalidate(true); |
Chet Haase | a1cff50 | 2012-02-21 13:43:44 -0800 | [diff] [blame] | 3760 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3761 | } |
| 3762 | |
| 3763 | /** |
Doris Liu | b134b5e | 2015-05-28 17:26:46 -0700 | [diff] [blame] | 3764 | * Sets whether this ViewGroup will clip its children to its padding and resize (but not |
| 3765 | * clip) any EdgeEffect to the padded region, if padding is present. |
Chris Craik | b165296 | 2014-11-14 17:05:06 -0800 | [diff] [blame] | 3766 | * <p> |
| 3767 | * By default, children are clipped to the padding of their parent |
Doris Liu | b134b5e | 2015-05-28 17:26:46 -0700 | [diff] [blame] | 3768 | * 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] | 3769 | * |
Doris Liu | b134b5e | 2015-05-28 17:26:46 -0700 | [diff] [blame] | 3770 | * @param clipToPadding true to clip children to the padding of the group, and resize (but |
| 3771 | * not clip) any EdgeEffect to the padded region. False otherwise. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3772 | * @attr ref android.R.styleable#ViewGroup_clipToPadding |
| 3773 | */ |
| 3774 | public void setClipToPadding(boolean clipToPadding) { |
John Reck | 9fa3a24 | 2014-06-27 15:57:19 -0700 | [diff] [blame] | 3775 | if (hasBooleanFlag(FLAG_CLIP_TO_PADDING) != clipToPadding) { |
| 3776 | setBooleanFlag(FLAG_CLIP_TO_PADDING, clipToPadding); |
John Reck | aae9f3b | 2014-07-28 09:30:36 -0700 | [diff] [blame] | 3777 | invalidate(true); |
John Reck | 9fa3a24 | 2014-06-27 15:57:19 -0700 | [diff] [blame] | 3778 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3779 | } |
| 3780 | |
| 3781 | /** |
Doris Liu | b134b5e | 2015-05-28 17:26:46 -0700 | [diff] [blame] | 3782 | * Returns whether this ViewGroup will clip its children to its padding, and resize (but |
| 3783 | * not clip) any EdgeEffect to the padded region, if padding is present. |
Chris Craik | b165296 | 2014-11-14 17:05:06 -0800 | [diff] [blame] | 3784 | * <p> |
| 3785 | * By default, children are clipped to the padding of their parent |
| 3786 | * Viewgroup. This clipping behavior is only enabled if padding is non-zero. |
Adam Powell | 1c35b08 | 2014-07-11 15:37:15 -0700 | [diff] [blame] | 3787 | * |
Doris Liu | b134b5e | 2015-05-28 17:26:46 -0700 | [diff] [blame] | 3788 | * @return true if this ViewGroup clips children to its padding and resizes (but doesn't |
| 3789 | * clip) any EdgeEffect to the padded region, false otherwise. |
Adam Powell | 1c35b08 | 2014-07-11 15:37:15 -0700 | [diff] [blame] | 3790 | * |
| 3791 | * @attr ref android.R.styleable#ViewGroup_clipToPadding |
| 3792 | */ |
Chris Craik | 5c75c52 | 2014-09-05 14:08:08 -0700 | [diff] [blame] | 3793 | @ViewDebug.ExportedProperty(category = "drawing") |
Adam Powell | 1c35b08 | 2014-07-11 15:37:15 -0700 | [diff] [blame] | 3794 | public boolean getClipToPadding() { |
| 3795 | return hasBooleanFlag(FLAG_CLIP_TO_PADDING); |
| 3796 | } |
| 3797 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3798 | @Override |
| 3799 | public void dispatchSetSelected(boolean selected) { |
| 3800 | final View[] children = mChildren; |
| 3801 | final int count = mChildrenCount; |
| 3802 | for (int i = 0; i < count; i++) { |
| 3803 | children[i].setSelected(selected); |
| 3804 | } |
| 3805 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 3806 | |
Dianne Hackborn | d0fa371 | 2010-09-14 18:57:14 -0700 | [diff] [blame] | 3807 | @Override |
| 3808 | public void dispatchSetActivated(boolean activated) { |
| 3809 | final View[] children = mChildren; |
| 3810 | final int count = mChildrenCount; |
| 3811 | for (int i = 0; i < count; i++) { |
Dianne Hackborn | d0fa371 | 2010-09-14 18:57:14 -0700 | [diff] [blame] | 3812 | children[i].setActivated(activated); |
| 3813 | } |
| 3814 | } |
| 3815 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3816 | @Override |
| 3817 | protected void dispatchSetPressed(boolean pressed) { |
| 3818 | final View[] children = mChildren; |
| 3819 | final int count = mChildrenCount; |
| 3820 | for (int i = 0; i < count; i++) { |
Adam Powell | 035a1fc | 2012-02-27 15:23:50 -0800 | [diff] [blame] | 3821 | final View child = children[i]; |
| 3822 | // Children that are clickable on their own should not |
| 3823 | // show a pressed state when their parent view does. |
| 3824 | // Clearing a pressed state always propagates. |
| 3825 | if (!pressed || (!child.isClickable() && !child.isLongClickable())) { |
| 3826 | child.setPressed(pressed); |
| 3827 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3828 | } |
| 3829 | } |
| 3830 | |
Alan Viverette | b942b6f | 2014-12-08 10:37:39 -0800 | [diff] [blame] | 3831 | /** |
| 3832 | * Dispatches drawable hotspot changes to child views that meet at least |
| 3833 | * one of the following criteria: |
| 3834 | * <ul> |
| 3835 | * <li>Returns {@code false} from both {@link View#isClickable()} and |
| 3836 | * {@link View#isLongClickable()}</li> |
| 3837 | * <li>Requests duplication of parent state via |
| 3838 | * {@link View#setDuplicateParentStateEnabled(boolean)}</li> |
| 3839 | * </ul> |
| 3840 | * |
| 3841 | * @param x hotspot x coordinate |
| 3842 | * @param y hotspot y coordinate |
| 3843 | * @see #drawableHotspotChanged(float, float) |
| 3844 | */ |
| 3845 | @Override |
| 3846 | public void dispatchDrawableHotspotChanged(float x, float y) { |
| 3847 | final int count = mChildrenCount; |
| 3848 | if (count == 0) { |
| 3849 | return; |
| 3850 | } |
| 3851 | |
| 3852 | final View[] children = mChildren; |
| 3853 | for (int i = 0; i < count; i++) { |
| 3854 | final View child = children[i]; |
| 3855 | // Children that are clickable on their own should not |
| 3856 | // receive hotspots when their parent view does. |
| 3857 | final boolean nonActionable = !child.isClickable() && !child.isLongClickable(); |
| 3858 | final boolean duplicatesState = (child.mViewFlags & DUPLICATE_PARENT_STATE) != 0; |
| 3859 | if (nonActionable || duplicatesState) { |
| 3860 | final float[] point = getTempPoint(); |
| 3861 | point[0] = x; |
| 3862 | point[1] = y; |
| 3863 | transformPointToViewLocal(point, child); |
| 3864 | child.drawableHotspotChanged(point[0], point[1]); |
| 3865 | } |
| 3866 | } |
| 3867 | } |
| 3868 | |
Adam Powell | 1487466 | 2013-07-18 19:42:41 -0700 | [diff] [blame] | 3869 | @Override |
| 3870 | void dispatchCancelPendingInputEvents() { |
| 3871 | super.dispatchCancelPendingInputEvents(); |
| 3872 | |
| 3873 | final View[] children = mChildren; |
| 3874 | final int count = mChildrenCount; |
| 3875 | for (int i = 0; i < count; i++) { |
| 3876 | children[i].dispatchCancelPendingInputEvents(); |
| 3877 | } |
| 3878 | } |
| 3879 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3880 | /** |
| 3881 | * When this property is set to true, this ViewGroup supports static transformations on |
| 3882 | * children; this causes |
| 3883 | * {@link #getChildStaticTransformation(View, android.view.animation.Transformation)} to be |
| 3884 | * invoked when a child is drawn. |
| 3885 | * |
| 3886 | * Any subclass overriding |
| 3887 | * {@link #getChildStaticTransformation(View, android.view.animation.Transformation)} should |
| 3888 | * set this property to true. |
| 3889 | * |
| 3890 | * @param enabled True to enable static transformations on children, false otherwise. |
| 3891 | * |
Chet Haase | 599913d | 2012-07-23 16:22:05 -0700 | [diff] [blame] | 3892 | * @see #getChildStaticTransformation(View, android.view.animation.Transformation) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3893 | */ |
| 3894 | protected void setStaticTransformationsEnabled(boolean enabled) { |
| 3895 | setBooleanFlag(FLAG_SUPPORT_STATIC_TRANSFORMATIONS, enabled); |
| 3896 | } |
| 3897 | |
| 3898 | /** |
Chet Haase | 2d46fcc | 2011-12-19 18:01:05 -0800 | [diff] [blame] | 3899 | * Sets <code>t</code> to be the static transformation of the child, if set, returning a |
| 3900 | * boolean to indicate whether a static transform was set. The default implementation |
| 3901 | * simply returns <code>false</code>; subclasses may override this method for different |
Chet Haase | 599913d | 2012-07-23 16:22:05 -0700 | [diff] [blame] | 3902 | * behavior. {@link #setStaticTransformationsEnabled(boolean)} must be set to true |
| 3903 | * for this method to be called. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3904 | * |
Chet Haase | 2d46fcc | 2011-12-19 18:01:05 -0800 | [diff] [blame] | 3905 | * @param child The child view whose static transform is being requested |
| 3906 | * @param t The Transformation which will hold the result |
| 3907 | * @return true if the transformation was set, false otherwise |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 3908 | * @see #setStaticTransformationsEnabled(boolean) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3909 | */ |
| 3910 | protected boolean getChildStaticTransformation(View child, Transformation t) { |
| 3911 | return false; |
| 3912 | } |
| 3913 | |
Romain Guy | f699130 | 2013-06-05 17:19:01 -0700 | [diff] [blame] | 3914 | Transformation getChildTransformation() { |
| 3915 | if (mChildTransformation == null) { |
| 3916 | mChildTransformation = new Transformation(); |
| 3917 | } |
| 3918 | return mChildTransformation; |
| 3919 | } |
| 3920 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3921 | /** |
| 3922 | * {@hide} |
| 3923 | */ |
| 3924 | @Override |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 3925 | protected View findViewTraversal(@IdRes int id) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3926 | if (id == mID) { |
| 3927 | return this; |
| 3928 | } |
| 3929 | |
| 3930 | final View[] where = mChildren; |
| 3931 | final int len = mChildrenCount; |
| 3932 | |
| 3933 | for (int i = 0; i < len; i++) { |
| 3934 | View v = where[i]; |
| 3935 | |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 3936 | if ((v.mPrivateFlags & PFLAG_IS_ROOT_NAMESPACE) == 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3937 | v = v.findViewById(id); |
| 3938 | |
| 3939 | if (v != null) { |
| 3940 | return v; |
| 3941 | } |
| 3942 | } |
| 3943 | } |
| 3944 | |
| 3945 | return null; |
| 3946 | } |
| 3947 | |
| 3948 | /** |
| 3949 | * {@hide} |
| 3950 | */ |
| 3951 | @Override |
| 3952 | protected View findViewWithTagTraversal(Object tag) { |
| 3953 | if (tag != null && tag.equals(mTag)) { |
| 3954 | return this; |
| 3955 | } |
| 3956 | |
| 3957 | final View[] where = mChildren; |
| 3958 | final int len = mChildrenCount; |
| 3959 | |
| 3960 | for (int i = 0; i < len; i++) { |
| 3961 | View v = where[i]; |
| 3962 | |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 3963 | if ((v.mPrivateFlags & PFLAG_IS_ROOT_NAMESPACE) == 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3964 | v = v.findViewWithTag(tag); |
| 3965 | |
| 3966 | if (v != null) { |
| 3967 | return v; |
| 3968 | } |
| 3969 | } |
| 3970 | } |
| 3971 | |
| 3972 | return null; |
| 3973 | } |
| 3974 | |
| 3975 | /** |
Jeff Brown | 4e6319b | 2010-12-13 10:36:51 -0800 | [diff] [blame] | 3976 | * {@hide} |
| 3977 | */ |
| 3978 | @Override |
Jeff Brown | 4dfbec2 | 2011-08-15 14:55:37 -0700 | [diff] [blame] | 3979 | protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) { |
Jeff Brown | 4e6319b | 2010-12-13 10:36:51 -0800 | [diff] [blame] | 3980 | if (predicate.apply(this)) { |
| 3981 | return this; |
| 3982 | } |
| 3983 | |
| 3984 | final View[] where = mChildren; |
| 3985 | final int len = mChildrenCount; |
| 3986 | |
| 3987 | for (int i = 0; i < len; i++) { |
| 3988 | View v = where[i]; |
| 3989 | |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 3990 | if (v != childToSkip && (v.mPrivateFlags & PFLAG_IS_ROOT_NAMESPACE) == 0) { |
Jeff Brown | 4e6319b | 2010-12-13 10:36:51 -0800 | [diff] [blame] | 3991 | v = v.findViewByPredicate(predicate); |
| 3992 | |
| 3993 | if (v != null) { |
| 3994 | return v; |
| 3995 | } |
| 3996 | } |
| 3997 | } |
| 3998 | |
| 3999 | return null; |
| 4000 | } |
| 4001 | |
| 4002 | /** |
Chet Haase | c633d2f | 2015-04-07 10:29:39 -0700 | [diff] [blame] | 4003 | * This method adds a view to this container at the specified index purely for the |
| 4004 | * purposes of allowing that view to draw even though it is not a normal child of |
| 4005 | * the container. That is, the view does not participate in layout, focus, accessibility, |
| 4006 | * input, or other normal view operations; it is purely an item to be drawn during the normal |
| 4007 | * rendering operation of this container. The index that it is added at is the order |
| 4008 | * in which it will be drawn, with respect to the other views in the container. |
| 4009 | * For example, a transient view added at index 0 will be drawn before all other views |
| 4010 | * in the container because it will be drawn first (including before any real view |
| 4011 | * at index 0). There can be more than one transient view at any particular index; |
| 4012 | * these views will be drawn in the order in which they were added to the list of |
| 4013 | * transient views. The index of transient views can also be greater than the number |
| 4014 | * of normal views in the container; that just means that they will be drawn after all |
| 4015 | * other views are drawn. |
| 4016 | * |
| 4017 | * <p>Note that since transient views do not participate in layout, they must be sized |
| 4018 | * manually or, more typically, they should just use the size that they had before they |
| 4019 | * were removed from their container.</p> |
| 4020 | * |
| 4021 | * <p>Transient views are useful for handling animations of views that have been removed |
| 4022 | * from the container, but which should be animated out after the removal. Adding these |
| 4023 | * views as transient views allows them to participate in drawing without side-effecting |
| 4024 | * the layout of the container.</p> |
| 4025 | * |
| 4026 | * <p>Transient views must always be explicitly {@link #removeTransientView(View) removed} |
| 4027 | * from the container when they are no longer needed. For example, a transient view |
| 4028 | * which is added in order to fade it out in its old location should be removed |
| 4029 | * once the animation is complete.</p> |
| 4030 | * |
| 4031 | * @param view The view to be added |
| 4032 | * @param index The index at which this view should be drawn, must be >= 0. |
| 4033 | * This value is relative to the {@link #getChildAt(int) index} values in the normal |
| 4034 | * child list of this container, where any transient view at a particular index will |
| 4035 | * be drawn before any normal child at that same index. |
Chris Craik | 66b4139 | 2015-04-17 10:08:10 -0700 | [diff] [blame] | 4036 | * |
| 4037 | * @hide |
Chet Haase | c633d2f | 2015-04-07 10:29:39 -0700 | [diff] [blame] | 4038 | */ |
| 4039 | public void addTransientView(View view, int index) { |
| 4040 | if (index < 0) { |
| 4041 | return; |
| 4042 | } |
| 4043 | if (mTransientIndices == null) { |
| 4044 | mTransientIndices = new ArrayList<Integer>(); |
| 4045 | mTransientViews = new ArrayList<View>(); |
| 4046 | } |
| 4047 | final int oldSize = mTransientIndices.size(); |
| 4048 | if (oldSize > 0) { |
| 4049 | int insertionIndex; |
| 4050 | for (insertionIndex = 0; insertionIndex < oldSize; ++insertionIndex) { |
| 4051 | if (index < mTransientIndices.get(insertionIndex)) { |
| 4052 | break; |
| 4053 | } |
| 4054 | } |
| 4055 | mTransientIndices.add(insertionIndex, index); |
| 4056 | mTransientViews.add(insertionIndex, view); |
| 4057 | } else { |
| 4058 | mTransientIndices.add(index); |
| 4059 | mTransientViews.add(view); |
| 4060 | } |
| 4061 | view.mParent = this; |
| 4062 | view.dispatchAttachedToWindow(mAttachInfo, (mViewFlags&VISIBILITY_MASK)); |
| 4063 | invalidate(true); |
| 4064 | } |
| 4065 | |
| 4066 | /** |
| 4067 | * Removes a view from the list of transient views in this container. If there is no |
| 4068 | * such transient view, this method does nothing. |
| 4069 | * |
| 4070 | * @param view The transient view to be removed |
Chris Craik | 66b4139 | 2015-04-17 10:08:10 -0700 | [diff] [blame] | 4071 | * |
| 4072 | * @hide |
Chet Haase | c633d2f | 2015-04-07 10:29:39 -0700 | [diff] [blame] | 4073 | */ |
| 4074 | public void removeTransientView(View view) { |
| 4075 | if (mTransientViews == null) { |
| 4076 | return; |
| 4077 | } |
| 4078 | final int size = mTransientViews.size(); |
| 4079 | for (int i = 0; i < size; ++i) { |
| 4080 | if (view == mTransientViews.get(i)) { |
| 4081 | mTransientViews.remove(i); |
| 4082 | mTransientIndices.remove(i); |
| 4083 | view.mParent = null; |
| 4084 | view.dispatchDetachedFromWindow(); |
| 4085 | invalidate(true); |
| 4086 | return; |
| 4087 | } |
| 4088 | } |
| 4089 | } |
| 4090 | |
| 4091 | /** |
| 4092 | * Returns the number of transient views in this container. Specific transient |
| 4093 | * views and the index at which they were added can be retrieved via |
| 4094 | * {@link #getTransientView(int)} and {@link #getTransientViewIndex(int)}. |
| 4095 | * |
| 4096 | * @see #addTransientView(View, int) |
| 4097 | * @return The number of transient views in this container |
Chris Craik | 66b4139 | 2015-04-17 10:08:10 -0700 | [diff] [blame] | 4098 | * |
| 4099 | * @hide |
Chet Haase | c633d2f | 2015-04-07 10:29:39 -0700 | [diff] [blame] | 4100 | */ |
| 4101 | public int getTransientViewCount() { |
| 4102 | return mTransientIndices == null ? 0 : mTransientIndices.size(); |
| 4103 | } |
| 4104 | |
| 4105 | /** |
| 4106 | * Given a valid position within the list of transient views, returns the index of |
| 4107 | * the transient view at that position. |
| 4108 | * |
| 4109 | * @param position The position of the index being queried. Must be at least 0 |
| 4110 | * and less than the value returned by {@link #getTransientViewCount()}. |
| 4111 | * @return The index of the transient view stored in the given position if the |
| 4112 | * position is valid, otherwise -1 |
Chris Craik | 66b4139 | 2015-04-17 10:08:10 -0700 | [diff] [blame] | 4113 | * |
| 4114 | * @hide |
Chet Haase | c633d2f | 2015-04-07 10:29:39 -0700 | [diff] [blame] | 4115 | */ |
| 4116 | public int getTransientViewIndex(int position) { |
| 4117 | if (position < 0 || mTransientIndices == null || position >= mTransientIndices.size()) { |
| 4118 | return -1; |
| 4119 | } |
| 4120 | return mTransientIndices.get(position); |
| 4121 | } |
| 4122 | |
| 4123 | /** |
| 4124 | * Given a valid position within the list of transient views, returns the |
| 4125 | * transient view at that position. |
| 4126 | * |
| 4127 | * @param position The position of the view being queried. Must be at least 0 |
| 4128 | * and less than the value returned by {@link #getTransientViewCount()}. |
| 4129 | * @return The transient view stored in the given position if the |
| 4130 | * position is valid, otherwise null |
Chris Craik | 66b4139 | 2015-04-17 10:08:10 -0700 | [diff] [blame] | 4131 | * |
| 4132 | * @hide |
Chet Haase | c633d2f | 2015-04-07 10:29:39 -0700 | [diff] [blame] | 4133 | */ |
| 4134 | public View getTransientView(int position) { |
| 4135 | if (mTransientViews == null || position >= mTransientViews.size()) { |
| 4136 | return null; |
| 4137 | } |
| 4138 | return mTransientViews.get(position); |
| 4139 | } |
| 4140 | |
| 4141 | /** |
Romain Guy | 393a52c | 2012-05-22 20:21:08 -0700 | [diff] [blame] | 4142 | * <p>Adds a child view. If no layout parameters are already set on the child, the |
| 4143 | * default parameters for this ViewGroup are set on the child.</p> |
Filip Gruszczynski | a33bdf3 | 2015-11-19 18:22:16 -0800 | [diff] [blame] | 4144 | * |
Romain Guy | 393a52c | 2012-05-22 20:21:08 -0700 | [diff] [blame] | 4145 | * <p><strong>Note:</strong> do not invoke this method from |
| 4146 | * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)}, |
| 4147 | * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4148 | * |
| 4149 | * @param child the child view to add |
| 4150 | * |
| 4151 | * @see #generateDefaultLayoutParams() |
| 4152 | */ |
| 4153 | public void addView(View child) { |
| 4154 | addView(child, -1); |
| 4155 | } |
| 4156 | |
| 4157 | /** |
| 4158 | * Adds a child view. If no layout parameters are already set on the child, the |
| 4159 | * default parameters for this ViewGroup are set on the child. |
Filip Gruszczynski | a33bdf3 | 2015-11-19 18:22:16 -0800 | [diff] [blame] | 4160 | * |
Romain Guy | 393a52c | 2012-05-22 20:21:08 -0700 | [diff] [blame] | 4161 | * <p><strong>Note:</strong> do not invoke this method from |
| 4162 | * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)}, |
| 4163 | * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4164 | * |
| 4165 | * @param child the child view to add |
| 4166 | * @param index the position at which to add the child |
| 4167 | * |
| 4168 | * @see #generateDefaultLayoutParams() |
| 4169 | */ |
| 4170 | public void addView(View child, int index) { |
Adam Powell | 45a9da5 | 2014-10-09 09:44:18 -0700 | [diff] [blame] | 4171 | if (child == null) { |
| 4172 | throw new IllegalArgumentException("Cannot add a null child view to a ViewGroup"); |
| 4173 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4174 | LayoutParams params = child.getLayoutParams(); |
| 4175 | if (params == null) { |
| 4176 | params = generateDefaultLayoutParams(); |
| 4177 | if (params == null) { |
| 4178 | throw new IllegalArgumentException("generateDefaultLayoutParams() cannot return null"); |
| 4179 | } |
| 4180 | } |
| 4181 | addView(child, index, params); |
| 4182 | } |
| 4183 | |
| 4184 | /** |
| 4185 | * Adds a child view with this ViewGroup's default layout parameters and the |
| 4186 | * specified width and height. |
| 4187 | * |
Romain Guy | 393a52c | 2012-05-22 20:21:08 -0700 | [diff] [blame] | 4188 | * <p><strong>Note:</strong> do not invoke this method from |
| 4189 | * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)}, |
| 4190 | * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p> |
| 4191 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4192 | * @param child the child view to add |
| 4193 | */ |
| 4194 | public void addView(View child, int width, int height) { |
| 4195 | final LayoutParams params = generateDefaultLayoutParams(); |
| 4196 | params.width = width; |
| 4197 | params.height = height; |
| 4198 | addView(child, -1, params); |
| 4199 | } |
| 4200 | |
| 4201 | /** |
| 4202 | * Adds a child view with the specified layout parameters. |
| 4203 | * |
Romain Guy | 393a52c | 2012-05-22 20:21:08 -0700 | [diff] [blame] | 4204 | * <p><strong>Note:</strong> do not invoke this method from |
| 4205 | * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)}, |
| 4206 | * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p> |
| 4207 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4208 | * @param child the child view to add |
| 4209 | * @param params the layout parameters to set on the child |
| 4210 | */ |
Alan Viverette | be463f2 | 2016-01-21 10:50:10 -0500 | [diff] [blame] | 4211 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4212 | public void addView(View child, LayoutParams params) { |
| 4213 | addView(child, -1, params); |
| 4214 | } |
| 4215 | |
| 4216 | /** |
| 4217 | * Adds a child view with the specified layout parameters. |
| 4218 | * |
Romain Guy | 393a52c | 2012-05-22 20:21:08 -0700 | [diff] [blame] | 4219 | * <p><strong>Note:</strong> do not invoke this method from |
| 4220 | * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)}, |
| 4221 | * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p> |
| 4222 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4223 | * @param child the child view to add |
Alan Viverette | 77bb6f1 | 2015-02-11 17:24:33 -0800 | [diff] [blame] | 4224 | * @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] | 4225 | * @param params the layout parameters to set on the child |
| 4226 | */ |
| 4227 | public void addView(View child, int index, LayoutParams params) { |
| 4228 | if (DBG) { |
| 4229 | System.out.println(this + " addView"); |
| 4230 | } |
| 4231 | |
Adam Powell | 45a9da5 | 2014-10-09 09:44:18 -0700 | [diff] [blame] | 4232 | if (child == null) { |
| 4233 | throw new IllegalArgumentException("Cannot add a null child view to a ViewGroup"); |
| 4234 | } |
| 4235 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4236 | // addViewInner() will call child.requestLayout() when setting the new LayoutParams |
| 4237 | // therefore, we call requestLayout() on ourselves before, so that the child's request |
| 4238 | // will be blocked at our level |
| 4239 | requestLayout(); |
Romain Guy | 849d0a3 | 2011-02-01 17:20:48 -0800 | [diff] [blame] | 4240 | invalidate(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4241 | addViewInner(child, index, params, false); |
| 4242 | } |
| 4243 | |
Alan Viverette | be463f2 | 2016-01-21 10:50:10 -0500 | [diff] [blame] | 4244 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4245 | public void updateViewLayout(View view, ViewGroup.LayoutParams params) { |
| 4246 | if (!checkLayoutParams(params)) { |
| 4247 | throw new IllegalArgumentException("Invalid LayoutParams supplied to " + this); |
| 4248 | } |
| 4249 | if (view.mParent != this) { |
| 4250 | throw new IllegalArgumentException("Given view not a child of " + this); |
| 4251 | } |
| 4252 | view.setLayoutParams(params); |
| 4253 | } |
| 4254 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4255 | protected boolean checkLayoutParams(ViewGroup.LayoutParams p) { |
| 4256 | return p != null; |
| 4257 | } |
| 4258 | |
| 4259 | /** |
| 4260 | * Interface definition for a callback to be invoked when the hierarchy |
| 4261 | * within this view changed. The hierarchy changes whenever a child is added |
| 4262 | * to or removed from this view. |
| 4263 | */ |
| 4264 | public interface OnHierarchyChangeListener { |
| 4265 | /** |
| 4266 | * Called when a new child is added to a parent view. |
| 4267 | * |
| 4268 | * @param parent the view in which a child was added |
| 4269 | * @param child the new child view added in the hierarchy |
| 4270 | */ |
| 4271 | void onChildViewAdded(View parent, View child); |
| 4272 | |
| 4273 | /** |
| 4274 | * Called when a child is removed from a parent view. |
| 4275 | * |
| 4276 | * @param parent the view from which the child was removed |
| 4277 | * @param child the child removed from the hierarchy |
| 4278 | */ |
| 4279 | void onChildViewRemoved(View parent, View child); |
| 4280 | } |
| 4281 | |
| 4282 | /** |
| 4283 | * Register a callback to be invoked when a child is added to or removed |
| 4284 | * from this view. |
| 4285 | * |
| 4286 | * @param listener the callback to invoke on hierarchy change |
| 4287 | */ |
| 4288 | public void setOnHierarchyChangeListener(OnHierarchyChangeListener listener) { |
| 4289 | mOnHierarchyChangeListener = listener; |
| 4290 | } |
| 4291 | |
Adam Powell | 6690d01 | 2015-06-17 16:41:56 -0700 | [diff] [blame] | 4292 | void dispatchViewAdded(View child) { |
| 4293 | onViewAdded(child); |
Philip Milne | f51d91c | 2011-07-18 16:12:19 -0700 | [diff] [blame] | 4294 | if (mOnHierarchyChangeListener != null) { |
| 4295 | mOnHierarchyChangeListener.onChildViewAdded(this, child); |
| 4296 | } |
| 4297 | } |
| 4298 | |
| 4299 | /** |
Adam Powell | 6690d01 | 2015-06-17 16:41:56 -0700 | [diff] [blame] | 4300 | * Called when a new child is added to this ViewGroup. Overrides should always |
| 4301 | * call super.onViewAdded. |
| 4302 | * |
| 4303 | * @param child the added child view |
Philip Milne | f51d91c | 2011-07-18 16:12:19 -0700 | [diff] [blame] | 4304 | */ |
Adam Powell | 6690d01 | 2015-06-17 16:41:56 -0700 | [diff] [blame] | 4305 | public void onViewAdded(View child) { |
| 4306 | } |
| 4307 | |
| 4308 | void dispatchViewRemoved(View child) { |
| 4309 | onViewRemoved(child); |
Philip Milne | f51d91c | 2011-07-18 16:12:19 -0700 | [diff] [blame] | 4310 | if (mOnHierarchyChangeListener != null) { |
| 4311 | mOnHierarchyChangeListener.onChildViewRemoved(this, child); |
| 4312 | } |
| 4313 | } |
| 4314 | |
Adam Powell | 6690d01 | 2015-06-17 16:41:56 -0700 | [diff] [blame] | 4315 | /** |
| 4316 | * Called when a child view is removed from this ViewGroup. Overrides should always |
| 4317 | * call super.onViewRemoved. |
| 4318 | * |
| 4319 | * @param child the removed child view |
| 4320 | */ |
| 4321 | public void onViewRemoved(View child) { |
| 4322 | } |
| 4323 | |
Philip Milne | cfb631b | 2012-10-26 10:51:46 -0700 | [diff] [blame] | 4324 | private void clearCachedLayoutMode() { |
Svetoslav | 6254f48 | 2013-06-04 17:22:14 -0700 | [diff] [blame] | 4325 | if (!hasBooleanFlag(FLAG_LAYOUT_MODE_WAS_EXPLICITLY_SET)) { |
Philip Milne | cfb631b | 2012-10-26 10:51:46 -0700 | [diff] [blame] | 4326 | mLayoutMode = LAYOUT_MODE_UNDEFINED; |
| 4327 | } |
| 4328 | } |
| 4329 | |
| 4330 | @Override |
| 4331 | protected void onAttachedToWindow() { |
| 4332 | super.onAttachedToWindow(); |
| 4333 | clearCachedLayoutMode(); |
| 4334 | } |
| 4335 | |
| 4336 | @Override |
| 4337 | protected void onDetachedFromWindow() { |
| 4338 | super.onDetachedFromWindow(); |
| 4339 | clearCachedLayoutMode(); |
| 4340 | } |
| 4341 | |
Philip Milne | f51d91c | 2011-07-18 16:12:19 -0700 | [diff] [blame] | 4342 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4343 | * Adds a view during layout. This is useful if in your onLayout() method, |
| 4344 | * you need to add more views (as does the list view for example). |
| 4345 | * |
| 4346 | * If index is negative, it means put it at the end of the list. |
| 4347 | * |
| 4348 | * @param child the view to add to the group |
Alan Viverette | 77bb6f1 | 2015-02-11 17:24:33 -0800 | [diff] [blame] | 4349 | * @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] | 4350 | * @param params the layout parameters to associate with the child |
| 4351 | * @return true if the child was added, false otherwise |
| 4352 | */ |
| 4353 | protected boolean addViewInLayout(View child, int index, LayoutParams params) { |
| 4354 | return addViewInLayout(child, index, params, false); |
| 4355 | } |
| 4356 | |
| 4357 | /** |
| 4358 | * Adds a view during layout. This is useful if in your onLayout() method, |
| 4359 | * you need to add more views (as does the list view for example). |
| 4360 | * |
| 4361 | * If index is negative, it means put it at the end of the list. |
| 4362 | * |
| 4363 | * @param child the view to add to the group |
Alan Viverette | 77bb6f1 | 2015-02-11 17:24:33 -0800 | [diff] [blame] | 4364 | * @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] | 4365 | * @param params the layout parameters to associate with the child |
| 4366 | * @param preventRequestLayout if true, calling this method will not trigger a |
| 4367 | * layout request on child |
| 4368 | * @return true if the child was added, false otherwise |
| 4369 | */ |
| 4370 | protected boolean addViewInLayout(View child, int index, LayoutParams params, |
| 4371 | boolean preventRequestLayout) { |
Adam Powell | 45a9da5 | 2014-10-09 09:44:18 -0700 | [diff] [blame] | 4372 | if (child == null) { |
| 4373 | throw new IllegalArgumentException("Cannot add a null child view to a ViewGroup"); |
| 4374 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4375 | child.mParent = null; |
| 4376 | addViewInner(child, index, params, preventRequestLayout); |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 4377 | child.mPrivateFlags = (child.mPrivateFlags & ~PFLAG_DIRTY_MASK) | PFLAG_DRAWN; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4378 | return true; |
| 4379 | } |
| 4380 | |
| 4381 | /** |
| 4382 | * Prevents the specified child to be laid out during the next layout pass. |
| 4383 | * |
| 4384 | * @param child the child on which to perform the cleanup |
| 4385 | */ |
| 4386 | protected void cleanupLayoutState(View child) { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 4387 | child.mPrivateFlags &= ~View.PFLAG_FORCE_LAYOUT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4388 | } |
| 4389 | |
| 4390 | private void addViewInner(View child, int index, LayoutParams params, |
| 4391 | boolean preventRequestLayout) { |
| 4392 | |
Chet Haase | e8e45d3 | 2011-03-02 17:07:35 -0800 | [diff] [blame] | 4393 | if (mTransition != null) { |
| 4394 | // Don't prevent other add transitions from completing, but cancel remove |
| 4395 | // transitions to let them complete the process before we add to the container |
| 4396 | mTransition.cancel(LayoutTransition.DISAPPEARING); |
Chet Haase | add6577 | 2011-02-09 16:47:29 -0800 | [diff] [blame] | 4397 | } |
| 4398 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4399 | if (child.getParent() != null) { |
| 4400 | throw new IllegalStateException("The specified child already has a parent. " + |
| 4401 | "You must call removeView() on the child's parent first."); |
| 4402 | } |
| 4403 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4404 | if (mTransition != null) { |
Chet Haase | 5e25c2c | 2010-09-16 11:15:56 -0700 | [diff] [blame] | 4405 | mTransition.addChild(this, child); |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4406 | } |
| 4407 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4408 | if (!checkLayoutParams(params)) { |
| 4409 | params = generateLayoutParams(params); |
| 4410 | } |
| 4411 | |
| 4412 | if (preventRequestLayout) { |
| 4413 | child.mLayoutParams = params; |
| 4414 | } else { |
| 4415 | child.setLayoutParams(params); |
| 4416 | } |
| 4417 | |
| 4418 | if (index < 0) { |
| 4419 | index = mChildrenCount; |
| 4420 | } |
| 4421 | |
| 4422 | addInArray(child, index); |
| 4423 | |
| 4424 | // tell our children |
| 4425 | if (preventRequestLayout) { |
| 4426 | child.assignParent(this); |
| 4427 | } else { |
| 4428 | child.mParent = this; |
| 4429 | } |
| 4430 | |
| 4431 | if (child.hasFocus()) { |
| 4432 | requestChildFocus(child, child.findFocus()); |
| 4433 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 4434 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4435 | AttachInfo ai = mAttachInfo; |
Adam Powell | 4b86788 | 2011-09-16 12:59:46 -0700 | [diff] [blame] | 4436 | if (ai != null && (mGroupFlags & FLAG_PREVENT_DISPATCH_ATTACHED_TO_WINDOW) == 0) { |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 4437 | boolean lastKeepOn = ai.mKeepScreenOn; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4438 | ai.mKeepScreenOn = false; |
| 4439 | child.dispatchAttachedToWindow(mAttachInfo, (mViewFlags&VISIBILITY_MASK)); |
| 4440 | if (ai.mKeepScreenOn) { |
| 4441 | needGlobalAttributesUpdate(true); |
| 4442 | } |
| 4443 | ai.mKeepScreenOn = lastKeepOn; |
| 4444 | } |
| 4445 | |
Fabrice Di Meglio | 9a04856 | 2012-09-26 14:55:56 -0700 | [diff] [blame] | 4446 | if (child.isLayoutDirectionInherited()) { |
Fabrice Di Meglio | a7e0bcd | 2012-10-16 19:55:01 -0700 | [diff] [blame] | 4447 | child.resetRtlProperties(); |
Fabrice Di Meglio | 9a04856 | 2012-09-26 14:55:56 -0700 | [diff] [blame] | 4448 | } |
| 4449 | |
Adam Powell | 6690d01 | 2015-06-17 16:41:56 -0700 | [diff] [blame] | 4450 | dispatchViewAdded(child); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4451 | |
| 4452 | if ((child.mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE) { |
| 4453 | mGroupFlags |= FLAG_NOTIFY_CHILDREN_ON_DRAWABLE_STATE_CHANGE; |
| 4454 | } |
Adam Powell | 539ee87 | 2012-02-03 19:00:49 -0800 | [diff] [blame] | 4455 | |
| 4456 | if (child.hasTransientState()) { |
| 4457 | childHasTransientStateChanged(child, true); |
| 4458 | } |
Svetoslav | 6254f48 | 2013-06-04 17:22:14 -0700 | [diff] [blame] | 4459 | |
Svetoslav | 8e3feb1 | 2014-02-24 13:46:47 -0800 | [diff] [blame] | 4460 | if (child.getVisibility() != View.GONE) { |
Svetoslav | 00dbe81 | 2013-06-10 12:51:09 -0700 | [diff] [blame] | 4461 | notifySubtreeAccessibilityStateChangedIfNeeded(); |
Svetoslav | 6254f48 | 2013-06-04 17:22:14 -0700 | [diff] [blame] | 4462 | } |
Chet Haase | c633d2f | 2015-04-07 10:29:39 -0700 | [diff] [blame] | 4463 | |
| 4464 | if (mTransientIndices != null) { |
| 4465 | final int transientCount = mTransientIndices.size(); |
| 4466 | for (int i = 0; i < transientCount; ++i) { |
| 4467 | final int oldIndex = mTransientIndices.get(i); |
| 4468 | if (index <= oldIndex) { |
| 4469 | mTransientIndices.set(i, oldIndex + 1); |
| 4470 | } |
| 4471 | } |
| 4472 | } |
Vadim Tryshev | 02ed4a0 | 2015-10-23 17:39:33 -0700 | [diff] [blame] | 4473 | |
| 4474 | if (mCurrentDragStartEvent != null && child.getVisibility() == VISIBLE) { |
| 4475 | notifyChildOfDragStart(child); |
| 4476 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4477 | } |
| 4478 | |
| 4479 | private void addInArray(View child, int index) { |
| 4480 | View[] children = mChildren; |
| 4481 | final int count = mChildrenCount; |
| 4482 | final int size = children.length; |
| 4483 | if (index == count) { |
| 4484 | if (size == count) { |
| 4485 | mChildren = new View[size + ARRAY_CAPACITY_INCREMENT]; |
| 4486 | System.arraycopy(children, 0, mChildren, 0, size); |
| 4487 | children = mChildren; |
| 4488 | } |
| 4489 | children[mChildrenCount++] = child; |
| 4490 | } else if (index < count) { |
| 4491 | if (size == count) { |
| 4492 | mChildren = new View[size + ARRAY_CAPACITY_INCREMENT]; |
| 4493 | System.arraycopy(children, 0, mChildren, 0, index); |
| 4494 | System.arraycopy(children, index, mChildren, index + 1, count - index); |
| 4495 | children = mChildren; |
| 4496 | } else { |
| 4497 | System.arraycopy(children, index, children, index + 1, count - index); |
| 4498 | } |
| 4499 | children[index] = child; |
| 4500 | mChildrenCount++; |
Joe Onorato | 03ab0c7 | 2011-01-06 15:46:27 -0800 | [diff] [blame] | 4501 | if (mLastTouchDownIndex >= index) { |
| 4502 | mLastTouchDownIndex++; |
| 4503 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4504 | } else { |
| 4505 | throw new IndexOutOfBoundsException("index=" + index + " count=" + count); |
| 4506 | } |
| 4507 | } |
| 4508 | |
| 4509 | // This method also sets the child's mParent to null |
| 4510 | private void removeFromArray(int index) { |
| 4511 | final View[] children = mChildren; |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4512 | if (!(mTransitioningViews != null && mTransitioningViews.contains(children[index]))) { |
| 4513 | children[index].mParent = null; |
| 4514 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4515 | final int count = mChildrenCount; |
| 4516 | if (index == count - 1) { |
| 4517 | children[--mChildrenCount] = null; |
| 4518 | } else if (index >= 0 && index < count) { |
| 4519 | System.arraycopy(children, index + 1, children, index, count - index - 1); |
| 4520 | children[--mChildrenCount] = null; |
| 4521 | } else { |
| 4522 | throw new IndexOutOfBoundsException(); |
| 4523 | } |
Joe Onorato | 03ab0c7 | 2011-01-06 15:46:27 -0800 | [diff] [blame] | 4524 | if (mLastTouchDownIndex == index) { |
| 4525 | mLastTouchDownTime = 0; |
| 4526 | mLastTouchDownIndex = -1; |
| 4527 | } else if (mLastTouchDownIndex > index) { |
| 4528 | mLastTouchDownIndex--; |
| 4529 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4530 | } |
| 4531 | |
| 4532 | // This method also sets the children's mParent to null |
| 4533 | private void removeFromArray(int start, int count) { |
| 4534 | final View[] children = mChildren; |
| 4535 | final int childrenCount = mChildrenCount; |
| 4536 | |
| 4537 | start = Math.max(0, start); |
| 4538 | final int end = Math.min(childrenCount, start + count); |
| 4539 | |
| 4540 | if (start == end) { |
| 4541 | return; |
| 4542 | } |
| 4543 | |
| 4544 | if (end == childrenCount) { |
| 4545 | for (int i = start; i < end; i++) { |
| 4546 | children[i].mParent = null; |
| 4547 | children[i] = null; |
| 4548 | } |
| 4549 | } else { |
| 4550 | for (int i = start; i < end; i++) { |
| 4551 | children[i].mParent = null; |
| 4552 | } |
| 4553 | |
| 4554 | // Since we're looping above, we might as well do the copy, but is arraycopy() |
| 4555 | // faster than the extra 2 bounds checks we would do in the loop? |
| 4556 | System.arraycopy(children, end, children, start, childrenCount - end); |
| 4557 | |
| 4558 | for (int i = childrenCount - (end - start); i < childrenCount; i++) { |
| 4559 | children[i] = null; |
| 4560 | } |
| 4561 | } |
| 4562 | |
| 4563 | mChildrenCount -= (end - start); |
| 4564 | } |
| 4565 | |
| 4566 | private void bindLayoutAnimation(View child) { |
| 4567 | Animation a = mLayoutAnimationController.getAnimationForView(child); |
| 4568 | child.setAnimation(a); |
| 4569 | } |
| 4570 | |
| 4571 | /** |
| 4572 | * Subclasses should override this method to set layout animation |
| 4573 | * parameters on the supplied child. |
| 4574 | * |
| 4575 | * @param child the child to associate with animation parameters |
| 4576 | * @param params the child's layout parameters which hold the animation |
| 4577 | * parameters |
| 4578 | * @param index the index of the child in the view group |
| 4579 | * @param count the number of children in the view group |
| 4580 | */ |
| 4581 | protected void attachLayoutAnimationParameters(View child, |
| 4582 | LayoutParams params, int index, int count) { |
| 4583 | LayoutAnimationController.AnimationParameters animationParams = |
| 4584 | params.layoutAnimationParameters; |
| 4585 | if (animationParams == null) { |
| 4586 | animationParams = new LayoutAnimationController.AnimationParameters(); |
| 4587 | params.layoutAnimationParameters = animationParams; |
| 4588 | } |
| 4589 | |
| 4590 | animationParams.count = count; |
| 4591 | animationParams.index = index; |
| 4592 | } |
| 4593 | |
| 4594 | /** |
| 4595 | * {@inheritDoc} |
Filip Gruszczynski | a33bdf3 | 2015-11-19 18:22:16 -0800 | [diff] [blame] | 4596 | * |
Romain Guy | 393a52c | 2012-05-22 20:21:08 -0700 | [diff] [blame] | 4597 | * <p><strong>Note:</strong> do not invoke this method from |
| 4598 | * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)}, |
| 4599 | * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4600 | */ |
Alan Viverette | be463f2 | 2016-01-21 10:50:10 -0500 | [diff] [blame] | 4601 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4602 | public void removeView(View view) { |
Alan Viverette | 177ec460 | 2014-10-17 13:34:50 -0700 | [diff] [blame] | 4603 | if (removeViewInternal(view)) { |
| 4604 | requestLayout(); |
| 4605 | invalidate(true); |
| 4606 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4607 | } |
| 4608 | |
| 4609 | /** |
| 4610 | * Removes a view during layout. This is useful if in your onLayout() method, |
| 4611 | * you need to remove more views. |
| 4612 | * |
Romain Guy | 393a52c | 2012-05-22 20:21:08 -0700 | [diff] [blame] | 4613 | * <p><strong>Note:</strong> do not invoke this method from |
| 4614 | * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)}, |
| 4615 | * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p> |
Filip Gruszczynski | a33bdf3 | 2015-11-19 18:22:16 -0800 | [diff] [blame] | 4616 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4617 | * @param view the view to remove from the group |
| 4618 | */ |
| 4619 | public void removeViewInLayout(View view) { |
| 4620 | removeViewInternal(view); |
| 4621 | } |
| 4622 | |
| 4623 | /** |
| 4624 | * Removes a range of views during layout. This is useful if in your onLayout() method, |
| 4625 | * you need to remove more views. |
| 4626 | * |
Romain Guy | 393a52c | 2012-05-22 20:21:08 -0700 | [diff] [blame] | 4627 | * <p><strong>Note:</strong> do not invoke this method from |
| 4628 | * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)}, |
| 4629 | * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p> |
| 4630 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4631 | * @param start the index of the first view to remove from the group |
| 4632 | * @param count the number of views to remove from the group |
| 4633 | */ |
| 4634 | public void removeViewsInLayout(int start, int count) { |
| 4635 | removeViewsInternal(start, count); |
| 4636 | } |
| 4637 | |
| 4638 | /** |
| 4639 | * Removes the view at the specified position in the group. |
| 4640 | * |
Romain Guy | 393a52c | 2012-05-22 20:21:08 -0700 | [diff] [blame] | 4641 | * <p><strong>Note:</strong> do not invoke this method from |
| 4642 | * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)}, |
| 4643 | * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p> |
Filip Gruszczynski | a33bdf3 | 2015-11-19 18:22:16 -0800 | [diff] [blame] | 4644 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4645 | * @param index the position in the group of the view to remove |
| 4646 | */ |
| 4647 | public void removeViewAt(int index) { |
| 4648 | removeViewInternal(index, getChildAt(index)); |
| 4649 | requestLayout(); |
Romain Guy | 849d0a3 | 2011-02-01 17:20:48 -0800 | [diff] [blame] | 4650 | invalidate(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4651 | } |
| 4652 | |
| 4653 | /** |
| 4654 | * Removes the specified range of views from the group. |
| 4655 | * |
Romain Guy | 393a52c | 2012-05-22 20:21:08 -0700 | [diff] [blame] | 4656 | * <p><strong>Note:</strong> do not invoke this method from |
| 4657 | * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)}, |
| 4658 | * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p> |
| 4659 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4660 | * @param start the first position in the group of the range of views to remove |
| 4661 | * @param count the number of views to remove |
| 4662 | */ |
| 4663 | public void removeViews(int start, int count) { |
| 4664 | removeViewsInternal(start, count); |
| 4665 | requestLayout(); |
Romain Guy | 849d0a3 | 2011-02-01 17:20:48 -0800 | [diff] [blame] | 4666 | invalidate(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4667 | } |
| 4668 | |
Alan Viverette | 177ec460 | 2014-10-17 13:34:50 -0700 | [diff] [blame] | 4669 | private boolean removeViewInternal(View view) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4670 | final int index = indexOfChild(view); |
| 4671 | if (index >= 0) { |
| 4672 | removeViewInternal(index, view); |
Alan Viverette | 177ec460 | 2014-10-17 13:34:50 -0700 | [diff] [blame] | 4673 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4674 | } |
Alan Viverette | 177ec460 | 2014-10-17 13:34:50 -0700 | [diff] [blame] | 4675 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4676 | } |
| 4677 | |
| 4678 | private void removeViewInternal(int index, View view) { |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4679 | if (mTransition != null) { |
Chet Haase | 5e25c2c | 2010-09-16 11:15:56 -0700 | [diff] [blame] | 4680 | mTransition.removeChild(this, view); |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4681 | } |
| 4682 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4683 | boolean clearChildFocus = false; |
| 4684 | if (view == mFocused) { |
Alan Viverette | 223622a | 2013-12-17 13:29:02 -0800 | [diff] [blame] | 4685 | view.unFocus(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4686 | clearChildFocus = true; |
| 4687 | } |
| 4688 | |
Alan Viverette | 632af84 | 2014-10-28 13:45:11 -0700 | [diff] [blame] | 4689 | view.clearAccessibilityFocus(); |
Svetoslav Ganov | 961bf0e | 2012-05-08 09:40:03 -0700 | [diff] [blame] | 4690 | |
Jeff Brown | 59a422e | 2012-04-19 15:19:19 -0700 | [diff] [blame] | 4691 | cancelTouchTarget(view); |
| 4692 | cancelHoverTarget(view); |
| 4693 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4694 | if (view.getAnimation() != null || |
| 4695 | (mTransitioningViews != null && mTransitioningViews.contains(view))) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4696 | addDisappearingView(view); |
| 4697 | } else if (view.mAttachInfo != null) { |
| 4698 | view.dispatchDetachedFromWindow(); |
| 4699 | } |
| 4700 | |
Adam Powell | 539ee87 | 2012-02-03 19:00:49 -0800 | [diff] [blame] | 4701 | if (view.hasTransientState()) { |
| 4702 | childHasTransientStateChanged(view, false); |
| 4703 | } |
| 4704 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4705 | needGlobalAttributesUpdate(false); |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 4706 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4707 | removeFromArray(index); |
| 4708 | |
| 4709 | if (clearChildFocus) { |
| 4710 | clearChildFocus(view); |
Svetoslav Ganov | 149567f | 2013-01-08 15:23:34 -0800 | [diff] [blame] | 4711 | if (!rootViewRequestFocus()) { |
| 4712 | notifyGlobalFocusCleared(this); |
| 4713 | } |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 4714 | } |
Romain Guy | 6fb0563 | 2012-11-29 10:50:33 -0800 | [diff] [blame] | 4715 | |
Adam Powell | 6690d01 | 2015-06-17 16:41:56 -0700 | [diff] [blame] | 4716 | dispatchViewRemoved(view); |
Svetoslav | 6254f48 | 2013-06-04 17:22:14 -0700 | [diff] [blame] | 4717 | |
Svetoslav | 8e3feb1 | 2014-02-24 13:46:47 -0800 | [diff] [blame] | 4718 | if (view.getVisibility() != View.GONE) { |
Svetoslav | 00dbe81 | 2013-06-10 12:51:09 -0700 | [diff] [blame] | 4719 | notifySubtreeAccessibilityStateChangedIfNeeded(); |
Svetoslav | 6254f48 | 2013-06-04 17:22:14 -0700 | [diff] [blame] | 4720 | } |
Chet Haase | c633d2f | 2015-04-07 10:29:39 -0700 | [diff] [blame] | 4721 | |
| 4722 | int transientCount = mTransientIndices == null ? 0 : mTransientIndices.size(); |
| 4723 | for (int i = 0; i < transientCount; ++i) { |
| 4724 | final int oldIndex = mTransientIndices.get(i); |
| 4725 | if (index < oldIndex) { |
| 4726 | mTransientIndices.set(i, oldIndex - 1); |
| 4727 | } |
| 4728 | } |
Vadim Tryshev | 02ed4a0 | 2015-10-23 17:39:33 -0700 | [diff] [blame] | 4729 | |
| 4730 | if (mCurrentDragStartEvent != null) { |
| 4731 | mChildrenInterestedInDrag.remove(view); |
| 4732 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4733 | } |
| 4734 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4735 | /** |
| 4736 | * Sets the LayoutTransition object for this ViewGroup. If the LayoutTransition object is |
| 4737 | * not null, changes in layout which occur because of children being added to or removed from |
| 4738 | * the ViewGroup will be animated according to the animations defined in that LayoutTransition |
| 4739 | * object. By default, the transition object is null (so layout changes are not animated). |
| 4740 | * |
Chet Haase | ef3cbfd | 2013-08-21 14:01:02 -0700 | [diff] [blame] | 4741 | * <p>Replacing a non-null transition will cause that previous transition to be |
| 4742 | * canceled, if it is currently running, to restore this container to |
| 4743 | * its correct post-transition state.</p> |
| 4744 | * |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4745 | * @param transition The LayoutTransition object that will animated changes in layout. A value |
| 4746 | * of <code>null</code> means no transition will run on layout changes. |
Chet Haase | 13cc120 | 2010-09-03 15:39:20 -0700 | [diff] [blame] | 4747 | * @attr ref android.R.styleable#ViewGroup_animateLayoutChanges |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4748 | */ |
| 4749 | public void setLayoutTransition(LayoutTransition transition) { |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 4750 | if (mTransition != null) { |
Chet Haase | fee6f2b | 2013-08-27 12:22:29 -0700 | [diff] [blame] | 4751 | LayoutTransition previousTransition = mTransition; |
| 4752 | previousTransition.cancel(); |
| 4753 | previousTransition.removeTransitionListener(mLayoutTransitionListener); |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 4754 | } |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4755 | mTransition = transition; |
Chet Haase | 13cc120 | 2010-09-03 15:39:20 -0700 | [diff] [blame] | 4756 | if (mTransition != null) { |
| 4757 | mTransition.addTransitionListener(mLayoutTransitionListener); |
| 4758 | } |
| 4759 | } |
| 4760 | |
| 4761 | /** |
| 4762 | * Gets the LayoutTransition object for this ViewGroup. If the LayoutTransition object is |
| 4763 | * not null, changes in layout which occur because of children being added to or removed from |
| 4764 | * the ViewGroup will be animated according to the animations defined in that LayoutTransition |
| 4765 | * object. By default, the transition object is null (so layout changes are not animated). |
| 4766 | * |
| 4767 | * @return LayoutTranstion The LayoutTransition object that will animated changes in layout. |
| 4768 | * A value of <code>null</code> means no transition will run on layout changes. |
| 4769 | */ |
| 4770 | public LayoutTransition getLayoutTransition() { |
| 4771 | return mTransition; |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4772 | } |
| 4773 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4774 | private void removeViewsInternal(int start, int count) { |
Chris Craik | 18a759d | 2015-10-14 14:16:33 -0700 | [diff] [blame] | 4775 | final int end = start + count; |
| 4776 | |
| 4777 | if (start < 0 || count < 0 || end > mChildrenCount) { |
| 4778 | throw new IndexOutOfBoundsException(); |
| 4779 | } |
| 4780 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4781 | final View focused = mFocused; |
| 4782 | final boolean detach = mAttachInfo != null; |
Svetoslav Ganov | 149567f | 2013-01-08 15:23:34 -0800 | [diff] [blame] | 4783 | boolean clearChildFocus = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4784 | |
| 4785 | final View[] children = mChildren; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4786 | |
| 4787 | for (int i = start; i < end; i++) { |
| 4788 | final View view = children[i]; |
| 4789 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4790 | if (mTransition != null) { |
Chet Haase | 5e25c2c | 2010-09-16 11:15:56 -0700 | [diff] [blame] | 4791 | mTransition.removeChild(this, view); |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4792 | } |
| 4793 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4794 | if (view == focused) { |
Alan Viverette | 223622a | 2013-12-17 13:29:02 -0800 | [diff] [blame] | 4795 | view.unFocus(null); |
Svetoslav Ganov | 149567f | 2013-01-08 15:23:34 -0800 | [diff] [blame] | 4796 | clearChildFocus = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4797 | } |
| 4798 | |
Alan Viverette | 632af84 | 2014-10-28 13:45:11 -0700 | [diff] [blame] | 4799 | view.clearAccessibilityFocus(); |
Svetoslav Ganov | 961bf0e | 2012-05-08 09:40:03 -0700 | [diff] [blame] | 4800 | |
Jeff Brown | 59a422e | 2012-04-19 15:19:19 -0700 | [diff] [blame] | 4801 | cancelTouchTarget(view); |
| 4802 | cancelHoverTarget(view); |
| 4803 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4804 | if (view.getAnimation() != null || |
| 4805 | (mTransitioningViews != null && mTransitioningViews.contains(view))) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4806 | addDisappearingView(view); |
| 4807 | } else if (detach) { |
| 4808 | view.dispatchDetachedFromWindow(); |
| 4809 | } |
| 4810 | |
Adam Powell | 539ee87 | 2012-02-03 19:00:49 -0800 | [diff] [blame] | 4811 | if (view.hasTransientState()) { |
| 4812 | childHasTransientStateChanged(view, false); |
| 4813 | } |
| 4814 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4815 | needGlobalAttributesUpdate(false); |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 4816 | |
Adam Powell | 6690d01 | 2015-06-17 16:41:56 -0700 | [diff] [blame] | 4817 | dispatchViewRemoved(view); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4818 | } |
| 4819 | |
| 4820 | removeFromArray(start, count); |
| 4821 | |
Svetoslav Ganov | 149567f | 2013-01-08 15:23:34 -0800 | [diff] [blame] | 4822 | if (clearChildFocus) { |
| 4823 | clearChildFocus(focused); |
| 4824 | if (!rootViewRequestFocus()) { |
| 4825 | notifyGlobalFocusCleared(focused); |
| 4826 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4827 | } |
| 4828 | } |
| 4829 | |
| 4830 | /** |
| 4831 | * Call this method to remove all child views from the |
| 4832 | * ViewGroup. |
Filip Gruszczynski | a33bdf3 | 2015-11-19 18:22:16 -0800 | [diff] [blame] | 4833 | * |
Romain Guy | 393a52c | 2012-05-22 20:21:08 -0700 | [diff] [blame] | 4834 | * <p><strong>Note:</strong> do not invoke this method from |
| 4835 | * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)}, |
| 4836 | * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4837 | */ |
| 4838 | public void removeAllViews() { |
| 4839 | removeAllViewsInLayout(); |
| 4840 | requestLayout(); |
Romain Guy | 849d0a3 | 2011-02-01 17:20:48 -0800 | [diff] [blame] | 4841 | invalidate(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4842 | } |
| 4843 | |
| 4844 | /** |
| 4845 | * Called by a ViewGroup subclass to remove child views from itself, |
| 4846 | * when it must first know its size on screen before it can calculate how many |
| 4847 | * child views it will render. An example is a Gallery or a ListView, which |
| 4848 | * may "have" 50 children, but actually only render the number of children |
| 4849 | * that can currently fit inside the object on screen. Do not call |
| 4850 | * this method unless you are extending ViewGroup and understand the |
| 4851 | * view measuring and layout pipeline. |
Romain Guy | 393a52c | 2012-05-22 20:21:08 -0700 | [diff] [blame] | 4852 | * |
| 4853 | * <p><strong>Note:</strong> do not invoke this method from |
| 4854 | * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)}, |
| 4855 | * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4856 | */ |
| 4857 | public void removeAllViewsInLayout() { |
| 4858 | final int count = mChildrenCount; |
| 4859 | if (count <= 0) { |
| 4860 | return; |
| 4861 | } |
| 4862 | |
| 4863 | final View[] children = mChildren; |
| 4864 | mChildrenCount = 0; |
| 4865 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4866 | final View focused = mFocused; |
| 4867 | final boolean detach = mAttachInfo != null; |
Svetoslav Ganov | 149567f | 2013-01-08 15:23:34 -0800 | [diff] [blame] | 4868 | boolean clearChildFocus = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4869 | |
| 4870 | needGlobalAttributesUpdate(false); |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 4871 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4872 | for (int i = count - 1; i >= 0; i--) { |
| 4873 | final View view = children[i]; |
| 4874 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4875 | if (mTransition != null) { |
Chet Haase | 5e25c2c | 2010-09-16 11:15:56 -0700 | [diff] [blame] | 4876 | mTransition.removeChild(this, view); |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4877 | } |
| 4878 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4879 | if (view == focused) { |
Alan Viverette | 223622a | 2013-12-17 13:29:02 -0800 | [diff] [blame] | 4880 | view.unFocus(null); |
Svetoslav Ganov | 149567f | 2013-01-08 15:23:34 -0800 | [diff] [blame] | 4881 | clearChildFocus = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4882 | } |
| 4883 | |
Alan Viverette | 632af84 | 2014-10-28 13:45:11 -0700 | [diff] [blame] | 4884 | view.clearAccessibilityFocus(); |
Svetoslav Ganov | 961bf0e | 2012-05-08 09:40:03 -0700 | [diff] [blame] | 4885 | |
Jeff Brown | 59a422e | 2012-04-19 15:19:19 -0700 | [diff] [blame] | 4886 | cancelTouchTarget(view); |
| 4887 | cancelHoverTarget(view); |
| 4888 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4889 | if (view.getAnimation() != null || |
| 4890 | (mTransitioningViews != null && mTransitioningViews.contains(view))) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4891 | addDisappearingView(view); |
| 4892 | } else if (detach) { |
| 4893 | view.dispatchDetachedFromWindow(); |
| 4894 | } |
| 4895 | |
Adam Powell | 539ee87 | 2012-02-03 19:00:49 -0800 | [diff] [blame] | 4896 | if (view.hasTransientState()) { |
| 4897 | childHasTransientStateChanged(view, false); |
| 4898 | } |
| 4899 | |
Adam Powell | 6690d01 | 2015-06-17 16:41:56 -0700 | [diff] [blame] | 4900 | dispatchViewRemoved(view); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4901 | |
| 4902 | view.mParent = null; |
| 4903 | children[i] = null; |
| 4904 | } |
| 4905 | |
Svetoslav Ganov | 149567f | 2013-01-08 15:23:34 -0800 | [diff] [blame] | 4906 | if (clearChildFocus) { |
| 4907 | clearChildFocus(focused); |
| 4908 | if (!rootViewRequestFocus()) { |
| 4909 | notifyGlobalFocusCleared(focused); |
| 4910 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4911 | } |
| 4912 | } |
| 4913 | |
| 4914 | /** |
| 4915 | * Finishes the removal of a detached view. This method will dispatch the detached from |
| 4916 | * window event and notify the hierarchy change listener. |
Chet Haase | ca479d4 | 2012-08-30 17:20:08 -0700 | [diff] [blame] | 4917 | * <p> |
| 4918 | * This method is intended to be lightweight and makes no assumptions about whether the |
| 4919 | * parent or child should be redrawn. Proper use of this method will include also making |
| 4920 | * any appropriate {@link #requestLayout()} or {@link #invalidate()} calls. |
| 4921 | * For example, callers can {@link #post(Runnable) post} a {@link Runnable} |
| 4922 | * which performs a {@link #requestLayout()} on the next frame, after all detach/remove |
| 4923 | * calls are finished, causing layout to be run prior to redrawing the view hierarchy. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4924 | * |
| 4925 | * @param child the child to be definitely removed from the view hierarchy |
| 4926 | * @param animate if true and the view has an animation, the view is placed in the |
| 4927 | * disappearing views list, otherwise, it is detached from the window |
| 4928 | * |
| 4929 | * @see #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams) |
| 4930 | * @see #detachAllViewsFromParent() |
| 4931 | * @see #detachViewFromParent(View) |
| 4932 | * @see #detachViewFromParent(int) |
| 4933 | */ |
| 4934 | protected void removeDetachedView(View child, boolean animate) { |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4935 | if (mTransition != null) { |
Chet Haase | 5e25c2c | 2010-09-16 11:15:56 -0700 | [diff] [blame] | 4936 | mTransition.removeChild(this, child); |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4937 | } |
| 4938 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4939 | if (child == mFocused) { |
| 4940 | child.clearFocus(); |
| 4941 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 4942 | |
Svetoslav Ganov | 961bf0e | 2012-05-08 09:40:03 -0700 | [diff] [blame] | 4943 | child.clearAccessibilityFocus(); |
| 4944 | |
Jeff Brown | 59a422e | 2012-04-19 15:19:19 -0700 | [diff] [blame] | 4945 | cancelTouchTarget(child); |
| 4946 | cancelHoverTarget(child); |
| 4947 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4948 | if ((animate && child.getAnimation() != null) || |
| 4949 | (mTransitioningViews != null && mTransitioningViews.contains(child))) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4950 | addDisappearingView(child); |
| 4951 | } else if (child.mAttachInfo != null) { |
| 4952 | child.dispatchDetachedFromWindow(); |
| 4953 | } |
| 4954 | |
Adam Powell | 539ee87 | 2012-02-03 19:00:49 -0800 | [diff] [blame] | 4955 | if (child.hasTransientState()) { |
| 4956 | childHasTransientStateChanged(child, false); |
| 4957 | } |
| 4958 | |
Adam Powell | 6690d01 | 2015-06-17 16:41:56 -0700 | [diff] [blame] | 4959 | dispatchViewRemoved(child); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4960 | } |
| 4961 | |
| 4962 | /** |
| 4963 | * 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] | 4964 | * sets the layout parameters and puts the view in the list of children so that |
| 4965 | * it can be retrieved by calling {@link #getChildAt(int)}. |
| 4966 | * <p> |
| 4967 | * This method is intended to be lightweight and makes no assumptions about whether the |
| 4968 | * parent or child should be redrawn. Proper use of this method will include also making |
| 4969 | * any appropriate {@link #requestLayout()} or {@link #invalidate()} calls. |
| 4970 | * For example, callers can {@link #post(Runnable) post} a {@link Runnable} |
| 4971 | * which performs a {@link #requestLayout()} on the next frame, after all detach/attach |
| 4972 | * calls are finished, causing layout to be run prior to redrawing the view hierarchy. |
| 4973 | * <p> |
| 4974 | * This method should be called only for views which were detached from their parent. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4975 | * |
| 4976 | * @param child the child to attach |
| 4977 | * @param index the index at which the child should be attached |
| 4978 | * @param params the layout parameters of the child |
| 4979 | * |
| 4980 | * @see #removeDetachedView(View, boolean) |
| 4981 | * @see #detachAllViewsFromParent() |
| 4982 | * @see #detachViewFromParent(View) |
| 4983 | * @see #detachViewFromParent(int) |
| 4984 | */ |
| 4985 | protected void attachViewToParent(View child, int index, LayoutParams params) { |
| 4986 | child.mLayoutParams = params; |
| 4987 | |
| 4988 | if (index < 0) { |
| 4989 | index = mChildrenCount; |
| 4990 | } |
| 4991 | |
| 4992 | addInArray(child, index); |
| 4993 | |
| 4994 | child.mParent = this; |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 4995 | child.mPrivateFlags = (child.mPrivateFlags & ~PFLAG_DIRTY_MASK |
| 4996 | & ~PFLAG_DRAWING_CACHE_VALID) |
| 4997 | | PFLAG_DRAWN | PFLAG_INVALIDATED; |
| 4998 | this.mPrivateFlags |= PFLAG_INVALIDATED; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4999 | |
| 5000 | if (child.hasFocus()) { |
| 5001 | requestChildFocus(child, child.findFocus()); |
| 5002 | } |
Adam Powell | c587409 | 2016-03-17 16:27:24 -0700 | [diff] [blame] | 5003 | dispatchVisibilityAggregated(isAttachedToWindow() && getWindowVisibility() == VISIBLE |
| 5004 | && isShown()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5005 | } |
| 5006 | |
| 5007 | /** |
Chet Haase | ca479d4 | 2012-08-30 17:20:08 -0700 | [diff] [blame] | 5008 | * Detaches a view from its parent. Detaching a view should be followed |
| 5009 | * either by a call to |
| 5010 | * {@link #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)} |
| 5011 | * or a call to {@link #removeDetachedView(View, boolean)}. Detachment should only be |
| 5012 | * temporary; reattachment or removal should happen within the same drawing cycle as |
| 5013 | * detachment. When a view is detached, its parent is null and cannot be retrieved by a |
| 5014 | * call to {@link #getChildAt(int)}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5015 | * |
| 5016 | * @param child the child to detach |
| 5017 | * |
| 5018 | * @see #detachViewFromParent(int) |
| 5019 | * @see #detachViewsFromParent(int, int) |
| 5020 | * @see #detachAllViewsFromParent() |
| 5021 | * @see #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams) |
| 5022 | * @see #removeDetachedView(View, boolean) |
| 5023 | */ |
| 5024 | protected void detachViewFromParent(View child) { |
| 5025 | removeFromArray(indexOfChild(child)); |
| 5026 | } |
| 5027 | |
| 5028 | /** |
Chet Haase | ca479d4 | 2012-08-30 17:20:08 -0700 | [diff] [blame] | 5029 | * Detaches a view from its parent. Detaching a view should be followed |
| 5030 | * either by a call to |
| 5031 | * {@link #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)} |
| 5032 | * or a call to {@link #removeDetachedView(View, boolean)}. Detachment should only be |
| 5033 | * temporary; reattachment or removal should happen within the same drawing cycle as |
| 5034 | * detachment. When a view is detached, its parent is null and cannot be retrieved by a |
| 5035 | * call to {@link #getChildAt(int)}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5036 | * |
| 5037 | * @param index the index of the child to detach |
| 5038 | * |
| 5039 | * @see #detachViewFromParent(View) |
| 5040 | * @see #detachAllViewsFromParent() |
| 5041 | * @see #detachViewsFromParent(int, int) |
| 5042 | * @see #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams) |
| 5043 | * @see #removeDetachedView(View, boolean) |
| 5044 | */ |
| 5045 | protected void detachViewFromParent(int index) { |
| 5046 | removeFromArray(index); |
| 5047 | } |
| 5048 | |
| 5049 | /** |
Chet Haase | ca479d4 | 2012-08-30 17:20:08 -0700 | [diff] [blame] | 5050 | * Detaches a range of views from their parents. Detaching a view should be followed |
| 5051 | * either by a call to |
| 5052 | * {@link #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)} |
| 5053 | * or a call to {@link #removeDetachedView(View, boolean)}. Detachment should only be |
| 5054 | * temporary; reattachment or removal should happen within the same drawing cycle as |
| 5055 | * detachment. When a view is detached, its parent is null and cannot be retrieved by a |
| 5056 | * call to {@link #getChildAt(int)}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5057 | * |
| 5058 | * @param start the first index of the childrend range to detach |
| 5059 | * @param count the number of children to detach |
| 5060 | * |
| 5061 | * @see #detachViewFromParent(View) |
| 5062 | * @see #detachViewFromParent(int) |
| 5063 | * @see #detachAllViewsFromParent() |
| 5064 | * @see #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams) |
| 5065 | * @see #removeDetachedView(View, boolean) |
| 5066 | */ |
| 5067 | protected void detachViewsFromParent(int start, int count) { |
| 5068 | removeFromArray(start, count); |
| 5069 | } |
| 5070 | |
| 5071 | /** |
Chet Haase | ca479d4 | 2012-08-30 17:20:08 -0700 | [diff] [blame] | 5072 | * Detaches all views from the parent. Detaching a view should be followed |
| 5073 | * either by a call to |
| 5074 | * {@link #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)} |
| 5075 | * or a call to {@link #removeDetachedView(View, boolean)}. Detachment should only be |
| 5076 | * temporary; reattachment or removal should happen within the same drawing cycle as |
| 5077 | * detachment. When a view is detached, its parent is null and cannot be retrieved by a |
| 5078 | * call to {@link #getChildAt(int)}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5079 | * |
| 5080 | * @see #detachViewFromParent(View) |
| 5081 | * @see #detachViewFromParent(int) |
| 5082 | * @see #detachViewsFromParent(int, int) |
| 5083 | * @see #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams) |
| 5084 | * @see #removeDetachedView(View, boolean) |
| 5085 | */ |
| 5086 | protected void detachAllViewsFromParent() { |
| 5087 | final int count = mChildrenCount; |
| 5088 | if (count <= 0) { |
| 5089 | return; |
| 5090 | } |
| 5091 | |
| 5092 | final View[] children = mChildren; |
| 5093 | mChildrenCount = 0; |
| 5094 | |
| 5095 | for (int i = count - 1; i >= 0; i--) { |
| 5096 | children[i].mParent = null; |
| 5097 | children[i] = null; |
| 5098 | } |
| 5099 | } |
| 5100 | |
| 5101 | /** |
| 5102 | * Don't call or override this method. It is used for the implementation of |
| 5103 | * the view hierarchy. |
| 5104 | */ |
Alan Viverette | be463f2 | 2016-01-21 10:50:10 -0500 | [diff] [blame] | 5105 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5106 | public final void invalidateChild(View child, final Rect dirty) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5107 | ViewParent parent = this; |
| 5108 | |
| 5109 | final AttachInfo attachInfo = mAttachInfo; |
| 5110 | if (attachInfo != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5111 | // If the child is drawing an animation, we want to copy this flag onto |
| 5112 | // ourselves and the parent to make sure the invalidate request goes |
| 5113 | // through |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 5114 | final boolean drawAnimation = (child.mPrivateFlags & PFLAG_DRAW_ANIMATION) |
| 5115 | == PFLAG_DRAW_ANIMATION; |
Romain Guy | 24443ea | 2009-05-11 11:56:30 -0700 | [diff] [blame] | 5116 | |
Romain Guy | fe455af | 2012-02-15 16:40:20 -0800 | [diff] [blame] | 5117 | // Check whether the child that requests the invalidate is fully opaque |
| 5118 | // Views being animated or transformed are not considered opaque because we may |
| 5119 | // be invalidating their old position and need the parent to paint behind them. |
| 5120 | Matrix childMatrix = child.getMatrix(); |
| 5121 | final boolean isOpaque = child.isOpaque() && !drawAnimation && |
| 5122 | child.getAnimation() == null && childMatrix.isIdentity(); |
| 5123 | // Mark the child as dirty, using the appropriate flag |
| 5124 | // Make sure we do not set both flags at the same time |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 5125 | int opaqueFlag = isOpaque ? PFLAG_DIRTY_OPAQUE : PFLAG_DIRTY; |
Romain Guy | fe455af | 2012-02-15 16:40:20 -0800 | [diff] [blame] | 5126 | |
John Reck | 96bb8ad | 2014-06-19 10:53:03 -0700 | [diff] [blame] | 5127 | if (child.mLayerType != LAYER_TYPE_NONE) { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 5128 | mPrivateFlags |= PFLAG_INVALIDATED; |
| 5129 | mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID; |
Romain Guy | fe455af | 2012-02-15 16:40:20 -0800 | [diff] [blame] | 5130 | } |
| 5131 | |
| 5132 | final int[] location = attachInfo.mInvalidateChildLocation; |
| 5133 | location[CHILD_LEFT_INDEX] = child.mLeft; |
| 5134 | location[CHILD_TOP_INDEX] = child.mTop; |
Chet Haase | 599913d | 2012-07-23 16:22:05 -0700 | [diff] [blame] | 5135 | if (!childMatrix.isIdentity() || |
| 5136 | (mGroupFlags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) { |
Romain Guy | fe455af | 2012-02-15 16:40:20 -0800 | [diff] [blame] | 5137 | RectF boundingRect = attachInfo.mTmpTransformRect; |
| 5138 | boundingRect.set(dirty); |
Chet Haase | 599913d | 2012-07-23 16:22:05 -0700 | [diff] [blame] | 5139 | Matrix transformMatrix; |
| 5140 | if ((mGroupFlags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) { |
| 5141 | Transformation t = attachInfo.mTmpTransformation; |
| 5142 | boolean transformed = getChildStaticTransformation(child, t); |
| 5143 | if (transformed) { |
| 5144 | transformMatrix = attachInfo.mTmpMatrix; |
| 5145 | transformMatrix.set(t.getMatrix()); |
| 5146 | if (!childMatrix.isIdentity()) { |
| 5147 | transformMatrix.preConcat(childMatrix); |
| 5148 | } |
| 5149 | } else { |
| 5150 | transformMatrix = childMatrix; |
| 5151 | } |
| 5152 | } else { |
| 5153 | transformMatrix = childMatrix; |
| 5154 | } |
| 5155 | transformMatrix.mapRect(boundingRect); |
Alan Viverette | c45b1d4 | 2015-11-16 15:38:59 -0500 | [diff] [blame] | 5156 | dirty.set((int) Math.floor(boundingRect.left), |
| 5157 | (int) Math.floor(boundingRect.top), |
| 5158 | (int) Math.ceil(boundingRect.right), |
| 5159 | (int) Math.ceil(boundingRect.bottom)); |
Romain Guy | fe455af | 2012-02-15 16:40:20 -0800 | [diff] [blame] | 5160 | } |
| 5161 | |
| 5162 | do { |
| 5163 | View view = null; |
| 5164 | if (parent instanceof View) { |
| 5165 | view = (View) parent; |
Romain Guy | fe455af | 2012-02-15 16:40:20 -0800 | [diff] [blame] | 5166 | } |
| 5167 | |
| 5168 | if (drawAnimation) { |
| 5169 | if (view != null) { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 5170 | view.mPrivateFlags |= PFLAG_DRAW_ANIMATION; |
Romain Guy | fe455af | 2012-02-15 16:40:20 -0800 | [diff] [blame] | 5171 | } else if (parent instanceof ViewRootImpl) { |
| 5172 | ((ViewRootImpl) parent).mIsAnimating = true; |
| 5173 | } |
| 5174 | } |
| 5175 | |
| 5176 | // If the parent is dirty opaque or not dirty, mark it dirty with the opaque |
| 5177 | // flag coming from the child that initiated the invalidate |
| 5178 | if (view != null) { |
| 5179 | if ((view.mViewFlags & FADING_EDGE_MASK) != 0 && |
| 5180 | view.getSolidColor() == 0) { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 5181 | opaqueFlag = PFLAG_DIRTY; |
Romain Guy | fe455af | 2012-02-15 16:40:20 -0800 | [diff] [blame] | 5182 | } |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 5183 | if ((view.mPrivateFlags & PFLAG_DIRTY_MASK) != PFLAG_DIRTY) { |
| 5184 | view.mPrivateFlags = (view.mPrivateFlags & ~PFLAG_DIRTY_MASK) | opaqueFlag; |
Romain Guy | fe455af | 2012-02-15 16:40:20 -0800 | [diff] [blame] | 5185 | } |
| 5186 | } |
| 5187 | |
| 5188 | parent = parent.invalidateChildInParent(location, dirty); |
| 5189 | if (view != null) { |
| 5190 | // Account for transform on current parent |
| 5191 | Matrix m = view.getMatrix(); |
| 5192 | if (!m.isIdentity()) { |
| 5193 | RectF boundingRect = attachInfo.mTmpTransformRect; |
| 5194 | boundingRect.set(dirty); |
| 5195 | m.mapRect(boundingRect); |
Alan Viverette | c45b1d4 | 2015-11-16 15:38:59 -0500 | [diff] [blame] | 5196 | dirty.set((int) Math.floor(boundingRect.left), |
| 5197 | (int) Math.floor(boundingRect.top), |
| 5198 | (int) Math.ceil(boundingRect.right), |
| 5199 | (int) Math.ceil(boundingRect.bottom)); |
Romain Guy | fe455af | 2012-02-15 16:40:20 -0800 | [diff] [blame] | 5200 | } |
| 5201 | } |
| 5202 | } while (parent != null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5203 | } |
| 5204 | } |
| 5205 | |
| 5206 | /** |
| 5207 | * Don't call or override this method. It is used for the implementation of |
| 5208 | * the view hierarchy. |
| 5209 | * |
| 5210 | * This implementation returns null if this ViewGroup does not have a parent, |
| 5211 | * if this ViewGroup is already fully invalidated or if the dirty rectangle |
| 5212 | * does not intersect with this ViewGroup's bounds. |
| 5213 | */ |
Alan Viverette | be463f2 | 2016-01-21 10:50:10 -0500 | [diff] [blame] | 5214 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5215 | public ViewParent invalidateChildInParent(final int[] location, final Rect dirty) { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 5216 | if ((mPrivateFlags & PFLAG_DRAWN) == PFLAG_DRAWN || |
| 5217 | (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == PFLAG_DRAWING_CACHE_VALID) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5218 | if ((mGroupFlags & (FLAG_OPTIMIZE_INVALIDATE | FLAG_ANIMATION_DONE)) != |
| 5219 | FLAG_OPTIMIZE_INVALIDATE) { |
| 5220 | dirty.offset(location[CHILD_LEFT_INDEX] - mScrollX, |
| 5221 | location[CHILD_TOP_INDEX] - mScrollY); |
Chet Haase | a4f14eb | 2013-04-22 11:11:39 -0700 | [diff] [blame] | 5222 | if ((mGroupFlags & FLAG_CLIP_CHILDREN) == 0) { |
| 5223 | dirty.union(0, 0, mRight - mLeft, mBottom - mTop); |
| 5224 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5225 | |
| 5226 | final int left = mLeft; |
| 5227 | final int top = mTop; |
| 5228 | |
Chet Haase | 05e91ed | 2012-07-03 14:17:57 -0700 | [diff] [blame] | 5229 | if ((mGroupFlags & FLAG_CLIP_CHILDREN) == FLAG_CLIP_CHILDREN) { |
| 5230 | if (!dirty.intersect(0, 0, mRight - left, mBottom - top)) { |
| 5231 | dirty.setEmpty(); |
Romain Guy | 3a3133d | 2011-02-01 22:59:58 -0800 | [diff] [blame] | 5232 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5233 | } |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 5234 | mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID; |
Chet Haase | 05e91ed | 2012-07-03 14:17:57 -0700 | [diff] [blame] | 5235 | |
| 5236 | location[CHILD_LEFT_INDEX] = left; |
| 5237 | location[CHILD_TOP_INDEX] = top; |
| 5238 | |
John Reck | 96bb8ad | 2014-06-19 10:53:03 -0700 | [diff] [blame] | 5239 | if (mLayerType != LAYER_TYPE_NONE) { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 5240 | mPrivateFlags |= PFLAG_INVALIDATED; |
Chet Haase | 05e91ed | 2012-07-03 14:17:57 -0700 | [diff] [blame] | 5241 | } |
| 5242 | |
| 5243 | return mParent; |
| 5244 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5245 | } else { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 5246 | mPrivateFlags &= ~PFLAG_DRAWN & ~PFLAG_DRAWING_CACHE_VALID; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5247 | |
| 5248 | location[CHILD_LEFT_INDEX] = mLeft; |
| 5249 | location[CHILD_TOP_INDEX] = mTop; |
Chet Haase | a3db866 | 2011-07-19 10:36:05 -0700 | [diff] [blame] | 5250 | if ((mGroupFlags & FLAG_CLIP_CHILDREN) == FLAG_CLIP_CHILDREN) { |
| 5251 | dirty.set(0, 0, mRight - mLeft, mBottom - mTop); |
| 5252 | } else { |
| 5253 | // in case the dirty rect extends outside the bounds of this container |
| 5254 | dirty.union(0, 0, mRight - mLeft, mBottom - mTop); |
| 5255 | } |
Romain Guy | 3a3133d | 2011-02-01 22:59:58 -0800 | [diff] [blame] | 5256 | |
John Reck | 96bb8ad | 2014-06-19 10:53:03 -0700 | [diff] [blame] | 5257 | if (mLayerType != LAYER_TYPE_NONE) { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 5258 | mPrivateFlags |= PFLAG_INVALIDATED; |
Romain Guy | 3a3133d | 2011-02-01 22:59:58 -0800 | [diff] [blame] | 5259 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5260 | |
| 5261 | return mParent; |
| 5262 | } |
| 5263 | } |
| 5264 | |
| 5265 | return null; |
| 5266 | } |
| 5267 | |
| 5268 | /** |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 5269 | * Native-calculated damage path |
| 5270 | * Returns false if this path was unable to complete successfully. This means |
| 5271 | * it hit a ViewParent it doesn't recognize and needs to fall back to calculating |
| 5272 | * damage area |
| 5273 | * @hide |
| 5274 | */ |
| 5275 | public boolean damageChildDeferred(View child) { |
| 5276 | ViewParent parent = getParent(); |
| 5277 | while (parent != null) { |
| 5278 | if (parent instanceof ViewGroup) { |
| 5279 | parent = parent.getParent(); |
| 5280 | } else if (parent instanceof ViewRootImpl) { |
| 5281 | ((ViewRootImpl) parent).invalidate(); |
| 5282 | return true; |
| 5283 | } else { |
| 5284 | parent = null; |
| 5285 | } |
| 5286 | } |
| 5287 | return false; |
| 5288 | } |
| 5289 | |
| 5290 | /** |
Chet Haase | 9d1992d | 2012-03-13 11:03:25 -0700 | [diff] [blame] | 5291 | * Quick invalidation method called by View.invalidateViewProperty. This doesn't set the |
| 5292 | * DRAWN flags and doesn't handle the Animation logic that the default invalidation methods |
| 5293 | * do; all we want to do here is schedule a traversal with the appropriate dirty rect. |
| 5294 | * |
| 5295 | * @hide |
| 5296 | */ |
Chris Craik | 49e6c73 | 2014-03-31 12:34:11 -0700 | [diff] [blame] | 5297 | public void damageChild(View child, final Rect dirty) { |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 5298 | if (damageChildDeferred(child)) { |
| 5299 | return; |
| 5300 | } |
| 5301 | |
Chet Haase | 9d1992d | 2012-03-13 11:03:25 -0700 | [diff] [blame] | 5302 | ViewParent parent = this; |
| 5303 | |
| 5304 | final AttachInfo attachInfo = mAttachInfo; |
| 5305 | if (attachInfo != null) { |
Chet Haase | 9d1992d | 2012-03-13 11:03:25 -0700 | [diff] [blame] | 5306 | int left = child.mLeft; |
| 5307 | int top = child.mTop; |
| 5308 | if (!child.getMatrix().isIdentity()) { |
| 5309 | child.transformRect(dirty); |
| 5310 | } |
| 5311 | |
| 5312 | do { |
| 5313 | if (parent instanceof ViewGroup) { |
| 5314 | ViewGroup parentVG = (ViewGroup) parent; |
Chet Haase | b85967b | 2012-03-26 14:37:51 -0700 | [diff] [blame] | 5315 | if (parentVG.mLayerType != LAYER_TYPE_NONE) { |
| 5316 | // Layered parents should be recreated, not just re-issued |
| 5317 | parentVG.invalidate(); |
| 5318 | parent = null; |
| 5319 | } else { |
Chris Craik | 49e6c73 | 2014-03-31 12:34:11 -0700 | [diff] [blame] | 5320 | parent = parentVG.damageChildInParent(left, top, dirty); |
Chet Haase | b85967b | 2012-03-26 14:37:51 -0700 | [diff] [blame] | 5321 | left = parentVG.mLeft; |
| 5322 | top = parentVG.mTop; |
| 5323 | } |
Chet Haase | 9d1992d | 2012-03-13 11:03:25 -0700 | [diff] [blame] | 5324 | } else { |
| 5325 | // Reached the top; this calls into the usual invalidate method in |
| 5326 | // ViewRootImpl, which schedules a traversal |
| 5327 | final int[] location = attachInfo.mInvalidateChildLocation; |
| 5328 | location[0] = left; |
| 5329 | location[1] = top; |
| 5330 | parent = parent.invalidateChildInParent(location, dirty); |
| 5331 | } |
| 5332 | } while (parent != null); |
| 5333 | } |
| 5334 | } |
| 5335 | |
| 5336 | /** |
| 5337 | * Quick invalidation method that simply transforms the dirty rect into the parent's |
| 5338 | * coordinate system, pruning the invalidation if the parent has already been invalidated. |
Chet Haase | e4a2d7c | 2013-06-21 17:49:36 -0700 | [diff] [blame] | 5339 | * |
| 5340 | * @hide |
Chet Haase | 9d1992d | 2012-03-13 11:03:25 -0700 | [diff] [blame] | 5341 | */ |
Chris Craik | 49e6c73 | 2014-03-31 12:34:11 -0700 | [diff] [blame] | 5342 | protected ViewParent damageChildInParent(int left, int top, final Rect dirty) { |
| 5343 | if ((mPrivateFlags & PFLAG_DRAWN) != 0 |
| 5344 | || (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) != 0) { |
Chet Haase | 9d1992d | 2012-03-13 11:03:25 -0700 | [diff] [blame] | 5345 | dirty.offset(left - mScrollX, top - mScrollY); |
Chet Haase | a4f14eb | 2013-04-22 11:11:39 -0700 | [diff] [blame] | 5346 | if ((mGroupFlags & FLAG_CLIP_CHILDREN) == 0) { |
| 5347 | dirty.union(0, 0, mRight - mLeft, mBottom - mTop); |
| 5348 | } |
Chet Haase | 9d1992d | 2012-03-13 11:03:25 -0700 | [diff] [blame] | 5349 | |
| 5350 | if ((mGroupFlags & FLAG_CLIP_CHILDREN) == 0 || |
| 5351 | dirty.intersect(0, 0, mRight - mLeft, mBottom - mTop)) { |
| 5352 | |
Chet Haase | 9d1992d | 2012-03-13 11:03:25 -0700 | [diff] [blame] | 5353 | if (!getMatrix().isIdentity()) { |
| 5354 | transformRect(dirty); |
| 5355 | } |
| 5356 | |
| 5357 | return mParent; |
| 5358 | } |
| 5359 | } |
| 5360 | |
| 5361 | return null; |
| 5362 | } |
| 5363 | |
| 5364 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5365 | * Offset a rectangle that is in a descendant's coordinate |
| 5366 | * space into our coordinate space. |
| 5367 | * @param descendant A descendant of this view |
| 5368 | * @param rect A rectangle defined in descendant's coordinate space. |
| 5369 | */ |
| 5370 | public final void offsetDescendantRectToMyCoords(View descendant, Rect rect) { |
| 5371 | offsetRectBetweenParentAndChild(descendant, rect, true, false); |
| 5372 | } |
| 5373 | |
| 5374 | /** |
| 5375 | * Offset a rectangle that is in our coordinate space into an ancestor's |
| 5376 | * coordinate space. |
| 5377 | * @param descendant A descendant of this view |
| 5378 | * @param rect A rectangle defined in descendant's coordinate space. |
| 5379 | */ |
| 5380 | public final void offsetRectIntoDescendantCoords(View descendant, Rect rect) { |
| 5381 | offsetRectBetweenParentAndChild(descendant, rect, false, false); |
| 5382 | } |
| 5383 | |
| 5384 | /** |
| 5385 | * Helper method that offsets a rect either from parent to descendant or |
| 5386 | * descendant to parent. |
| 5387 | */ |
| 5388 | void offsetRectBetweenParentAndChild(View descendant, Rect rect, |
| 5389 | boolean offsetFromChildToParent, boolean clipToBounds) { |
| 5390 | |
| 5391 | // already in the same coord system :) |
| 5392 | if (descendant == this) { |
| 5393 | return; |
| 5394 | } |
| 5395 | |
| 5396 | ViewParent theParent = descendant.mParent; |
| 5397 | |
| 5398 | // search and offset up to the parent |
| 5399 | while ((theParent != null) |
| 5400 | && (theParent instanceof View) |
| 5401 | && (theParent != this)) { |
| 5402 | |
| 5403 | if (offsetFromChildToParent) { |
Hyunyoung Song | 4eb1a4e | 2016-03-09 22:51:02 +0000 | [diff] [blame] | 5404 | rect.offset(descendant.mLeft - descendant.mScrollX, |
| 5405 | descendant.mTop - descendant.mScrollY); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5406 | if (clipToBounds) { |
| 5407 | View p = (View) theParent; |
Doris Liu | 9607fbe | 2015-05-28 17:17:28 -0700 | [diff] [blame] | 5408 | boolean intersected = rect.intersect(0, 0, p.mRight - p.mLeft, |
| 5409 | p.mBottom - p.mTop); |
| 5410 | if (!intersected) { |
| 5411 | rect.setEmpty(); |
| 5412 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5413 | } |
| 5414 | } else { |
| 5415 | if (clipToBounds) { |
| 5416 | View p = (View) theParent; |
Doris Liu | 9607fbe | 2015-05-28 17:17:28 -0700 | [diff] [blame] | 5417 | boolean intersected = rect.intersect(0, 0, p.mRight - p.mLeft, |
| 5418 | p.mBottom - p.mTop); |
| 5419 | if (!intersected) { |
| 5420 | rect.setEmpty(); |
| 5421 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5422 | } |
Hyunyoung Song | 4eb1a4e | 2016-03-09 22:51:02 +0000 | [diff] [blame] | 5423 | rect.offset(descendant.mScrollX - descendant.mLeft, |
| 5424 | descendant.mScrollY - descendant.mTop); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5425 | } |
| 5426 | |
| 5427 | descendant = (View) theParent; |
| 5428 | theParent = descendant.mParent; |
| 5429 | } |
| 5430 | |
| 5431 | // now that we are up to this view, need to offset one more time |
| 5432 | // to get into our coordinate space |
| 5433 | if (theParent == this) { |
| 5434 | if (offsetFromChildToParent) { |
Hyunyoung Song | 4eb1a4e | 2016-03-09 22:51:02 +0000 | [diff] [blame] | 5435 | rect.offset(descendant.mLeft - descendant.mScrollX, |
| 5436 | descendant.mTop - descendant.mScrollY); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5437 | } else { |
Hyunyoung Song | 4eb1a4e | 2016-03-09 22:51:02 +0000 | [diff] [blame] | 5438 | rect.offset(descendant.mScrollX - descendant.mLeft, |
| 5439 | descendant.mScrollY - descendant.mTop); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5440 | } |
| 5441 | } else { |
| 5442 | throw new IllegalArgumentException("parameter must be a descendant of this view"); |
| 5443 | } |
| 5444 | } |
| 5445 | |
| 5446 | /** |
| 5447 | * Offset the vertical location of all children of this view by the specified number of pixels. |
| 5448 | * |
| 5449 | * @param offset the number of pixels to offset |
| 5450 | * |
| 5451 | * @hide |
| 5452 | */ |
| 5453 | public void offsetChildrenTopAndBottom(int offset) { |
| 5454 | final int count = mChildrenCount; |
| 5455 | final View[] children = mChildren; |
Romain Guy | 5549cb5 | 2013-05-06 18:42:08 -0700 | [diff] [blame] | 5456 | boolean invalidate = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5457 | |
| 5458 | for (int i = 0; i < count; i++) { |
| 5459 | final View v = children[i]; |
| 5460 | v.mTop += offset; |
| 5461 | v.mBottom += offset; |
Chris Craik | 64a12e1 | 2014-03-28 18:12:12 -0700 | [diff] [blame] | 5462 | if (v.mRenderNode != null) { |
Romain Guy | 5549cb5 | 2013-05-06 18:42:08 -0700 | [diff] [blame] | 5463 | invalidate = true; |
Chris Craik | 64a12e1 | 2014-03-28 18:12:12 -0700 | [diff] [blame] | 5464 | v.mRenderNode.offsetTopAndBottom(offset); |
Chet Haase | a1cff50 | 2012-02-21 13:43:44 -0800 | [diff] [blame] | 5465 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5466 | } |
Romain Guy | 5549cb5 | 2013-05-06 18:42:08 -0700 | [diff] [blame] | 5467 | |
| 5468 | if (invalidate) { |
| 5469 | invalidateViewProperty(false, false); |
| 5470 | } |
Guang Zhu | 84e2509 | 2014-05-01 21:12:55 -0700 | [diff] [blame] | 5471 | notifySubtreeAccessibilityStateChangedIfNeeded(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5472 | } |
| 5473 | |
Alan Viverette | be463f2 | 2016-01-21 10:50:10 -0500 | [diff] [blame] | 5474 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5475 | public boolean getChildVisibleRect(View child, Rect r, android.graphics.Point offset) { |
Adam Powell | f93bb6d | 2011-12-12 15:21:57 -0800 | [diff] [blame] | 5476 | // It doesn't make a whole lot of sense to call this on a view that isn't attached, |
| 5477 | // but for some simple tests it can be useful. If we don't have attach info this |
| 5478 | // will allocate memory. |
| 5479 | final RectF rect = mAttachInfo != null ? mAttachInfo.mTmpTransformRect : new RectF(); |
Gilles Debunne | cea4513 | 2011-11-24 02:19:27 +0100 | [diff] [blame] | 5480 | rect.set(r); |
| 5481 | |
| 5482 | if (!child.hasIdentityMatrix()) { |
George Mount | 002d43d | 2014-11-11 12:54:43 -0800 | [diff] [blame] | 5483 | child.getMatrix().mapRect(rect); |
Gilles Debunne | cea4513 | 2011-11-24 02:19:27 +0100 | [diff] [blame] | 5484 | } |
| 5485 | |
Alan Viverette | ab2cf6d | 2014-11-26 14:34:36 -0800 | [diff] [blame] | 5486 | final int dx = child.mLeft - mScrollX; |
| 5487 | final int dy = child.mTop - mScrollY; |
Gilles Debunne | cea4513 | 2011-11-24 02:19:27 +0100 | [diff] [blame] | 5488 | |
| 5489 | rect.offset(dx, dy); |
| 5490 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5491 | if (offset != null) { |
Gilles Debunne | cea4513 | 2011-11-24 02:19:27 +0100 | [diff] [blame] | 5492 | if (!child.hasIdentityMatrix()) { |
Adam Powell | f93bb6d | 2011-12-12 15:21:57 -0800 | [diff] [blame] | 5493 | float[] position = mAttachInfo != null ? mAttachInfo.mTmpTransformLocation |
| 5494 | : new float[2]; |
Gilles Debunne | cea4513 | 2011-11-24 02:19:27 +0100 | [diff] [blame] | 5495 | position[0] = offset.x; |
| 5496 | position[1] = offset.y; |
| 5497 | child.getMatrix().mapPoints(position); |
Alan Viverette | c45b1d4 | 2015-11-16 15:38:59 -0500 | [diff] [blame] | 5498 | offset.x = Math.round(position[0]); |
| 5499 | offset.y = Math.round(position[1]); |
Gilles Debunne | cea4513 | 2011-11-24 02:19:27 +0100 | [diff] [blame] | 5500 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5501 | offset.x += dx; |
| 5502 | offset.y += dy; |
| 5503 | } |
Gilles Debunne | cea4513 | 2011-11-24 02:19:27 +0100 | [diff] [blame] | 5504 | |
Alan Viverette | ab2cf6d | 2014-11-26 14:34:36 -0800 | [diff] [blame] | 5505 | final int width = mRight - mLeft; |
| 5506 | final int height = mBottom - mTop; |
| 5507 | |
George Mount | 002d43d | 2014-11-11 12:54:43 -0800 | [diff] [blame] | 5508 | boolean rectIsVisible = true; |
Adam Powell | 35da41e | 2014-12-10 18:59:34 -0800 | [diff] [blame] | 5509 | if (mParent == null || |
| 5510 | (mParent instanceof ViewGroup && ((ViewGroup) mParent).getClipChildren())) { |
Alan Viverette | ab2cf6d | 2014-11-26 14:34:36 -0800 | [diff] [blame] | 5511 | // Clip to bounds. |
| 5512 | rectIsVisible = rect.intersect(0, 0, width, height); |
Gilles Debunne | cea4513 | 2011-11-24 02:19:27 +0100 | [diff] [blame] | 5513 | } |
| 5514 | |
George Mount | 002d43d | 2014-11-11 12:54:43 -0800 | [diff] [blame] | 5515 | if (rectIsVisible && (mGroupFlags & CLIP_TO_PADDING_MASK) == CLIP_TO_PADDING_MASK) { |
Alan Viverette | ab2cf6d | 2014-11-26 14:34:36 -0800 | [diff] [blame] | 5516 | // Clip to padding. |
George Mount | 002d43d | 2014-11-11 12:54:43 -0800 | [diff] [blame] | 5517 | rectIsVisible = rect.intersect(mPaddingLeft, mPaddingTop, |
Alan Viverette | ab2cf6d | 2014-11-26 14:34:36 -0800 | [diff] [blame] | 5518 | width - mPaddingRight, height - mPaddingBottom); |
George Mount | 002d43d | 2014-11-11 12:54:43 -0800 | [diff] [blame] | 5519 | } |
| 5520 | |
| 5521 | if (rectIsVisible && mClipBounds != null) { |
Alan Viverette | ab2cf6d | 2014-11-26 14:34:36 -0800 | [diff] [blame] | 5522 | // Clip to clipBounds. |
George Mount | 002d43d | 2014-11-11 12:54:43 -0800 | [diff] [blame] | 5523 | rectIsVisible = rect.intersect(mClipBounds.left, mClipBounds.top, mClipBounds.right, |
| 5524 | mClipBounds.bottom); |
| 5525 | } |
Alan Viverette | c45b1d4 | 2015-11-16 15:38:59 -0500 | [diff] [blame] | 5526 | r.set((int) Math.floor(rect.left), (int) Math.floor(rect.top), |
| 5527 | (int) Math.ceil(rect.right), (int) Math.ceil(rect.bottom)); |
George Mount | 002d43d | 2014-11-11 12:54:43 -0800 | [diff] [blame] | 5528 | if (rectIsVisible && mParent != null) { |
| 5529 | rectIsVisible = mParent.getChildVisibleRect(this, r, offset); |
| 5530 | } |
| 5531 | return rectIsVisible; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5532 | } |
| 5533 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5534 | @Override |
Chet Haase | 9c08744 | 2011-01-12 16:20:16 -0800 | [diff] [blame] | 5535 | public final void layout(int l, int t, int r, int b) { |
Chet Haase | 430742f | 2013-04-12 11:18:36 -0700 | [diff] [blame] | 5536 | if (!mSuppressLayout && (mTransition == null || !mTransition.isChangingLayout())) { |
Chet Haase | 7dd4a53 | 2012-04-16 13:35:09 -0700 | [diff] [blame] | 5537 | if (mTransition != null) { |
| 5538 | mTransition.layoutChange(this); |
| 5539 | } |
Chet Haase | 9c08744 | 2011-01-12 16:20:16 -0800 | [diff] [blame] | 5540 | super.layout(l, t, r, b); |
| 5541 | } else { |
| 5542 | // 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] | 5543 | mLayoutCalledWhileSuppressed = true; |
Chet Haase | 9c08744 | 2011-01-12 16:20:16 -0800 | [diff] [blame] | 5544 | } |
| 5545 | } |
| 5546 | |
Chet Haase | 9c08744 | 2011-01-12 16:20:16 -0800 | [diff] [blame] | 5547 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5548 | protected abstract void onLayout(boolean changed, |
| 5549 | int l, int t, int r, int b); |
| 5550 | |
| 5551 | /** |
| 5552 | * Indicates whether the view group has the ability to animate its children |
| 5553 | * after the first layout. |
| 5554 | * |
| 5555 | * @return true if the children can be animated, false otherwise |
| 5556 | */ |
| 5557 | protected boolean canAnimate() { |
| 5558 | return mLayoutAnimationController != null; |
| 5559 | } |
| 5560 | |
| 5561 | /** |
| 5562 | * Runs the layout animation. Calling this method triggers a relayout of |
| 5563 | * this view group. |
| 5564 | */ |
| 5565 | public void startLayoutAnimation() { |
| 5566 | if (mLayoutAnimationController != null) { |
| 5567 | mGroupFlags |= FLAG_RUN_ANIMATION; |
| 5568 | requestLayout(); |
| 5569 | } |
| 5570 | } |
| 5571 | |
| 5572 | /** |
| 5573 | * Schedules the layout animation to be played after the next layout pass |
| 5574 | * of this view group. This can be used to restart the layout animation |
| 5575 | * when the content of the view group changes or when the activity is |
| 5576 | * paused and resumed. |
| 5577 | */ |
| 5578 | public void scheduleLayoutAnimation() { |
| 5579 | mGroupFlags |= FLAG_RUN_ANIMATION; |
| 5580 | } |
| 5581 | |
| 5582 | /** |
| 5583 | * Sets the layout animation controller used to animate the group's |
| 5584 | * children after the first layout. |
| 5585 | * |
| 5586 | * @param controller the animation controller |
| 5587 | */ |
| 5588 | public void setLayoutAnimation(LayoutAnimationController controller) { |
| 5589 | mLayoutAnimationController = controller; |
| 5590 | if (mLayoutAnimationController != null) { |
| 5591 | mGroupFlags |= FLAG_RUN_ANIMATION; |
| 5592 | } |
| 5593 | } |
| 5594 | |
| 5595 | /** |
| 5596 | * Returns the layout animation controller used to animate the group's |
| 5597 | * children. |
| 5598 | * |
| 5599 | * @return the current animation controller |
| 5600 | */ |
| 5601 | public LayoutAnimationController getLayoutAnimation() { |
| 5602 | return mLayoutAnimationController; |
| 5603 | } |
| 5604 | |
| 5605 | /** |
| 5606 | * Indicates whether the children's drawing cache is used during a layout |
| 5607 | * animation. By default, the drawing cache is enabled but this will prevent |
| 5608 | * nested layout animations from working. To nest animations, you must disable |
| 5609 | * the cache. |
| 5610 | * |
| 5611 | * @return true if the animation cache is enabled, false otherwise |
| 5612 | * |
| 5613 | * @see #setAnimationCacheEnabled(boolean) |
| 5614 | * @see View#setDrawingCacheEnabled(boolean) |
Chris Craik | 5a6bbae | 2015-04-10 17:41:34 -0700 | [diff] [blame] | 5615 | * |
Dianne Hackborn | 0e3de6c | 2015-07-29 15:20:21 -0700 | [diff] [blame] | 5616 | * @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] | 5617 | * 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] | 5618 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5619 | public boolean isAnimationCacheEnabled() { |
| 5620 | return (mGroupFlags & FLAG_ANIMATION_CACHE) == FLAG_ANIMATION_CACHE; |
| 5621 | } |
| 5622 | |
| 5623 | /** |
| 5624 | * Enables or disables the children's drawing cache during a layout animation. |
| 5625 | * By default, the drawing cache is enabled but this will prevent nested |
| 5626 | * layout animations from working. To nest animations, you must disable the |
| 5627 | * cache. |
| 5628 | * |
| 5629 | * @param enabled true to enable the animation cache, false otherwise |
| 5630 | * |
| 5631 | * @see #isAnimationCacheEnabled() |
| 5632 | * @see View#setDrawingCacheEnabled(boolean) |
Chris Craik | 5a6bbae | 2015-04-10 17:41:34 -0700 | [diff] [blame] | 5633 | * |
Dianne Hackborn | 0e3de6c | 2015-07-29 15:20:21 -0700 | [diff] [blame] | 5634 | * @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] | 5635 | * 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] | 5636 | */ |
| 5637 | public void setAnimationCacheEnabled(boolean enabled) { |
| 5638 | setBooleanFlag(FLAG_ANIMATION_CACHE, enabled); |
| 5639 | } |
| 5640 | |
| 5641 | /** |
| 5642 | * Indicates whether this ViewGroup will always try to draw its children using their |
| 5643 | * drawing cache. By default this property is enabled. |
| 5644 | * |
| 5645 | * @return true if the animation cache is enabled, false otherwise |
| 5646 | * |
| 5647 | * @see #setAlwaysDrawnWithCacheEnabled(boolean) |
| 5648 | * @see #setChildrenDrawnWithCacheEnabled(boolean) |
| 5649 | * @see View#setDrawingCacheEnabled(boolean) |
Chris Craik | 5a6bbae | 2015-04-10 17:41:34 -0700 | [diff] [blame] | 5650 | * |
Dianne Hackborn | 0e3de6c | 2015-07-29 15:20:21 -0700 | [diff] [blame] | 5651 | * @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] | 5652 | * 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] | 5653 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5654 | public boolean isAlwaysDrawnWithCacheEnabled() { |
| 5655 | return (mGroupFlags & FLAG_ALWAYS_DRAWN_WITH_CACHE) == FLAG_ALWAYS_DRAWN_WITH_CACHE; |
| 5656 | } |
| 5657 | |
| 5658 | /** |
| 5659 | * Indicates whether this ViewGroup will always try to draw its children using their |
| 5660 | * drawing cache. This property can be set to true when the cache rendering is |
| 5661 | * slightly different from the children's normal rendering. Renderings can be different, |
| 5662 | * for instance, when the cache's quality is set to low. |
| 5663 | * |
| 5664 | * When this property is disabled, the ViewGroup will use the drawing cache of its |
| 5665 | * children only when asked to. It's usually the task of subclasses to tell ViewGroup |
| 5666 | * when to start using the drawing cache and when to stop using it. |
| 5667 | * |
| 5668 | * @param always true to always draw with the drawing cache, false otherwise |
| 5669 | * |
| 5670 | * @see #isAlwaysDrawnWithCacheEnabled() |
| 5671 | * @see #setChildrenDrawnWithCacheEnabled(boolean) |
| 5672 | * @see View#setDrawingCacheEnabled(boolean) |
| 5673 | * @see View#setDrawingCacheQuality(int) |
Chris Craik | 5a6bbae | 2015-04-10 17:41:34 -0700 | [diff] [blame] | 5674 | * |
Dianne Hackborn | 0e3de6c | 2015-07-29 15:20:21 -0700 | [diff] [blame] | 5675 | * @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] | 5676 | * 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] | 5677 | */ |
| 5678 | public void setAlwaysDrawnWithCacheEnabled(boolean always) { |
| 5679 | setBooleanFlag(FLAG_ALWAYS_DRAWN_WITH_CACHE, always); |
| 5680 | } |
| 5681 | |
| 5682 | /** |
| 5683 | * Indicates whether the ViewGroup is currently drawing its children using |
| 5684 | * their drawing cache. |
| 5685 | * |
| 5686 | * @return true if children should be drawn with their cache, false otherwise |
| 5687 | * |
| 5688 | * @see #setAlwaysDrawnWithCacheEnabled(boolean) |
| 5689 | * @see #setChildrenDrawnWithCacheEnabled(boolean) |
Chris Craik | 5a6bbae | 2015-04-10 17:41:34 -0700 | [diff] [blame] | 5690 | * |
Dianne Hackborn | 0e3de6c | 2015-07-29 15:20:21 -0700 | [diff] [blame] | 5691 | * @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] | 5692 | * Child views may no longer be forced to cache their rendering state by their parents. |
| 5693 | * Use {@link View#setLayerType(int, Paint)} on individual Views instead. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5694 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5695 | protected boolean isChildrenDrawnWithCacheEnabled() { |
| 5696 | return (mGroupFlags & FLAG_CHILDREN_DRAWN_WITH_CACHE) == FLAG_CHILDREN_DRAWN_WITH_CACHE; |
| 5697 | } |
| 5698 | |
| 5699 | /** |
| 5700 | * Tells the ViewGroup to draw its children using their drawing cache. This property |
| 5701 | * is ignored when {@link #isAlwaysDrawnWithCacheEnabled()} is true. A child's drawing cache |
| 5702 | * will be used only if it has been enabled. |
| 5703 | * |
| 5704 | * Subclasses should call this method to start and stop using the drawing cache when |
| 5705 | * they perform performance sensitive operations, like scrolling or animating. |
| 5706 | * |
| 5707 | * @param enabled true if children should be drawn with their cache, false otherwise |
| 5708 | * |
| 5709 | * @see #setAlwaysDrawnWithCacheEnabled(boolean) |
| 5710 | * @see #isChildrenDrawnWithCacheEnabled() |
Chris Craik | 5a6bbae | 2015-04-10 17:41:34 -0700 | [diff] [blame] | 5711 | * |
Dianne Hackborn | 0e3de6c | 2015-07-29 15:20:21 -0700 | [diff] [blame] | 5712 | * @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] | 5713 | * Child views may no longer be forced to cache their rendering state by their parents. |
| 5714 | * Use {@link View#setLayerType(int, Paint)} on individual Views instead. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5715 | */ |
| 5716 | protected void setChildrenDrawnWithCacheEnabled(boolean enabled) { |
| 5717 | setBooleanFlag(FLAG_CHILDREN_DRAWN_WITH_CACHE, enabled); |
| 5718 | } |
| 5719 | |
Romain Guy | 293451e | 2009-11-04 13:59:48 -0800 | [diff] [blame] | 5720 | /** |
| 5721 | * Indicates whether the ViewGroup is drawing its children in the order defined by |
| 5722 | * {@link #getChildDrawingOrder(int, int)}. |
| 5723 | * |
| 5724 | * @return true if children drawing order is defined by {@link #getChildDrawingOrder(int, int)}, |
| 5725 | * false otherwise |
| 5726 | * |
| 5727 | * @see #setChildrenDrawingOrderEnabled(boolean) |
| 5728 | * @see #getChildDrawingOrder(int, int) |
| 5729 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 5730 | @ViewDebug.ExportedProperty(category = "drawing") |
Romain Guy | 293451e | 2009-11-04 13:59:48 -0800 | [diff] [blame] | 5731 | protected boolean isChildrenDrawingOrderEnabled() { |
| 5732 | return (mGroupFlags & FLAG_USE_CHILD_DRAWING_ORDER) == FLAG_USE_CHILD_DRAWING_ORDER; |
| 5733 | } |
| 5734 | |
| 5735 | /** |
| 5736 | * Tells the ViewGroup whether to draw its children in the order defined by the method |
| 5737 | * {@link #getChildDrawingOrder(int, int)}. |
Chris Craik | e83cbd4 | 2014-09-03 17:52:24 -0700 | [diff] [blame] | 5738 | * <p> |
| 5739 | * Note that {@link View#getZ() Z} reordering, done by {@link #dispatchDraw(Canvas)}, |
| 5740 | * will override custom child ordering done via this method. |
Romain Guy | 293451e | 2009-11-04 13:59:48 -0800 | [diff] [blame] | 5741 | * |
| 5742 | * @param enabled true if the order of the children when drawing is determined by |
| 5743 | * {@link #getChildDrawingOrder(int, int)}, false otherwise |
| 5744 | * |
| 5745 | * @see #isChildrenDrawingOrderEnabled() |
| 5746 | * @see #getChildDrawingOrder(int, int) |
| 5747 | */ |
| 5748 | protected void setChildrenDrawingOrderEnabled(boolean enabled) { |
| 5749 | setBooleanFlag(FLAG_USE_CHILD_DRAWING_ORDER, enabled); |
| 5750 | } |
| 5751 | |
Svetoslav | 6254f48 | 2013-06-04 17:22:14 -0700 | [diff] [blame] | 5752 | private boolean hasBooleanFlag(int flag) { |
Philip Milne | f091b66 | 2013-02-27 11:15:21 -0800 | [diff] [blame] | 5753 | return (mGroupFlags & flag) == flag; |
| 5754 | } |
| 5755 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5756 | private void setBooleanFlag(int flag, boolean value) { |
| 5757 | if (value) { |
| 5758 | mGroupFlags |= flag; |
| 5759 | } else { |
| 5760 | mGroupFlags &= ~flag; |
| 5761 | } |
| 5762 | } |
| 5763 | |
| 5764 | /** |
| 5765 | * Returns an integer indicating what types of drawing caches are kept in memory. |
| 5766 | * |
| 5767 | * @see #setPersistentDrawingCache(int) |
| 5768 | * @see #setAnimationCacheEnabled(boolean) |
| 5769 | * |
| 5770 | * @return one or a combination of {@link #PERSISTENT_NO_CACHE}, |
| 5771 | * {@link #PERSISTENT_ANIMATION_CACHE}, {@link #PERSISTENT_SCROLLING_CACHE} |
| 5772 | * and {@link #PERSISTENT_ALL_CACHES} |
| 5773 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 5774 | @ViewDebug.ExportedProperty(category = "drawing", mapping = { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5775 | @ViewDebug.IntToString(from = PERSISTENT_NO_CACHE, to = "NONE"), |
Romain Guy | 203688c | 2010-05-12 15:41:32 -0700 | [diff] [blame] | 5776 | @ViewDebug.IntToString(from = PERSISTENT_ANIMATION_CACHE, to = "ANIMATION"), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5777 | @ViewDebug.IntToString(from = PERSISTENT_SCROLLING_CACHE, to = "SCROLLING"), |
| 5778 | @ViewDebug.IntToString(from = PERSISTENT_ALL_CACHES, to = "ALL") |
| 5779 | }) |
| 5780 | public int getPersistentDrawingCache() { |
| 5781 | return mPersistentDrawingCache; |
| 5782 | } |
| 5783 | |
| 5784 | /** |
| 5785 | * Indicates what types of drawing caches should be kept in memory after |
| 5786 | * they have been created. |
| 5787 | * |
| 5788 | * @see #getPersistentDrawingCache() |
| 5789 | * @see #setAnimationCacheEnabled(boolean) |
| 5790 | * |
| 5791 | * @param drawingCacheToKeep one or a combination of {@link #PERSISTENT_NO_CACHE}, |
| 5792 | * {@link #PERSISTENT_ANIMATION_CACHE}, {@link #PERSISTENT_SCROLLING_CACHE} |
| 5793 | * and {@link #PERSISTENT_ALL_CACHES} |
| 5794 | */ |
| 5795 | public void setPersistentDrawingCache(int drawingCacheToKeep) { |
| 5796 | mPersistentDrawingCache = drawingCacheToKeep & PERSISTENT_ALL_CACHES; |
| 5797 | } |
| 5798 | |
Philip Milne | f091b66 | 2013-02-27 11:15:21 -0800 | [diff] [blame] | 5799 | private void setLayoutMode(int layoutMode, boolean explicitly) { |
| 5800 | mLayoutMode = layoutMode; |
| 5801 | setBooleanFlag(FLAG_LAYOUT_MODE_WAS_EXPLICITLY_SET, explicitly); |
| 5802 | } |
| 5803 | |
| 5804 | /** |
| 5805 | * Recursively traverse the view hierarchy, resetting the layoutMode of any |
| 5806 | * descendants that had inherited a different layoutMode from a previous parent. |
| 5807 | * Recursion terminates when a descendant's mode is: |
| 5808 | * <ul> |
| 5809 | * <li>Undefined</li> |
| 5810 | * <li>The same as the root node's</li> |
| 5811 | * <li>A mode that had been explicitly set</li> |
| 5812 | * <ul/> |
| 5813 | * The first two clauses are optimizations. |
| 5814 | * @param layoutModeOfRoot |
| 5815 | */ |
| 5816 | @Override |
| 5817 | void invalidateInheritedLayoutMode(int layoutModeOfRoot) { |
| 5818 | if (mLayoutMode == LAYOUT_MODE_UNDEFINED || |
| 5819 | mLayoutMode == layoutModeOfRoot || |
Svetoslav | 6254f48 | 2013-06-04 17:22:14 -0700 | [diff] [blame] | 5820 | hasBooleanFlag(FLAG_LAYOUT_MODE_WAS_EXPLICITLY_SET)) { |
Philip Milne | f091b66 | 2013-02-27 11:15:21 -0800 | [diff] [blame] | 5821 | return; |
| 5822 | } |
| 5823 | setLayoutMode(LAYOUT_MODE_UNDEFINED, false); |
| 5824 | |
| 5825 | // apply recursively |
| 5826 | for (int i = 0, N = getChildCount(); i < N; i++) { |
| 5827 | getChildAt(i).invalidateInheritedLayoutMode(layoutModeOfRoot); |
| 5828 | } |
| 5829 | } |
| 5830 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5831 | /** |
Philip Milne | cfb631b | 2012-10-26 10:51:46 -0700 | [diff] [blame] | 5832 | * Returns the basis of alignment during layout operations on this ViewGroup: |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 5833 | * either {@link #LAYOUT_MODE_CLIP_BOUNDS} or {@link #LAYOUT_MODE_OPTICAL_BOUNDS}. |
Philip Milne | cfb631b | 2012-10-26 10:51:46 -0700 | [diff] [blame] | 5834 | * <p> |
| 5835 | * If no layoutMode was explicitly set, either programmatically or in an XML resource, |
| 5836 | * the method returns the layoutMode of the view's parent ViewGroup if such a parent exists, |
| 5837 | * otherwise the method returns a default value of {@link #LAYOUT_MODE_CLIP_BOUNDS}. |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 5838 | * |
Philip Milne | fcc6a0f | 2012-04-16 16:12:19 -0700 | [diff] [blame] | 5839 | * @return the layout mode to use during layout operations |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 5840 | * |
| 5841 | * @see #setLayoutMode(int) |
| 5842 | */ |
| 5843 | public int getLayoutMode() { |
Philip Milne | cfb631b | 2012-10-26 10:51:46 -0700 | [diff] [blame] | 5844 | if (mLayoutMode == LAYOUT_MODE_UNDEFINED) { |
Philip Milne | f091b66 | 2013-02-27 11:15:21 -0800 | [diff] [blame] | 5845 | int inheritedLayoutMode = (mParent instanceof ViewGroup) ? |
| 5846 | ((ViewGroup) mParent).getLayoutMode() : LAYOUT_MODE_DEFAULT; |
| 5847 | setLayoutMode(inheritedLayoutMode, false); |
Philip Milne | cfb631b | 2012-10-26 10:51:46 -0700 | [diff] [blame] | 5848 | } |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 5849 | return mLayoutMode; |
| 5850 | } |
| 5851 | |
| 5852 | /** |
Philip Milne | cfb631b | 2012-10-26 10:51:46 -0700 | [diff] [blame] | 5853 | * Sets the basis of alignment during the layout of this ViewGroup. |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 5854 | * Valid values are either {@link #LAYOUT_MODE_CLIP_BOUNDS} or |
| 5855 | * {@link #LAYOUT_MODE_OPTICAL_BOUNDS}. |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 5856 | * |
Philip Milne | fcc6a0f | 2012-04-16 16:12:19 -0700 | [diff] [blame] | 5857 | * @param layoutMode the layout mode to use during layout operations |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 5858 | * |
| 5859 | * @see #getLayoutMode() |
Scott Main | 27a8508 | 2013-06-10 10:39:48 -0700 | [diff] [blame] | 5860 | * @attr ref android.R.styleable#ViewGroup_layoutMode |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 5861 | */ |
| 5862 | public void setLayoutMode(int layoutMode) { |
| 5863 | if (mLayoutMode != layoutMode) { |
Philip Milne | f091b66 | 2013-02-27 11:15:21 -0800 | [diff] [blame] | 5864 | invalidateInheritedLayoutMode(layoutMode); |
| 5865 | setLayoutMode(layoutMode, layoutMode != LAYOUT_MODE_UNDEFINED); |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 5866 | requestLayout(); |
| 5867 | } |
| 5868 | } |
| 5869 | |
| 5870 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5871 | * Returns a new set of layout parameters based on the supplied attributes set. |
| 5872 | * |
| 5873 | * @param attrs the attributes to build the layout parameters from |
| 5874 | * |
| 5875 | * @return an instance of {@link android.view.ViewGroup.LayoutParams} or one |
| 5876 | * of its descendants |
| 5877 | */ |
| 5878 | public LayoutParams generateLayoutParams(AttributeSet attrs) { |
| 5879 | return new LayoutParams(getContext(), attrs); |
| 5880 | } |
| 5881 | |
| 5882 | /** |
| 5883 | * Returns a safe set of layout parameters based on the supplied layout params. |
| 5884 | * When a ViewGroup is passed a View whose layout params do not pass the test of |
| 5885 | * {@link #checkLayoutParams(android.view.ViewGroup.LayoutParams)}, this method |
| 5886 | * is invoked. This method should return a new set of layout params suitable for |
| 5887 | * this ViewGroup, possibly by copying the appropriate attributes from the |
| 5888 | * specified set of layout params. |
| 5889 | * |
| 5890 | * @param p The layout parameters to convert into a suitable set of layout parameters |
| 5891 | * for this ViewGroup. |
| 5892 | * |
| 5893 | * @return an instance of {@link android.view.ViewGroup.LayoutParams} or one |
| 5894 | * of its descendants |
| 5895 | */ |
| 5896 | protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) { |
Chet Haase | 4610eef | 2015-12-03 07:38:11 -0800 | [diff] [blame] | 5897 | return p; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5898 | } |
| 5899 | |
| 5900 | /** |
| 5901 | * Returns a set of default layout parameters. These parameters are requested |
| 5902 | * when the View passed to {@link #addView(View)} has no layout parameters |
| 5903 | * already set. If null is returned, an exception is thrown from addView. |
| 5904 | * |
| 5905 | * @return a set of default layout parameters or null |
| 5906 | */ |
| 5907 | protected LayoutParams generateDefaultLayoutParams() { |
| 5908 | return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); |
| 5909 | } |
| 5910 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5911 | @Override |
| 5912 | protected void debug(int depth) { |
| 5913 | super.debug(depth); |
| 5914 | String output; |
| 5915 | |
| 5916 | if (mFocused != null) { |
| 5917 | output = debugIndent(depth); |
| 5918 | output += "mFocused"; |
| 5919 | Log.d(VIEW_LOG_TAG, output); |
| 5920 | } |
| 5921 | if (mChildrenCount != 0) { |
| 5922 | output = debugIndent(depth); |
| 5923 | output += "{"; |
| 5924 | Log.d(VIEW_LOG_TAG, output); |
| 5925 | } |
| 5926 | int count = mChildrenCount; |
| 5927 | for (int i = 0; i < count; i++) { |
| 5928 | View child = mChildren[i]; |
| 5929 | child.debug(depth + 1); |
| 5930 | } |
| 5931 | |
| 5932 | if (mChildrenCount != 0) { |
| 5933 | output = debugIndent(depth); |
| 5934 | output += "}"; |
| 5935 | Log.d(VIEW_LOG_TAG, output); |
| 5936 | } |
| 5937 | } |
| 5938 | |
| 5939 | /** |
| 5940 | * Returns the position in the group of the specified child view. |
| 5941 | * |
| 5942 | * @param child the view for which to get the position |
| 5943 | * @return a positive integer representing the position of the view in the |
| 5944 | * group, or -1 if the view does not exist in the group |
| 5945 | */ |
| 5946 | public int indexOfChild(View child) { |
| 5947 | final int count = mChildrenCount; |
| 5948 | final View[] children = mChildren; |
| 5949 | for (int i = 0; i < count; i++) { |
| 5950 | if (children[i] == child) { |
| 5951 | return i; |
| 5952 | } |
| 5953 | } |
| 5954 | return -1; |
| 5955 | } |
| 5956 | |
| 5957 | /** |
| 5958 | * Returns the number of children in the group. |
| 5959 | * |
| 5960 | * @return a positive integer representing the number of children in |
| 5961 | * the group |
| 5962 | */ |
| 5963 | public int getChildCount() { |
| 5964 | return mChildrenCount; |
| 5965 | } |
| 5966 | |
| 5967 | /** |
| 5968 | * Returns the view at the specified position in the group. |
| 5969 | * |
| 5970 | * @param index the position at which to get the view from |
| 5971 | * @return the view at the specified position or null if the position |
| 5972 | * does not exist within the group |
| 5973 | */ |
| 5974 | public View getChildAt(int index) { |
Adam Powell | 3ba8f5d6 | 2011-03-07 15:36:33 -0800 | [diff] [blame] | 5975 | if (index < 0 || index >= mChildrenCount) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5976 | return null; |
| 5977 | } |
Adam Powell | 3ba8f5d6 | 2011-03-07 15:36:33 -0800 | [diff] [blame] | 5978 | return mChildren[index]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5979 | } |
| 5980 | |
| 5981 | /** |
| 5982 | * Ask all of the children of this view to measure themselves, taking into |
| 5983 | * account both the MeasureSpec requirements for this view and its padding. |
| 5984 | * We skip children that are in the GONE state The heavy lifting is done in |
| 5985 | * getChildMeasureSpec. |
| 5986 | * |
| 5987 | * @param widthMeasureSpec The width requirements for this view |
| 5988 | * @param heightMeasureSpec The height requirements for this view |
| 5989 | */ |
| 5990 | protected void measureChildren(int widthMeasureSpec, int heightMeasureSpec) { |
| 5991 | final int size = mChildrenCount; |
| 5992 | final View[] children = mChildren; |
| 5993 | for (int i = 0; i < size; ++i) { |
| 5994 | final View child = children[i]; |
| 5995 | if ((child.mViewFlags & VISIBILITY_MASK) != GONE) { |
| 5996 | measureChild(child, widthMeasureSpec, heightMeasureSpec); |
| 5997 | } |
| 5998 | } |
| 5999 | } |
| 6000 | |
| 6001 | /** |
| 6002 | * Ask one of the children of this view to measure itself, taking into |
| 6003 | * account both the MeasureSpec requirements for this view and its padding. |
| 6004 | * The heavy lifting is done in getChildMeasureSpec. |
| 6005 | * |
| 6006 | * @param child The child to measure |
| 6007 | * @param parentWidthMeasureSpec The width requirements for this view |
| 6008 | * @param parentHeightMeasureSpec The height requirements for this view |
| 6009 | */ |
| 6010 | protected void measureChild(View child, int parentWidthMeasureSpec, |
| 6011 | int parentHeightMeasureSpec) { |
| 6012 | final LayoutParams lp = child.getLayoutParams(); |
| 6013 | |
| 6014 | final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec, |
| 6015 | mPaddingLeft + mPaddingRight, lp.width); |
| 6016 | final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec, |
| 6017 | mPaddingTop + mPaddingBottom, lp.height); |
| 6018 | |
| 6019 | child.measure(childWidthMeasureSpec, childHeightMeasureSpec); |
| 6020 | } |
| 6021 | |
| 6022 | /** |
| 6023 | * Ask one of the children of this view to measure itself, taking into |
| 6024 | * account both the MeasureSpec requirements for this view and its padding |
| 6025 | * and margins. The child must have MarginLayoutParams The heavy lifting is |
| 6026 | * done in getChildMeasureSpec. |
| 6027 | * |
| 6028 | * @param child The child to measure |
| 6029 | * @param parentWidthMeasureSpec The width requirements for this view |
| 6030 | * @param widthUsed Extra space that has been used up by the parent |
| 6031 | * horizontally (possibly by other children of the parent) |
| 6032 | * @param parentHeightMeasureSpec The height requirements for this view |
| 6033 | * @param heightUsed Extra space that has been used up by the parent |
| 6034 | * vertically (possibly by other children of the parent) |
| 6035 | */ |
| 6036 | protected void measureChildWithMargins(View child, |
| 6037 | int parentWidthMeasureSpec, int widthUsed, |
| 6038 | int parentHeightMeasureSpec, int heightUsed) { |
| 6039 | final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams(); |
| 6040 | |
| 6041 | final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec, |
| 6042 | mPaddingLeft + mPaddingRight + lp.leftMargin + lp.rightMargin |
| 6043 | + widthUsed, lp.width); |
| 6044 | final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec, |
| 6045 | mPaddingTop + mPaddingBottom + lp.topMargin + lp.bottomMargin |
| 6046 | + heightUsed, lp.height); |
| 6047 | |
| 6048 | child.measure(childWidthMeasureSpec, childHeightMeasureSpec); |
| 6049 | } |
| 6050 | |
| 6051 | /** |
| 6052 | * Does the hard part of measureChildren: figuring out the MeasureSpec to |
| 6053 | * pass to a particular child. This method figures out the right MeasureSpec |
| 6054 | * for one dimension (height or width) of one child view. |
| 6055 | * |
| 6056 | * The goal is to combine information from our MeasureSpec with the |
| 6057 | * LayoutParams of the child to get the best possible results. For example, |
| 6058 | * if the this view knows its size (because its MeasureSpec has a mode of |
| 6059 | * EXACTLY), and the child has indicated in its LayoutParams that it wants |
| 6060 | * to be the same size as the parent, the parent should ask the child to |
| 6061 | * layout given an exact size. |
| 6062 | * |
| 6063 | * @param spec The requirements for this view |
| 6064 | * @param padding The padding of this view for the current dimension and |
| 6065 | * margins, if applicable |
| 6066 | * @param childDimension How big the child wants to be in the current |
| 6067 | * dimension |
| 6068 | * @return a MeasureSpec integer for the child |
| 6069 | */ |
| 6070 | public static int getChildMeasureSpec(int spec, int padding, int childDimension) { |
| 6071 | int specMode = MeasureSpec.getMode(spec); |
| 6072 | int specSize = MeasureSpec.getSize(spec); |
| 6073 | |
| 6074 | int size = Math.max(0, specSize - padding); |
| 6075 | |
| 6076 | int resultSize = 0; |
| 6077 | int resultMode = 0; |
| 6078 | |
| 6079 | switch (specMode) { |
| 6080 | // Parent has imposed an exact size on us |
| 6081 | case MeasureSpec.EXACTLY: |
| 6082 | if (childDimension >= 0) { |
| 6083 | resultSize = childDimension; |
| 6084 | resultMode = MeasureSpec.EXACTLY; |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 6085 | } else if (childDimension == LayoutParams.MATCH_PARENT) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6086 | // Child wants to be our size. So be it. |
| 6087 | resultSize = size; |
| 6088 | resultMode = MeasureSpec.EXACTLY; |
| 6089 | } else if (childDimension == LayoutParams.WRAP_CONTENT) { |
| 6090 | // Child wants to determine its own size. It can't be |
| 6091 | // bigger than us. |
| 6092 | resultSize = size; |
| 6093 | resultMode = MeasureSpec.AT_MOST; |
| 6094 | } |
| 6095 | break; |
| 6096 | |
| 6097 | // Parent has imposed a maximum size on us |
| 6098 | case MeasureSpec.AT_MOST: |
| 6099 | if (childDimension >= 0) { |
| 6100 | // Child wants a specific size... so be it |
| 6101 | resultSize = childDimension; |
| 6102 | resultMode = MeasureSpec.EXACTLY; |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 6103 | } else if (childDimension == LayoutParams.MATCH_PARENT) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6104 | // Child wants to be our size, but our size is not fixed. |
| 6105 | // Constrain child to not be bigger than us. |
| 6106 | resultSize = size; |
| 6107 | resultMode = MeasureSpec.AT_MOST; |
| 6108 | } else if (childDimension == LayoutParams.WRAP_CONTENT) { |
| 6109 | // Child wants to determine its own size. It can't be |
| 6110 | // bigger than us. |
| 6111 | resultSize = size; |
| 6112 | resultMode = MeasureSpec.AT_MOST; |
| 6113 | } |
| 6114 | break; |
| 6115 | |
| 6116 | // Parent asked to see how big we want to be |
| 6117 | case MeasureSpec.UNSPECIFIED: |
| 6118 | if (childDimension >= 0) { |
| 6119 | // Child wants a specific size... let him have it |
| 6120 | resultSize = childDimension; |
| 6121 | resultMode = MeasureSpec.EXACTLY; |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 6122 | } else if (childDimension == LayoutParams.MATCH_PARENT) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6123 | // Child wants to be our size... find out how big it should |
| 6124 | // be |
Adam Powell | d5dbf4b | 2015-06-11 13:19:24 -0700 | [diff] [blame] | 6125 | resultSize = View.sUseZeroUnspecifiedMeasureSpec ? 0 : size; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6126 | resultMode = MeasureSpec.UNSPECIFIED; |
| 6127 | } else if (childDimension == LayoutParams.WRAP_CONTENT) { |
| 6128 | // Child wants to determine its own size.... find out how |
| 6129 | // big it should be |
Adam Powell | d5dbf4b | 2015-06-11 13:19:24 -0700 | [diff] [blame] | 6130 | resultSize = View.sUseZeroUnspecifiedMeasureSpec ? 0 : size; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6131 | resultMode = MeasureSpec.UNSPECIFIED; |
| 6132 | } |
| 6133 | break; |
| 6134 | } |
Tor Norbye | 6756864 | 2015-03-31 07:47:23 -0700 | [diff] [blame] | 6135 | //noinspection ResourceType |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6136 | return MeasureSpec.makeMeasureSpec(resultSize, resultMode); |
| 6137 | } |
| 6138 | |
| 6139 | |
| 6140 | /** |
| 6141 | * Removes any pending animations for views that have been removed. Call |
| 6142 | * this if you don't want animations for exiting views to stack up. |
| 6143 | */ |
| 6144 | public void clearDisappearingChildren() { |
John Reck | ca7a9da | 2014-03-05 16:29:07 -0800 | [diff] [blame] | 6145 | final ArrayList<View> disappearingChildren = mDisappearingChildren; |
| 6146 | if (disappearingChildren != null) { |
| 6147 | final int count = disappearingChildren.size(); |
| 6148 | for (int i = 0; i < count; i++) { |
| 6149 | final View view = disappearingChildren.get(i); |
| 6150 | if (view.mAttachInfo != null) { |
| 6151 | view.dispatchDetachedFromWindow(); |
| 6152 | } |
| 6153 | view.clearAnimation(); |
| 6154 | } |
| 6155 | disappearingChildren.clear(); |
Chet Haase | b85967b | 2012-03-26 14:37:51 -0700 | [diff] [blame] | 6156 | invalidate(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6157 | } |
| 6158 | } |
| 6159 | |
| 6160 | /** |
| 6161 | * Add a view which is removed from mChildren but still needs animation |
| 6162 | * |
| 6163 | * @param v View to add |
| 6164 | */ |
| 6165 | private void addDisappearingView(View v) { |
| 6166 | ArrayList<View> disappearingChildren = mDisappearingChildren; |
| 6167 | |
| 6168 | if (disappearingChildren == null) { |
| 6169 | disappearingChildren = mDisappearingChildren = new ArrayList<View>(); |
| 6170 | } |
| 6171 | |
| 6172 | disappearingChildren.add(v); |
| 6173 | } |
| 6174 | |
| 6175 | /** |
| 6176 | * Cleanup a view when its animation is done. This may mean removing it from |
| 6177 | * the list of disappearing views. |
| 6178 | * |
| 6179 | * @param view The view whose animation has finished |
| 6180 | * @param animation The animation, cannot be null |
| 6181 | */ |
Chet Haase | 64a48c1 | 2012-02-13 16:33:29 -0800 | [diff] [blame] | 6182 | void finishAnimatingView(final View view, Animation animation) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6183 | final ArrayList<View> disappearingChildren = mDisappearingChildren; |
| 6184 | if (disappearingChildren != null) { |
| 6185 | if (disappearingChildren.contains(view)) { |
| 6186 | disappearingChildren.remove(view); |
| 6187 | |
| 6188 | if (view.mAttachInfo != null) { |
| 6189 | view.dispatchDetachedFromWindow(); |
| 6190 | } |
| 6191 | |
| 6192 | view.clearAnimation(); |
| 6193 | mGroupFlags |= FLAG_INVALIDATE_REQUIRED; |
| 6194 | } |
| 6195 | } |
| 6196 | |
| 6197 | if (animation != null && !animation.getFillAfter()) { |
| 6198 | view.clearAnimation(); |
| 6199 | } |
| 6200 | |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 6201 | if ((view.mPrivateFlags & PFLAG_ANIMATION_STARTED) == PFLAG_ANIMATION_STARTED) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6202 | view.onAnimationEnd(); |
| 6203 | // Should be performed by onAnimationEnd() but this avoid an infinite loop, |
| 6204 | // so we'd rather be safe than sorry |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 6205 | view.mPrivateFlags &= ~PFLAG_ANIMATION_STARTED; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6206 | // Draw one more frame after the animation is done |
| 6207 | mGroupFlags |= FLAG_INVALIDATE_REQUIRED; |
| 6208 | } |
| 6209 | } |
| 6210 | |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 6211 | /** |
Chet Haase | aceafe6 | 2011-08-26 15:44:33 -0700 | [diff] [blame] | 6212 | * Utility function called by View during invalidation to determine whether a view that |
| 6213 | * is invisible or gone should still be invalidated because it is being transitioned (and |
| 6214 | * therefore still needs to be drawn). |
| 6215 | */ |
| 6216 | boolean isViewTransitioning(View view) { |
| 6217 | return (mTransitioningViews != null && mTransitioningViews.contains(view)); |
| 6218 | } |
| 6219 | |
| 6220 | /** |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 6221 | * This method tells the ViewGroup that the given View object, which should have this |
| 6222 | * ViewGroup as its parent, |
| 6223 | * should be kept around (re-displayed when the ViewGroup draws its children) even if it |
| 6224 | * is removed from its parent. This allows animations, such as those used by |
| 6225 | * {@link android.app.Fragment} and {@link android.animation.LayoutTransition} to animate |
| 6226 | * the removal of views. A call to this method should always be accompanied by a later call |
| 6227 | * to {@link #endViewTransition(View)}, such as after an animation on the View has finished, |
| 6228 | * so that the View finally gets removed. |
| 6229 | * |
| 6230 | * @param view The View object to be kept visible even if it gets removed from its parent. |
| 6231 | */ |
| 6232 | public void startViewTransition(View view) { |
| 6233 | if (view.mParent == this) { |
| 6234 | if (mTransitioningViews == null) { |
| 6235 | mTransitioningViews = new ArrayList<View>(); |
| 6236 | } |
| 6237 | mTransitioningViews.add(view); |
| 6238 | } |
| 6239 | } |
| 6240 | |
| 6241 | /** |
| 6242 | * This method should always be called following an earlier call to |
| 6243 | * {@link #startViewTransition(View)}. The given View is finally removed from its parent |
| 6244 | * and will no longer be displayed. Note that this method does not perform the functionality |
| 6245 | * of removing a view from its parent; it just discontinues the display of a View that |
| 6246 | * has previously been removed. |
| 6247 | * |
| 6248 | * @return view The View object that has been removed but is being kept around in the visible |
| 6249 | * hierarchy by an earlier call to {@link #startViewTransition(View)}. |
| 6250 | */ |
| 6251 | public void endViewTransition(View view) { |
| 6252 | if (mTransitioningViews != null) { |
| 6253 | mTransitioningViews.remove(view); |
| 6254 | final ArrayList<View> disappearingChildren = mDisappearingChildren; |
| 6255 | if (disappearingChildren != null && disappearingChildren.contains(view)) { |
| 6256 | disappearingChildren.remove(view); |
Chet Haase | 5e25c2c | 2010-09-16 11:15:56 -0700 | [diff] [blame] | 6257 | if (mVisibilityChangingChildren != null && |
| 6258 | mVisibilityChangingChildren.contains(view)) { |
| 6259 | mVisibilityChangingChildren.remove(view); |
| 6260 | } else { |
| 6261 | if (view.mAttachInfo != null) { |
| 6262 | view.dispatchDetachedFromWindow(); |
| 6263 | } |
| 6264 | if (view.mParent != null) { |
| 6265 | view.mParent = null; |
| 6266 | } |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 6267 | } |
Chet Haase | b85967b | 2012-03-26 14:37:51 -0700 | [diff] [blame] | 6268 | invalidate(); |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 6269 | } |
| 6270 | } |
| 6271 | } |
| 6272 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 6273 | private LayoutTransition.TransitionListener mLayoutTransitionListener = |
| 6274 | new LayoutTransition.TransitionListener() { |
| 6275 | @Override |
| 6276 | public void startTransition(LayoutTransition transition, ViewGroup container, |
| 6277 | View view, int transitionType) { |
| 6278 | // We only care about disappearing items, since we need special logic to keep |
| 6279 | // those items visible after they've been 'removed' |
| 6280 | if (transitionType == LayoutTransition.DISAPPEARING) { |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 6281 | startViewTransition(view); |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 6282 | } |
| 6283 | } |
| 6284 | |
| 6285 | @Override |
| 6286 | public void endTransition(LayoutTransition transition, ViewGroup container, |
| 6287 | View view, int transitionType) { |
Chet Haase | b989502 | 2013-04-02 15:10:58 -0700 | [diff] [blame] | 6288 | if (mLayoutCalledWhileSuppressed && !transition.isChangingLayout()) { |
Chet Haase | 9c08744 | 2011-01-12 16:20:16 -0800 | [diff] [blame] | 6289 | requestLayout(); |
Chet Haase | b989502 | 2013-04-02 15:10:58 -0700 | [diff] [blame] | 6290 | mLayoutCalledWhileSuppressed = false; |
Chet Haase | 9c08744 | 2011-01-12 16:20:16 -0800 | [diff] [blame] | 6291 | } |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 6292 | if (transitionType == LayoutTransition.DISAPPEARING && mTransitioningViews != null) { |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 6293 | endViewTransition(view); |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 6294 | } |
| 6295 | } |
| 6296 | }; |
| 6297 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6298 | /** |
Chet Haase | b989502 | 2013-04-02 15:10:58 -0700 | [diff] [blame] | 6299 | * Tells this ViewGroup to suppress all layout() calls until layout |
| 6300 | * suppression is disabled with a later call to suppressLayout(false). |
| 6301 | * When layout suppression is disabled, a requestLayout() call is sent |
| 6302 | * if layout() was attempted while layout was being suppressed. |
| 6303 | * |
| 6304 | * @hide |
| 6305 | */ |
| 6306 | public void suppressLayout(boolean suppress) { |
| 6307 | mSuppressLayout = suppress; |
| 6308 | if (!suppress) { |
| 6309 | if (mLayoutCalledWhileSuppressed) { |
| 6310 | requestLayout(); |
| 6311 | mLayoutCalledWhileSuppressed = false; |
| 6312 | } |
| 6313 | } |
| 6314 | } |
| 6315 | |
| 6316 | /** |
Chet Haase | 199acdf | 2013-07-24 18:40:55 -0700 | [diff] [blame] | 6317 | * Returns whether layout calls on this container are currently being |
| 6318 | * suppressed, due to an earlier call to {@link #suppressLayout(boolean)}. |
| 6319 | * |
| 6320 | * @return true if layout calls are currently suppressed, false otherwise. |
| 6321 | * |
| 6322 | * @hide |
| 6323 | */ |
| 6324 | public boolean isLayoutSuppressed() { |
| 6325 | return mSuppressLayout; |
| 6326 | } |
| 6327 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6328 | @Override |
| 6329 | public boolean gatherTransparentRegion(Region region) { |
| 6330 | // If no transparent regions requested, we are always opaque. |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 6331 | 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] | 6332 | if (meOpaque && region == null) { |
| 6333 | // The caller doesn't care about the region, so stop now. |
| 6334 | return true; |
| 6335 | } |
| 6336 | super.gatherTransparentRegion(region); |
| 6337 | final View[] children = mChildren; |
| 6338 | final int count = mChildrenCount; |
| 6339 | boolean noneOfTheChildrenAreTransparent = true; |
| 6340 | for (int i = 0; i < count; i++) { |
| 6341 | final View child = children[i]; |
Mathias Agopian | e338115 | 2010-12-02 15:19:36 -0800 | [diff] [blame] | 6342 | if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6343 | if (!child.gatherTransparentRegion(region)) { |
| 6344 | noneOfTheChildrenAreTransparent = false; |
| 6345 | } |
| 6346 | } |
| 6347 | } |
| 6348 | return meOpaque || noneOfTheChildrenAreTransparent; |
| 6349 | } |
| 6350 | |
Alan Viverette | be463f2 | 2016-01-21 10:50:10 -0500 | [diff] [blame] | 6351 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6352 | public void requestTransparentRegion(View child) { |
| 6353 | if (child != null) { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 6354 | child.mPrivateFlags |= View.PFLAG_REQUEST_TRANSPARENT_REGIONS; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6355 | if (mParent != null) { |
| 6356 | mParent.requestTransparentRegion(this); |
| 6357 | } |
| 6358 | } |
| 6359 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 6360 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6361 | @Override |
Adam Powell | 46e38fd | 2014-02-03 10:16:49 -0800 | [diff] [blame] | 6362 | public WindowInsets dispatchApplyWindowInsets(WindowInsets insets) { |
| 6363 | insets = super.dispatchApplyWindowInsets(insets); |
Adam Powell | 0d9fdba | 2014-06-11 15:33:08 -0700 | [diff] [blame] | 6364 | if (!insets.isConsumed()) { |
Adam Powell | 46e38fd | 2014-02-03 10:16:49 -0800 | [diff] [blame] | 6365 | final int count = getChildCount(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6366 | for (int i = 0; i < count; i++) { |
Adam Powell | 46e38fd | 2014-02-03 10:16:49 -0800 | [diff] [blame] | 6367 | insets = getChildAt(i).dispatchApplyWindowInsets(insets); |
Adam Powell | 0d9fdba | 2014-06-11 15:33:08 -0700 | [diff] [blame] | 6368 | if (insets.isConsumed()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6369 | break; |
| 6370 | } |
| 6371 | } |
| 6372 | } |
Adam Powell | 46e38fd | 2014-02-03 10:16:49 -0800 | [diff] [blame] | 6373 | return insets; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6374 | } |
| 6375 | |
| 6376 | /** |
| 6377 | * Returns the animation listener to which layout animation events are |
| 6378 | * sent. |
| 6379 | * |
| 6380 | * @return an {@link android.view.animation.Animation.AnimationListener} |
| 6381 | */ |
| 6382 | public Animation.AnimationListener getLayoutAnimationListener() { |
| 6383 | return mAnimationListener; |
| 6384 | } |
| 6385 | |
| 6386 | @Override |
| 6387 | protected void drawableStateChanged() { |
| 6388 | super.drawableStateChanged(); |
| 6389 | |
| 6390 | if ((mGroupFlags & FLAG_NOTIFY_CHILDREN_ON_DRAWABLE_STATE_CHANGE) != 0) { |
| 6391 | if ((mGroupFlags & FLAG_ADD_STATES_FROM_CHILDREN) != 0) { |
| 6392 | throw new IllegalStateException("addStateFromChildren cannot be enabled if a" |
| 6393 | + " child has duplicateParentState set to true"); |
| 6394 | } |
| 6395 | |
| 6396 | final View[] children = mChildren; |
| 6397 | final int count = mChildrenCount; |
| 6398 | |
| 6399 | for (int i = 0; i < count; i++) { |
| 6400 | final View child = children[i]; |
| 6401 | if ((child.mViewFlags & DUPLICATE_PARENT_STATE) != 0) { |
| 6402 | child.refreshDrawableState(); |
| 6403 | } |
| 6404 | } |
| 6405 | } |
| 6406 | } |
| 6407 | |
| 6408 | @Override |
Dianne Hackborn | e213677 | 2010-11-04 15:08:59 -0700 | [diff] [blame] | 6409 | public void jumpDrawablesToCurrentState() { |
| 6410 | super.jumpDrawablesToCurrentState(); |
| 6411 | final View[] children = mChildren; |
| 6412 | final int count = mChildrenCount; |
| 6413 | for (int i = 0; i < count; i++) { |
| 6414 | children[i].jumpDrawablesToCurrentState(); |
| 6415 | } |
| 6416 | } |
| 6417 | |
| 6418 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6419 | protected int[] onCreateDrawableState(int extraSpace) { |
| 6420 | if ((mGroupFlags & FLAG_ADD_STATES_FROM_CHILDREN) == 0) { |
| 6421 | return super.onCreateDrawableState(extraSpace); |
| 6422 | } |
| 6423 | |
| 6424 | int need = 0; |
| 6425 | int n = getChildCount(); |
| 6426 | for (int i = 0; i < n; i++) { |
| 6427 | int[] childState = getChildAt(i).getDrawableState(); |
| 6428 | |
| 6429 | if (childState != null) { |
| 6430 | need += childState.length; |
| 6431 | } |
| 6432 | } |
| 6433 | |
| 6434 | int[] state = super.onCreateDrawableState(extraSpace + need); |
| 6435 | |
| 6436 | for (int i = 0; i < n; i++) { |
| 6437 | int[] childState = getChildAt(i).getDrawableState(); |
| 6438 | |
| 6439 | if (childState != null) { |
| 6440 | state = mergeDrawableStates(state, childState); |
| 6441 | } |
| 6442 | } |
| 6443 | |
| 6444 | return state; |
| 6445 | } |
| 6446 | |
| 6447 | /** |
| 6448 | * Sets whether this ViewGroup's drawable states also include |
| 6449 | * its children's drawable states. This is used, for example, to |
| 6450 | * make a group appear to be focused when its child EditText or button |
| 6451 | * is focused. |
| 6452 | */ |
| 6453 | public void setAddStatesFromChildren(boolean addsStates) { |
| 6454 | if (addsStates) { |
| 6455 | mGroupFlags |= FLAG_ADD_STATES_FROM_CHILDREN; |
| 6456 | } else { |
| 6457 | mGroupFlags &= ~FLAG_ADD_STATES_FROM_CHILDREN; |
| 6458 | } |
| 6459 | |
| 6460 | refreshDrawableState(); |
| 6461 | } |
| 6462 | |
| 6463 | /** |
| 6464 | * Returns whether this ViewGroup's drawable states also include |
| 6465 | * its children's drawable states. This is used, for example, to |
| 6466 | * make a group appear to be focused when its child EditText or button |
| 6467 | * is focused. |
| 6468 | */ |
| 6469 | public boolean addStatesFromChildren() { |
| 6470 | return (mGroupFlags & FLAG_ADD_STATES_FROM_CHILDREN) != 0; |
| 6471 | } |
| 6472 | |
| 6473 | /** |
Jeff Smith | a45746e | 2012-07-19 14:19:24 -0500 | [diff] [blame] | 6474 | * If {@link #addStatesFromChildren} is true, refreshes this group's |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6475 | * drawable state (to include the states from its children). |
| 6476 | */ |
Alan Viverette | be463f2 | 2016-01-21 10:50:10 -0500 | [diff] [blame] | 6477 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6478 | public void childDrawableStateChanged(View child) { |
| 6479 | if ((mGroupFlags & FLAG_ADD_STATES_FROM_CHILDREN) != 0) { |
| 6480 | refreshDrawableState(); |
| 6481 | } |
| 6482 | } |
| 6483 | |
| 6484 | /** |
| 6485 | * Specifies the animation listener to which layout animation events must |
| 6486 | * be sent. Only |
| 6487 | * {@link android.view.animation.Animation.AnimationListener#onAnimationStart(Animation)} |
| 6488 | * and |
| 6489 | * {@link android.view.animation.Animation.AnimationListener#onAnimationEnd(Animation)} |
| 6490 | * are invoked. |
| 6491 | * |
| 6492 | * @param animationListener the layout animation listener |
| 6493 | */ |
| 6494 | public void setLayoutAnimationListener(Animation.AnimationListener animationListener) { |
| 6495 | mAnimationListener = animationListener; |
| 6496 | } |
| 6497 | |
| 6498 | /** |
Chet Haase | cca2c98 | 2011-05-20 14:34:18 -0700 | [diff] [blame] | 6499 | * This method is called by LayoutTransition when there are 'changing' animations that need |
| 6500 | * to start after the layout/setup phase. The request is forwarded to the ViewAncestor, who |
| 6501 | * starts all pending transitions prior to the drawing phase in the current traversal. |
| 6502 | * |
| 6503 | * @param transition The LayoutTransition to be started on the next traversal. |
| 6504 | * |
| 6505 | * @hide |
| 6506 | */ |
| 6507 | public void requestTransitionStart(LayoutTransition transition) { |
Dianne Hackborn | 6dd005b | 2011-07-18 13:22:50 -0700 | [diff] [blame] | 6508 | ViewRootImpl viewAncestor = getViewRootImpl(); |
Chet Haase | 1abf7fa | 2011-08-17 18:31:56 -0700 | [diff] [blame] | 6509 | if (viewAncestor != null) { |
| 6510 | viewAncestor.requestTransitionStart(transition); |
| 6511 | } |
Chet Haase | cca2c98 | 2011-05-20 14:34:18 -0700 | [diff] [blame] | 6512 | } |
| 6513 | |
Fabrice Di Meglio | 4457e85 | 2012-09-18 19:23:12 -0700 | [diff] [blame] | 6514 | /** |
| 6515 | * @hide |
| 6516 | */ |
Fabrice Di Meglio | 80dc53d | 2011-06-21 18:36:33 -0700 | [diff] [blame] | 6517 | @Override |
Fabrice Di Meglio | 09ecb25 | 2013-05-03 16:51:55 -0700 | [diff] [blame] | 6518 | public boolean resolveRtlPropertiesIfNeeded() { |
| 6519 | final boolean result = super.resolveRtlPropertiesIfNeeded(); |
| 6520 | // We dont need to resolve the children RTL properties if nothing has changed for the parent |
| 6521 | if (result) { |
| 6522 | int count = getChildCount(); |
| 6523 | for (int i = 0; i < count; i++) { |
| 6524 | final View child = getChildAt(i); |
| 6525 | if (child.isLayoutDirectionInherited()) { |
| 6526 | child.resolveRtlPropertiesIfNeeded(); |
| 6527 | } |
Fabrice Di Meglio | 84ebb35 | 2012-10-11 16:27:37 -0700 | [diff] [blame] | 6528 | } |
| 6529 | } |
Fabrice Di Meglio | 09ecb25 | 2013-05-03 16:51:55 -0700 | [diff] [blame] | 6530 | return result; |
Fabrice Di Meglio | 84ebb35 | 2012-10-11 16:27:37 -0700 | [diff] [blame] | 6531 | } |
| 6532 | |
| 6533 | /** |
| 6534 | * @hide |
| 6535 | */ |
| 6536 | @Override |
Fabrice Di Meglio | 9a04856 | 2012-09-26 14:55:56 -0700 | [diff] [blame] | 6537 | public boolean resolveLayoutDirection() { |
| 6538 | final boolean result = super.resolveLayoutDirection(); |
| 6539 | if (result) { |
| 6540 | int count = getChildCount(); |
| 6541 | for (int i = 0; i < count; i++) { |
| 6542 | final View child = getChildAt(i); |
| 6543 | if (child.isLayoutDirectionInherited()) { |
| 6544 | child.resolveLayoutDirection(); |
| 6545 | } |
Fabrice Di Meglio | 1f88ba8 | 2012-09-24 14:56:49 -0700 | [diff] [blame] | 6546 | } |
| 6547 | } |
Fabrice Di Meglio | 9a04856 | 2012-09-26 14:55:56 -0700 | [diff] [blame] | 6548 | return result; |
Fabrice Di Meglio | 1f88ba8 | 2012-09-24 14:56:49 -0700 | [diff] [blame] | 6549 | } |
| 6550 | |
| 6551 | /** |
| 6552 | * @hide |
| 6553 | */ |
| 6554 | @Override |
Fabrice Di Meglio | 9a04856 | 2012-09-26 14:55:56 -0700 | [diff] [blame] | 6555 | public boolean resolveTextDirection() { |
| 6556 | final boolean result = super.resolveTextDirection(); |
| 6557 | if (result) { |
| 6558 | int count = getChildCount(); |
| 6559 | for (int i = 0; i < count; i++) { |
| 6560 | final View child = getChildAt(i); |
| 6561 | if (child.isTextDirectionInherited()) { |
| 6562 | child.resolveTextDirection(); |
| 6563 | } |
Fabrice Di Meglio | 1f88ba8 | 2012-09-24 14:56:49 -0700 | [diff] [blame] | 6564 | } |
| 6565 | } |
Fabrice Di Meglio | 9a04856 | 2012-09-26 14:55:56 -0700 | [diff] [blame] | 6566 | return result; |
Fabrice Di Meglio | 1f88ba8 | 2012-09-24 14:56:49 -0700 | [diff] [blame] | 6567 | } |
| 6568 | |
| 6569 | /** |
| 6570 | * @hide |
| 6571 | */ |
| 6572 | @Override |
Fabrice Di Meglio | 9a04856 | 2012-09-26 14:55:56 -0700 | [diff] [blame] | 6573 | public boolean resolveTextAlignment() { |
| 6574 | final boolean result = super.resolveTextAlignment(); |
| 6575 | if (result) { |
| 6576 | int count = getChildCount(); |
| 6577 | for (int i = 0; i < count; i++) { |
| 6578 | final View child = getChildAt(i); |
| 6579 | if (child.isTextAlignmentInherited()) { |
| 6580 | child.resolveTextAlignment(); |
| 6581 | } |
Fabrice Di Meglio | 1f88ba8 | 2012-09-24 14:56:49 -0700 | [diff] [blame] | 6582 | } |
| 6583 | } |
Fabrice Di Meglio | 9a04856 | 2012-09-26 14:55:56 -0700 | [diff] [blame] | 6584 | return result; |
Fabrice Di Meglio | 1f88ba8 | 2012-09-24 14:56:49 -0700 | [diff] [blame] | 6585 | } |
| 6586 | |
| 6587 | /** |
| 6588 | * @hide |
| 6589 | */ |
| 6590 | @Override |
Fabrice Di Meglio | 84ebb35 | 2012-10-11 16:27:37 -0700 | [diff] [blame] | 6591 | public void resolvePadding() { |
| 6592 | super.resolvePadding(); |
| 6593 | int count = getChildCount(); |
| 6594 | for (int i = 0; i < count; i++) { |
| 6595 | final View child = getChildAt(i); |
Adam Powell | 05f3512 | 2014-11-10 17:47:37 -0800 | [diff] [blame] | 6596 | if (child.isLayoutDirectionInherited() && !child.isPaddingResolved()) { |
Fabrice Di Meglio | 84ebb35 | 2012-10-11 16:27:37 -0700 | [diff] [blame] | 6597 | child.resolvePadding(); |
| 6598 | } |
| 6599 | } |
| 6600 | } |
| 6601 | |
| 6602 | /** |
| 6603 | * @hide |
| 6604 | */ |
| 6605 | @Override |
| 6606 | protected void resolveDrawables() { |
| 6607 | super.resolveDrawables(); |
| 6608 | int count = getChildCount(); |
| 6609 | for (int i = 0; i < count; i++) { |
| 6610 | final View child = getChildAt(i); |
Adam Powell | 05f3512 | 2014-11-10 17:47:37 -0800 | [diff] [blame] | 6611 | if (child.isLayoutDirectionInherited() && !child.areDrawablesResolved()) { |
Fabrice Di Meglio | 84ebb35 | 2012-10-11 16:27:37 -0700 | [diff] [blame] | 6612 | child.resolveDrawables(); |
| 6613 | } |
| 6614 | } |
| 6615 | } |
| 6616 | |
Fabrice Di Meglio | 1e0ed6b | 2012-10-18 16:06:52 -0700 | [diff] [blame] | 6617 | /** |
| 6618 | * @hide |
| 6619 | */ |
Fabrice Di Meglio | fcc3348 | 2012-10-18 11:11:51 -0700 | [diff] [blame] | 6620 | @Override |
| 6621 | public void resolveLayoutParams() { |
| 6622 | super.resolveLayoutParams(); |
| 6623 | int count = getChildCount(); |
| 6624 | for (int i = 0; i < count; i++) { |
| 6625 | final View child = getChildAt(i); |
| 6626 | child.resolveLayoutParams(); |
| 6627 | } |
| 6628 | } |
| 6629 | |
Fabrice Di Meglio | 84ebb35 | 2012-10-11 16:27:37 -0700 | [diff] [blame] | 6630 | /** |
| 6631 | * @hide |
| 6632 | */ |
| 6633 | @Override |
Fabrice Di Meglio | 4457e85 | 2012-09-18 19:23:12 -0700 | [diff] [blame] | 6634 | public void resetResolvedLayoutDirection() { |
| 6635 | super.resetResolvedLayoutDirection(); |
| 6636 | |
Fabrice Di Meglio | 4457e85 | 2012-09-18 19:23:12 -0700 | [diff] [blame] | 6637 | int count = getChildCount(); |
Fabrice Di Meglio | 80dc53d | 2011-06-21 18:36:33 -0700 | [diff] [blame] | 6638 | for (int i = 0; i < count; i++) { |
| 6639 | final View child = getChildAt(i); |
Fabrice Di Meglio | e56ffdc | 2012-09-23 14:51:16 -0700 | [diff] [blame] | 6640 | if (child.isLayoutDirectionInherited()) { |
Fabrice Di Meglio | 7f86c80 | 2011-07-01 15:09:24 -0700 | [diff] [blame] | 6641 | child.resetResolvedLayoutDirection(); |
Fabrice Di Meglio | 80dc53d | 2011-06-21 18:36:33 -0700 | [diff] [blame] | 6642 | } |
Fabrice Di Meglio | 1f88ba8 | 2012-09-24 14:56:49 -0700 | [diff] [blame] | 6643 | } |
| 6644 | } |
| 6645 | |
| 6646 | /** |
| 6647 | * @hide |
| 6648 | */ |
| 6649 | @Override |
| 6650 | public void resetResolvedTextDirection() { |
| 6651 | super.resetResolvedTextDirection(); |
| 6652 | |
| 6653 | int count = getChildCount(); |
| 6654 | for (int i = 0; i < count; i++) { |
| 6655 | final View child = getChildAt(i); |
Fabrice Di Meglio | 97e146c | 2012-09-23 15:45:16 -0700 | [diff] [blame] | 6656 | if (child.isTextDirectionInherited()) { |
Fabrice Di Meglio | 2226886 | 2011-06-27 18:13:18 -0700 | [diff] [blame] | 6657 | child.resetResolvedTextDirection(); |
| 6658 | } |
Fabrice Di Meglio | 1f88ba8 | 2012-09-24 14:56:49 -0700 | [diff] [blame] | 6659 | } |
| 6660 | } |
| 6661 | |
| 6662 | /** |
| 6663 | * @hide |
| 6664 | */ |
| 6665 | @Override |
| 6666 | public void resetResolvedTextAlignment() { |
| 6667 | super.resetResolvedTextAlignment(); |
| 6668 | |
| 6669 | int count = getChildCount(); |
| 6670 | for (int i = 0; i < count; i++) { |
| 6671 | final View child = getChildAt(i); |
Fabrice Di Meglio | 1a7d487 | 2012-09-23 16:19:58 -0700 | [diff] [blame] | 6672 | if (child.isTextAlignmentInherited()) { |
Fabrice Di Meglio | 9da0f8a | 2012-03-13 19:37:57 -0700 | [diff] [blame] | 6673 | child.resetResolvedTextAlignment(); |
| 6674 | } |
| 6675 | } |
| 6676 | } |
| 6677 | |
Fabrice Di Meglio | 2226886 | 2011-06-27 18:13:18 -0700 | [diff] [blame] | 6678 | /** |
Fabrice Di Meglio | 84ebb35 | 2012-10-11 16:27:37 -0700 | [diff] [blame] | 6679 | * @hide |
| 6680 | */ |
| 6681 | @Override |
| 6682 | public void resetResolvedPadding() { |
| 6683 | super.resetResolvedPadding(); |
| 6684 | |
| 6685 | int count = getChildCount(); |
| 6686 | for (int i = 0; i < count; i++) { |
| 6687 | final View child = getChildAt(i); |
| 6688 | if (child.isLayoutDirectionInherited()) { |
| 6689 | child.resetResolvedPadding(); |
| 6690 | } |
| 6691 | } |
| 6692 | } |
| 6693 | |
| 6694 | /** |
| 6695 | * @hide |
| 6696 | */ |
| 6697 | @Override |
| 6698 | protected void resetResolvedDrawables() { |
| 6699 | super.resetResolvedDrawables(); |
| 6700 | |
| 6701 | int count = getChildCount(); |
| 6702 | for (int i = 0; i < count; i++) { |
| 6703 | final View child = getChildAt(i); |
| 6704 | if (child.isLayoutDirectionInherited()) { |
| 6705 | child.resetResolvedDrawables(); |
| 6706 | } |
| 6707 | } |
| 6708 | } |
| 6709 | |
| 6710 | /** |
Patrick Dubroy | e0a799a | 2011-05-04 16:19:22 -0700 | [diff] [blame] | 6711 | * Return true if the pressed state should be delayed for children or descendants of this |
| 6712 | * ViewGroup. Generally, this should be done for containers that can scroll, such as a List. |
| 6713 | * This prevents the pressed state from appearing when the user is actually trying to scroll |
| 6714 | * the content. |
| 6715 | * |
| 6716 | * The default implementation returns true for compatibility reasons. Subclasses that do |
| 6717 | * not scroll should generally override this method and return false. |
| 6718 | */ |
| 6719 | public boolean shouldDelayChildPressedState() { |
| 6720 | return true; |
| 6721 | } |
| 6722 | |
Adam Powell | 10ba277 | 2014-04-15 09:46:51 -0700 | [diff] [blame] | 6723 | /** |
| 6724 | * @inheritDoc |
| 6725 | */ |
| 6726 | @Override |
| 6727 | public boolean onStartNestedScroll(View child, View target, int nestedScrollAxes) { |
| 6728 | return false; |
| 6729 | } |
| 6730 | |
| 6731 | /** |
| 6732 | * @inheritDoc |
| 6733 | */ |
| 6734 | @Override |
| 6735 | public void onNestedScrollAccepted(View child, View target, int axes) { |
| 6736 | mNestedScrollAxes = axes; |
| 6737 | } |
| 6738 | |
| 6739 | /** |
| 6740 | * @inheritDoc |
| 6741 | * |
| 6742 | * <p>The default implementation of onStopNestedScroll calls |
| 6743 | * {@link #stopNestedScroll()} to halt any recursive nested scrolling in progress.</p> |
| 6744 | */ |
| 6745 | @Override |
| 6746 | public void onStopNestedScroll(View child) { |
| 6747 | // Stop any recursive nested scrolling. |
| 6748 | stopNestedScroll(); |
Adam Powell | d4a22d4 | 2015-04-16 15:44:10 -0700 | [diff] [blame] | 6749 | mNestedScrollAxes = 0; |
Adam Powell | 10ba277 | 2014-04-15 09:46:51 -0700 | [diff] [blame] | 6750 | } |
| 6751 | |
| 6752 | /** |
| 6753 | * @inheritDoc |
| 6754 | */ |
| 6755 | @Override |
| 6756 | public void onNestedScroll(View target, int dxConsumed, int dyConsumed, |
| 6757 | int dxUnconsumed, int dyUnconsumed) { |
Chris Banes | 61d5070 | 2016-03-23 13:11:45 +0000 | [diff] [blame] | 6758 | // Re-dispatch up the tree by default |
| 6759 | dispatchNestedScroll(dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed, null); |
Adam Powell | 10ba277 | 2014-04-15 09:46:51 -0700 | [diff] [blame] | 6760 | } |
| 6761 | |
| 6762 | /** |
| 6763 | * @inheritDoc |
| 6764 | */ |
| 6765 | @Override |
| 6766 | public void onNestedPreScroll(View target, int dx, int dy, int[] consumed) { |
Chris Banes | 61d5070 | 2016-03-23 13:11:45 +0000 | [diff] [blame] | 6767 | // Re-dispatch up the tree by default |
| 6768 | dispatchNestedPreScroll(dx, dy, consumed, null); |
Adam Powell | 10ba277 | 2014-04-15 09:46:51 -0700 | [diff] [blame] | 6769 | } |
| 6770 | |
| 6771 | /** |
| 6772 | * @inheritDoc |
| 6773 | */ |
| 6774 | @Override |
Adam Powell | b36e4f9 | 2014-05-01 10:23:33 -0700 | [diff] [blame] | 6775 | public boolean onNestedFling(View target, float velocityX, float velocityY, boolean consumed) { |
Chris Banes | 61d5070 | 2016-03-23 13:11:45 +0000 | [diff] [blame] | 6776 | // Re-dispatch up the tree by default |
| 6777 | return dispatchNestedFling(velocityX, velocityY, consumed); |
Adam Powell | 10ba277 | 2014-04-15 09:46:51 -0700 | [diff] [blame] | 6778 | } |
| 6779 | |
| 6780 | /** |
Adam Powell | b72be59 | 2014-07-16 21:41:31 -0700 | [diff] [blame] | 6781 | * @inheritDoc |
| 6782 | */ |
| 6783 | @Override |
| 6784 | public boolean onNestedPreFling(View target, float velocityX, float velocityY) { |
Chris Banes | 61d5070 | 2016-03-23 13:11:45 +0000 | [diff] [blame] | 6785 | // Re-dispatch up the tree by default |
| 6786 | return dispatchNestedPreFling(velocityX, velocityY); |
Adam Powell | b72be59 | 2014-07-16 21:41:31 -0700 | [diff] [blame] | 6787 | } |
| 6788 | |
| 6789 | /** |
Adam Powell | 10ba277 | 2014-04-15 09:46:51 -0700 | [diff] [blame] | 6790 | * Return the current axes of nested scrolling for this ViewGroup. |
| 6791 | * |
| 6792 | * <p>A ViewGroup returning something other than {@link #SCROLL_AXIS_NONE} is currently |
| 6793 | * acting as a nested scrolling parent for one or more descendant views in the hierarchy.</p> |
| 6794 | * |
| 6795 | * @return Flags indicating the current axes of nested scrolling |
| 6796 | * @see #SCROLL_AXIS_HORIZONTAL |
| 6797 | * @see #SCROLL_AXIS_VERTICAL |
| 6798 | * @see #SCROLL_AXIS_NONE |
| 6799 | */ |
| 6800 | public int getNestedScrollAxes() { |
| 6801 | return mNestedScrollAxes; |
| 6802 | } |
| 6803 | |
Philip Milne | d7dd890 | 2012-01-26 16:55:30 -0800 | [diff] [blame] | 6804 | /** @hide */ |
| 6805 | protected void onSetLayoutParams(View child, LayoutParams layoutParams) { |
| 6806 | } |
| 6807 | |
George Mount | e180337 | 2014-02-26 19:00:52 +0000 | [diff] [blame] | 6808 | /** @hide */ |
| 6809 | @Override |
| 6810 | public void captureTransitioningViews(List<View> transitioningViews) { |
| 6811 | if (getVisibility() != View.VISIBLE) { |
| 6812 | return; |
| 6813 | } |
| 6814 | if (isTransitionGroup()) { |
| 6815 | transitioningViews.add(this); |
| 6816 | } else { |
| 6817 | int count = getChildCount(); |
| 6818 | for (int i = 0; i < count; i++) { |
| 6819 | View child = getChildAt(i); |
| 6820 | child.captureTransitioningViews(transitioningViews); |
| 6821 | } |
| 6822 | } |
| 6823 | } |
| 6824 | |
| 6825 | /** @hide */ |
| 6826 | @Override |
George Mount | abb352a | 2014-05-09 10:27:20 -0700 | [diff] [blame] | 6827 | public void findNamedViews(Map<String, View> namedElements) { |
George Mount | fe361d2 | 2014-07-08 17:25:25 -0700 | [diff] [blame] | 6828 | if (getVisibility() != VISIBLE && mGhostView == null) { |
George Mount | e180337 | 2014-02-26 19:00:52 +0000 | [diff] [blame] | 6829 | return; |
| 6830 | } |
George Mount | abb352a | 2014-05-09 10:27:20 -0700 | [diff] [blame] | 6831 | super.findNamedViews(namedElements); |
George Mount | e180337 | 2014-02-26 19:00:52 +0000 | [diff] [blame] | 6832 | int count = getChildCount(); |
| 6833 | for (int i = 0; i < count; i++) { |
| 6834 | View child = getChildAt(i); |
George Mount | abb352a | 2014-05-09 10:27:20 -0700 | [diff] [blame] | 6835 | child.findNamedViews(namedElements); |
George Mount | e180337 | 2014-02-26 19:00:52 +0000 | [diff] [blame] | 6836 | } |
| 6837 | } |
| 6838 | |
Patrick Dubroy | e0a799a | 2011-05-04 16:19:22 -0700 | [diff] [blame] | 6839 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6840 | * LayoutParams are used by views to tell their parents how they want to be |
| 6841 | * laid out. See |
| 6842 | * {@link android.R.styleable#ViewGroup_Layout ViewGroup Layout Attributes} |
| 6843 | * for a list of all child view attributes that this class supports. |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 6844 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6845 | * <p> |
| 6846 | * The base LayoutParams class just describes how big the view wants to be |
| 6847 | * for both width and height. For each dimension, it can specify one of: |
| 6848 | * <ul> |
Dirk Dougherty | 75c66da | 2010-03-25 16:33:33 -0700 | [diff] [blame] | 6849 | * <li>FILL_PARENT (renamed MATCH_PARENT in API Level 8 and higher), which |
| 6850 | * means that the view wants to be as big as its parent (minus padding) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6851 | * <li> WRAP_CONTENT, which means that the view wants to be just big enough |
| 6852 | * to enclose its content (plus padding) |
Dirk Dougherty | 75c66da | 2010-03-25 16:33:33 -0700 | [diff] [blame] | 6853 | * <li> an exact number |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6854 | * </ul> |
| 6855 | * There are subclasses of LayoutParams for different subclasses of |
| 6856 | * ViewGroup. For example, AbsoluteLayout has its own subclass of |
Joe Fernandez | 558459f | 2011-10-13 16:47:36 -0700 | [diff] [blame] | 6857 | * LayoutParams which adds an X and Y value.</p> |
| 6858 | * |
| 6859 | * <div class="special reference"> |
| 6860 | * <h3>Developer Guides</h3> |
| 6861 | * <p>For more information about creating user interface layouts, read the |
| 6862 | * <a href="{@docRoot}guide/topics/ui/declaring-layout.html">XML Layouts</a> developer |
| 6863 | * guide.</p></div> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6864 | * |
| 6865 | * @attr ref android.R.styleable#ViewGroup_Layout_layout_height |
| 6866 | * @attr ref android.R.styleable#ViewGroup_Layout_layout_width |
| 6867 | */ |
| 6868 | public static class LayoutParams { |
| 6869 | /** |
Dirk Dougherty | 75c66da | 2010-03-25 16:33:33 -0700 | [diff] [blame] | 6870 | * Special value for the height or width requested by a View. |
| 6871 | * FILL_PARENT means that the view wants to be as big as its parent, |
| 6872 | * minus the parent's padding, if any. This value is deprecated |
| 6873 | * starting in API Level 8 and replaced by {@link #MATCH_PARENT}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6874 | */ |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 6875 | @SuppressWarnings({"UnusedDeclaration"}) |
| 6876 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6877 | public static final int FILL_PARENT = -1; |
| 6878 | |
| 6879 | /** |
| 6880 | * Special value for the height or width requested by a View. |
Gilles Debunne | f5c6eff | 2010-02-09 19:08:36 -0800 | [diff] [blame] | 6881 | * 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] | 6882 | * minus the parent's padding, if any. Introduced in API Level 8. |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 6883 | */ |
| 6884 | public static final int MATCH_PARENT = -1; |
| 6885 | |
| 6886 | /** |
| 6887 | * Special value for the height or width requested by a View. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6888 | * WRAP_CONTENT means that the view wants to be just large enough to fit |
| 6889 | * its own internal content, taking its own padding into account. |
| 6890 | */ |
| 6891 | public static final int WRAP_CONTENT = -2; |
| 6892 | |
| 6893 | /** |
Dirk Dougherty | 75c66da | 2010-03-25 16:33:33 -0700 | [diff] [blame] | 6894 | * 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] | 6895 | * constants FILL_PARENT (replaced by MATCH_PARENT |
| 6896 | * 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] | 6897 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 6898 | @ViewDebug.ExportedProperty(category = "layout", mapping = { |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 6899 | @ViewDebug.IntToString(from = MATCH_PARENT, to = "MATCH_PARENT"), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6900 | @ViewDebug.IntToString(from = WRAP_CONTENT, to = "WRAP_CONTENT") |
| 6901 | }) |
| 6902 | public int width; |
| 6903 | |
| 6904 | /** |
Dirk Dougherty | 75c66da | 2010-03-25 16:33:33 -0700 | [diff] [blame] | 6905 | * 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] | 6906 | * constants FILL_PARENT (replaced by MATCH_PARENT |
| 6907 | * 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] | 6908 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 6909 | @ViewDebug.ExportedProperty(category = "layout", mapping = { |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 6910 | @ViewDebug.IntToString(from = MATCH_PARENT, to = "MATCH_PARENT"), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6911 | @ViewDebug.IntToString(from = WRAP_CONTENT, to = "WRAP_CONTENT") |
| 6912 | }) |
| 6913 | public int height; |
| 6914 | |
| 6915 | /** |
| 6916 | * Used to animate layouts. |
| 6917 | */ |
| 6918 | public LayoutAnimationController.AnimationParameters layoutAnimationParameters; |
| 6919 | |
| 6920 | /** |
| 6921 | * Creates a new set of layout parameters. The values are extracted from |
| 6922 | * the supplied attributes set and context. The XML attributes mapped |
| 6923 | * to this set of layout parameters are: |
| 6924 | * |
| 6925 | * <ul> |
| 6926 | * <li><code>layout_width</code>: the width, either an exact value, |
Dirk Dougherty | 75c66da | 2010-03-25 16:33:33 -0700 | [diff] [blame] | 6927 | * {@link #WRAP_CONTENT}, or {@link #FILL_PARENT} (replaced by |
| 6928 | * {@link #MATCH_PARENT} in API Level 8)</li> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6929 | * <li><code>layout_height</code>: the height, either an exact value, |
Dirk Dougherty | 75c66da | 2010-03-25 16:33:33 -0700 | [diff] [blame] | 6930 | * {@link #WRAP_CONTENT}, or {@link #FILL_PARENT} (replaced by |
| 6931 | * {@link #MATCH_PARENT} in API Level 8)</li> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6932 | * </ul> |
| 6933 | * |
| 6934 | * @param c the application environment |
| 6935 | * @param attrs the set of attributes from which to extract the layout |
| 6936 | * parameters' values |
| 6937 | */ |
| 6938 | public LayoutParams(Context c, AttributeSet attrs) { |
| 6939 | TypedArray a = c.obtainStyledAttributes(attrs, R.styleable.ViewGroup_Layout); |
| 6940 | setBaseAttributes(a, |
| 6941 | R.styleable.ViewGroup_Layout_layout_width, |
| 6942 | R.styleable.ViewGroup_Layout_layout_height); |
| 6943 | a.recycle(); |
| 6944 | } |
| 6945 | |
| 6946 | /** |
| 6947 | * Creates a new set of layout parameters with the specified width |
| 6948 | * and height. |
| 6949 | * |
Dirk Dougherty | 75c66da | 2010-03-25 16:33:33 -0700 | [diff] [blame] | 6950 | * @param width the width, either {@link #WRAP_CONTENT}, |
| 6951 | * {@link #FILL_PARENT} (replaced by {@link #MATCH_PARENT} in |
| 6952 | * API Level 8), or a fixed size in pixels |
| 6953 | * @param height the height, either {@link #WRAP_CONTENT}, |
| 6954 | * {@link #FILL_PARENT} (replaced by {@link #MATCH_PARENT} in |
| 6955 | * API Level 8), or a fixed size in pixels |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6956 | */ |
| 6957 | public LayoutParams(int width, int height) { |
| 6958 | this.width = width; |
| 6959 | this.height = height; |
| 6960 | } |
| 6961 | |
| 6962 | /** |
| 6963 | * Copy constructor. Clones the width and height values of the source. |
| 6964 | * |
| 6965 | * @param source The layout params to copy from. |
| 6966 | */ |
| 6967 | public LayoutParams(LayoutParams source) { |
| 6968 | this.width = source.width; |
| 6969 | this.height = source.height; |
| 6970 | } |
| 6971 | |
| 6972 | /** |
| 6973 | * Used internally by MarginLayoutParams. |
| 6974 | * @hide |
| 6975 | */ |
| 6976 | LayoutParams() { |
| 6977 | } |
| 6978 | |
| 6979 | /** |
Dave Burke | 579e140 | 2012-10-18 20:41:55 -0700 | [diff] [blame] | 6980 | * Extracts the layout parameters from the supplied attributes. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6981 | * |
| 6982 | * @param a the style attributes to extract the parameters from |
| 6983 | * @param widthAttr the identifier of the width attribute |
| 6984 | * @param heightAttr the identifier of the height attribute |
| 6985 | */ |
| 6986 | protected void setBaseAttributes(TypedArray a, int widthAttr, int heightAttr) { |
Dave Burke | 579e140 | 2012-10-18 20:41:55 -0700 | [diff] [blame] | 6987 | width = a.getLayoutDimension(widthAttr, "layout_width"); |
| 6988 | height = a.getLayoutDimension(heightAttr, "layout_height"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6989 | } |
| 6990 | |
| 6991 | /** |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 6992 | * Resolve layout parameters depending on the layout direction. Subclasses that care about |
| 6993 | * layoutDirection changes should override this method. The default implementation does |
| 6994 | * nothing. |
| 6995 | * |
| 6996 | * @param layoutDirection the direction of the layout |
| 6997 | * |
| 6998 | * {@link View#LAYOUT_DIRECTION_LTR} |
| 6999 | * {@link View#LAYOUT_DIRECTION_RTL} |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7000 | */ |
Fabrice Di Meglio | 2918ab6 | 2012-10-10 16:39:25 -0700 | [diff] [blame] | 7001 | public void resolveLayoutDirection(int layoutDirection) { |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7002 | } |
| 7003 | |
| 7004 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7005 | * Returns a String representation of this set of layout parameters. |
| 7006 | * |
| 7007 | * @param output the String to prepend to the internal representation |
| 7008 | * @return a String with the following format: output + |
| 7009 | * "ViewGroup.LayoutParams={ width=WIDTH, height=HEIGHT }" |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 7010 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7011 | * @hide |
| 7012 | */ |
| 7013 | public String debug(String output) { |
| 7014 | return output + "ViewGroup.LayoutParams={ width=" |
| 7015 | + sizeToString(width) + ", height=" + sizeToString(height) + " }"; |
| 7016 | } |
| 7017 | |
| 7018 | /** |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 7019 | * Use {@code canvas} to draw suitable debugging annotations for these LayoutParameters. |
| 7020 | * |
| 7021 | * @param view the view that contains these layout parameters |
| 7022 | * @param canvas the canvas on which to draw |
| 7023 | * |
| 7024 | * @hide |
| 7025 | */ |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 7026 | public void onDebugDraw(View view, Canvas canvas, Paint paint) { |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 7027 | } |
| 7028 | |
| 7029 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7030 | * Converts the specified size to a readable String. |
| 7031 | * |
| 7032 | * @param size the size to convert |
| 7033 | * @return a String instance representing the supplied size |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 7034 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7035 | * @hide |
| 7036 | */ |
| 7037 | protected static String sizeToString(int size) { |
| 7038 | if (size == WRAP_CONTENT) { |
| 7039 | return "wrap-content"; |
| 7040 | } |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 7041 | if (size == MATCH_PARENT) { |
| 7042 | return "match-parent"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7043 | } |
| 7044 | return String.valueOf(size); |
| 7045 | } |
Siva Velusamy | 0d857b9 | 2015-04-22 10:23:56 -0700 | [diff] [blame] | 7046 | |
| 7047 | /** @hide */ |
| 7048 | void encode(@NonNull ViewHierarchyEncoder encoder) { |
| 7049 | encoder.beginObject(this); |
| 7050 | encodeProperties(encoder); |
| 7051 | encoder.endObject(); |
| 7052 | } |
| 7053 | |
| 7054 | /** @hide */ |
| 7055 | protected void encodeProperties(@NonNull ViewHierarchyEncoder encoder) { |
| 7056 | encoder.addProperty("width", width); |
| 7057 | encoder.addProperty("height", height); |
| 7058 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7059 | } |
| 7060 | |
| 7061 | /** |
| 7062 | * Per-child layout information for layouts that support margins. |
| 7063 | * See |
| 7064 | * {@link android.R.styleable#ViewGroup_MarginLayout ViewGroup Margin Layout Attributes} |
| 7065 | * for a list of all child view attributes that this class supports. |
| 7066 | */ |
| 7067 | public static class MarginLayoutParams extends ViewGroup.LayoutParams { |
| 7068 | /** |
Adam Powell | a7a735f | 2014-10-09 12:54:52 -0700 | [diff] [blame] | 7069 | * 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] | 7070 | * Call {@link ViewGroup#setLayoutParams(LayoutParams)} after reassigning a new value |
| 7071 | * to this field. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7072 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 7073 | @ViewDebug.ExportedProperty(category = "layout") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7074 | public int leftMargin; |
| 7075 | |
| 7076 | /** |
Adam Powell | a7a735f | 2014-10-09 12:54:52 -0700 | [diff] [blame] | 7077 | * 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] | 7078 | * Call {@link ViewGroup#setLayoutParams(LayoutParams)} after reassigning a new value |
| 7079 | * to this field. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7080 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 7081 | @ViewDebug.ExportedProperty(category = "layout") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7082 | public int topMargin; |
| 7083 | |
| 7084 | /** |
Adam Powell | a7a735f | 2014-10-09 12:54:52 -0700 | [diff] [blame] | 7085 | * 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] | 7086 | * Call {@link ViewGroup#setLayoutParams(LayoutParams)} after reassigning a new value |
| 7087 | * to this field. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7088 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 7089 | @ViewDebug.ExportedProperty(category = "layout") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7090 | public int rightMargin; |
| 7091 | |
| 7092 | /** |
Adam Powell | a7a735f | 2014-10-09 12:54:52 -0700 | [diff] [blame] | 7093 | * 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] | 7094 | * Call {@link ViewGroup#setLayoutParams(LayoutParams)} after reassigning a new value |
| 7095 | * to this field. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7096 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 7097 | @ViewDebug.ExportedProperty(category = "layout") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7098 | public int bottomMargin; |
| 7099 | |
| 7100 | /** |
Adam Powell | a7a735f | 2014-10-09 12:54:52 -0700 | [diff] [blame] | 7101 | * 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] | 7102 | * Call {@link ViewGroup#setLayoutParams(LayoutParams)} after reassigning a new value |
| 7103 | * to this field. |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7104 | */ |
| 7105 | @ViewDebug.ExportedProperty(category = "layout") |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 7106 | private int startMargin = DEFAULT_MARGIN_RELATIVE; |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7107 | |
| 7108 | /** |
Adam Powell | a7a735f | 2014-10-09 12:54:52 -0700 | [diff] [blame] | 7109 | * 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] | 7110 | * Call {@link ViewGroup#setLayoutParams(LayoutParams)} after reassigning a new value |
| 7111 | * to this field. |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7112 | */ |
| 7113 | @ViewDebug.ExportedProperty(category = "layout") |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 7114 | private int endMargin = DEFAULT_MARGIN_RELATIVE; |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7115 | |
| 7116 | /** |
| 7117 | * The default start and end margin. |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 7118 | * @hide |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7119 | */ |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 7120 | public static final int DEFAULT_MARGIN_RELATIVE = Integer.MIN_VALUE; |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7121 | |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7122 | /** |
| 7123 | * Bit 0: layout direction |
| 7124 | * Bit 1: layout direction |
| 7125 | * Bit 2: left margin undefined |
| 7126 | * Bit 3: right margin undefined |
| 7127 | * Bit 4: is RTL compatibility mode |
| 7128 | * Bit 5: need resolution |
| 7129 | * |
| 7130 | * Bit 6 to 7 not used |
| 7131 | * |
| 7132 | * @hide |
| 7133 | */ |
| 7134 | @ViewDebug.ExportedProperty(category = "layout", flagMapping = { |
| 7135 | @ViewDebug.FlagToString(mask = LAYOUT_DIRECTION_MASK, |
| 7136 | equals = LAYOUT_DIRECTION_MASK, name = "LAYOUT_DIRECTION"), |
| 7137 | @ViewDebug.FlagToString(mask = LEFT_MARGIN_UNDEFINED_MASK, |
| 7138 | equals = LEFT_MARGIN_UNDEFINED_MASK, name = "LEFT_MARGIN_UNDEFINED_MASK"), |
| 7139 | @ViewDebug.FlagToString(mask = RIGHT_MARGIN_UNDEFINED_MASK, |
| 7140 | equals = RIGHT_MARGIN_UNDEFINED_MASK, name = "RIGHT_MARGIN_UNDEFINED_MASK"), |
| 7141 | @ViewDebug.FlagToString(mask = RTL_COMPATIBILITY_MODE_MASK, |
| 7142 | equals = RTL_COMPATIBILITY_MODE_MASK, name = "RTL_COMPATIBILITY_MODE_MASK"), |
| 7143 | @ViewDebug.FlagToString(mask = NEED_RESOLUTION_MASK, |
| 7144 | equals = NEED_RESOLUTION_MASK, name = "NEED_RESOLUTION_MASK") |
Jon Miranda | 4597e98 | 2014-07-29 07:25:49 -0700 | [diff] [blame] | 7145 | }, formatToHexString = true) |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7146 | byte mMarginFlags; |
Fabrice Di Meglio | 69bd558 | 2012-07-02 13:17:24 -0700 | [diff] [blame] | 7147 | |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7148 | private static final int LAYOUT_DIRECTION_MASK = 0x00000003; |
| 7149 | private static final int LEFT_MARGIN_UNDEFINED_MASK = 0x00000004; |
| 7150 | private static final int RIGHT_MARGIN_UNDEFINED_MASK = 0x00000008; |
| 7151 | private static final int RTL_COMPATIBILITY_MODE_MASK = 0x00000010; |
| 7152 | private static final int NEED_RESOLUTION_MASK = 0x00000020; |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 7153 | |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7154 | private static final int DEFAULT_MARGIN_RESOLVED = 0; |
| 7155 | private static final int UNDEFINED_MARGIN = DEFAULT_MARGIN_RELATIVE; |
Fabrice Di Meglio | 02a7d56 | 2013-03-27 12:41:22 -0700 | [diff] [blame] | 7156 | |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7157 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7158 | * Creates a new set of layout parameters. The values are extracted from |
| 7159 | * the supplied attributes set and context. |
| 7160 | * |
| 7161 | * @param c the application environment |
| 7162 | * @param attrs the set of attributes from which to extract the layout |
| 7163 | * parameters' values |
| 7164 | */ |
| 7165 | public MarginLayoutParams(Context c, AttributeSet attrs) { |
| 7166 | super(); |
| 7167 | |
| 7168 | TypedArray a = c.obtainStyledAttributes(attrs, R.styleable.ViewGroup_MarginLayout); |
| 7169 | setBaseAttributes(a, |
| 7170 | R.styleable.ViewGroup_MarginLayout_layout_width, |
| 7171 | R.styleable.ViewGroup_MarginLayout_layout_height); |
| 7172 | |
| 7173 | int margin = a.getDimensionPixelSize( |
| 7174 | com.android.internal.R.styleable.ViewGroup_MarginLayout_layout_margin, -1); |
| 7175 | if (margin >= 0) { |
| 7176 | leftMargin = margin; |
| 7177 | topMargin = margin; |
| 7178 | rightMargin= margin; |
| 7179 | bottomMargin = margin; |
| 7180 | } else { |
| 7181 | leftMargin = a.getDimensionPixelSize( |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 7182 | R.styleable.ViewGroup_MarginLayout_layout_marginLeft, |
Fabrice Di Meglio | 02a7d56 | 2013-03-27 12:41:22 -0700 | [diff] [blame] | 7183 | UNDEFINED_MARGIN); |
| 7184 | if (leftMargin == UNDEFINED_MARGIN) { |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7185 | mMarginFlags |= LEFT_MARGIN_UNDEFINED_MASK; |
Fabrice Di Meglio | 02a7d56 | 2013-03-27 12:41:22 -0700 | [diff] [blame] | 7186 | leftMargin = DEFAULT_MARGIN_RESOLVED; |
| 7187 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7188 | rightMargin = a.getDimensionPixelSize( |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 7189 | R.styleable.ViewGroup_MarginLayout_layout_marginRight, |
Fabrice Di Meglio | 02a7d56 | 2013-03-27 12:41:22 -0700 | [diff] [blame] | 7190 | UNDEFINED_MARGIN); |
| 7191 | if (rightMargin == UNDEFINED_MARGIN) { |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7192 | mMarginFlags |= RIGHT_MARGIN_UNDEFINED_MASK; |
Fabrice Di Meglio | 02a7d56 | 2013-03-27 12:41:22 -0700 | [diff] [blame] | 7193 | rightMargin = DEFAULT_MARGIN_RESOLVED; |
| 7194 | } |
| 7195 | |
| 7196 | topMargin = a.getDimensionPixelSize( |
| 7197 | R.styleable.ViewGroup_MarginLayout_layout_marginTop, |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 7198 | DEFAULT_MARGIN_RESOLVED); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7199 | bottomMargin = a.getDimensionPixelSize( |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 7200 | R.styleable.ViewGroup_MarginLayout_layout_marginBottom, |
| 7201 | DEFAULT_MARGIN_RESOLVED); |
Fabrice Di Meglio | 02a7d56 | 2013-03-27 12:41:22 -0700 | [diff] [blame] | 7202 | |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7203 | startMargin = a.getDimensionPixelSize( |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 7204 | R.styleable.ViewGroup_MarginLayout_layout_marginStart, |
| 7205 | DEFAULT_MARGIN_RELATIVE); |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7206 | endMargin = a.getDimensionPixelSize( |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 7207 | R.styleable.ViewGroup_MarginLayout_layout_marginEnd, |
| 7208 | DEFAULT_MARGIN_RELATIVE); |
| 7209 | |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7210 | if (isMarginRelative()) { |
| 7211 | mMarginFlags |= NEED_RESOLUTION_MASK; |
| 7212 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7213 | } |
| 7214 | |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 7215 | final boolean hasRtlSupport = c.getApplicationInfo().hasRtlSupport(); |
| 7216 | final int targetSdkVersion = c.getApplicationInfo().targetSdkVersion; |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7217 | if (targetSdkVersion < JELLY_BEAN_MR1 || !hasRtlSupport) { |
| 7218 | mMarginFlags |= RTL_COMPATIBILITY_MODE_MASK; |
| 7219 | } |
| 7220 | |
| 7221 | // Layout direction is LTR by default |
| 7222 | mMarginFlags |= LAYOUT_DIRECTION_LTR; |
Fabrice Di Meglio | 69bd558 | 2012-07-02 13:17:24 -0700 | [diff] [blame] | 7223 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7224 | a.recycle(); |
| 7225 | } |
| 7226 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7227 | public MarginLayoutParams(int width, int height) { |
| 7228 | super(width, height); |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 7229 | |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7230 | mMarginFlags |= LEFT_MARGIN_UNDEFINED_MASK; |
| 7231 | mMarginFlags |= RIGHT_MARGIN_UNDEFINED_MASK; |
Fabrice Di Meglio | 02a7d56 | 2013-03-27 12:41:22 -0700 | [diff] [blame] | 7232 | |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7233 | mMarginFlags &= ~NEED_RESOLUTION_MASK; |
| 7234 | mMarginFlags &= ~RTL_COMPATIBILITY_MODE_MASK; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7235 | } |
| 7236 | |
| 7237 | /** |
| 7238 | * Copy constructor. Clones the width, height and margin values of the source. |
| 7239 | * |
| 7240 | * @param source The layout params to copy from. |
| 7241 | */ |
| 7242 | public MarginLayoutParams(MarginLayoutParams source) { |
| 7243 | this.width = source.width; |
| 7244 | this.height = source.height; |
| 7245 | |
| 7246 | this.leftMargin = source.leftMargin; |
| 7247 | this.topMargin = source.topMargin; |
| 7248 | this.rightMargin = source.rightMargin; |
| 7249 | this.bottomMargin = source.bottomMargin; |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7250 | this.startMargin = source.startMargin; |
| 7251 | this.endMargin = source.endMargin; |
Fabrice Di Meglio | 69bd558 | 2012-07-02 13:17:24 -0700 | [diff] [blame] | 7252 | |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7253 | this.mMarginFlags = source.mMarginFlags; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7254 | } |
| 7255 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7256 | public MarginLayoutParams(LayoutParams source) { |
| 7257 | super(source); |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 7258 | |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7259 | mMarginFlags |= LEFT_MARGIN_UNDEFINED_MASK; |
| 7260 | mMarginFlags |= RIGHT_MARGIN_UNDEFINED_MASK; |
Fabrice Di Meglio | 02a7d56 | 2013-03-27 12:41:22 -0700 | [diff] [blame] | 7261 | |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7262 | mMarginFlags &= ~NEED_RESOLUTION_MASK; |
| 7263 | mMarginFlags &= ~RTL_COMPATIBILITY_MODE_MASK; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7264 | } |
| 7265 | |
| 7266 | /** |
Adam Powell | d760083 | 2014-07-01 15:22:50 -0700 | [diff] [blame] | 7267 | * @hide Used internally. |
| 7268 | */ |
| 7269 | public final void copyMarginsFrom(MarginLayoutParams source) { |
| 7270 | this.leftMargin = source.leftMargin; |
| 7271 | this.topMargin = source.topMargin; |
| 7272 | this.rightMargin = source.rightMargin; |
| 7273 | this.bottomMargin = source.bottomMargin; |
| 7274 | this.startMargin = source.startMargin; |
| 7275 | this.endMargin = source.endMargin; |
| 7276 | |
| 7277 | this.mMarginFlags = source.mMarginFlags; |
| 7278 | } |
| 7279 | |
| 7280 | /** |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7281 | * Sets the margins, in pixels. A call to {@link android.view.View#requestLayout()} needs |
| 7282 | * to be done so that the new margins are taken into account. Left and right margins may be |
| 7283 | * overriden by {@link android.view.View#requestLayout()} depending on layout direction. |
Adam Powell | a7a735f | 2014-10-09 12:54:52 -0700 | [diff] [blame] | 7284 | * Margin values should be positive. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7285 | * |
| 7286 | * @param left the left margin size |
| 7287 | * @param top the top margin size |
| 7288 | * @param right the right margin size |
| 7289 | * @param bottom the bottom margin size |
| 7290 | * |
| 7291 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginLeft |
| 7292 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginTop |
| 7293 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginRight |
| 7294 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginBottom |
| 7295 | */ |
| 7296 | public void setMargins(int left, int top, int right, int bottom) { |
| 7297 | leftMargin = left; |
| 7298 | topMargin = top; |
| 7299 | rightMargin = right; |
| 7300 | bottomMargin = bottom; |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7301 | mMarginFlags &= ~LEFT_MARGIN_UNDEFINED_MASK; |
| 7302 | mMarginFlags &= ~RIGHT_MARGIN_UNDEFINED_MASK; |
| 7303 | if (isMarginRelative()) { |
| 7304 | mMarginFlags |= NEED_RESOLUTION_MASK; |
| 7305 | } else { |
| 7306 | mMarginFlags &= ~NEED_RESOLUTION_MASK; |
| 7307 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7308 | } |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7309 | |
| 7310 | /** |
| 7311 | * Sets the relative margins, in pixels. A call to {@link android.view.View#requestLayout()} |
| 7312 | * needs to be done so that the new relative margins are taken into account. Left and right |
| 7313 | * margins may be overriden by {@link android.view.View#requestLayout()} depending on layout |
Adam Powell | a7a735f | 2014-10-09 12:54:52 -0700 | [diff] [blame] | 7314 | * direction. Margin values should be positive. |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7315 | * |
| 7316 | * @param start the start margin size |
| 7317 | * @param top the top margin size |
| 7318 | * @param end the right margin size |
| 7319 | * @param bottom the bottom margin size |
| 7320 | * |
| 7321 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginStart |
| 7322 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginTop |
| 7323 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginEnd |
| 7324 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginBottom |
| 7325 | * |
| 7326 | * @hide |
| 7327 | */ |
| 7328 | public void setMarginsRelative(int start, int top, int end, int bottom) { |
| 7329 | startMargin = start; |
| 7330 | topMargin = top; |
| 7331 | endMargin = end; |
| 7332 | bottomMargin = bottom; |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7333 | mMarginFlags |= NEED_RESOLUTION_MASK; |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7334 | } |
| 7335 | |
| 7336 | /** |
Adam Powell | a7a735f | 2014-10-09 12:54:52 -0700 | [diff] [blame] | 7337 | * Sets the relative start margin. Margin values should be positive. |
Fabrice Di Meglio | a40627d | 2012-09-11 16:47:21 -0700 | [diff] [blame] | 7338 | * |
Fabrice Di Meglio | 61a2177 | 2012-09-12 16:33:13 -0700 | [diff] [blame] | 7339 | * @param start the start margin size |
Fabrice Di Meglio | a40627d | 2012-09-11 16:47:21 -0700 | [diff] [blame] | 7340 | * |
| 7341 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginStart |
| 7342 | */ |
| 7343 | public void setMarginStart(int start) { |
| 7344 | startMargin = start; |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7345 | mMarginFlags |= NEED_RESOLUTION_MASK; |
Fabrice Di Meglio | a40627d | 2012-09-11 16:47:21 -0700 | [diff] [blame] | 7346 | } |
| 7347 | |
| 7348 | /** |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7349 | * Returns the start margin in pixels. |
| 7350 | * |
| 7351 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginStart |
| 7352 | * |
| 7353 | * @return the start margin in pixels. |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7354 | */ |
| 7355 | public int getMarginStart() { |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 7356 | if (startMargin != DEFAULT_MARGIN_RELATIVE) return startMargin; |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7357 | if ((mMarginFlags & NEED_RESOLUTION_MASK) == NEED_RESOLUTION_MASK) { |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 7358 | doResolveMargins(); |
| 7359 | } |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7360 | switch(mMarginFlags & LAYOUT_DIRECTION_MASK) { |
Fabrice Di Meglio | 69bd558 | 2012-07-02 13:17:24 -0700 | [diff] [blame] | 7361 | case View.LAYOUT_DIRECTION_RTL: |
| 7362 | return rightMargin; |
| 7363 | case View.LAYOUT_DIRECTION_LTR: |
| 7364 | default: |
| 7365 | return leftMargin; |
| 7366 | } |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7367 | } |
| 7368 | |
| 7369 | /** |
Adam Powell | a7a735f | 2014-10-09 12:54:52 -0700 | [diff] [blame] | 7370 | * Sets the relative end margin. Margin values should be positive. |
Fabrice Di Meglio | a40627d | 2012-09-11 16:47:21 -0700 | [diff] [blame] | 7371 | * |
Fabrice Di Meglio | 61a2177 | 2012-09-12 16:33:13 -0700 | [diff] [blame] | 7372 | * @param end the end margin size |
Fabrice Di Meglio | a40627d | 2012-09-11 16:47:21 -0700 | [diff] [blame] | 7373 | * |
| 7374 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginEnd |
| 7375 | */ |
| 7376 | public void setMarginEnd(int end) { |
| 7377 | endMargin = end; |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7378 | mMarginFlags |= NEED_RESOLUTION_MASK; |
Fabrice Di Meglio | a40627d | 2012-09-11 16:47:21 -0700 | [diff] [blame] | 7379 | } |
| 7380 | |
| 7381 | /** |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7382 | * Returns the end margin in pixels. |
| 7383 | * |
| 7384 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginEnd |
| 7385 | * |
| 7386 | * @return the end margin in pixels. |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7387 | */ |
| 7388 | public int getMarginEnd() { |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 7389 | if (endMargin != DEFAULT_MARGIN_RELATIVE) return endMargin; |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7390 | if ((mMarginFlags & NEED_RESOLUTION_MASK) == NEED_RESOLUTION_MASK) { |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 7391 | doResolveMargins(); |
| 7392 | } |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7393 | switch(mMarginFlags & LAYOUT_DIRECTION_MASK) { |
Fabrice Di Meglio | 69bd558 | 2012-07-02 13:17:24 -0700 | [diff] [blame] | 7394 | case View.LAYOUT_DIRECTION_RTL: |
| 7395 | return leftMargin; |
| 7396 | case View.LAYOUT_DIRECTION_LTR: |
| 7397 | default: |
| 7398 | return rightMargin; |
| 7399 | } |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7400 | } |
| 7401 | |
| 7402 | /** |
| 7403 | * Check if margins are relative. |
| 7404 | * |
| 7405 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginStart |
| 7406 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginEnd |
| 7407 | * |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 7408 | * @return true if either marginStart or marginEnd has been set. |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7409 | */ |
| 7410 | public boolean isMarginRelative() { |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 7411 | return (startMargin != DEFAULT_MARGIN_RELATIVE || endMargin != DEFAULT_MARGIN_RELATIVE); |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7412 | } |
| 7413 | |
| 7414 | /** |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 7415 | * Set the layout direction |
| 7416 | * @param layoutDirection the layout direction. |
| 7417 | * Should be either {@link View#LAYOUT_DIRECTION_LTR} |
| 7418 | * or {@link View#LAYOUT_DIRECTION_RTL}. |
| 7419 | */ |
| 7420 | public void setLayoutDirection(int layoutDirection) { |
| 7421 | if (layoutDirection != View.LAYOUT_DIRECTION_LTR && |
| 7422 | layoutDirection != View.LAYOUT_DIRECTION_RTL) return; |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7423 | if (layoutDirection != (mMarginFlags & LAYOUT_DIRECTION_MASK)) { |
| 7424 | mMarginFlags &= ~LAYOUT_DIRECTION_MASK; |
| 7425 | mMarginFlags |= (layoutDirection & LAYOUT_DIRECTION_MASK); |
| 7426 | if (isMarginRelative()) { |
| 7427 | mMarginFlags |= NEED_RESOLUTION_MASK; |
| 7428 | } else { |
| 7429 | mMarginFlags &= ~NEED_RESOLUTION_MASK; |
| 7430 | } |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 7431 | } |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 7432 | } |
| 7433 | |
| 7434 | /** |
| 7435 | * Retuns the layout direction. Can be either {@link View#LAYOUT_DIRECTION_LTR} or |
| 7436 | * {@link View#LAYOUT_DIRECTION_RTL}. |
| 7437 | * |
| 7438 | * @return the layout direction. |
| 7439 | */ |
| 7440 | public int getLayoutDirection() { |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7441 | return (mMarginFlags & LAYOUT_DIRECTION_MASK); |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 7442 | } |
| 7443 | |
| 7444 | /** |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7445 | * 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] | 7446 | * may be overridden depending on layout direction. |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7447 | */ |
| 7448 | @Override |
Fabrice Di Meglio | 2918ab6 | 2012-10-10 16:39:25 -0700 | [diff] [blame] | 7449 | public void resolveLayoutDirection(int layoutDirection) { |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 7450 | setLayoutDirection(layoutDirection); |
Fabrice Di Meglio | 69bd558 | 2012-07-02 13:17:24 -0700 | [diff] [blame] | 7451 | |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 7452 | // No relative margin or pre JB-MR1 case or no need to resolve, just dont do anything |
| 7453 | // Will use the left and right margins if no relative margin is defined. |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7454 | if (!isMarginRelative() || |
| 7455 | (mMarginFlags & NEED_RESOLUTION_MASK) != NEED_RESOLUTION_MASK) return; |
Fabrice Di Meglio | 69bd558 | 2012-07-02 13:17:24 -0700 | [diff] [blame] | 7456 | |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 7457 | // Proceed with resolution |
| 7458 | doResolveMargins(); |
| 7459 | } |
| 7460 | |
| 7461 | private void doResolveMargins() { |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7462 | if ((mMarginFlags & RTL_COMPATIBILITY_MODE_MASK) == RTL_COMPATIBILITY_MODE_MASK) { |
Fabrice Di Meglio | 02a7d56 | 2013-03-27 12:41:22 -0700 | [diff] [blame] | 7463 | // if left or right margins are not defined and if we have some start or end margin |
| 7464 | // defined then use those start and end margins. |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7465 | if ((mMarginFlags & LEFT_MARGIN_UNDEFINED_MASK) == LEFT_MARGIN_UNDEFINED_MASK |
| 7466 | && startMargin > DEFAULT_MARGIN_RELATIVE) { |
Fabrice Di Meglio | 02a7d56 | 2013-03-27 12:41:22 -0700 | [diff] [blame] | 7467 | leftMargin = startMargin; |
| 7468 | } |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7469 | if ((mMarginFlags & RIGHT_MARGIN_UNDEFINED_MASK) == RIGHT_MARGIN_UNDEFINED_MASK |
| 7470 | && endMargin > DEFAULT_MARGIN_RELATIVE) { |
Fabrice Di Meglio | 02a7d56 | 2013-03-27 12:41:22 -0700 | [diff] [blame] | 7471 | rightMargin = endMargin; |
| 7472 | } |
| 7473 | } else { |
| 7474 | // We have some relative margins (either the start one or the end one or both). So use |
| 7475 | // them and override what has been defined for left and right margins. If either start |
| 7476 | // or end margin is not defined, just set it to default "0". |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7477 | switch(mMarginFlags & LAYOUT_DIRECTION_MASK) { |
Fabrice Di Meglio | 02a7d56 | 2013-03-27 12:41:22 -0700 | [diff] [blame] | 7478 | case View.LAYOUT_DIRECTION_RTL: |
| 7479 | leftMargin = (endMargin > DEFAULT_MARGIN_RELATIVE) ? |
| 7480 | endMargin : DEFAULT_MARGIN_RESOLVED; |
| 7481 | rightMargin = (startMargin > DEFAULT_MARGIN_RELATIVE) ? |
| 7482 | startMargin : DEFAULT_MARGIN_RESOLVED; |
| 7483 | break; |
| 7484 | case View.LAYOUT_DIRECTION_LTR: |
| 7485 | default: |
| 7486 | leftMargin = (startMargin > DEFAULT_MARGIN_RELATIVE) ? |
| 7487 | startMargin : DEFAULT_MARGIN_RESOLVED; |
| 7488 | rightMargin = (endMargin > DEFAULT_MARGIN_RELATIVE) ? |
| 7489 | endMargin : DEFAULT_MARGIN_RESOLVED; |
| 7490 | break; |
| 7491 | } |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7492 | } |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7493 | mMarginFlags &= ~NEED_RESOLUTION_MASK; |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7494 | } |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 7495 | |
Fabrice Di Meglio | 03b8d3a | 2012-09-27 17:05:27 -0700 | [diff] [blame] | 7496 | /** |
| 7497 | * @hide |
| 7498 | */ |
| 7499 | public boolean isLayoutRtl() { |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7500 | return ((mMarginFlags & LAYOUT_DIRECTION_MASK) == View.LAYOUT_DIRECTION_RTL); |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 7501 | } |
| 7502 | |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 7503 | /** |
| 7504 | * @hide |
| 7505 | */ |
| 7506 | @Override |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 7507 | public void onDebugDraw(View view, Canvas canvas, Paint paint) { |
| 7508 | Insets oi = isLayoutModeOptical(view.mParent) ? view.getOpticalInsets() : Insets.NONE; |
| 7509 | |
| 7510 | fillDifference(canvas, |
| 7511 | view.getLeft() + oi.left, |
| 7512 | view.getTop() + oi.top, |
| 7513 | view.getRight() - oi.right, |
| 7514 | view.getBottom() - oi.bottom, |
| 7515 | leftMargin, |
| 7516 | topMargin, |
| 7517 | rightMargin, |
| 7518 | bottomMargin, |
| 7519 | paint); |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 7520 | } |
Siva Velusamy | 0d857b9 | 2015-04-22 10:23:56 -0700 | [diff] [blame] | 7521 | |
| 7522 | /** @hide */ |
| 7523 | @Override |
| 7524 | protected void encodeProperties(@NonNull ViewHierarchyEncoder encoder) { |
| 7525 | super.encodeProperties(encoder); |
| 7526 | encoder.addProperty("leftMargin", leftMargin); |
| 7527 | encoder.addProperty("topMargin", topMargin); |
| 7528 | encoder.addProperty("rightMargin", rightMargin); |
| 7529 | encoder.addProperty("bottomMargin", bottomMargin); |
| 7530 | encoder.addProperty("startMargin", startMargin); |
| 7531 | encoder.addProperty("endMargin", endMargin); |
| 7532 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7533 | } |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 7534 | |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 7535 | /* Describes a touched view and the ids of the pointers that it has captured. |
| 7536 | * |
| 7537 | * This code assumes that pointer ids are always in the range 0..31 such that |
| 7538 | * it can use a bitfield to track which pointer ids are present. |
| 7539 | * As it happens, the lower layers of the input dispatch pipeline also use the |
| 7540 | * same trick so the assumption should be safe here... |
| 7541 | */ |
| 7542 | private static final class TouchTarget { |
| 7543 | private static final int MAX_RECYCLED = 32; |
Romain Guy | 6410c0a | 2013-06-17 11:21:58 -0700 | [diff] [blame] | 7544 | private static final Object sRecycleLock = new Object[0]; |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 7545 | private static TouchTarget sRecycleBin; |
| 7546 | private static int sRecycledCount; |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 7547 | |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 7548 | public static final int ALL_POINTER_IDS = -1; // all ones |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 7549 | |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 7550 | // The touched child view. |
| 7551 | public View child; |
| 7552 | |
| 7553 | // The combined bit mask of pointer ids for all pointers captured by the target. |
| 7554 | public int pointerIdBits; |
| 7555 | |
| 7556 | // The next target in the target list. |
| 7557 | public TouchTarget next; |
| 7558 | |
| 7559 | private TouchTarget() { |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 7560 | } |
| 7561 | |
Alan Viverette | a7b85e6 | 2016-01-22 10:14:02 -0500 | [diff] [blame] | 7562 | public static TouchTarget obtain(@NonNull View child, int pointerIdBits) { |
| 7563 | if (child == null) { |
| 7564 | throw new IllegalArgumentException("child must be non-null"); |
| 7565 | } |
| 7566 | |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 7567 | final TouchTarget target; |
| 7568 | synchronized (sRecycleLock) { |
Adam Powell | 816c3be | 2010-08-23 18:00:05 -0700 | [diff] [blame] | 7569 | if (sRecycleBin == null) { |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 7570 | target = new TouchTarget(); |
Adam Powell | 816c3be | 2010-08-23 18:00:05 -0700 | [diff] [blame] | 7571 | } else { |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 7572 | target = sRecycleBin; |
| 7573 | sRecycleBin = target.next; |
| 7574 | sRecycledCount--; |
| 7575 | target.next = null; |
Adam Powell | 816c3be | 2010-08-23 18:00:05 -0700 | [diff] [blame] | 7576 | } |
Adam Powell | 816c3be | 2010-08-23 18:00:05 -0700 | [diff] [blame] | 7577 | } |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 7578 | target.child = child; |
| 7579 | target.pointerIdBits = pointerIdBits; |
| 7580 | return target; |
| 7581 | } |
Adam Powell | 816c3be | 2010-08-23 18:00:05 -0700 | [diff] [blame] | 7582 | |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 7583 | public void recycle() { |
Alan Viverette | a7b85e6 | 2016-01-22 10:14:02 -0500 | [diff] [blame] | 7584 | if (child == null) { |
| 7585 | throw new IllegalStateException("already recycled once"); |
| 7586 | } |
| 7587 | |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 7588 | synchronized (sRecycleLock) { |
| 7589 | if (sRecycledCount < MAX_RECYCLED) { |
| 7590 | next = sRecycleBin; |
| 7591 | sRecycleBin = this; |
| 7592 | sRecycledCount += 1; |
Patrick Dubroy | fb0547d2 | 2010-10-19 17:36:18 -0700 | [diff] [blame] | 7593 | } else { |
| 7594 | next = null; |
Adam Powell | 816c3be | 2010-08-23 18:00:05 -0700 | [diff] [blame] | 7595 | } |
Patrick Dubroy | fb0547d2 | 2010-10-19 17:36:18 -0700 | [diff] [blame] | 7596 | child = null; |
Adam Powell | 816c3be | 2010-08-23 18:00:05 -0700 | [diff] [blame] | 7597 | } |
| 7598 | } |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 7599 | } |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 7600 | |
| 7601 | /* Describes a hovered view. */ |
| 7602 | private static final class HoverTarget { |
| 7603 | private static final int MAX_RECYCLED = 32; |
Romain Guy | 6410c0a | 2013-06-17 11:21:58 -0700 | [diff] [blame] | 7604 | private static final Object sRecycleLock = new Object[0]; |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 7605 | private static HoverTarget sRecycleBin; |
| 7606 | private static int sRecycledCount; |
| 7607 | |
| 7608 | // The hovered child view. |
| 7609 | public View child; |
| 7610 | |
| 7611 | // The next target in the target list. |
| 7612 | public HoverTarget next; |
| 7613 | |
| 7614 | private HoverTarget() { |
| 7615 | } |
| 7616 | |
Alan Viverette | a7b85e6 | 2016-01-22 10:14:02 -0500 | [diff] [blame] | 7617 | public static HoverTarget obtain(@NonNull View child) { |
| 7618 | if (child == null) { |
| 7619 | throw new IllegalArgumentException("child must be non-null"); |
| 7620 | } |
| 7621 | |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 7622 | final HoverTarget target; |
| 7623 | synchronized (sRecycleLock) { |
| 7624 | if (sRecycleBin == null) { |
| 7625 | target = new HoverTarget(); |
| 7626 | } else { |
| 7627 | target = sRecycleBin; |
| 7628 | sRecycleBin = target.next; |
Alan Viverette | a7b85e6 | 2016-01-22 10:14:02 -0500 | [diff] [blame] | 7629 | sRecycledCount--; |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 7630 | target.next = null; |
| 7631 | } |
| 7632 | } |
| 7633 | target.child = child; |
| 7634 | return target; |
| 7635 | } |
| 7636 | |
| 7637 | public void recycle() { |
Alan Viverette | a7b85e6 | 2016-01-22 10:14:02 -0500 | [diff] [blame] | 7638 | if (child == null) { |
| 7639 | throw new IllegalStateException("already recycled once"); |
| 7640 | } |
| 7641 | |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 7642 | synchronized (sRecycleLock) { |
| 7643 | if (sRecycledCount < MAX_RECYCLED) { |
| 7644 | next = sRecycleBin; |
| 7645 | sRecycleBin = this; |
| 7646 | sRecycledCount += 1; |
| 7647 | } else { |
| 7648 | next = null; |
| 7649 | } |
| 7650 | child = null; |
| 7651 | } |
| 7652 | } |
| 7653 | } |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 7654 | |
| 7655 | /** |
| 7656 | * Pooled class that orderes the children of a ViewGroup from start |
| 7657 | * to end based on how they are laid out and the layout direction. |
| 7658 | */ |
| 7659 | static class ChildListForAccessibility { |
| 7660 | |
| 7661 | private static final int MAX_POOL_SIZE = 32; |
| 7662 | |
Svetoslav Ganov | be922dc | 2012-11-30 16:46:26 -0800 | [diff] [blame] | 7663 | private static final SynchronizedPool<ChildListForAccessibility> sPool = |
| 7664 | new SynchronizedPool<ChildListForAccessibility>(MAX_POOL_SIZE); |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 7665 | |
| 7666 | private final ArrayList<View> mChildren = new ArrayList<View>(); |
| 7667 | |
| 7668 | private final ArrayList<ViewLocationHolder> mHolders = new ArrayList<ViewLocationHolder>(); |
| 7669 | |
| 7670 | public static ChildListForAccessibility obtain(ViewGroup parent, boolean sort) { |
Svetoslav Ganov | be922dc | 2012-11-30 16:46:26 -0800 | [diff] [blame] | 7671 | ChildListForAccessibility list = sPool.acquire(); |
| 7672 | if (list == null) { |
| 7673 | list = new ChildListForAccessibility(); |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 7674 | } |
Svetoslav Ganov | be922dc | 2012-11-30 16:46:26 -0800 | [diff] [blame] | 7675 | list.init(parent, sort); |
| 7676 | return list; |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 7677 | } |
| 7678 | |
| 7679 | public void recycle() { |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 7680 | clear(); |
Svetoslav Ganov | be922dc | 2012-11-30 16:46:26 -0800 | [diff] [blame] | 7681 | sPool.release(this); |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 7682 | } |
| 7683 | |
| 7684 | public int getChildCount() { |
| 7685 | return mChildren.size(); |
| 7686 | } |
| 7687 | |
| 7688 | public View getChildAt(int index) { |
| 7689 | return mChildren.get(index); |
| 7690 | } |
| 7691 | |
| 7692 | public int getChildIndex(View child) { |
| 7693 | return mChildren.indexOf(child); |
| 7694 | } |
| 7695 | |
| 7696 | private void init(ViewGroup parent, boolean sort) { |
| 7697 | ArrayList<View> children = mChildren; |
| 7698 | final int childCount = parent.getChildCount(); |
| 7699 | for (int i = 0; i < childCount; i++) { |
| 7700 | View child = parent.getChildAt(i); |
| 7701 | children.add(child); |
| 7702 | } |
| 7703 | if (sort) { |
| 7704 | ArrayList<ViewLocationHolder> holders = mHolders; |
| 7705 | for (int i = 0; i < childCount; i++) { |
| 7706 | View child = children.get(i); |
| 7707 | ViewLocationHolder holder = ViewLocationHolder.obtain(parent, child); |
| 7708 | holders.add(holder); |
| 7709 | } |
Svetoslav | 88e447b | 2014-10-09 15:49:02 -0700 | [diff] [blame] | 7710 | sort(holders); |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 7711 | for (int i = 0; i < childCount; i++) { |
| 7712 | ViewLocationHolder holder = holders.get(i); |
| 7713 | children.set(i, holder.mView); |
| 7714 | holder.recycle(); |
| 7715 | } |
| 7716 | holders.clear(); |
| 7717 | } |
| 7718 | } |
| 7719 | |
Svetoslav | 88e447b | 2014-10-09 15:49:02 -0700 | [diff] [blame] | 7720 | private void sort(ArrayList<ViewLocationHolder> holders) { |
| 7721 | // This is gross but the least risky solution. The current comparison |
| 7722 | // strategy breaks transitivity but produces very good results. Coming |
| 7723 | // up with a new strategy requires time which we do not have, so ... |
| 7724 | try { |
| 7725 | ViewLocationHolder.setComparisonStrategy( |
| 7726 | ViewLocationHolder.COMPARISON_STRATEGY_STRIPE); |
| 7727 | Collections.sort(holders); |
| 7728 | } catch (IllegalArgumentException iae) { |
| 7729 | // Note that in practice this occurs extremely rarely in a couple |
| 7730 | // of pathological cases. |
| 7731 | ViewLocationHolder.setComparisonStrategy( |
| 7732 | ViewLocationHolder.COMPARISON_STRATEGY_LOCATION); |
| 7733 | Collections.sort(holders); |
| 7734 | } |
| 7735 | } |
| 7736 | |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 7737 | private void clear() { |
| 7738 | mChildren.clear(); |
| 7739 | } |
| 7740 | } |
| 7741 | |
| 7742 | /** |
| 7743 | * Pooled class that holds a View and its location with respect to |
| 7744 | * a specified root. This enables sorting of views based on their |
| 7745 | * coordinates without recomputing the position relative to the root |
| 7746 | * on every comparison. |
| 7747 | */ |
| 7748 | static class ViewLocationHolder implements Comparable<ViewLocationHolder> { |
| 7749 | |
| 7750 | private static final int MAX_POOL_SIZE = 32; |
| 7751 | |
Svetoslav Ganov | be922dc | 2012-11-30 16:46:26 -0800 | [diff] [blame] | 7752 | private static final SynchronizedPool<ViewLocationHolder> sPool = |
| 7753 | new SynchronizedPool<ViewLocationHolder>(MAX_POOL_SIZE); |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 7754 | |
Svetoslav | 88e447b | 2014-10-09 15:49:02 -0700 | [diff] [blame] | 7755 | public static final int COMPARISON_STRATEGY_STRIPE = 1; |
| 7756 | |
| 7757 | public static final int COMPARISON_STRATEGY_LOCATION = 2; |
| 7758 | |
| 7759 | private static int sComparisonStrategy = COMPARISON_STRATEGY_STRIPE; |
| 7760 | |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 7761 | private final Rect mLocation = new Rect(); |
| 7762 | |
| 7763 | public View mView; |
| 7764 | |
| 7765 | private int mLayoutDirection; |
| 7766 | |
| 7767 | public static ViewLocationHolder obtain(ViewGroup root, View view) { |
Svetoslav Ganov | be922dc | 2012-11-30 16:46:26 -0800 | [diff] [blame] | 7768 | ViewLocationHolder holder = sPool.acquire(); |
| 7769 | if (holder == null) { |
| 7770 | holder = new ViewLocationHolder(); |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 7771 | } |
Svetoslav Ganov | be922dc | 2012-11-30 16:46:26 -0800 | [diff] [blame] | 7772 | holder.init(root, view); |
| 7773 | return holder; |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 7774 | } |
| 7775 | |
Svetoslav | 88e447b | 2014-10-09 15:49:02 -0700 | [diff] [blame] | 7776 | public static void setComparisonStrategy(int strategy) { |
| 7777 | sComparisonStrategy = strategy; |
| 7778 | } |
| 7779 | |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 7780 | public void recycle() { |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 7781 | clear(); |
Svetoslav Ganov | be922dc | 2012-11-30 16:46:26 -0800 | [diff] [blame] | 7782 | sPool.release(this); |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 7783 | } |
| 7784 | |
| 7785 | @Override |
| 7786 | public int compareTo(ViewLocationHolder another) { |
| 7787 | // This instance is greater than an invalid argument. |
| 7788 | if (another == null) { |
| 7789 | return 1; |
| 7790 | } |
Svetoslav | 88e447b | 2014-10-09 15:49:02 -0700 | [diff] [blame] | 7791 | |
| 7792 | if (sComparisonStrategy == COMPARISON_STRATEGY_STRIPE) { |
| 7793 | // First is above second. |
| 7794 | if (mLocation.bottom - another.mLocation.top <= 0) { |
| 7795 | return -1; |
| 7796 | } |
| 7797 | // First is below second. |
| 7798 | if (mLocation.top - another.mLocation.bottom >= 0) { |
| 7799 | return 1; |
| 7800 | } |
| 7801 | } |
| 7802 | |
Svetoslav | 04cab1b | 2014-08-25 18:35:57 -0700 | [diff] [blame] | 7803 | // We are ordering left-to-right, top-to-bottom. |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 7804 | if (mLayoutDirection == LAYOUT_DIRECTION_LTR) { |
| 7805 | final int leftDifference = mLocation.left - another.mLocation.left; |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 7806 | if (leftDifference != 0) { |
| 7807 | return leftDifference; |
| 7808 | } |
| 7809 | } else { // RTL |
| 7810 | final int rightDifference = mLocation.right - another.mLocation.right; |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 7811 | if (rightDifference != 0) { |
| 7812 | return -rightDifference; |
| 7813 | } |
| 7814 | } |
Svetoslav | 04cab1b | 2014-08-25 18:35:57 -0700 | [diff] [blame] | 7815 | // We are ordering left-to-right, top-to-bottom. |
| 7816 | final int topDifference = mLocation.top - another.mLocation.top; |
| 7817 | if (topDifference != 0) { |
| 7818 | return topDifference; |
| 7819 | } |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 7820 | // Break tie by height. |
| 7821 | final int heightDiference = mLocation.height() - another.mLocation.height(); |
| 7822 | if (heightDiference != 0) { |
| 7823 | return -heightDiference; |
| 7824 | } |
| 7825 | // Break tie by width. |
| 7826 | final int widthDiference = mLocation.width() - another.mLocation.width(); |
| 7827 | if (widthDiference != 0) { |
| 7828 | return -widthDiference; |
| 7829 | } |
Svetoslav Ganov | 005b83b | 2012-04-16 18:17:17 -0700 | [diff] [blame] | 7830 | // Just break the tie somehow. The accessibliity ids are unique |
| 7831 | // and stable, hence this is deterministic tie breaking. |
| 7832 | return mView.getAccessibilityViewId() - another.mView.getAccessibilityViewId(); |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 7833 | } |
| 7834 | |
| 7835 | private void init(ViewGroup root, View view) { |
| 7836 | Rect viewLocation = mLocation; |
| 7837 | view.getDrawingRect(viewLocation); |
| 7838 | root.offsetDescendantRectToMyCoords(view, viewLocation); |
| 7839 | mView = view; |
Fabrice Di Meglio | e56ffdc | 2012-09-23 14:51:16 -0700 | [diff] [blame] | 7840 | mLayoutDirection = root.getLayoutDirection(); |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 7841 | } |
| 7842 | |
| 7843 | private void clear() { |
| 7844 | mView = null; |
| 7845 | mLocation.set(0, 0, 0, 0); |
| 7846 | } |
| 7847 | } |
Romain Guy | cbc6774 | 2012-04-27 16:12:57 -0700 | [diff] [blame] | 7848 | |
| 7849 | private static Paint getDebugPaint() { |
| 7850 | if (sDebugPaint == null) { |
| 7851 | sDebugPaint = new Paint(); |
| 7852 | sDebugPaint.setAntiAlias(false); |
| 7853 | } |
| 7854 | return sDebugPaint; |
| 7855 | } |
| 7856 | |
Romain Guy | 6410c0a | 2013-06-17 11:21:58 -0700 | [diff] [blame] | 7857 | 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] | 7858 | if (sDebugLines== null) { |
Romain Guy | 6410c0a | 2013-06-17 11:21:58 -0700 | [diff] [blame] | 7859 | // 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] | 7860 | sDebugLines = new float[16]; |
| 7861 | } |
| 7862 | |
Romain Guy | cbc6774 | 2012-04-27 16:12:57 -0700 | [diff] [blame] | 7863 | sDebugLines[0] = x1; |
| 7864 | sDebugLines[1] = y1; |
| 7865 | sDebugLines[2] = x2; |
| 7866 | sDebugLines[3] = y1; |
| 7867 | |
| 7868 | sDebugLines[4] = x2; |
| 7869 | sDebugLines[5] = y1; |
| 7870 | sDebugLines[6] = x2; |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 7871 | sDebugLines[7] = y2; |
Romain Guy | cbc6774 | 2012-04-27 16:12:57 -0700 | [diff] [blame] | 7872 | |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 7873 | sDebugLines[8] = x2; |
Romain Guy | cbc6774 | 2012-04-27 16:12:57 -0700 | [diff] [blame] | 7874 | sDebugLines[9] = y2; |
| 7875 | sDebugLines[10] = x1; |
| 7876 | sDebugLines[11] = y2; |
| 7877 | |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 7878 | sDebugLines[12] = x1; |
| 7879 | sDebugLines[13] = y2; |
Romain Guy | cbc6774 | 2012-04-27 16:12:57 -0700 | [diff] [blame] | 7880 | sDebugLines[14] = x1; |
| 7881 | sDebugLines[15] = y1; |
| 7882 | |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 7883 | canvas.drawLines(sDebugLines, paint); |
Romain Guy | cbc6774 | 2012-04-27 16:12:57 -0700 | [diff] [blame] | 7884 | } |
Siva Velusamy | 0d857b9 | 2015-04-22 10:23:56 -0700 | [diff] [blame] | 7885 | |
| 7886 | /** @hide */ |
| 7887 | @Override |
| 7888 | protected void encodeProperties(@NonNull ViewHierarchyEncoder encoder) { |
| 7889 | super.encodeProperties(encoder); |
| 7890 | |
| 7891 | encoder.addProperty("focus:descendantFocusability", getDescendantFocusability()); |
| 7892 | encoder.addProperty("drawing:clipChildren", getClipChildren()); |
| 7893 | encoder.addProperty("drawing:clipToPadding", getClipToPadding()); |
| 7894 | encoder.addProperty("drawing:childrenDrawingOrderEnabled", isChildrenDrawingOrderEnabled()); |
| 7895 | encoder.addProperty("drawing:persistentDrawingCache", getPersistentDrawingCache()); |
| 7896 | |
| 7897 | int n = getChildCount(); |
| 7898 | encoder.addProperty("meta:__childCount__", (short)n); |
| 7899 | for (int i = 0; i < n; i++) { |
| 7900 | encoder.addPropertyKey("meta:__child__" + i); |
| 7901 | getChildAt(i).encode(encoder); |
| 7902 | } |
| 7903 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7904 | } |