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; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | import android.content.Context; |
Adam Powell | ff0d298 | 2014-07-10 20:34:14 -0700 | [diff] [blame] | 21 | import android.content.pm.PackageManager; |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 22 | import android.content.res.Configuration; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | import android.content.res.TypedArray; |
| 24 | import android.graphics.Bitmap; |
| 25 | import android.graphics.Canvas; |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 26 | import android.graphics.Color; |
| 27 | import android.graphics.Insets; |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 28 | import android.graphics.Matrix; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | import android.graphics.Paint; |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 30 | import android.graphics.PointF; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 31 | import android.graphics.Rect; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | import android.graphics.RectF; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 33 | import android.graphics.Region; |
Jeff Brown | 995e774 | 2010-12-22 16:59:36 -0800 | [diff] [blame] | 34 | import android.os.Build; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | import android.os.Parcelable; |
| 36 | import android.os.SystemClock; |
| 37 | import android.util.AttributeSet; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | import android.util.Log; |
Svetoslav Ganov | be922dc | 2012-11-30 16:46:26 -0800 | [diff] [blame] | 39 | import android.util.Pools.SynchronizedPool; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | import android.util.SparseArray; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 41 | import android.view.accessibility.AccessibilityEvent; |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 42 | import android.view.accessibility.AccessibilityNodeInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | import android.view.animation.Animation; |
| 44 | import android.view.animation.AnimationUtils; |
| 45 | import android.view.animation.LayoutAnimationController; |
| 46 | import android.view.animation.Transformation; |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 47 | |
Romain Guy | 0211a0a | 2011-02-14 16:34:59 -0800 | [diff] [blame] | 48 | import com.android.internal.R; |
| 49 | import com.android.internal.util.Predicate; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 50 | |
| 51 | import java.util.ArrayList; |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 52 | import java.util.Collections; |
Christopher Tate | 86cab1b | 2011-01-13 20:28:55 -0800 | [diff] [blame] | 53 | import java.util.HashSet; |
George Mount | e180337 | 2014-02-26 19:00:52 +0000 | [diff] [blame] | 54 | import java.util.List; |
| 55 | import java.util.Map; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 57 | import static android.os.Build.VERSION_CODES.JELLY_BEAN_MR1; |
| 58 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | /** |
| 60 | * <p> |
| 61 | * A <code>ViewGroup</code> is a special view that can contain other views |
| 62 | * (called children.) The view group is the base class for layouts and views |
| 63 | * containers. This class also defines the |
| 64 | * {@link android.view.ViewGroup.LayoutParams} class which serves as the base |
| 65 | * class for layouts parameters. |
| 66 | * </p> |
| 67 | * |
| 68 | * <p> |
| 69 | * Also see {@link LayoutParams} for layout attributes. |
| 70 | * </p> |
Romain Guy | d6a463a | 2009-05-21 23:10:10 -0700 | [diff] [blame] | 71 | * |
Joe Fernandez | 558459f | 2011-10-13 16:47:36 -0700 | [diff] [blame] | 72 | * <div class="special reference"> |
| 73 | * <h3>Developer Guides</h3> |
| 74 | * <p>For more information about creating user interface layouts, read the |
| 75 | * <a href="{@docRoot}guide/topics/ui/declaring-layout.html">XML Layouts</a> developer |
| 76 | * guide.</p></div> |
| 77 | * |
Dianne Hackborn | 7caab0f | 2013-03-06 13:47:04 -0800 | [diff] [blame] | 78 | * <p>Here is a complete implementation of a custom ViewGroup that implements |
| 79 | * a simple {@link android.widget.FrameLayout} along with the ability to stack |
| 80 | * children in left and right gutters.</p> |
| 81 | * |
| 82 | * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/CustomLayout.java |
| 83 | * Complete} |
| 84 | * |
| 85 | * <p>If you are implementing XML layout attributes as shown in the example, this is the |
| 86 | * corresponding definition for them that would go in <code>res/values/attrs.xml</code>:</p> |
| 87 | * |
| 88 | * {@sample development/samples/ApiDemos/res/values/attrs.xml CustomLayout} |
| 89 | * |
| 90 | * <p>Finally the layout manager can be used in an XML layout like so:</p> |
| 91 | * |
| 92 | * {@sample development/samples/ApiDemos/res/layout/custom_layout.xml Complete} |
| 93 | * |
Romain Guy | d6a463a | 2009-05-21 23:10:10 -0700 | [diff] [blame] | 94 | * @attr ref android.R.styleable#ViewGroup_clipChildren |
| 95 | * @attr ref android.R.styleable#ViewGroup_clipToPadding |
| 96 | * @attr ref android.R.styleable#ViewGroup_layoutAnimation |
| 97 | * @attr ref android.R.styleable#ViewGroup_animationCache |
| 98 | * @attr ref android.R.styleable#ViewGroup_persistentDrawingCache |
| 99 | * @attr ref android.R.styleable#ViewGroup_alwaysDrawnWithCache |
| 100 | * @attr ref android.R.styleable#ViewGroup_addStatesFromChildren |
| 101 | * @attr ref android.R.styleable#ViewGroup_descendantFocusability |
Chet Haase | 13cc120 | 2010-09-03 15:39:20 -0700 | [diff] [blame] | 102 | * @attr ref android.R.styleable#ViewGroup_animateLayoutChanges |
Scott Main | 27a8508 | 2013-06-10 10:39:48 -0700 | [diff] [blame] | 103 | * @attr ref android.R.styleable#ViewGroup_splitMotionEvents |
| 104 | * @attr ref android.R.styleable#ViewGroup_layoutMode |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 105 | */ |
| 106 | public abstract class ViewGroup extends View implements ViewParent, ViewManager { |
Adam Powell | 539ee87 | 2012-02-03 19:00:49 -0800 | [diff] [blame] | 107 | private static final String TAG = "ViewGroup"; |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 108 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 109 | private static final boolean DBG = false; |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 110 | /** @hide */ |
| 111 | public static boolean DEBUG_DRAW = false; |
Gilles Debunne | cea4513 | 2011-11-24 02:19:27 +0100 | [diff] [blame] | 112 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 113 | /** |
| 114 | * Views which have been hidden or removed which need to be animated on |
| 115 | * their way out. |
| 116 | * This field should be made private, so it is hidden from the SDK. |
| 117 | * {@hide} |
| 118 | */ |
| 119 | protected ArrayList<View> mDisappearingChildren; |
| 120 | |
| 121 | /** |
| 122 | * Listener used to propagate events indicating when children are added |
| 123 | * and/or removed from a view group. |
| 124 | * This field should be made private, so it is hidden from the SDK. |
| 125 | * {@hide} |
| 126 | */ |
| 127 | protected OnHierarchyChangeListener mOnHierarchyChangeListener; |
| 128 | |
| 129 | // The view contained within this ViewGroup that has or contains focus. |
| 130 | private View mFocused; |
| 131 | |
Chet Haase | 4846032 | 2010-06-11 14:22:25 -0700 | [diff] [blame] | 132 | /** |
| 133 | * A Transformation used when drawing children, to |
| 134 | * apply on the child being drawn. |
| 135 | */ |
Romain Guy | f699130 | 2013-06-05 17:19:01 -0700 | [diff] [blame] | 136 | private Transformation mChildTransformation; |
Chet Haase | 4846032 | 2010-06-11 14:22:25 -0700 | [diff] [blame] | 137 | |
| 138 | /** |
| 139 | * Used to track the current invalidation region. |
| 140 | */ |
Chet Haase | 64a48c1 | 2012-02-13 16:33:29 -0800 | [diff] [blame] | 141 | RectF mInvalidateRegion; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 142 | |
Chet Haase | 4846032 | 2010-06-11 14:22:25 -0700 | [diff] [blame] | 143 | /** |
| 144 | * A Transformation used to calculate a correct |
| 145 | * invalidation area when the application is autoscaled. |
| 146 | */ |
Chet Haase | 64a48c1 | 2012-02-13 16:33:29 -0800 | [diff] [blame] | 147 | Transformation mInvalidationTransformation; |
Chet Haase | 4846032 | 2010-06-11 14:22:25 -0700 | [diff] [blame] | 148 | |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 149 | // View currently under an ongoing drag |
| 150 | private View mCurrentDragView; |
| 151 | |
Christopher Tate | 86cab1b | 2011-01-13 20:28:55 -0800 | [diff] [blame] | 152 | // Metadata about the ongoing drag |
| 153 | private DragEvent mCurrentDrag; |
| 154 | private HashSet<View> mDragNotifiedChildren; |
| 155 | |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 156 | // Does this group have a child that can accept the current drag payload? |
| 157 | private boolean mChildAcceptsDrag; |
| 158 | |
| 159 | // Used during drag dispatch |
Romain Guy | 6410c0a | 2013-06-17 11:21:58 -0700 | [diff] [blame] | 160 | private PointF mLocalPoint; |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 161 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 162 | // Layout animation |
| 163 | private LayoutAnimationController mLayoutAnimationController; |
| 164 | private Animation.AnimationListener mAnimationListener; |
| 165 | |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 166 | // First touch target in the linked list of touch targets. |
| 167 | private TouchTarget mFirstTouchTarget; |
| 168 | |
Joe Onorato | 03ab0c7 | 2011-01-06 15:46:27 -0800 | [diff] [blame] | 169 | // For debugging only. You can see these in hierarchyviewer. |
Romain Guy | e95003e | 2011-01-09 13:53:06 -0800 | [diff] [blame] | 170 | @SuppressWarnings({"FieldCanBeLocal", "UnusedDeclaration"}) |
Joe Onorato | 03ab0c7 | 2011-01-06 15:46:27 -0800 | [diff] [blame] | 171 | @ViewDebug.ExportedProperty(category = "events") |
| 172 | private long mLastTouchDownTime; |
| 173 | @ViewDebug.ExportedProperty(category = "events") |
| 174 | private int mLastTouchDownIndex = -1; |
Romain Guy | e95003e | 2011-01-09 13:53:06 -0800 | [diff] [blame] | 175 | @SuppressWarnings({"FieldCanBeLocal", "UnusedDeclaration"}) |
Joe Onorato | 03ab0c7 | 2011-01-06 15:46:27 -0800 | [diff] [blame] | 176 | @ViewDebug.ExportedProperty(category = "events") |
| 177 | private float mLastTouchDownX; |
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 float mLastTouchDownY; |
| 181 | |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 182 | // First hover target in the linked list of hover targets. |
| 183 | // The hover targets are children which have received ACTION_HOVER_ENTER. |
| 184 | // They might not have actually handled the hover event, but we will |
| 185 | // continue sending hover events to them as long as the pointer remains over |
| 186 | // their bounds and the view group does not intercept hover. |
| 187 | private HoverTarget mFirstHoverTarget; |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 188 | |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 189 | // True if the view group itself received a hover event. |
| 190 | // It might not have actually handled the hover event. |
| 191 | private boolean mHoveredSelf; |
| 192 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 193 | /** |
| 194 | * Internal flags. |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 195 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 196 | * This field should be made private, so it is hidden from the SDK. |
| 197 | * {@hide} |
| 198 | */ |
Romain Guy | 2440e67 | 2012-08-07 14:43:43 -0700 | [diff] [blame] | 199 | @ViewDebug.ExportedProperty(flagMapping = { |
| 200 | @ViewDebug.FlagToString(mask = FLAG_CLIP_CHILDREN, equals = FLAG_CLIP_CHILDREN, |
| 201 | name = "CLIP_CHILDREN"), |
| 202 | @ViewDebug.FlagToString(mask = FLAG_CLIP_TO_PADDING, equals = FLAG_CLIP_TO_PADDING, |
| 203 | name = "CLIP_TO_PADDING"), |
| 204 | @ViewDebug.FlagToString(mask = FLAG_PADDING_NOT_NULL, equals = FLAG_PADDING_NOT_NULL, |
| 205 | name = "PADDING_NOT_NULL") |
Jon Miranda | 4597e98 | 2014-07-29 07:25:49 -0700 | [diff] [blame] | 206 | }, formatToHexString = true) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 207 | protected int mGroupFlags; |
| 208 | |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 209 | /** |
| 210 | * Either {@link #LAYOUT_MODE_CLIP_BOUNDS} or {@link #LAYOUT_MODE_OPTICAL_BOUNDS}. |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 211 | */ |
Philip Milne | cfb631b | 2012-10-26 10:51:46 -0700 | [diff] [blame] | 212 | private int mLayoutMode = LAYOUT_MODE_UNDEFINED; |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 213 | |
Romain Guy | 33f6beb | 2012-02-16 19:24:51 -0800 | [diff] [blame] | 214 | /** |
| 215 | * NOTE: If you change the flags below make sure to reflect the changes |
| 216 | * the DisplayList class |
| 217 | */ |
| 218 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 219 | // When set, ViewGroup invalidates only the child's rectangle |
| 220 | // Set by default |
Chet Haase | 64a48c1 | 2012-02-13 16:33:29 -0800 | [diff] [blame] | 221 | static final int FLAG_CLIP_CHILDREN = 0x1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 222 | |
| 223 | // When set, ViewGroup excludes the padding area from the invalidate rectangle |
| 224 | // Set by default |
| 225 | private static final int FLAG_CLIP_TO_PADDING = 0x2; |
| 226 | |
| 227 | // When set, dispatchDraw() will invoke invalidate(); this is set by drawChild() when |
| 228 | // a child needs to be invalidated and FLAG_OPTIMIZE_INVALIDATE is set |
Chet Haase | 64a48c1 | 2012-02-13 16:33:29 -0800 | [diff] [blame] | 229 | static final int FLAG_INVALIDATE_REQUIRED = 0x4; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 230 | |
| 231 | // When set, dispatchDraw() will run the layout animation and unset the flag |
| 232 | private static final int FLAG_RUN_ANIMATION = 0x8; |
| 233 | |
| 234 | // When set, there is either no layout animation on the ViewGroup or the layout |
| 235 | // animation is over |
| 236 | // Set by default |
Chet Haase | 64a48c1 | 2012-02-13 16:33:29 -0800 | [diff] [blame] | 237 | static final int FLAG_ANIMATION_DONE = 0x10; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 238 | |
| 239 | // If set, this ViewGroup has padding; if unset there is no padding and we don't need |
| 240 | // to clip it, even if FLAG_CLIP_TO_PADDING is set |
| 241 | private static final int FLAG_PADDING_NOT_NULL = 0x20; |
| 242 | |
| 243 | // When set, this ViewGroup caches its children in a Bitmap before starting a layout animation |
| 244 | // Set by default |
| 245 | private static final int FLAG_ANIMATION_CACHE = 0x40; |
| 246 | |
| 247 | // When set, this ViewGroup converts calls to invalidate(Rect) to invalidate() during a |
| 248 | // layout animation; this avoid clobbering the hierarchy |
| 249 | // Automatically set when the layout animation starts, depending on the animation's |
| 250 | // characteristics |
Chet Haase | 64a48c1 | 2012-02-13 16:33:29 -0800 | [diff] [blame] | 251 | static final int FLAG_OPTIMIZE_INVALIDATE = 0x80; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 252 | |
| 253 | // When set, the next call to drawChild() will clear mChildTransformation's matrix |
Chet Haase | 64a48c1 | 2012-02-13 16:33:29 -0800 | [diff] [blame] | 254 | static final int FLAG_CLEAR_TRANSFORMATION = 0x100; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 255 | |
| 256 | // When set, this ViewGroup invokes mAnimationListener.onAnimationEnd() and removes |
| 257 | // the children's Bitmap caches if necessary |
| 258 | // This flag is set when the layout animation is over (after FLAG_ANIMATION_DONE is set) |
| 259 | private static final int FLAG_NOTIFY_ANIMATION_LISTENER = 0x200; |
| 260 | |
| 261 | /** |
| 262 | * When set, the drawing method will call {@link #getChildDrawingOrder(int, int)} |
| 263 | * to get the index of the child to draw for that iteration. |
Romain Guy | 293451e | 2009-11-04 13:59:48 -0800 | [diff] [blame] | 264 | * |
| 265 | * @hide |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 266 | */ |
| 267 | protected static final int FLAG_USE_CHILD_DRAWING_ORDER = 0x400; |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 268 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 269 | /** |
| 270 | * When set, this ViewGroup supports static transformations on children; this causes |
| 271 | * {@link #getChildStaticTransformation(View, android.view.animation.Transformation)} to be |
| 272 | * invoked when a child is drawn. |
| 273 | * |
| 274 | * Any subclass overriding |
| 275 | * {@link #getChildStaticTransformation(View, android.view.animation.Transformation)} should |
| 276 | * set this flags in {@link #mGroupFlags}. |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 277 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 278 | * {@hide} |
| 279 | */ |
| 280 | protected static final int FLAG_SUPPORT_STATIC_TRANSFORMATIONS = 0x800; |
| 281 | |
John Reck | fb5899d | 2014-08-15 18:51:27 -0700 | [diff] [blame] | 282 | // UNUSED FLAG VALUE: 0x1000; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 283 | |
| 284 | /** |
| 285 | * When set, this ViewGroup's drawable states also include those |
| 286 | * of its children. |
| 287 | */ |
| 288 | private static final int FLAG_ADD_STATES_FROM_CHILDREN = 0x2000; |
| 289 | |
| 290 | /** |
| 291 | * When set, this ViewGroup tries to always draw its children using their drawing cache. |
| 292 | */ |
Chet Haase | 64a48c1 | 2012-02-13 16:33:29 -0800 | [diff] [blame] | 293 | static final int FLAG_ALWAYS_DRAWN_WITH_CACHE = 0x4000; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 294 | |
| 295 | /** |
| 296 | * When set, and if FLAG_ALWAYS_DRAWN_WITH_CACHE is not set, this ViewGroup will try to |
| 297 | * draw its children with their drawing cache. |
| 298 | */ |
Chet Haase | 64a48c1 | 2012-02-13 16:33:29 -0800 | [diff] [blame] | 299 | static final int FLAG_CHILDREN_DRAWN_WITH_CACHE = 0x8000; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 300 | |
| 301 | /** |
| 302 | * When set, this group will go through its list of children to notify them of |
| 303 | * any drawable state change. |
| 304 | */ |
| 305 | private static final int FLAG_NOTIFY_CHILDREN_ON_DRAWABLE_STATE_CHANGE = 0x10000; |
| 306 | |
| 307 | private static final int FLAG_MASK_FOCUSABILITY = 0x60000; |
| 308 | |
| 309 | /** |
| 310 | * This view will get focus before any of its descendants. |
| 311 | */ |
| 312 | public static final int FOCUS_BEFORE_DESCENDANTS = 0x20000; |
| 313 | |
| 314 | /** |
| 315 | * This view will get focus only if none of its descendants want it. |
| 316 | */ |
| 317 | public static final int FOCUS_AFTER_DESCENDANTS = 0x40000; |
| 318 | |
| 319 | /** |
| 320 | * This view will block any of its descendants from getting focus, even |
| 321 | * if they are focusable. |
| 322 | */ |
| 323 | public static final int FOCUS_BLOCK_DESCENDANTS = 0x60000; |
| 324 | |
| 325 | /** |
| 326 | * Used to map between enum in attrubutes and flag values. |
| 327 | */ |
| 328 | private static final int[] DESCENDANT_FOCUSABILITY_FLAGS = |
| 329 | {FOCUS_BEFORE_DESCENDANTS, FOCUS_AFTER_DESCENDANTS, |
| 330 | FOCUS_BLOCK_DESCENDANTS}; |
| 331 | |
| 332 | /** |
| 333 | * When set, this ViewGroup should not intercept touch events. |
Adam Powell | 110486f | 2010-06-22 17:14:44 -0700 | [diff] [blame] | 334 | * {@hide} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 335 | */ |
Adam Powell | 110486f | 2010-06-22 17:14:44 -0700 | [diff] [blame] | 336 | protected static final int FLAG_DISALLOW_INTERCEPT = 0x80000; |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 337 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 338 | /** |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 339 | * When set, this ViewGroup will split MotionEvents to multiple child Views when appropriate. |
| 340 | */ |
Adam Powell | f37df07 | 2010-09-17 16:22:49 -0700 | [diff] [blame] | 341 | private static final int FLAG_SPLIT_MOTION_EVENTS = 0x200000; |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 342 | |
| 343 | /** |
Adam Powell | 4b86788 | 2011-09-16 12:59:46 -0700 | [diff] [blame] | 344 | * When set, this ViewGroup will not dispatch onAttachedToWindow calls |
| 345 | * to children when adding new views. This is used to prevent multiple |
| 346 | * onAttached calls when a ViewGroup adds children in its own onAttached method. |
| 347 | */ |
| 348 | private static final int FLAG_PREVENT_DISPATCH_ATTACHED_TO_WINDOW = 0x400000; |
| 349 | |
| 350 | /** |
Philip Milne | cfb631b | 2012-10-26 10:51:46 -0700 | [diff] [blame] | 351 | * When true, indicates that a layoutMode has been explicitly set, either with |
| 352 | * an explicit call to {@link #setLayoutMode(int)} in code or from an XML resource. |
| 353 | * This distinguishes the situation in which a layout mode was inherited from |
| 354 | * one of the ViewGroup's ancestors and cached locally. |
| 355 | */ |
| 356 | private static final int FLAG_LAYOUT_MODE_WAS_EXPLICITLY_SET = 0x800000; |
| 357 | |
Chris Craik | b49f446 | 2014-03-20 12:44:20 -0700 | [diff] [blame] | 358 | static final int FLAG_IS_TRANSITION_GROUP = 0x1000000; |
Chris Craik | d863a10 | 2013-12-19 13:31:15 -0800 | [diff] [blame] | 359 | |
Chris Craik | b49f446 | 2014-03-20 12:44:20 -0700 | [diff] [blame] | 360 | static final int FLAG_IS_TRANSITION_GROUP_SET = 0x2000000; |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 361 | |
Chris Craik | d863a10 | 2013-12-19 13:31:15 -0800 | [diff] [blame] | 362 | /** |
Adam Powell | ff0d298 | 2014-07-10 20:34:14 -0700 | [diff] [blame] | 363 | * When set, focus will not be permitted to enter this group if a touchscreen is present. |
| 364 | */ |
| 365 | static final int FLAG_TOUCHSCREEN_BLOCKS_FOCUS = 0x4000000; |
| 366 | |
| 367 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 368 | * Indicates which types of drawing caches are to be kept in memory. |
| 369 | * This field should be made private, so it is hidden from the SDK. |
| 370 | * {@hide} |
| 371 | */ |
| 372 | protected int mPersistentDrawingCache; |
| 373 | |
| 374 | /** |
| 375 | * Used to indicate that no drawing cache should be kept in memory. |
| 376 | */ |
| 377 | public static final int PERSISTENT_NO_CACHE = 0x0; |
| 378 | |
| 379 | /** |
| 380 | * Used to indicate that the animation drawing cache should be kept in memory. |
| 381 | */ |
| 382 | public static final int PERSISTENT_ANIMATION_CACHE = 0x1; |
| 383 | |
| 384 | /** |
| 385 | * Used to indicate that the scrolling drawing cache should be kept in memory. |
| 386 | */ |
| 387 | public static final int PERSISTENT_SCROLLING_CACHE = 0x2; |
| 388 | |
| 389 | /** |
| 390 | * Used to indicate that all drawing caches should be kept in memory. |
| 391 | */ |
| 392 | public static final int PERSISTENT_ALL_CACHES = 0x3; |
| 393 | |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 394 | // Layout Modes |
| 395 | |
Philip Milne | cfb631b | 2012-10-26 10:51:46 -0700 | [diff] [blame] | 396 | private static final int LAYOUT_MODE_UNDEFINED = -1; |
| 397 | |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 398 | /** |
| 399 | * This constant is a {@link #setLayoutMode(int) layoutMode}. |
Philip Milne | 7a23b49 | 2012-04-24 22:12:36 -0700 | [diff] [blame] | 400 | * 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] | 401 | * {@link #getRight() right} and {@link #getBottom() bottom}. |
| 402 | */ |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 403 | public static final int LAYOUT_MODE_CLIP_BOUNDS = 0; |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 404 | |
| 405 | /** |
| 406 | * This constant is a {@link #setLayoutMode(int) layoutMode}. |
Philip Milne | 7a23b49 | 2012-04-24 22:12:36 -0700 | [diff] [blame] | 407 | * Optical bounds describe where a widget appears to be. They sit inside the clip |
| 408 | * bounds which need to cover a larger area to allow other effects, |
| 409 | * such as shadows and glows, to be drawn. |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 410 | */ |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 411 | public static final int LAYOUT_MODE_OPTICAL_BOUNDS = 1; |
| 412 | |
| 413 | /** @hide */ |
Philip Milne | cfb631b | 2012-10-26 10:51:46 -0700 | [diff] [blame] | 414 | public static int LAYOUT_MODE_DEFAULT = LAYOUT_MODE_CLIP_BOUNDS; |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 415 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 416 | /** |
| 417 | * We clip to padding when FLAG_CLIP_TO_PADDING and FLAG_PADDING_NOT_NULL |
| 418 | * are set at the same time. |
| 419 | */ |
| 420 | protected static final int CLIP_TO_PADDING_MASK = FLAG_CLIP_TO_PADDING | FLAG_PADDING_NOT_NULL; |
| 421 | |
| 422 | // Index of the child's left position in the mLocation array |
| 423 | private static final int CHILD_LEFT_INDEX = 0; |
| 424 | // Index of the child's top position in the mLocation array |
| 425 | private static final int CHILD_TOP_INDEX = 1; |
| 426 | |
| 427 | // Child views of this ViewGroup |
| 428 | private View[] mChildren; |
| 429 | // Number of valid children in the mChildren array, the rest should be null or not |
| 430 | // considered as children |
| 431 | private int mChildrenCount; |
| 432 | |
Chet Haase | b989502 | 2013-04-02 15:10:58 -0700 | [diff] [blame] | 433 | // Whether layout calls are currently being suppressed, controlled by calls to |
| 434 | // suppressLayout() |
| 435 | boolean mSuppressLayout = false; |
| 436 | |
| 437 | // Whether any layout calls have actually been suppressed while mSuppressLayout |
| 438 | // has been true. This tracks whether we need to issue a requestLayout() when |
| 439 | // layout is later re-enabled. |
| 440 | private boolean mLayoutCalledWhileSuppressed = false; |
| 441 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 442 | private static final int ARRAY_INITIAL_CAPACITY = 12; |
| 443 | private static final int ARRAY_CAPACITY_INCREMENT = 12; |
| 444 | |
Romain Guy | cbc6774 | 2012-04-27 16:12:57 -0700 | [diff] [blame] | 445 | private static Paint sDebugPaint; |
| 446 | private static float[] sDebugLines; |
Philip Milne | 604f440 | 2012-04-24 19:27:11 -0700 | [diff] [blame] | 447 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 448 | // Used to draw cached views |
Chet Haase | 64a48c1 | 2012-02-13 16:33:29 -0800 | [diff] [blame] | 449 | Paint mCachePaint; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 450 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 451 | // Used to animate add/remove changes in layout |
| 452 | private LayoutTransition mTransition; |
| 453 | |
| 454 | // The set of views that are currently being transitioned. This list is used to track views |
| 455 | // being removed that should not actually be removed from the parent yet because they are |
| 456 | // being animated. |
| 457 | private ArrayList<View> mTransitioningViews; |
| 458 | |
Chet Haase | 5e25c2c | 2010-09-16 11:15:56 -0700 | [diff] [blame] | 459 | // List of children changing visibility. This is used to potentially keep rendering |
| 460 | // views during a transition when they otherwise would have become gone/invisible |
| 461 | private ArrayList<View> mVisibilityChangingChildren; |
| 462 | |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 463 | // Temporary holder of presorted children, only used for |
| 464 | // input/software draw dispatch for correctly Z ordering. |
| 465 | private ArrayList<View> mPreSortedChildren; |
| 466 | |
Adam Powell | 539ee87 | 2012-02-03 19:00:49 -0800 | [diff] [blame] | 467 | // Indicates how many of this container's child subtrees contain transient state |
| 468 | @ViewDebug.ExportedProperty(category = "layout") |
| 469 | private int mChildCountWithTransientState = 0; |
| 470 | |
Adam Powell | 10ba277 | 2014-04-15 09:46:51 -0700 | [diff] [blame] | 471 | /** |
| 472 | * Currently registered axes for nested scrolling. Flag set consisting of |
| 473 | * {@link #SCROLL_AXIS_HORIZONTAL} {@link #SCROLL_AXIS_VERTICAL} or {@link #SCROLL_AXIS_NONE} |
| 474 | * for null. |
| 475 | */ |
| 476 | private int mNestedScrollAxes; |
| 477 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 478 | public ViewGroup(Context context) { |
Alan Viverette | d6479ec | 2013-09-10 17:03:02 -0700 | [diff] [blame] | 479 | this(context, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | public ViewGroup(Context context, AttributeSet attrs) { |
Alan Viverette | d6479ec | 2013-09-10 17:03:02 -0700 | [diff] [blame] | 483 | this(context, attrs, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 484 | } |
| 485 | |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 486 | public ViewGroup(Context context, AttributeSet attrs, int defStyleAttr) { |
Alan Viverette | d6479ec | 2013-09-10 17:03:02 -0700 | [diff] [blame] | 487 | this(context, attrs, defStyleAttr, 0); |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | public ViewGroup(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 491 | super(context, attrs, defStyleAttr, defStyleRes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 492 | initViewGroup(); |
Alan Viverette | d6479ec | 2013-09-10 17:03:02 -0700 | [diff] [blame] | 493 | initFromAttributes(context, attrs, defStyleAttr, defStyleRes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 494 | } |
| 495 | |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 496 | private boolean debugDraw() { |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 497 | return DEBUG_DRAW || mAttachInfo != null && mAttachInfo.mDebugLayout; |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 498 | } |
| 499 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 500 | private void initViewGroup() { |
| 501 | // ViewGroup doesn't draw by default |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 502 | if (!debugDraw()) { |
| 503 | setFlags(WILL_NOT_DRAW, DRAW_MASK); |
| 504 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 505 | mGroupFlags |= FLAG_CLIP_CHILDREN; |
| 506 | mGroupFlags |= FLAG_CLIP_TO_PADDING; |
| 507 | mGroupFlags |= FLAG_ANIMATION_DONE; |
| 508 | mGroupFlags |= FLAG_ANIMATION_CACHE; |
| 509 | mGroupFlags |= FLAG_ALWAYS_DRAWN_WITH_CACHE; |
| 510 | |
Jeff Brown | 995e774 | 2010-12-22 16:59:36 -0800 | [diff] [blame] | 511 | if (mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.HONEYCOMB) { |
| 512 | mGroupFlags |= FLAG_SPLIT_MOTION_EVENTS; |
| 513 | } |
| 514 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 515 | setDescendantFocusability(FOCUS_BEFORE_DESCENDANTS); |
| 516 | |
| 517 | mChildren = new View[ARRAY_INITIAL_CAPACITY]; |
| 518 | mChildrenCount = 0; |
| 519 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 520 | mPersistentDrawingCache = PERSISTENT_SCROLLING_CACHE; |
| 521 | } |
| 522 | |
Alan Viverette | d6479ec | 2013-09-10 17:03:02 -0700 | [diff] [blame] | 523 | private void initFromAttributes( |
| 524 | Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
Filip Gruszczyński | b50cea0 | 2014-03-05 17:54:58 -0800 | [diff] [blame] | 525 | final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ViewGroup, defStyleAttr, |
| 526 | defStyleRes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 527 | |
| 528 | final int N = a.getIndexCount(); |
| 529 | for (int i = 0; i < N; i++) { |
| 530 | int attr = a.getIndex(i); |
| 531 | switch (attr) { |
| 532 | case R.styleable.ViewGroup_clipChildren: |
| 533 | setClipChildren(a.getBoolean(attr, true)); |
| 534 | break; |
| 535 | case R.styleable.ViewGroup_clipToPadding: |
| 536 | setClipToPadding(a.getBoolean(attr, true)); |
| 537 | break; |
| 538 | case R.styleable.ViewGroup_animationCache: |
| 539 | setAnimationCacheEnabled(a.getBoolean(attr, true)); |
| 540 | break; |
| 541 | case R.styleable.ViewGroup_persistentDrawingCache: |
| 542 | setPersistentDrawingCache(a.getInt(attr, PERSISTENT_SCROLLING_CACHE)); |
| 543 | break; |
| 544 | case R.styleable.ViewGroup_addStatesFromChildren: |
| 545 | setAddStatesFromChildren(a.getBoolean(attr, false)); |
| 546 | break; |
| 547 | case R.styleable.ViewGroup_alwaysDrawnWithCache: |
| 548 | setAlwaysDrawnWithCacheEnabled(a.getBoolean(attr, true)); |
| 549 | break; |
| 550 | case R.styleable.ViewGroup_layoutAnimation: |
| 551 | int id = a.getResourceId(attr, -1); |
| 552 | if (id > 0) { |
| 553 | setLayoutAnimation(AnimationUtils.loadLayoutAnimation(mContext, id)); |
| 554 | } |
| 555 | break; |
| 556 | case R.styleable.ViewGroup_descendantFocusability: |
| 557 | setDescendantFocusability(DESCENDANT_FOCUSABILITY_FLAGS[a.getInt(attr, 0)]); |
| 558 | break; |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 559 | case R.styleable.ViewGroup_splitMotionEvents: |
| 560 | setMotionEventSplittingEnabled(a.getBoolean(attr, false)); |
| 561 | break; |
Chet Haase | 13cc120 | 2010-09-03 15:39:20 -0700 | [diff] [blame] | 562 | case R.styleable.ViewGroup_animateLayoutChanges: |
| 563 | boolean animateLayoutChanges = a.getBoolean(attr, false); |
| 564 | if (animateLayoutChanges) { |
| 565 | setLayoutTransition(new LayoutTransition()); |
| 566 | } |
| 567 | break; |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 568 | case R.styleable.ViewGroup_layoutMode: |
Philip Milne | cfb631b | 2012-10-26 10:51:46 -0700 | [diff] [blame] | 569 | setLayoutMode(a.getInt(attr, LAYOUT_MODE_UNDEFINED)); |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 570 | break; |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 571 | case R.styleable.ViewGroup_transitionGroup: |
| 572 | setTransitionGroup(a.getBoolean(attr, false)); |
| 573 | break; |
Adam Powell | ff0d298 | 2014-07-10 20:34:14 -0700 | [diff] [blame] | 574 | case R.styleable.ViewGroup_touchscreenBlocksFocus: |
| 575 | setTouchscreenBlocksFocus(a.getBoolean(attr, false)); |
| 576 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 577 | } |
| 578 | } |
| 579 | |
| 580 | a.recycle(); |
| 581 | } |
| 582 | |
| 583 | /** |
| 584 | * Gets the descendant focusability of this view group. The descendant |
| 585 | * focusability defines the relationship between this view group and its |
| 586 | * descendants when looking for a view to take focus in |
| 587 | * {@link #requestFocus(int, android.graphics.Rect)}. |
| 588 | * |
| 589 | * @return one of {@link #FOCUS_BEFORE_DESCENDANTS}, {@link #FOCUS_AFTER_DESCENDANTS}, |
| 590 | * {@link #FOCUS_BLOCK_DESCENDANTS}. |
| 591 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 592 | @ViewDebug.ExportedProperty(category = "focus", mapping = { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 593 | @ViewDebug.IntToString(from = FOCUS_BEFORE_DESCENDANTS, to = "FOCUS_BEFORE_DESCENDANTS"), |
| 594 | @ViewDebug.IntToString(from = FOCUS_AFTER_DESCENDANTS, to = "FOCUS_AFTER_DESCENDANTS"), |
| 595 | @ViewDebug.IntToString(from = FOCUS_BLOCK_DESCENDANTS, to = "FOCUS_BLOCK_DESCENDANTS") |
| 596 | }) |
| 597 | public int getDescendantFocusability() { |
| 598 | return mGroupFlags & FLAG_MASK_FOCUSABILITY; |
| 599 | } |
| 600 | |
| 601 | /** |
| 602 | * Set the descendant focusability of this view group. This defines the relationship |
| 603 | * between this view group and its descendants when looking for a view to |
| 604 | * take focus in {@link #requestFocus(int, android.graphics.Rect)}. |
| 605 | * |
| 606 | * @param focusability one of {@link #FOCUS_BEFORE_DESCENDANTS}, {@link #FOCUS_AFTER_DESCENDANTS}, |
| 607 | * {@link #FOCUS_BLOCK_DESCENDANTS}. |
| 608 | */ |
| 609 | public void setDescendantFocusability(int focusability) { |
| 610 | switch (focusability) { |
| 611 | case FOCUS_BEFORE_DESCENDANTS: |
| 612 | case FOCUS_AFTER_DESCENDANTS: |
| 613 | case FOCUS_BLOCK_DESCENDANTS: |
| 614 | break; |
| 615 | default: |
| 616 | throw new IllegalArgumentException("must be one of FOCUS_BEFORE_DESCENDANTS, " |
| 617 | + "FOCUS_AFTER_DESCENDANTS, FOCUS_BLOCK_DESCENDANTS"); |
| 618 | } |
| 619 | mGroupFlags &= ~FLAG_MASK_FOCUSABILITY; |
| 620 | mGroupFlags |= (focusability & FLAG_MASK_FOCUSABILITY); |
| 621 | } |
| 622 | |
| 623 | /** |
| 624 | * {@inheritDoc} |
| 625 | */ |
| 626 | @Override |
| 627 | void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) { |
| 628 | if (mFocused != null) { |
Alan Viverette | 223622a | 2013-12-17 13:29:02 -0800 | [diff] [blame] | 629 | mFocused.unFocus(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 630 | mFocused = null; |
| 631 | } |
| 632 | super.handleFocusGainInternal(direction, previouslyFocusedRect); |
| 633 | } |
| 634 | |
| 635 | /** |
| 636 | * {@inheritDoc} |
| 637 | */ |
| 638 | public void requestChildFocus(View child, View focused) { |
| 639 | if (DBG) { |
| 640 | System.out.println(this + " requestChildFocus()"); |
| 641 | } |
| 642 | if (getDescendantFocusability() == FOCUS_BLOCK_DESCENDANTS) { |
| 643 | return; |
| 644 | } |
| 645 | |
| 646 | // Unfocus us, if necessary |
Alan Viverette | 223622a | 2013-12-17 13:29:02 -0800 | [diff] [blame] | 647 | super.unFocus(focused); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 648 | |
| 649 | // We had a previous notion of who had focus. Clear it. |
| 650 | if (mFocused != child) { |
| 651 | if (mFocused != null) { |
Alan Viverette | 223622a | 2013-12-17 13:29:02 -0800 | [diff] [blame] | 652 | mFocused.unFocus(focused); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | mFocused = child; |
| 656 | } |
| 657 | if (mParent != null) { |
| 658 | mParent.requestChildFocus(this, focused); |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | /** |
| 663 | * {@inheritDoc} |
| 664 | */ |
| 665 | public void focusableViewAvailable(View v) { |
| 666 | if (mParent != null |
| 667 | // shortcut: don't report a new focusable view if we block our descendants from |
| 668 | // getting focus |
| 669 | && (getDescendantFocusability() != FOCUS_BLOCK_DESCENDANTS) |
Adam Powell | 88c1175 | 2014-07-21 17:19:16 -0700 | [diff] [blame] | 670 | && (isFocusableInTouchMode() || !shouldBlockFocusForTouchscreen()) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 671 | // shortcut: don't report a new focusable view if we already are focused |
| 672 | // (and we don't prefer our descendants) |
| 673 | // |
| 674 | // note: knowing that mFocused is non-null is not a good enough reason |
| 675 | // to break the traversal since in that case we'd actually have to find |
| 676 | // 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] | 677 | // 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] | 678 | && !(isFocused() && getDescendantFocusability() != FOCUS_AFTER_DESCENDANTS)) { |
| 679 | mParent.focusableViewAvailable(v); |
| 680 | } |
| 681 | } |
| 682 | |
| 683 | /** |
| 684 | * {@inheritDoc} |
| 685 | */ |
| 686 | public boolean showContextMenuForChild(View originalView) { |
| 687 | return mParent != null && mParent.showContextMenuForChild(originalView); |
| 688 | } |
| 689 | |
| 690 | /** |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 691 | * {@inheritDoc} |
| 692 | */ |
| 693 | public ActionMode startActionModeForChild(View originalView, ActionMode.Callback callback) { |
| 694 | return mParent != null ? mParent.startActionModeForChild(originalView, callback) : null; |
| 695 | } |
| 696 | |
| 697 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 698 | * Find the nearest view in the specified direction that wants to take |
| 699 | * focus. |
| 700 | * |
| 701 | * @param focused The view that currently has focus |
| 702 | * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and |
| 703 | * FOCUS_RIGHT, or 0 for not applicable. |
| 704 | */ |
| 705 | public View focusSearch(View focused, int direction) { |
Svetoslav Ganov | 27e2da7 | 2012-07-02 18:12:00 -0700 | [diff] [blame] | 706 | if (isRootNamespace()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 707 | // root namespace means we should consider ourselves the top of the |
| 708 | // tree for focus searching; otherwise we could be focus searching |
| 709 | // into other tabs. see LocalActivityManager and TabHost for more info |
| 710 | return FocusFinder.getInstance().findNextFocus(this, focused, direction); |
| 711 | } else if (mParent != null) { |
| 712 | return mParent.focusSearch(focused, direction); |
| 713 | } |
| 714 | return null; |
| 715 | } |
| 716 | |
| 717 | /** |
| 718 | * {@inheritDoc} |
| 719 | */ |
| 720 | public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) { |
| 721 | return false; |
| 722 | } |
| 723 | |
| 724 | /** |
| 725 | * {@inheritDoc} |
| 726 | */ |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 727 | @Override |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 728 | public boolean requestSendAccessibilityEvent(View child, AccessibilityEvent event) { |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 729 | ViewParent parent = mParent; |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 730 | if (parent == null) { |
| 731 | return false; |
| 732 | } |
| 733 | final boolean propagate = onRequestSendAccessibilityEvent(child, event); |
| 734 | if (!propagate) { |
| 735 | return false; |
| 736 | } |
| 737 | return parent.requestSendAccessibilityEvent(this, event); |
| 738 | } |
| 739 | |
| 740 | /** |
| 741 | * Called when a child has requested sending an {@link AccessibilityEvent} and |
| 742 | * gives an opportunity to its parent to augment the event. |
Svetoslav Ganov | 031d9c1 | 2011-09-09 16:41:13 -0700 | [diff] [blame] | 743 | * <p> |
Adam Powell | 2fcbbd0 | 2011-09-28 18:56:43 -0700 | [diff] [blame] | 744 | * If an {@link android.view.View.AccessibilityDelegate} has been specified via calling |
| 745 | * {@link android.view.View#setAccessibilityDelegate(android.view.View.AccessibilityDelegate)} its |
| 746 | * {@link android.view.View.AccessibilityDelegate#onRequestSendAccessibilityEvent(ViewGroup, View, AccessibilityEvent)} |
Svetoslav Ganov | 031d9c1 | 2011-09-09 16:41:13 -0700 | [diff] [blame] | 747 | * is responsible for handling this call. |
| 748 | * </p> |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 749 | * |
| 750 | * @param child The child which requests sending the event. |
| 751 | * @param event The event to be sent. |
| 752 | * @return True if the event should be sent. |
| 753 | * |
| 754 | * @see #requestSendAccessibilityEvent(View, AccessibilityEvent) |
| 755 | */ |
| 756 | public boolean onRequestSendAccessibilityEvent(View child, AccessibilityEvent event) { |
Svetoslav Ganov | 031d9c1 | 2011-09-09 16:41:13 -0700 | [diff] [blame] | 757 | if (mAccessibilityDelegate != null) { |
| 758 | return mAccessibilityDelegate.onRequestSendAccessibilityEvent(this, child, event); |
| 759 | } else { |
| 760 | return onRequestSendAccessibilityEventInternal(child, event); |
| 761 | } |
| 762 | } |
| 763 | |
| 764 | /** |
| 765 | * @see #onRequestSendAccessibilityEvent(View, AccessibilityEvent) |
| 766 | * |
| 767 | * Note: Called from the default {@link View.AccessibilityDelegate}. |
| 768 | */ |
| 769 | boolean onRequestSendAccessibilityEventInternal(View child, AccessibilityEvent event) { |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 770 | return true; |
| 771 | } |
| 772 | |
| 773 | /** |
Adam Powell | 539ee87 | 2012-02-03 19:00:49 -0800 | [diff] [blame] | 774 | * 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] | 775 | */ |
| 776 | public void childHasTransientStateChanged(View child, boolean childHasTransientState) { |
| 777 | final boolean oldHasTransientState = hasTransientState(); |
| 778 | if (childHasTransientState) { |
| 779 | mChildCountWithTransientState++; |
| 780 | } else { |
| 781 | mChildCountWithTransientState--; |
| 782 | } |
| 783 | |
| 784 | final boolean newHasTransientState = hasTransientState(); |
| 785 | if (mParent != null && oldHasTransientState != newHasTransientState) { |
| 786 | try { |
| 787 | mParent.childHasTransientStateChanged(this, newHasTransientState); |
| 788 | } catch (AbstractMethodError e) { |
| 789 | Log.e(TAG, mParent.getClass().getSimpleName() + |
| 790 | " does not fully implement ViewParent", e); |
| 791 | } |
| 792 | } |
| 793 | } |
| 794 | |
Adam Powell | 539ee87 | 2012-02-03 19:00:49 -0800 | [diff] [blame] | 795 | @Override |
| 796 | public boolean hasTransientState() { |
| 797 | return mChildCountWithTransientState > 0 || super.hasTransientState(); |
| 798 | } |
| 799 | |
| 800 | /** |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 801 | * {@inheritDoc} |
| 802 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 803 | @Override |
| 804 | public boolean dispatchUnhandledMove(View focused, int direction) { |
| 805 | return mFocused != null && |
| 806 | mFocused.dispatchUnhandledMove(focused, direction); |
| 807 | } |
| 808 | |
| 809 | /** |
| 810 | * {@inheritDoc} |
| 811 | */ |
| 812 | public void clearChildFocus(View child) { |
| 813 | if (DBG) { |
| 814 | System.out.println(this + " clearChildFocus()"); |
| 815 | } |
| 816 | |
| 817 | mFocused = null; |
| 818 | if (mParent != null) { |
| 819 | mParent.clearChildFocus(this); |
| 820 | } |
| 821 | } |
| 822 | |
| 823 | /** |
| 824 | * {@inheritDoc} |
| 825 | */ |
| 826 | @Override |
| 827 | public void clearFocus() { |
Svetoslav Ganov | b36a0ac | 2012-02-14 17:46:47 -0800 | [diff] [blame] | 828 | if (DBG) { |
| 829 | System.out.println(this + " clearFocus()"); |
| 830 | } |
| 831 | if (mFocused == null) { |
| 832 | super.clearFocus(); |
| 833 | } else { |
Svetoslav Ganov | b552d89 | 2012-06-02 14:35:02 -0700 | [diff] [blame] | 834 | View focused = mFocused; |
Svetoslav Ganov | b36a0ac | 2012-02-14 17:46:47 -0800 | [diff] [blame] | 835 | mFocused = null; |
Svetoslav Ganov | b552d89 | 2012-06-02 14:35:02 -0700 | [diff] [blame] | 836 | focused.clearFocus(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 837 | } |
| 838 | } |
| 839 | |
| 840 | /** |
| 841 | * {@inheritDoc} |
| 842 | */ |
| 843 | @Override |
Alan Viverette | 223622a | 2013-12-17 13:29:02 -0800 | [diff] [blame] | 844 | void unFocus(View focused) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 845 | if (DBG) { |
| 846 | System.out.println(this + " unFocus()"); |
| 847 | } |
Svetoslav Ganov | b36a0ac | 2012-02-14 17:46:47 -0800 | [diff] [blame] | 848 | if (mFocused == null) { |
Alan Viverette | 223622a | 2013-12-17 13:29:02 -0800 | [diff] [blame] | 849 | super.unFocus(focused); |
Svetoslav Ganov | b36a0ac | 2012-02-14 17:46:47 -0800 | [diff] [blame] | 850 | } else { |
Alan Viverette | 223622a | 2013-12-17 13:29:02 -0800 | [diff] [blame] | 851 | mFocused.unFocus(focused); |
Svetoslav Ganov | b36a0ac | 2012-02-14 17:46:47 -0800 | [diff] [blame] | 852 | mFocused = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 853 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 854 | } |
| 855 | |
| 856 | /** |
| 857 | * Returns the focused child of this view, if any. The child may have focus |
| 858 | * or contain focus. |
| 859 | * |
| 860 | * @return the focused child or null. |
| 861 | */ |
| 862 | public View getFocusedChild() { |
| 863 | return mFocused; |
| 864 | } |
| 865 | |
Adam Powell | 88c1175 | 2014-07-21 17:19:16 -0700 | [diff] [blame] | 866 | View getDeepestFocusedChild() { |
| 867 | View v = this; |
| 868 | while (v != null) { |
| 869 | if (v.isFocused()) { |
| 870 | return v; |
| 871 | } |
| 872 | v = v instanceof ViewGroup ? ((ViewGroup) v).getFocusedChild() : null; |
| 873 | } |
| 874 | return null; |
| 875 | } |
| 876 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 877 | /** |
| 878 | * Returns true if this view has or contains focus |
| 879 | * |
| 880 | * @return true if this view has or contains focus |
| 881 | */ |
| 882 | @Override |
| 883 | public boolean hasFocus() { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 884 | return (mPrivateFlags & PFLAG_FOCUSED) != 0 || mFocused != null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | /* |
| 888 | * (non-Javadoc) |
| 889 | * |
| 890 | * @see android.view.View#findFocus() |
| 891 | */ |
| 892 | @Override |
| 893 | public View findFocus() { |
| 894 | if (DBG) { |
| 895 | System.out.println("Find focus in " + this + ": flags=" |
| 896 | + isFocused() + ", child=" + mFocused); |
| 897 | } |
| 898 | |
| 899 | if (isFocused()) { |
| 900 | return this; |
| 901 | } |
| 902 | |
| 903 | if (mFocused != null) { |
| 904 | return mFocused.findFocus(); |
| 905 | } |
| 906 | return null; |
| 907 | } |
| 908 | |
| 909 | /** |
| 910 | * {@inheritDoc} |
| 911 | */ |
| 912 | @Override |
| 913 | public boolean hasFocusable() { |
| 914 | if ((mViewFlags & VISIBILITY_MASK) != VISIBLE) { |
| 915 | return false; |
| 916 | } |
| 917 | |
| 918 | if (isFocusable()) { |
| 919 | return true; |
| 920 | } |
| 921 | |
| 922 | final int descendantFocusability = getDescendantFocusability(); |
Adam Powell | 88c1175 | 2014-07-21 17:19:16 -0700 | [diff] [blame] | 923 | if (descendantFocusability != FOCUS_BLOCK_DESCENDANTS) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 924 | final int count = mChildrenCount; |
| 925 | final View[] children = mChildren; |
| 926 | |
| 927 | for (int i = 0; i < count; i++) { |
| 928 | final View child = children[i]; |
| 929 | if (child.hasFocusable()) { |
| 930 | return true; |
| 931 | } |
| 932 | } |
| 933 | } |
| 934 | |
| 935 | return false; |
| 936 | } |
| 937 | |
| 938 | /** |
| 939 | * {@inheritDoc} |
| 940 | */ |
| 941 | @Override |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 942 | 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] | 943 | final int focusableCount = views.size(); |
| 944 | |
| 945 | final int descendantFocusability = getDescendantFocusability(); |
| 946 | |
Adam Powell | 88c1175 | 2014-07-21 17:19:16 -0700 | [diff] [blame] | 947 | if (descendantFocusability != FOCUS_BLOCK_DESCENDANTS) { |
| 948 | if (shouldBlockFocusForTouchscreen()) { |
| 949 | focusableMode |= FOCUSABLES_TOUCH_MODE; |
| 950 | } |
| 951 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 952 | final int count = mChildrenCount; |
| 953 | final View[] children = mChildren; |
| 954 | |
| 955 | for (int i = 0; i < count; i++) { |
| 956 | final View child = children[i]; |
| 957 | if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) { |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 958 | child.addFocusables(views, direction, focusableMode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 959 | } |
| 960 | } |
| 961 | } |
| 962 | |
| 963 | // we add ourselves (if focusable) in all cases except for when we are |
| 964 | // FOCUS_AFTER_DESCENDANTS and there are some descendants focusable. this is |
| 965 | // to avoid the focus search finding layouts when a more precise search |
| 966 | // among the focusable children would be more interesting. |
Adam Powell | ff0d298 | 2014-07-10 20:34:14 -0700 | [diff] [blame] | 967 | if ((descendantFocusability != FOCUS_AFTER_DESCENDANTS |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 968 | // No focusable descendants |
Adam Powell | 88c1175 | 2014-07-21 17:19:16 -0700 | [diff] [blame] | 969 | || (focusableCount == views.size())) && |
| 970 | (isFocusableInTouchMode() || !shouldBlockFocusForTouchscreen())) { |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 971 | super.addFocusables(views, direction, focusableMode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 972 | } |
| 973 | } |
| 974 | |
Adam Powell | ff0d298 | 2014-07-10 20:34:14 -0700 | [diff] [blame] | 975 | /** |
| 976 | * Set whether this ViewGroup should ignore focus requests for itself and its children. |
| 977 | * If this option is enabled and the ViewGroup or a descendant currently has focus, focus |
| 978 | * will proceed forward. |
| 979 | * |
| 980 | * @param touchscreenBlocksFocus true to enable blocking focus in the presence of a touchscreen |
| 981 | */ |
| 982 | public void setTouchscreenBlocksFocus(boolean touchscreenBlocksFocus) { |
| 983 | if (touchscreenBlocksFocus) { |
| 984 | mGroupFlags |= FLAG_TOUCHSCREEN_BLOCKS_FOCUS; |
| 985 | if (hasFocus()) { |
Adam Powell | 88c1175 | 2014-07-21 17:19:16 -0700 | [diff] [blame] | 986 | final View focusedChild = getDeepestFocusedChild(); |
| 987 | if (!focusedChild.isFocusableInTouchMode()) { |
| 988 | final View newFocus = focusSearch(FOCUS_FORWARD); |
| 989 | if (newFocus != null) { |
| 990 | newFocus.requestFocus(); |
| 991 | } |
Adam Powell | ff0d298 | 2014-07-10 20:34:14 -0700 | [diff] [blame] | 992 | } |
| 993 | } |
| 994 | } else { |
| 995 | mGroupFlags &= ~FLAG_TOUCHSCREEN_BLOCKS_FOCUS; |
| 996 | } |
| 997 | } |
| 998 | |
| 999 | /** |
| 1000 | * Check whether this ViewGroup should ignore focus requests for itself and its children. |
| 1001 | */ |
| 1002 | public boolean getTouchscreenBlocksFocus() { |
| 1003 | return (mGroupFlags & FLAG_TOUCHSCREEN_BLOCKS_FOCUS) != 0; |
| 1004 | } |
| 1005 | |
| 1006 | boolean shouldBlockFocusForTouchscreen() { |
| 1007 | return getTouchscreenBlocksFocus() && |
| 1008 | mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_TOUCHSCREEN); |
| 1009 | } |
| 1010 | |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 1011 | @Override |
Svetoslav Ganov | ea515ae | 2011-09-14 18:15:32 -0700 | [diff] [blame] | 1012 | public void findViewsWithText(ArrayList<View> outViews, CharSequence text, int flags) { |
| 1013 | super.findViewsWithText(outViews, text, flags); |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 1014 | final int childrenCount = mChildrenCount; |
| 1015 | final View[] children = mChildren; |
| 1016 | for (int i = 0; i < childrenCount; i++) { |
| 1017 | View child = children[i]; |
Svetoslav Ganov | ea515ae | 2011-09-14 18:15:32 -0700 | [diff] [blame] | 1018 | if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 1019 | && (child.mPrivateFlags & PFLAG_IS_ROOT_NAMESPACE) == 0) { |
Svetoslav Ganov | ea515ae | 2011-09-14 18:15:32 -0700 | [diff] [blame] | 1020 | child.findViewsWithText(outViews, text, flags); |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 1021 | } |
| 1022 | } |
| 1023 | } |
| 1024 | |
Svetoslav | 5b578da | 2013-05-08 14:23:32 -0700 | [diff] [blame] | 1025 | /** @hide */ |
Svetoslav Ganov | 2cdedff | 2011-10-03 14:18:42 -0700 | [diff] [blame] | 1026 | @Override |
Svetoslav | 5b578da | 2013-05-08 14:23:32 -0700 | [diff] [blame] | 1027 | public View findViewByAccessibilityIdTraversal(int accessibilityId) { |
Svetoslav Ganov | 2cdedff | 2011-10-03 14:18:42 -0700 | [diff] [blame] | 1028 | View foundView = super.findViewByAccessibilityIdTraversal(accessibilityId); |
| 1029 | if (foundView != null) { |
| 1030 | return foundView; |
| 1031 | } |
| 1032 | final int childrenCount = mChildrenCount; |
| 1033 | final View[] children = mChildren; |
| 1034 | for (int i = 0; i < childrenCount; i++) { |
| 1035 | View child = children[i]; |
| 1036 | foundView = child.findViewByAccessibilityIdTraversal(accessibilityId); |
| 1037 | if (foundView != null) { |
| 1038 | return foundView; |
| 1039 | } |
| 1040 | } |
| 1041 | return null; |
| 1042 | } |
| 1043 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1044 | /** |
| 1045 | * {@inheritDoc} |
| 1046 | */ |
| 1047 | @Override |
| 1048 | public void dispatchWindowFocusChanged(boolean hasFocus) { |
| 1049 | super.dispatchWindowFocusChanged(hasFocus); |
| 1050 | final int count = mChildrenCount; |
| 1051 | final View[] children = mChildren; |
| 1052 | for (int i = 0; i < count; i++) { |
| 1053 | children[i].dispatchWindowFocusChanged(hasFocus); |
| 1054 | } |
| 1055 | } |
| 1056 | |
| 1057 | /** |
| 1058 | * {@inheritDoc} |
| 1059 | */ |
| 1060 | @Override |
| 1061 | public void addTouchables(ArrayList<View> views) { |
| 1062 | super.addTouchables(views); |
| 1063 | |
| 1064 | final int count = mChildrenCount; |
| 1065 | final View[] children = mChildren; |
| 1066 | |
| 1067 | for (int i = 0; i < count; i++) { |
| 1068 | final View child = children[i]; |
| 1069 | if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) { |
| 1070 | child.addTouchables(views); |
| 1071 | } |
| 1072 | } |
| 1073 | } |
Dianne Hackborn | 3a3a6cf | 2012-03-26 10:24:04 -0700 | [diff] [blame] | 1074 | |
| 1075 | /** |
| 1076 | * @hide |
| 1077 | */ |
| 1078 | @Override |
| 1079 | public void makeOptionalFitsSystemWindows() { |
| 1080 | super.makeOptionalFitsSystemWindows(); |
| 1081 | final int count = mChildrenCount; |
| 1082 | final View[] children = mChildren; |
| 1083 | for (int i = 0; i < count; i++) { |
| 1084 | children[i].makeOptionalFitsSystemWindows(); |
| 1085 | } |
| 1086 | } |
| 1087 | |
Romain Guy | 43c9cdf | 2010-01-27 13:53:55 -0800 | [diff] [blame] | 1088 | /** |
| 1089 | * {@inheritDoc} |
| 1090 | */ |
| 1091 | @Override |
| 1092 | public void dispatchDisplayHint(int hint) { |
| 1093 | super.dispatchDisplayHint(hint); |
| 1094 | final int count = mChildrenCount; |
| 1095 | final View[] children = mChildren; |
| 1096 | for (int i = 0; i < count; i++) { |
| 1097 | children[i].dispatchDisplayHint(hint); |
| 1098 | } |
| 1099 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1100 | |
| 1101 | /** |
Chet Haase | 0d29936 | 2012-01-26 10:51:48 -0800 | [diff] [blame] | 1102 | * Called when a view's visibility has changed. Notify the parent to take any appropriate |
| 1103 | * action. |
| 1104 | * |
| 1105 | * @param child The view whose visibility has changed |
| 1106 | * @param oldVisibility The previous visibility value (GONE, INVISIBLE, or VISIBLE). |
| 1107 | * @param newVisibility The new visibility value (GONE, INVISIBLE, or VISIBLE). |
Chet Haase | 5e25c2c | 2010-09-16 11:15:56 -0700 | [diff] [blame] | 1108 | * @hide |
Chet Haase | 5e25c2c | 2010-09-16 11:15:56 -0700 | [diff] [blame] | 1109 | */ |
Chet Haase | 0d29936 | 2012-01-26 10:51:48 -0800 | [diff] [blame] | 1110 | protected void onChildVisibilityChanged(View child, int oldVisibility, int newVisibility) { |
Chet Haase | 5e25c2c | 2010-09-16 11:15:56 -0700 | [diff] [blame] | 1111 | if (mTransition != null) { |
Chet Haase | 0d29936 | 2012-01-26 10:51:48 -0800 | [diff] [blame] | 1112 | if (newVisibility == VISIBLE) { |
| 1113 | mTransition.showChild(this, child, oldVisibility); |
Chet Haase | 5e25c2c | 2010-09-16 11:15:56 -0700 | [diff] [blame] | 1114 | } else { |
Chet Haase | 0d29936 | 2012-01-26 10:51:48 -0800 | [diff] [blame] | 1115 | mTransition.hideChild(this, child, newVisibility); |
Chet Haase | 5e25c2c | 2010-09-16 11:15:56 -0700 | [diff] [blame] | 1116 | if (mTransitioningViews != null && mTransitioningViews.contains(child)) { |
Chet Haase | ddbb346 | 2012-06-19 13:54:29 -0700 | [diff] [blame] | 1117 | // Only track this on disappearing views - appearing views are already visible |
| 1118 | // and don't need special handling during drawChild() |
| 1119 | if (mVisibilityChangingChildren == null) { |
| 1120 | mVisibilityChangingChildren = new ArrayList<View>(); |
| 1121 | } |
| 1122 | mVisibilityChangingChildren.add(child); |
Chet Haase | 5e25c2c | 2010-09-16 11:15:56 -0700 | [diff] [blame] | 1123 | addDisappearingView(child); |
| 1124 | } |
| 1125 | } |
| 1126 | } |
Christopher Tate | 86cab1b | 2011-01-13 20:28:55 -0800 | [diff] [blame] | 1127 | |
| 1128 | // in all cases, for drags |
| 1129 | if (mCurrentDrag != null) { |
Chet Haase | 0d29936 | 2012-01-26 10:51:48 -0800 | [diff] [blame] | 1130 | if (newVisibility == VISIBLE) { |
Christopher Tate | 86cab1b | 2011-01-13 20:28:55 -0800 | [diff] [blame] | 1131 | notifyChildOfDrag(child); |
| 1132 | } |
| 1133 | } |
Chet Haase | 5e25c2c | 2010-09-16 11:15:56 -0700 | [diff] [blame] | 1134 | } |
| 1135 | |
| 1136 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1137 | * {@inheritDoc} |
| 1138 | */ |
| 1139 | @Override |
Adam Powell | 326d808 | 2009-12-09 15:10:07 -0800 | [diff] [blame] | 1140 | protected void dispatchVisibilityChanged(View changedView, int visibility) { |
| 1141 | super.dispatchVisibilityChanged(changedView, visibility); |
| 1142 | final int count = mChildrenCount; |
| 1143 | final View[] children = mChildren; |
| 1144 | for (int i = 0; i < count; i++) { |
| 1145 | children[i].dispatchVisibilityChanged(changedView, visibility); |
| 1146 | } |
| 1147 | } |
| 1148 | |
| 1149 | /** |
| 1150 | * {@inheritDoc} |
| 1151 | */ |
| 1152 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1153 | public void dispatchWindowVisibilityChanged(int visibility) { |
| 1154 | super.dispatchWindowVisibilityChanged(visibility); |
| 1155 | final int count = mChildrenCount; |
| 1156 | final View[] children = mChildren; |
| 1157 | for (int i = 0; i < count; i++) { |
| 1158 | children[i].dispatchWindowVisibilityChanged(visibility); |
| 1159 | } |
| 1160 | } |
| 1161 | |
| 1162 | /** |
| 1163 | * {@inheritDoc} |
| 1164 | */ |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 1165 | @Override |
| 1166 | public void dispatchConfigurationChanged(Configuration newConfig) { |
| 1167 | super.dispatchConfigurationChanged(newConfig); |
| 1168 | final int count = mChildrenCount; |
| 1169 | final View[] children = mChildren; |
| 1170 | for (int i = 0; i < count; i++) { |
| 1171 | children[i].dispatchConfigurationChanged(newConfig); |
| 1172 | } |
| 1173 | } |
| 1174 | |
| 1175 | /** |
| 1176 | * {@inheritDoc} |
| 1177 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1178 | public void recomputeViewAttributes(View child) { |
Joe Onorato | 664644d | 2011-01-23 17:53:23 -0800 | [diff] [blame] | 1179 | if (mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) { |
| 1180 | ViewParent parent = mParent; |
| 1181 | if (parent != null) parent.recomputeViewAttributes(this); |
| 1182 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1183 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 1184 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1185 | @Override |
Dianne Hackborn | 3a3a6cf | 2012-03-26 10:24:04 -0700 | [diff] [blame] | 1186 | void dispatchCollectViewAttributes(AttachInfo attachInfo, int visibility) { |
| 1187 | if ((visibility & VISIBILITY_MASK) == VISIBLE) { |
| 1188 | super.dispatchCollectViewAttributes(attachInfo, visibility); |
| 1189 | final int count = mChildrenCount; |
| 1190 | final View[] children = mChildren; |
| 1191 | for (int i = 0; i < count; i++) { |
| 1192 | final View child = children[i]; |
| 1193 | child.dispatchCollectViewAttributes(attachInfo, |
| 1194 | visibility | (child.mViewFlags&VISIBILITY_MASK)); |
| 1195 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1196 | } |
| 1197 | } |
| 1198 | |
| 1199 | /** |
| 1200 | * {@inheritDoc} |
| 1201 | */ |
| 1202 | public void bringChildToFront(View child) { |
| 1203 | int index = indexOfChild(child); |
| 1204 | if (index >= 0) { |
| 1205 | removeFromArray(index); |
| 1206 | addInArray(child, mChildrenCount); |
| 1207 | child.mParent = this; |
Chet Haase | cb96db8 | 2013-09-04 10:21:46 -0700 | [diff] [blame] | 1208 | requestLayout(); |
| 1209 | invalidate(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1210 | } |
| 1211 | } |
| 1212 | |
Romain Guy | 6410c0a | 2013-06-17 11:21:58 -0700 | [diff] [blame] | 1213 | private PointF getLocalPoint() { |
| 1214 | if (mLocalPoint == null) mLocalPoint = new PointF(); |
| 1215 | return mLocalPoint; |
| 1216 | } |
| 1217 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1218 | /** |
| 1219 | * {@inheritDoc} |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1220 | */ |
Steve Block | 8a7259b | 2012-03-01 11:24:41 +0000 | [diff] [blame] | 1221 | // TODO: Write real docs |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1222 | @Override |
| 1223 | public boolean dispatchDragEvent(DragEvent event) { |
| 1224 | boolean retval = false; |
| 1225 | final float tx = event.mX; |
| 1226 | final float ty = event.mY; |
| 1227 | |
Dianne Hackborn | 6dd005b | 2011-07-18 13:22:50 -0700 | [diff] [blame] | 1228 | ViewRootImpl root = getViewRootImpl(); |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1229 | |
| 1230 | // Dispatch down the view hierarchy |
Romain Guy | 6410c0a | 2013-06-17 11:21:58 -0700 | [diff] [blame] | 1231 | final PointF localPoint = getLocalPoint(); |
| 1232 | |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1233 | switch (event.mAction) { |
| 1234 | case DragEvent.ACTION_DRAG_STARTED: { |
| 1235 | // clear state to recalculate which views we drag over |
Chris Tate | 9d1ab88 | 2010-11-02 15:55:39 -0700 | [diff] [blame] | 1236 | mCurrentDragView = null; |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1237 | |
Christopher Tate | 86cab1b | 2011-01-13 20:28:55 -0800 | [diff] [blame] | 1238 | // Set up our tracking of drag-started notifications |
| 1239 | mCurrentDrag = DragEvent.obtain(event); |
| 1240 | if (mDragNotifiedChildren == null) { |
| 1241 | mDragNotifiedChildren = new HashSet<View>(); |
| 1242 | } else { |
| 1243 | mDragNotifiedChildren.clear(); |
| 1244 | } |
| 1245 | |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1246 | // Now dispatch down to our children, caching the responses |
| 1247 | mChildAcceptsDrag = false; |
| 1248 | final int count = mChildrenCount; |
| 1249 | final View[] children = mChildren; |
| 1250 | for (int i = 0; i < count; i++) { |
Christopher Tate | 2c095f3 | 2010-10-04 14:13:40 -0700 | [diff] [blame] | 1251 | final View child = children[i]; |
Christopher Tate | 3d4bf17 | 2011-03-28 16:16:46 -0700 | [diff] [blame] | 1252 | child.mPrivateFlags2 &= ~View.DRAG_MASK; |
Christopher Tate | 2c095f3 | 2010-10-04 14:13:40 -0700 | [diff] [blame] | 1253 | if (child.getVisibility() == VISIBLE) { |
Christopher Tate | 86cab1b | 2011-01-13 20:28:55 -0800 | [diff] [blame] | 1254 | final boolean handled = notifyChildOfDrag(children[i]); |
Christopher Tate | 2c095f3 | 2010-10-04 14:13:40 -0700 | [diff] [blame] | 1255 | if (handled) { |
| 1256 | mChildAcceptsDrag = true; |
| 1257 | } |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1258 | } |
| 1259 | } |
| 1260 | |
| 1261 | // Return HANDLED if one of our children can accept the drag |
| 1262 | if (mChildAcceptsDrag) { |
| 1263 | retval = true; |
| 1264 | } |
| 1265 | } break; |
| 1266 | |
| 1267 | case DragEvent.ACTION_DRAG_ENDED: { |
Christopher Tate | 86cab1b | 2011-01-13 20:28:55 -0800 | [diff] [blame] | 1268 | // Release the bookkeeping now that the drag lifecycle has ended |
Christopher Tate | 1fc014f | 2011-01-19 12:56:26 -0800 | [diff] [blame] | 1269 | if (mDragNotifiedChildren != null) { |
| 1270 | for (View child : mDragNotifiedChildren) { |
| 1271 | // If a child was notified about an ongoing drag, it's told that it's over |
| 1272 | child.dispatchDragEvent(event); |
Christopher Tate | 3d4bf17 | 2011-03-28 16:16:46 -0700 | [diff] [blame] | 1273 | child.mPrivateFlags2 &= ~View.DRAG_MASK; |
| 1274 | child.refreshDrawableState(); |
Christopher Tate | 1fc014f | 2011-01-19 12:56:26 -0800 | [diff] [blame] | 1275 | } |
| 1276 | |
| 1277 | mDragNotifiedChildren.clear(); |
Christopher Tate | e9accff | 2013-03-04 12:57:23 -0800 | [diff] [blame] | 1278 | if (mCurrentDrag != null) { |
| 1279 | mCurrentDrag.recycle(); |
| 1280 | mCurrentDrag = null; |
| 1281 | } |
Christopher Tate | 1fc014f | 2011-01-19 12:56:26 -0800 | [diff] [blame] | 1282 | } |
Christopher Tate | 86cab1b | 2011-01-13 20:28:55 -0800 | [diff] [blame] | 1283 | |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1284 | // We consider drag-ended to have been handled if one of our children |
| 1285 | // had offered to handle the drag. |
| 1286 | if (mChildAcceptsDrag) { |
| 1287 | retval = true; |
| 1288 | } |
| 1289 | } break; |
| 1290 | |
| 1291 | case DragEvent.ACTION_DRAG_LOCATION: { |
| 1292 | // Find the [possibly new] drag target |
Romain Guy | 6410c0a | 2013-06-17 11:21:58 -0700 | [diff] [blame] | 1293 | final View target = findFrontmostDroppableChildAt(event.mX, event.mY, localPoint); |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1294 | |
| 1295 | // 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] | 1296 | // we're over now [for purposes of the eventual drag-recipient-changed |
| 1297 | // notifications to the framework] and tell the new target that the drag |
| 1298 | // has entered its bounds. The root will see setDragFocus() calls all |
| 1299 | // the way down to the final leaf view that is handling the LOCATION event |
| 1300 | // before reporting the new potential recipient to the framework. |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1301 | if (mCurrentDragView != target) { |
Chris Tate | 9d1ab88 | 2010-11-02 15:55:39 -0700 | [diff] [blame] | 1302 | root.setDragFocus(target); |
| 1303 | |
| 1304 | final int action = event.mAction; |
| 1305 | // If we've dragged off of a child view, send it the EXITED message |
| 1306 | if (mCurrentDragView != null) { |
Christopher Tate | 3d4bf17 | 2011-03-28 16:16:46 -0700 | [diff] [blame] | 1307 | final View view = mCurrentDragView; |
Chris Tate | 9d1ab88 | 2010-11-02 15:55:39 -0700 | [diff] [blame] | 1308 | event.mAction = DragEvent.ACTION_DRAG_EXITED; |
Christopher Tate | 3d4bf17 | 2011-03-28 16:16:46 -0700 | [diff] [blame] | 1309 | view.dispatchDragEvent(event); |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 1310 | view.mPrivateFlags2 &= ~View.PFLAG2_DRAG_HOVERED; |
Christopher Tate | 3d4bf17 | 2011-03-28 16:16:46 -0700 | [diff] [blame] | 1311 | view.refreshDrawableState(); |
Chris Tate | 9d1ab88 | 2010-11-02 15:55:39 -0700 | [diff] [blame] | 1312 | } |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1313 | mCurrentDragView = target; |
Chris Tate | 9d1ab88 | 2010-11-02 15:55:39 -0700 | [diff] [blame] | 1314 | |
| 1315 | // If we've dragged over a new child view, send it the ENTERED message |
| 1316 | if (target != null) { |
| 1317 | event.mAction = DragEvent.ACTION_DRAG_ENTERED; |
| 1318 | target.dispatchDragEvent(event); |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 1319 | target.mPrivateFlags2 |= View.PFLAG2_DRAG_HOVERED; |
Christopher Tate | 3d4bf17 | 2011-03-28 16:16:46 -0700 | [diff] [blame] | 1320 | target.refreshDrawableState(); |
Chris Tate | 9d1ab88 | 2010-11-02 15:55:39 -0700 | [diff] [blame] | 1321 | } |
| 1322 | event.mAction = action; // restore the event's original state |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1323 | } |
Christopher Tate | 2c095f3 | 2010-10-04 14:13:40 -0700 | [diff] [blame] | 1324 | |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1325 | // Dispatch the actual drag location notice, localized into its coordinates |
| 1326 | if (target != null) { |
Romain Guy | 6410c0a | 2013-06-17 11:21:58 -0700 | [diff] [blame] | 1327 | event.mX = localPoint.x; |
| 1328 | event.mY = localPoint.y; |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1329 | |
| 1330 | retval = target.dispatchDragEvent(event); |
| 1331 | |
| 1332 | event.mX = tx; |
| 1333 | event.mY = ty; |
| 1334 | } |
| 1335 | } break; |
| 1336 | |
Chris Tate | 9d1ab88 | 2010-11-02 15:55:39 -0700 | [diff] [blame] | 1337 | /* Entered / exited dispatch |
| 1338 | * |
| 1339 | * DRAG_ENTERED is not dispatched downwards from ViewGroup. The reason for this is |
| 1340 | * that we're about to get the corresponding LOCATION event, which we will use to |
| 1341 | * determine which of our children is the new target; at that point we will |
| 1342 | * push a DRAG_ENTERED down to the new target child [which may itself be a ViewGroup]. |
| 1343 | * |
| 1344 | * DRAG_EXITED *is* dispatched all the way down immediately: once we know the |
| 1345 | * drag has left this ViewGroup, we know by definition that every contained subview |
| 1346 | * is also no longer under the drag point. |
| 1347 | */ |
| 1348 | |
| 1349 | case DragEvent.ACTION_DRAG_EXITED: { |
| 1350 | if (mCurrentDragView != null) { |
Christopher Tate | 3d4bf17 | 2011-03-28 16:16:46 -0700 | [diff] [blame] | 1351 | final View view = mCurrentDragView; |
| 1352 | view.dispatchDragEvent(event); |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 1353 | view.mPrivateFlags2 &= ~View.PFLAG2_DRAG_HOVERED; |
Christopher Tate | 3d4bf17 | 2011-03-28 16:16:46 -0700 | [diff] [blame] | 1354 | view.refreshDrawableState(); |
| 1355 | |
Chris Tate | 9d1ab88 | 2010-11-02 15:55:39 -0700 | [diff] [blame] | 1356 | mCurrentDragView = null; |
| 1357 | } |
| 1358 | } break; |
| 1359 | |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1360 | case DragEvent.ACTION_DROP: { |
Christopher Tate | 2c095f3 | 2010-10-04 14:13:40 -0700 | [diff] [blame] | 1361 | 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] | 1362 | View target = findFrontmostDroppableChildAt(event.mX, event.mY, localPoint); |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1363 | if (target != null) { |
Christopher Tate | 5ada6cb | 2010-10-05 14:15:29 -0700 | [diff] [blame] | 1364 | 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] | 1365 | event.mX = localPoint.x; |
| 1366 | event.mY = localPoint.y; |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1367 | retval = target.dispatchDragEvent(event); |
| 1368 | event.mX = tx; |
| 1369 | event.mY = ty; |
Christopher Tate | 5ada6cb | 2010-10-05 14:15:29 -0700 | [diff] [blame] | 1370 | } else { |
| 1371 | if (ViewDebug.DEBUG_DRAG) { |
| 1372 | Log.d(View.VIEW_LOG_TAG, " not dropped on an accepting view"); |
| 1373 | } |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1374 | } |
| 1375 | } break; |
| 1376 | } |
| 1377 | |
| 1378 | // If none of our children could handle the event, try here |
| 1379 | if (!retval) { |
Chris Tate | 32affef | 2010-10-18 15:29:21 -0700 | [diff] [blame] | 1380 | // Call up to the View implementation that dispatches to installed listeners |
| 1381 | retval = super.dispatchDragEvent(event); |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1382 | } |
| 1383 | return retval; |
| 1384 | } |
| 1385 | |
| 1386 | // Find the frontmost child view that lies under the given point, and calculate |
| 1387 | // the position within its own local coordinate system. |
| 1388 | View findFrontmostDroppableChildAt(float x, float y, PointF outLocalPoint) { |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1389 | final int count = mChildrenCount; |
| 1390 | final View[] children = mChildren; |
| 1391 | for (int i = count - 1; i >= 0; i--) { |
| 1392 | final View child = children[i]; |
Christopher Tate | 3d4bf17 | 2011-03-28 16:16:46 -0700 | [diff] [blame] | 1393 | if (!child.canAcceptDrag()) { |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1394 | continue; |
| 1395 | } |
| 1396 | |
Christopher Tate | 2c095f3 | 2010-10-04 14:13:40 -0700 | [diff] [blame] | 1397 | if (isTransformedTouchPointInView(x, y, child, outLocalPoint)) { |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1398 | return child; |
| 1399 | } |
| 1400 | } |
| 1401 | return null; |
| 1402 | } |
| 1403 | |
Christopher Tate | 86cab1b | 2011-01-13 20:28:55 -0800 | [diff] [blame] | 1404 | boolean notifyChildOfDrag(View child) { |
| 1405 | if (ViewDebug.DEBUG_DRAG) { |
| 1406 | Log.d(View.VIEW_LOG_TAG, "Sending drag-started to view: " + child); |
| 1407 | } |
| 1408 | |
Christopher Tate | 3d4bf17 | 2011-03-28 16:16:46 -0700 | [diff] [blame] | 1409 | boolean canAccept = false; |
Christopher Tate | 86cab1b | 2011-01-13 20:28:55 -0800 | [diff] [blame] | 1410 | if (! mDragNotifiedChildren.contains(child)) { |
| 1411 | mDragNotifiedChildren.add(child); |
Christopher Tate | 3d4bf17 | 2011-03-28 16:16:46 -0700 | [diff] [blame] | 1412 | canAccept = child.dispatchDragEvent(mCurrentDrag); |
| 1413 | if (canAccept && !child.canAcceptDrag()) { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 1414 | child.mPrivateFlags2 |= View.PFLAG2_DRAG_CAN_ACCEPT; |
Christopher Tate | 3d4bf17 | 2011-03-28 16:16:46 -0700 | [diff] [blame] | 1415 | child.refreshDrawableState(); |
| 1416 | } |
Christopher Tate | 86cab1b | 2011-01-13 20:28:55 -0800 | [diff] [blame] | 1417 | } |
Christopher Tate | 3d4bf17 | 2011-03-28 16:16:46 -0700 | [diff] [blame] | 1418 | return canAccept; |
Christopher Tate | 86cab1b | 2011-01-13 20:28:55 -0800 | [diff] [blame] | 1419 | } |
| 1420 | |
Joe Onorato | 664644d | 2011-01-23 17:53:23 -0800 | [diff] [blame] | 1421 | @Override |
Dianne Hackborn | 3a3a6cf | 2012-03-26 10:24:04 -0700 | [diff] [blame] | 1422 | public void dispatchWindowSystemUiVisiblityChanged(int visible) { |
| 1423 | super.dispatchWindowSystemUiVisiblityChanged(visible); |
| 1424 | |
| 1425 | final int count = mChildrenCount; |
| 1426 | final View[] children = mChildren; |
| 1427 | for (int i=0; i <count; i++) { |
| 1428 | final View child = children[i]; |
| 1429 | child.dispatchWindowSystemUiVisiblityChanged(visible); |
| 1430 | } |
| 1431 | } |
| 1432 | |
| 1433 | @Override |
Joe Onorato | 664644d | 2011-01-23 17:53:23 -0800 | [diff] [blame] | 1434 | public void dispatchSystemUiVisibilityChanged(int visible) { |
| 1435 | super.dispatchSystemUiVisibilityChanged(visible); |
| 1436 | |
| 1437 | final int count = mChildrenCount; |
| 1438 | final View[] children = mChildren; |
| 1439 | for (int i=0; i <count; i++) { |
| 1440 | final View child = children[i]; |
| 1441 | child.dispatchSystemUiVisibilityChanged(visible); |
| 1442 | } |
| 1443 | } |
| 1444 | |
Dianne Hackborn | 9a230e0 | 2011-10-06 11:51:27 -0700 | [diff] [blame] | 1445 | @Override |
Dianne Hackborn | cf67578 | 2012-05-10 15:07:24 -0700 | [diff] [blame] | 1446 | boolean updateLocalSystemUiVisibility(int localValue, int localChanges) { |
| 1447 | boolean changed = super.updateLocalSystemUiVisibility(localValue, localChanges); |
Dianne Hackborn | 9a230e0 | 2011-10-06 11:51:27 -0700 | [diff] [blame] | 1448 | |
| 1449 | final int count = mChildrenCount; |
| 1450 | final View[] children = mChildren; |
| 1451 | for (int i=0; i <count; i++) { |
| 1452 | final View child = children[i]; |
Dianne Hackborn | cf67578 | 2012-05-10 15:07:24 -0700 | [diff] [blame] | 1453 | changed |= child.updateLocalSystemUiVisibility(localValue, localChanges); |
Dianne Hackborn | 9a230e0 | 2011-10-06 11:51:27 -0700 | [diff] [blame] | 1454 | } |
Dianne Hackborn | cf67578 | 2012-05-10 15:07:24 -0700 | [diff] [blame] | 1455 | return changed; |
Dianne Hackborn | 9a230e0 | 2011-10-06 11:51:27 -0700 | [diff] [blame] | 1456 | } |
| 1457 | |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1458 | /** |
| 1459 | * {@inheritDoc} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1460 | */ |
| 1461 | @Override |
| 1462 | public boolean dispatchKeyEventPreIme(KeyEvent event) { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 1463 | if ((mPrivateFlags & (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) |
| 1464 | == (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1465 | return super.dispatchKeyEventPreIme(event); |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 1466 | } else if (mFocused != null && (mFocused.mPrivateFlags & PFLAG_HAS_BOUNDS) |
| 1467 | == PFLAG_HAS_BOUNDS) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1468 | return mFocused.dispatchKeyEventPreIme(event); |
| 1469 | } |
| 1470 | return false; |
| 1471 | } |
| 1472 | |
| 1473 | /** |
| 1474 | * {@inheritDoc} |
| 1475 | */ |
| 1476 | @Override |
| 1477 | public boolean dispatchKeyEvent(KeyEvent event) { |
Jeff Brown | 21bc5c9 | 2011-02-28 18:27:14 -0800 | [diff] [blame] | 1478 | if (mInputEventConsistencyVerifier != null) { |
| 1479 | mInputEventConsistencyVerifier.onKeyEvent(event, 1); |
| 1480 | } |
| 1481 | |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 1482 | if ((mPrivateFlags & (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) |
| 1483 | == (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) { |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 1484 | if (super.dispatchKeyEvent(event)) { |
| 1485 | return true; |
| 1486 | } |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 1487 | } else if (mFocused != null && (mFocused.mPrivateFlags & PFLAG_HAS_BOUNDS) |
| 1488 | == PFLAG_HAS_BOUNDS) { |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 1489 | if (mFocused.dispatchKeyEvent(event)) { |
| 1490 | return true; |
| 1491 | } |
| 1492 | } |
| 1493 | |
| 1494 | if (mInputEventConsistencyVerifier != null) { |
| 1495 | mInputEventConsistencyVerifier.onUnhandledEvent(event, 1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1496 | } |
| 1497 | return false; |
| 1498 | } |
| 1499 | |
| 1500 | /** |
| 1501 | * {@inheritDoc} |
| 1502 | */ |
| 1503 | @Override |
| 1504 | public boolean dispatchKeyShortcutEvent(KeyEvent event) { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 1505 | if ((mPrivateFlags & (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) |
| 1506 | == (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1507 | return super.dispatchKeyShortcutEvent(event); |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 1508 | } else if (mFocused != null && (mFocused.mPrivateFlags & PFLAG_HAS_BOUNDS) |
| 1509 | == PFLAG_HAS_BOUNDS) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1510 | return mFocused.dispatchKeyShortcutEvent(event); |
| 1511 | } |
| 1512 | return false; |
| 1513 | } |
| 1514 | |
| 1515 | /** |
| 1516 | * {@inheritDoc} |
| 1517 | */ |
| 1518 | @Override |
| 1519 | public boolean dispatchTrackballEvent(MotionEvent event) { |
Jeff Brown | 21bc5c9 | 2011-02-28 18:27:14 -0800 | [diff] [blame] | 1520 | if (mInputEventConsistencyVerifier != null) { |
| 1521 | mInputEventConsistencyVerifier.onTrackballEvent(event, 1); |
| 1522 | } |
| 1523 | |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 1524 | if ((mPrivateFlags & (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) |
| 1525 | == (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) { |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 1526 | if (super.dispatchTrackballEvent(event)) { |
| 1527 | return true; |
| 1528 | } |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 1529 | } else if (mFocused != null && (mFocused.mPrivateFlags & PFLAG_HAS_BOUNDS) |
| 1530 | == PFLAG_HAS_BOUNDS) { |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 1531 | if (mFocused.dispatchTrackballEvent(event)) { |
| 1532 | return true; |
| 1533 | } |
| 1534 | } |
| 1535 | |
| 1536 | if (mInputEventConsistencyVerifier != null) { |
| 1537 | mInputEventConsistencyVerifier.onUnhandledEvent(event, 1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1538 | } |
| 1539 | return false; |
| 1540 | } |
| 1541 | |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 1542 | /** |
| 1543 | * {@inheritDoc} |
| 1544 | */ |
Romain Guy | a948927 | 2011-06-22 20:58:11 -0700 | [diff] [blame] | 1545 | @SuppressWarnings({"ConstantConditions"}) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1546 | @Override |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 1547 | protected boolean dispatchHoverEvent(MotionEvent event) { |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 1548 | final int action = event.getAction(); |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 1549 | |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 1550 | // First check whether the view group wants to intercept the hover event. |
| 1551 | final boolean interceptHover = onInterceptHoverEvent(event); |
| 1552 | event.setAction(action); // restore action in case it was changed |
| 1553 | |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 1554 | MotionEvent eventNoHistory = event; |
| 1555 | boolean handled = false; |
| 1556 | |
| 1557 | // Send events to the hovered children and build a new list of hover targets until |
| 1558 | // one is found that handles the event. |
| 1559 | HoverTarget firstOldHoverTarget = mFirstHoverTarget; |
| 1560 | mFirstHoverTarget = null; |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 1561 | if (!interceptHover && action != MotionEvent.ACTION_HOVER_EXIT) { |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 1562 | final float x = event.getX(); |
| 1563 | final float y = event.getY(); |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 1564 | final int childrenCount = mChildrenCount; |
| 1565 | if (childrenCount != 0) { |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 1566 | final ArrayList<View> preorderedList = buildOrderedChildList(); |
| 1567 | final boolean customOrder = preorderedList == null |
| 1568 | && isChildrenDrawingOrderEnabled(); |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 1569 | final View[] children = mChildren; |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 1570 | HoverTarget lastHoverTarget = null; |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 1571 | for (int i = childrenCount - 1; i >= 0; i--) { |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 1572 | int childIndex = customOrder ? getChildDrawingOrder(childrenCount, i) : i; |
| 1573 | final View child = (preorderedList == null) |
| 1574 | ? children[childIndex] : preorderedList.get(childIndex); |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 1575 | if (!canViewReceivePointerEvents(child) |
| 1576 | || !isTransformedTouchPointInView(x, y, child, null)) { |
| 1577 | continue; |
| 1578 | } |
| 1579 | |
| 1580 | // Obtain a hover target for this child. Dequeue it from the |
| 1581 | // old hover target list if the child was previously hovered. |
| 1582 | HoverTarget hoverTarget = firstOldHoverTarget; |
| 1583 | final boolean wasHovered; |
| 1584 | for (HoverTarget predecessor = null; ;) { |
| 1585 | if (hoverTarget == null) { |
| 1586 | hoverTarget = HoverTarget.obtain(child); |
| 1587 | wasHovered = false; |
| 1588 | break; |
| 1589 | } |
| 1590 | |
| 1591 | if (hoverTarget.child == child) { |
| 1592 | if (predecessor != null) { |
| 1593 | predecessor.next = hoverTarget.next; |
| 1594 | } else { |
| 1595 | firstOldHoverTarget = hoverTarget.next; |
| 1596 | } |
| 1597 | hoverTarget.next = null; |
| 1598 | wasHovered = true; |
| 1599 | break; |
| 1600 | } |
| 1601 | |
| 1602 | predecessor = hoverTarget; |
| 1603 | hoverTarget = hoverTarget.next; |
| 1604 | } |
| 1605 | |
| 1606 | // Enqueue the hover target onto the new hover target list. |
| 1607 | if (lastHoverTarget != null) { |
| 1608 | lastHoverTarget.next = hoverTarget; |
| 1609 | } else { |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 1610 | mFirstHoverTarget = hoverTarget; |
| 1611 | } |
Sangkyu Lee | 8725f36 | 2013-03-13 09:38:45 +0900 | [diff] [blame] | 1612 | lastHoverTarget = hoverTarget; |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 1613 | |
| 1614 | // Dispatch the event to the child. |
| 1615 | if (action == MotionEvent.ACTION_HOVER_ENTER) { |
| 1616 | if (!wasHovered) { |
| 1617 | // Send the enter as is. |
| 1618 | handled |= dispatchTransformedGenericPointerEvent( |
| 1619 | event, child); // enter |
| 1620 | } |
| 1621 | } else if (action == MotionEvent.ACTION_HOVER_MOVE) { |
| 1622 | if (!wasHovered) { |
| 1623 | // Synthesize an enter from a move. |
| 1624 | eventNoHistory = obtainMotionEventNoHistoryOrSelf(eventNoHistory); |
| 1625 | eventNoHistory.setAction(MotionEvent.ACTION_HOVER_ENTER); |
| 1626 | handled |= dispatchTransformedGenericPointerEvent( |
| 1627 | eventNoHistory, child); // enter |
| 1628 | eventNoHistory.setAction(action); |
| 1629 | |
| 1630 | handled |= dispatchTransformedGenericPointerEvent( |
| 1631 | eventNoHistory, child); // move |
| 1632 | } else { |
| 1633 | // Send the move as is. |
| 1634 | handled |= dispatchTransformedGenericPointerEvent(event, child); |
| 1635 | } |
| 1636 | } |
| 1637 | if (handled) { |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 1638 | break; |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 1639 | } |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 1640 | } |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 1641 | if (preorderedList != null) preorderedList.clear(); |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 1642 | } |
| 1643 | } |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 1644 | |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 1645 | // Send exit events to all previously hovered children that are no longer hovered. |
| 1646 | while (firstOldHoverTarget != null) { |
| 1647 | final View child = firstOldHoverTarget.child; |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 1648 | |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 1649 | // Exit the old hovered child. |
| 1650 | if (action == MotionEvent.ACTION_HOVER_EXIT) { |
| 1651 | // Send the exit as is. |
| 1652 | handled |= dispatchTransformedGenericPointerEvent( |
| 1653 | event, child); // exit |
| 1654 | } else { |
| 1655 | // Synthesize an exit from a move or enter. |
| 1656 | // Ignore the result because hover focus has moved to a different view. |
| 1657 | if (action == MotionEvent.ACTION_HOVER_MOVE) { |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 1658 | dispatchTransformedGenericPointerEvent( |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 1659 | event, child); // move |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 1660 | } |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 1661 | eventNoHistory = obtainMotionEventNoHistoryOrSelf(eventNoHistory); |
| 1662 | eventNoHistory.setAction(MotionEvent.ACTION_HOVER_EXIT); |
| 1663 | dispatchTransformedGenericPointerEvent( |
| 1664 | eventNoHistory, child); // exit |
| 1665 | eventNoHistory.setAction(action); |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 1666 | } |
| 1667 | |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 1668 | final HoverTarget nextOldHoverTarget = firstOldHoverTarget.next; |
| 1669 | firstOldHoverTarget.recycle(); |
| 1670 | firstOldHoverTarget = nextOldHoverTarget; |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 1671 | } |
| 1672 | |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 1673 | // 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] | 1674 | boolean newHoveredSelf = !handled; |
| 1675 | if (newHoveredSelf == mHoveredSelf) { |
| 1676 | if (newHoveredSelf) { |
| 1677 | // Send event to the view group as before. |
| 1678 | handled |= super.dispatchHoverEvent(event); |
| 1679 | } |
| 1680 | } else { |
| 1681 | if (mHoveredSelf) { |
| 1682 | // Exit the view group. |
| 1683 | if (action == MotionEvent.ACTION_HOVER_EXIT) { |
| 1684 | // Send the exit as is. |
| 1685 | handled |= super.dispatchHoverEvent(event); // exit |
| 1686 | } else { |
| 1687 | // Synthesize an exit from a move or enter. |
| 1688 | // Ignore the result because hover focus is moving to a different view. |
| 1689 | if (action == MotionEvent.ACTION_HOVER_MOVE) { |
| 1690 | super.dispatchHoverEvent(event); // move |
| 1691 | } |
| 1692 | eventNoHistory = obtainMotionEventNoHistoryOrSelf(eventNoHistory); |
| 1693 | eventNoHistory.setAction(MotionEvent.ACTION_HOVER_EXIT); |
| 1694 | super.dispatchHoverEvent(eventNoHistory); // exit |
| 1695 | eventNoHistory.setAction(action); |
| 1696 | } |
| 1697 | mHoveredSelf = false; |
| 1698 | } |
| 1699 | |
| 1700 | if (newHoveredSelf) { |
| 1701 | // Enter the view group. |
| 1702 | if (action == MotionEvent.ACTION_HOVER_ENTER) { |
| 1703 | // Send the enter as is. |
| 1704 | handled |= super.dispatchHoverEvent(event); // enter |
| 1705 | mHoveredSelf = true; |
| 1706 | } else if (action == MotionEvent.ACTION_HOVER_MOVE) { |
| 1707 | // Synthesize an enter from a move. |
| 1708 | eventNoHistory = obtainMotionEventNoHistoryOrSelf(eventNoHistory); |
| 1709 | eventNoHistory.setAction(MotionEvent.ACTION_HOVER_ENTER); |
| 1710 | handled |= super.dispatchHoverEvent(eventNoHistory); // enter |
| 1711 | eventNoHistory.setAction(action); |
| 1712 | |
| 1713 | handled |= super.dispatchHoverEvent(eventNoHistory); // move |
| 1714 | mHoveredSelf = true; |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 1715 | } |
| 1716 | } |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 1717 | } |
| 1718 | |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 1719 | // Recycle the copy of the event that we made. |
| 1720 | if (eventNoHistory != event) { |
| 1721 | eventNoHistory.recycle(); |
| 1722 | } |
| 1723 | |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 1724 | // Done. |
| 1725 | return handled; |
| 1726 | } |
| 1727 | |
Jeff Brown | 59a422e | 2012-04-19 15:19:19 -0700 | [diff] [blame] | 1728 | private void exitHoverTargets() { |
| 1729 | if (mHoveredSelf || mFirstHoverTarget != null) { |
| 1730 | final long now = SystemClock.uptimeMillis(); |
| 1731 | MotionEvent event = MotionEvent.obtain(now, now, |
| 1732 | MotionEvent.ACTION_HOVER_EXIT, 0.0f, 0.0f, 0); |
| 1733 | event.setSource(InputDevice.SOURCE_TOUCHSCREEN); |
| 1734 | dispatchHoverEvent(event); |
| 1735 | event.recycle(); |
| 1736 | } |
| 1737 | } |
| 1738 | |
| 1739 | private void cancelHoverTarget(View view) { |
| 1740 | HoverTarget predecessor = null; |
| 1741 | HoverTarget target = mFirstHoverTarget; |
| 1742 | while (target != null) { |
| 1743 | final HoverTarget next = target.next; |
| 1744 | if (target.child == view) { |
| 1745 | if (predecessor == null) { |
| 1746 | mFirstHoverTarget = next; |
| 1747 | } else { |
| 1748 | predecessor.next = next; |
| 1749 | } |
| 1750 | target.recycle(); |
| 1751 | |
| 1752 | final long now = SystemClock.uptimeMillis(); |
| 1753 | MotionEvent event = MotionEvent.obtain(now, now, |
| 1754 | MotionEvent.ACTION_HOVER_EXIT, 0.0f, 0.0f, 0); |
| 1755 | event.setSource(InputDevice.SOURCE_TOUCHSCREEN); |
| 1756 | view.dispatchHoverEvent(event); |
| 1757 | event.recycle(); |
| 1758 | return; |
| 1759 | } |
| 1760 | predecessor = target; |
| 1761 | target = next; |
| 1762 | } |
| 1763 | } |
| 1764 | |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 1765 | /** @hide */ |
| 1766 | @Override |
| 1767 | protected boolean hasHoveredChild() { |
| 1768 | return mFirstHoverTarget != null; |
| 1769 | } |
| 1770 | |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 1771 | @Override |
| 1772 | public void addChildrenForAccessibility(ArrayList<View> childrenForAccessibility) { |
Svetoslav Ganov | 76f287e | 2012-04-23 11:02:36 -0700 | [diff] [blame] | 1773 | ChildListForAccessibility children = ChildListForAccessibility.obtain(this, true); |
| 1774 | try { |
| 1775 | final int childrenCount = children.getChildCount(); |
| 1776 | for (int i = 0; i < childrenCount; i++) { |
| 1777 | View child = children.getChildAt(i); |
Svetoslav Ganov | c406be9 | 2012-05-11 16:12:32 -0700 | [diff] [blame] | 1778 | if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) { |
Svetoslav Ganov | 76f287e | 2012-04-23 11:02:36 -0700 | [diff] [blame] | 1779 | if (child.includeForAccessibility()) { |
| 1780 | childrenForAccessibility.add(child); |
| 1781 | } else { |
| 1782 | child.addChildrenForAccessibility(childrenForAccessibility); |
| 1783 | } |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 1784 | } |
| 1785 | } |
Svetoslav Ganov | 76f287e | 2012-04-23 11:02:36 -0700 | [diff] [blame] | 1786 | } finally { |
| 1787 | children.recycle(); |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 1788 | } |
| 1789 | } |
| 1790 | |
| 1791 | /** |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 1792 | * Implement this method to intercept hover events before they are handled |
| 1793 | * by child views. |
| 1794 | * <p> |
| 1795 | * This method is called before dispatching a hover event to a child of |
| 1796 | * the view group or to the view group's own {@link #onHoverEvent} to allow |
| 1797 | * the view group a chance to intercept the hover event. |
| 1798 | * This method can also be used to watch all pointer motions that occur within |
| 1799 | * the bounds of the view group even when the pointer is hovering over |
| 1800 | * a child of the view group rather than over the view group itself. |
| 1801 | * </p><p> |
| 1802 | * The view group can prevent its children from receiving hover events by |
| 1803 | * implementing this method and returning <code>true</code> to indicate |
| 1804 | * that it would like to intercept hover events. The view group must |
| 1805 | * continuously return <code>true</code> from {@link #onInterceptHoverEvent} |
| 1806 | * for as long as it wishes to continue intercepting hover events from |
| 1807 | * its children. |
| 1808 | * </p><p> |
| 1809 | * Interception preserves the invariant that at most one view can be |
| 1810 | * hovered at a time by transferring hover focus from the currently hovered |
| 1811 | * child to the view group or vice-versa as needed. |
| 1812 | * </p><p> |
| 1813 | * If this method returns <code>true</code> and a child is already hovered, then the |
| 1814 | * child view will first receive a hover exit event and then the view group |
| 1815 | * itself will receive a hover enter event in {@link #onHoverEvent}. |
| 1816 | * Likewise, if this method had previously returned <code>true</code> to intercept hover |
| 1817 | * events and instead returns <code>false</code> while the pointer is hovering |
| 1818 | * within the bounds of one of a child, then the view group will first receive a |
| 1819 | * hover exit event in {@link #onHoverEvent} and then the hovered child will |
| 1820 | * receive a hover enter event. |
| 1821 | * </p><p> |
| 1822 | * The default implementation always returns false. |
| 1823 | * </p> |
| 1824 | * |
| 1825 | * @param event The motion event that describes the hover. |
| 1826 | * @return True if the view group would like to intercept the hover event |
| 1827 | * and prevent its children from receiving it. |
| 1828 | */ |
| 1829 | public boolean onInterceptHoverEvent(MotionEvent event) { |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 1830 | return false; |
| 1831 | } |
| 1832 | |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 1833 | private static MotionEvent obtainMotionEventNoHistoryOrSelf(MotionEvent event) { |
| 1834 | if (event.getHistorySize() == 0) { |
| 1835 | return event; |
| 1836 | } |
| 1837 | return MotionEvent.obtainNoHistory(event); |
| 1838 | } |
| 1839 | |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 1840 | /** |
| 1841 | * {@inheritDoc} |
| 1842 | */ |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 1843 | @Override |
| 1844 | protected boolean dispatchGenericPointerEvent(MotionEvent event) { |
| 1845 | // Send the event to the child under the pointer. |
| 1846 | final int childrenCount = mChildrenCount; |
| 1847 | if (childrenCount != 0) { |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 1848 | final float x = event.getX(); |
| 1849 | final float y = event.getY(); |
| 1850 | |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 1851 | final ArrayList<View> preorderedList = buildOrderedChildList(); |
| 1852 | final boolean customOrder = preorderedList == null |
| 1853 | && isChildrenDrawingOrderEnabled(); |
| 1854 | final View[] children = mChildren; |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 1855 | for (int i = childrenCount - 1; i >= 0; i--) { |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 1856 | int childIndex = customOrder ? getChildDrawingOrder(childrenCount, i) : i; |
| 1857 | final View child = (preorderedList == null) |
| 1858 | ? children[childIndex] : preorderedList.get(childIndex); |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 1859 | if (!canViewReceivePointerEvents(child) |
| 1860 | || !isTransformedTouchPointInView(x, y, child, null)) { |
| 1861 | continue; |
| 1862 | } |
| 1863 | |
| 1864 | if (dispatchTransformedGenericPointerEvent(event, child)) { |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 1865 | if (preorderedList != null) preorderedList.clear(); |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 1866 | return true; |
| 1867 | } |
| 1868 | } |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 1869 | if (preorderedList != null) preorderedList.clear(); |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 1870 | } |
| 1871 | |
| 1872 | // No child handled the event. Send it to this view group. |
| 1873 | return super.dispatchGenericPointerEvent(event); |
| 1874 | } |
| 1875 | |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 1876 | /** |
| 1877 | * {@inheritDoc} |
| 1878 | */ |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 1879 | @Override |
| 1880 | protected boolean dispatchGenericFocusedEvent(MotionEvent event) { |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 1881 | // 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] | 1882 | if ((mPrivateFlags & (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) |
| 1883 | == (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) { |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 1884 | return super.dispatchGenericFocusedEvent(event); |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 1885 | } else if (mFocused != null && (mFocused.mPrivateFlags & PFLAG_HAS_BOUNDS) |
| 1886 | == PFLAG_HAS_BOUNDS) { |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 1887 | return mFocused.dispatchGenericMotionEvent(event); |
| 1888 | } |
| 1889 | return false; |
| 1890 | } |
| 1891 | |
| 1892 | /** |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 1893 | * Dispatches a generic pointer event to a child, taking into account |
| 1894 | * transformations that apply to the child. |
| 1895 | * |
| 1896 | * @param event The event to send. |
| 1897 | * @param child The view to send the event to. |
| 1898 | * @return {@code true} if the child handled the event. |
| 1899 | */ |
| 1900 | private boolean dispatchTransformedGenericPointerEvent(MotionEvent event, View child) { |
| 1901 | final float offsetX = mScrollX - child.mLeft; |
| 1902 | final float offsetY = mScrollY - child.mTop; |
| 1903 | |
| 1904 | boolean handled; |
| 1905 | if (!child.hasIdentityMatrix()) { |
| 1906 | MotionEvent transformedEvent = MotionEvent.obtain(event); |
| 1907 | transformedEvent.offsetLocation(offsetX, offsetY); |
| 1908 | transformedEvent.transform(child.getInverseMatrix()); |
| 1909 | handled = child.dispatchGenericMotionEvent(transformedEvent); |
| 1910 | transformedEvent.recycle(); |
| 1911 | } else { |
| 1912 | event.offsetLocation(offsetX, offsetY); |
| 1913 | handled = child.dispatchGenericMotionEvent(event); |
| 1914 | event.offsetLocation(-offsetX, -offsetY); |
| 1915 | } |
| 1916 | return handled; |
| 1917 | } |
| 1918 | |
| 1919 | /** |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 1920 | * {@inheritDoc} |
| 1921 | */ |
| 1922 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1923 | public boolean dispatchTouchEvent(MotionEvent ev) { |
Jeff Brown | 21bc5c9 | 2011-02-28 18:27:14 -0800 | [diff] [blame] | 1924 | if (mInputEventConsistencyVerifier != null) { |
| 1925 | mInputEventConsistencyVerifier.onTouchEvent(ev, 1); |
| 1926 | } |
| 1927 | |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 1928 | boolean handled = false; |
| 1929 | if (onFilterTouchEventForSecurity(ev)) { |
| 1930 | final int action = ev.getAction(); |
| 1931 | final int actionMasked = action & MotionEvent.ACTION_MASK; |
Jeff Brown | 85a3176 | 2010-09-01 17:01:00 -0700 | [diff] [blame] | 1932 | |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 1933 | // Handle an initial down. |
| 1934 | if (actionMasked == MotionEvent.ACTION_DOWN) { |
| 1935 | // Throw away all previous state when starting a new touch gesture. |
| 1936 | // The framework may have dropped the up or cancel event for the previous gesture |
| 1937 | // due to an app switch, ANR, or some other state change. |
| 1938 | cancelAndClearTouchTargets(ev); |
| 1939 | resetTouchState(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1940 | } |
Adam Powell | b08013c | 2010-09-16 16:28:11 -0700 | [diff] [blame] | 1941 | |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 1942 | // Check for interception. |
| 1943 | final boolean intercepted; |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 1944 | if (actionMasked == MotionEvent.ACTION_DOWN |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 1945 | || mFirstTouchTarget != null) { |
| 1946 | final boolean disallowIntercept = (mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0; |
| 1947 | if (!disallowIntercept) { |
| 1948 | intercepted = onInterceptTouchEvent(ev); |
| 1949 | ev.setAction(action); // restore action in case it was changed |
| 1950 | } else { |
| 1951 | intercepted = false; |
| 1952 | } |
| 1953 | } else { |
| 1954 | // There are no touch targets and this action is not an initial down |
| 1955 | // so this view group continues to intercept touches. |
| 1956 | intercepted = true; |
| 1957 | } |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 1958 | |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 1959 | // Check for cancelation. |
| 1960 | final boolean canceled = resetCancelNextUpFlag(this) |
| 1961 | || actionMasked == MotionEvent.ACTION_CANCEL; |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 1962 | |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 1963 | // Update list of touch targets for pointer down, if needed. |
| 1964 | final boolean split = (mGroupFlags & FLAG_SPLIT_MOTION_EVENTS) != 0; |
| 1965 | TouchTarget newTouchTarget = null; |
| 1966 | boolean alreadyDispatchedToNewTouchTarget = false; |
| 1967 | if (!canceled && !intercepted) { |
| 1968 | if (actionMasked == MotionEvent.ACTION_DOWN |
| 1969 | || (split && actionMasked == MotionEvent.ACTION_POINTER_DOWN) |
| 1970 | || actionMasked == MotionEvent.ACTION_HOVER_MOVE) { |
| 1971 | final int actionIndex = ev.getActionIndex(); // always 0 for down |
| 1972 | final int idBitsToAssign = split ? 1 << ev.getPointerId(actionIndex) |
| 1973 | : TouchTarget.ALL_POINTER_IDS; |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 1974 | |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 1975 | // Clean up earlier touch targets for this pointer id in case they |
| 1976 | // have become out of sync. |
| 1977 | removePointersFromTouchTargets(idBitsToAssign); |
| 1978 | |
| 1979 | final int childrenCount = mChildrenCount; |
Chet Haase | 9c17fe6 | 2013-03-22 17:05:55 -0700 | [diff] [blame] | 1980 | if (newTouchTarget == null && childrenCount != 0) { |
Chet Haase | edf6f4b | 2013-03-26 07:55:30 -0700 | [diff] [blame] | 1981 | final float x = ev.getX(actionIndex); |
| 1982 | final float y = ev.getY(actionIndex); |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 1983 | // Find a child that can receive the event. |
| 1984 | // Scan children from front to back. |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 1985 | final ArrayList<View> preorderedList = buildOrderedChildList(); |
| 1986 | final boolean customOrder = preorderedList == null |
| 1987 | && isChildrenDrawingOrderEnabled(); |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 1988 | final View[] children = mChildren; |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 1989 | for (int i = childrenCount - 1; i >= 0; i--) { |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 1990 | final int childIndex = customOrder |
| 1991 | ? getChildDrawingOrder(childrenCount, i) : i; |
| 1992 | final View child = (preorderedList == null) |
| 1993 | ? children[childIndex] : preorderedList.get(childIndex); |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 1994 | if (!canViewReceivePointerEvents(child) |
| 1995 | || !isTransformedTouchPointInView(x, y, child, null)) { |
| 1996 | continue; |
| 1997 | } |
| 1998 | |
| 1999 | newTouchTarget = getTouchTarget(child); |
| 2000 | if (newTouchTarget != null) { |
| 2001 | // Child is already receiving touch within its bounds. |
| 2002 | // Give it the new pointer in addition to the ones it is handling. |
| 2003 | newTouchTarget.pointerIdBits |= idBitsToAssign; |
| 2004 | break; |
| 2005 | } |
| 2006 | |
| 2007 | resetCancelNextUpFlag(child); |
| 2008 | if (dispatchTransformedTouchEvent(ev, false, child, idBitsToAssign)) { |
| 2009 | // Child wants to receive touch within its bounds. |
| 2010 | mLastTouchDownTime = ev.getDownTime(); |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 2011 | if (preorderedList != null) { |
| 2012 | // childIndex points into presorted list, find original index |
| 2013 | for (int j = 0; j < childrenCount; j++) { |
| 2014 | if (children[childIndex] == mChildren[j]) { |
| 2015 | mLastTouchDownIndex = j; |
| 2016 | break; |
| 2017 | } |
| 2018 | } |
| 2019 | } else { |
| 2020 | mLastTouchDownIndex = childIndex; |
| 2021 | } |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2022 | mLastTouchDownX = ev.getX(); |
| 2023 | mLastTouchDownY = ev.getY(); |
| 2024 | newTouchTarget = addTouchTarget(child, idBitsToAssign); |
| 2025 | alreadyDispatchedToNewTouchTarget = true; |
| 2026 | break; |
| 2027 | } |
| 2028 | } |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 2029 | if (preorderedList != null) preorderedList.clear(); |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2030 | } |
| 2031 | |
| 2032 | if (newTouchTarget == null && mFirstTouchTarget != null) { |
| 2033 | // Did not find a child to receive the event. |
| 2034 | // Assign the pointer to the least recently added target. |
| 2035 | newTouchTarget = mFirstTouchTarget; |
| 2036 | while (newTouchTarget.next != null) { |
| 2037 | newTouchTarget = newTouchTarget.next; |
| 2038 | } |
| 2039 | newTouchTarget.pointerIdBits |= idBitsToAssign; |
| 2040 | } |
| 2041 | } |
| 2042 | } |
| 2043 | |
| 2044 | // Dispatch to touch targets. |
| 2045 | if (mFirstTouchTarget == null) { |
| 2046 | // No touch targets so treat this as an ordinary view. |
| 2047 | handled = dispatchTransformedTouchEvent(ev, canceled, null, |
| 2048 | TouchTarget.ALL_POINTER_IDS); |
| 2049 | } else { |
| 2050 | // Dispatch to touch targets, excluding the new touch target if we already |
| 2051 | // dispatched to it. Cancel touch targets if necessary. |
| 2052 | TouchTarget predecessor = null; |
| 2053 | TouchTarget target = mFirstTouchTarget; |
| 2054 | while (target != null) { |
| 2055 | final TouchTarget next = target.next; |
| 2056 | if (alreadyDispatchedToNewTouchTarget && target == newTouchTarget) { |
| 2057 | handled = true; |
| 2058 | } else { |
| 2059 | final boolean cancelChild = resetCancelNextUpFlag(target.child) |
Chet Haase | 9c17fe6 | 2013-03-22 17:05:55 -0700 | [diff] [blame] | 2060 | || intercepted; |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2061 | if (dispatchTransformedTouchEvent(ev, cancelChild, |
| 2062 | target.child, target.pointerIdBits)) { |
| 2063 | handled = true; |
| 2064 | } |
| 2065 | if (cancelChild) { |
| 2066 | if (predecessor == null) { |
| 2067 | mFirstTouchTarget = next; |
| 2068 | } else { |
| 2069 | predecessor.next = next; |
| 2070 | } |
| 2071 | target.recycle(); |
| 2072 | target = next; |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2073 | continue; |
| 2074 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2075 | } |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2076 | predecessor = target; |
| 2077 | target = next; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2078 | } |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2079 | } |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2080 | |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2081 | // Update list of touch targets for pointer up or cancel, if needed. |
| 2082 | if (canceled |
| 2083 | || actionMasked == MotionEvent.ACTION_UP |
| 2084 | || actionMasked == MotionEvent.ACTION_HOVER_MOVE) { |
| 2085 | resetTouchState(); |
| 2086 | } else if (split && actionMasked == MotionEvent.ACTION_POINTER_UP) { |
| 2087 | final int actionIndex = ev.getActionIndex(); |
| 2088 | final int idBitsToRemove = 1 << ev.getPointerId(actionIndex); |
| 2089 | removePointersFromTouchTargets(idBitsToRemove); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2090 | } |
| 2091 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 2092 | |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2093 | if (!handled && mInputEventConsistencyVerifier != null) { |
| 2094 | mInputEventConsistencyVerifier.onUnhandledEvent(ev, 1); |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2095 | } |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2096 | return handled; |
| 2097 | } |
| 2098 | |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 2099 | /** |
| 2100 | * Resets all touch state in preparation for a new cycle. |
| 2101 | */ |
| 2102 | private void resetTouchState() { |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2103 | clearTouchTargets(); |
| 2104 | resetCancelNextUpFlag(this); |
| 2105 | mGroupFlags &= ~FLAG_DISALLOW_INTERCEPT; |
Adam Powell | 10ba277 | 2014-04-15 09:46:51 -0700 | [diff] [blame] | 2106 | mNestedScrollAxes = SCROLL_AXIS_NONE; |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2107 | } |
| 2108 | |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 2109 | /** |
| 2110 | * Resets the cancel next up flag. |
| 2111 | * Returns true if the flag was previously set. |
| 2112 | */ |
Romain Guy | a998dff | 2012-03-23 18:58:36 -0700 | [diff] [blame] | 2113 | private static boolean resetCancelNextUpFlag(View view) { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 2114 | if ((view.mPrivateFlags & PFLAG_CANCEL_NEXT_UP_EVENT) != 0) { |
| 2115 | view.mPrivateFlags &= ~PFLAG_CANCEL_NEXT_UP_EVENT; |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2116 | return true; |
Adam Cohen | 9b07394 | 2010-08-19 16:49:52 -0700 | [diff] [blame] | 2117 | } |
| 2118 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2119 | } |
| 2120 | |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 2121 | /** |
| 2122 | * Clears all touch targets. |
| 2123 | */ |
| 2124 | private void clearTouchTargets() { |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2125 | TouchTarget target = mFirstTouchTarget; |
| 2126 | if (target != null) { |
| 2127 | do { |
| 2128 | TouchTarget next = target.next; |
| 2129 | target.recycle(); |
| 2130 | target = next; |
| 2131 | } while (target != null); |
| 2132 | mFirstTouchTarget = null; |
| 2133 | } |
| 2134 | } |
| 2135 | |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 2136 | /** |
| 2137 | * Cancels and clears all touch targets. |
| 2138 | */ |
| 2139 | private void cancelAndClearTouchTargets(MotionEvent event) { |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2140 | if (mFirstTouchTarget != null) { |
| 2141 | boolean syntheticEvent = false; |
| 2142 | if (event == null) { |
| 2143 | final long now = SystemClock.uptimeMillis(); |
| 2144 | event = MotionEvent.obtain(now, now, |
| 2145 | MotionEvent.ACTION_CANCEL, 0.0f, 0.0f, 0); |
Jeff Brown | 2fdbc5a | 2011-06-30 12:25:54 -0700 | [diff] [blame] | 2146 | event.setSource(InputDevice.SOURCE_TOUCHSCREEN); |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2147 | syntheticEvent = true; |
| 2148 | } |
| 2149 | |
| 2150 | for (TouchTarget target = mFirstTouchTarget; target != null; target = target.next) { |
| 2151 | resetCancelNextUpFlag(target.child); |
| 2152 | dispatchTransformedTouchEvent(event, true, target.child, target.pointerIdBits); |
| 2153 | } |
| 2154 | clearTouchTargets(); |
| 2155 | |
| 2156 | if (syntheticEvent) { |
| 2157 | event.recycle(); |
| 2158 | } |
| 2159 | } |
| 2160 | } |
| 2161 | |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 2162 | /** |
| 2163 | * Gets the touch target for specified child view. |
| 2164 | * Returns null if not found. |
| 2165 | */ |
| 2166 | private TouchTarget getTouchTarget(View child) { |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2167 | for (TouchTarget target = mFirstTouchTarget; target != null; target = target.next) { |
| 2168 | if (target.child == child) { |
| 2169 | return target; |
| 2170 | } |
| 2171 | } |
| 2172 | return null; |
| 2173 | } |
| 2174 | |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 2175 | /** |
| 2176 | * Adds a touch target for specified child to the beginning of the list. |
| 2177 | * Assumes the target child is not already present. |
| 2178 | */ |
| 2179 | private TouchTarget addTouchTarget(View child, int pointerIdBits) { |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2180 | TouchTarget target = TouchTarget.obtain(child, pointerIdBits); |
| 2181 | target.next = mFirstTouchTarget; |
| 2182 | mFirstTouchTarget = target; |
| 2183 | return target; |
| 2184 | } |
| 2185 | |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 2186 | /** |
| 2187 | * Removes the pointer ids from consideration. |
| 2188 | */ |
| 2189 | private void removePointersFromTouchTargets(int pointerIdBits) { |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2190 | TouchTarget predecessor = null; |
| 2191 | TouchTarget target = mFirstTouchTarget; |
| 2192 | while (target != null) { |
| 2193 | final TouchTarget next = target.next; |
| 2194 | if ((target.pointerIdBits & pointerIdBits) != 0) { |
| 2195 | target.pointerIdBits &= ~pointerIdBits; |
| 2196 | if (target.pointerIdBits == 0) { |
| 2197 | if (predecessor == null) { |
| 2198 | mFirstTouchTarget = next; |
| 2199 | } else { |
| 2200 | predecessor.next = next; |
| 2201 | } |
| 2202 | target.recycle(); |
| 2203 | target = next; |
| 2204 | continue; |
| 2205 | } |
| 2206 | } |
| 2207 | predecessor = target; |
| 2208 | target = next; |
| 2209 | } |
| 2210 | } |
| 2211 | |
Jeff Brown | 59a422e | 2012-04-19 15:19:19 -0700 | [diff] [blame] | 2212 | private void cancelTouchTarget(View view) { |
| 2213 | TouchTarget predecessor = null; |
| 2214 | TouchTarget target = mFirstTouchTarget; |
| 2215 | while (target != null) { |
| 2216 | final TouchTarget next = target.next; |
| 2217 | if (target.child == view) { |
| 2218 | if (predecessor == null) { |
| 2219 | mFirstTouchTarget = next; |
| 2220 | } else { |
| 2221 | predecessor.next = next; |
| 2222 | } |
| 2223 | target.recycle(); |
| 2224 | |
| 2225 | final long now = SystemClock.uptimeMillis(); |
| 2226 | MotionEvent event = MotionEvent.obtain(now, now, |
| 2227 | MotionEvent.ACTION_CANCEL, 0.0f, 0.0f, 0); |
| 2228 | event.setSource(InputDevice.SOURCE_TOUCHSCREEN); |
| 2229 | view.dispatchTouchEvent(event); |
| 2230 | event.recycle(); |
| 2231 | return; |
| 2232 | } |
| 2233 | predecessor = target; |
| 2234 | target = next; |
| 2235 | } |
| 2236 | } |
| 2237 | |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 2238 | /** |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2239 | * Returns true if a child view can receive pointer events. |
| 2240 | * @hide |
| 2241 | */ |
| 2242 | private static boolean canViewReceivePointerEvents(View child) { |
| 2243 | return (child.mViewFlags & VISIBILITY_MASK) == VISIBLE |
| 2244 | || child.getAnimation() != null; |
| 2245 | } |
| 2246 | |
| 2247 | /** |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 2248 | * Returns true if a child view contains the specified point when transformed |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2249 | * into its coordinate space. |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 2250 | * Child must not be null. |
Adam Cohen | a32edd4 | 2010-10-26 10:35:01 -0700 | [diff] [blame] | 2251 | * @hide |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 2252 | */ |
Adam Cohen | a32edd4 | 2010-10-26 10:35:01 -0700 | [diff] [blame] | 2253 | protected boolean isTransformedTouchPointInView(float x, float y, View child, |
Christopher Tate | 2c095f3 | 2010-10-04 14:13:40 -0700 | [diff] [blame] | 2254 | PointF outLocalPoint) { |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2255 | float localX = x + mScrollX - child.mLeft; |
| 2256 | float localY = y + mScrollY - child.mTop; |
| 2257 | if (! child.hasIdentityMatrix() && mAttachInfo != null) { |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 2258 | final float[] localXY = mAttachInfo.mTmpTransformLocation; |
| 2259 | localXY[0] = localX; |
| 2260 | localXY[1] = localY; |
| 2261 | child.getInverseMatrix().mapPoints(localXY); |
| 2262 | localX = localXY[0]; |
| 2263 | localY = localXY[1]; |
| 2264 | } |
Christopher Tate | 2c095f3 | 2010-10-04 14:13:40 -0700 | [diff] [blame] | 2265 | final boolean isInView = child.pointInView(localX, localY); |
| 2266 | if (isInView && outLocalPoint != null) { |
| 2267 | outLocalPoint.set(localX, localY); |
| 2268 | } |
| 2269 | return isInView; |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 2270 | } |
| 2271 | |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 2272 | /** |
| 2273 | * 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] | 2274 | * filters out irrelevant pointer ids, and overrides its action if necessary. |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 2275 | * If child is null, assumes the MotionEvent will be sent to this ViewGroup instead. |
| 2276 | */ |
| 2277 | private boolean dispatchTransformedTouchEvent(MotionEvent event, boolean cancel, |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2278 | View child, int desiredPointerIdBits) { |
| 2279 | final boolean handled; |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 2280 | |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2281 | // Canceling motions is a special case. We don't need to perform any transformations |
| 2282 | // or filtering. The important part is the action, not the contents. |
| 2283 | final int oldAction = event.getAction(); |
| 2284 | if (cancel || oldAction == MotionEvent.ACTION_CANCEL) { |
| 2285 | event.setAction(MotionEvent.ACTION_CANCEL); |
| 2286 | if (child == null) { |
| 2287 | handled = super.dispatchTouchEvent(event); |
| 2288 | } else { |
| 2289 | handled = child.dispatchTouchEvent(event); |
| 2290 | } |
| 2291 | event.setAction(oldAction); |
| 2292 | return handled; |
| 2293 | } |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 2294 | |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2295 | // Calculate the number of pointers to deliver. |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 2296 | final int oldPointerIdBits = event.getPointerIdBits(); |
| 2297 | final int newPointerIdBits = oldPointerIdBits & desiredPointerIdBits; |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 2298 | |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2299 | // If for some reason we ended up in an inconsistent state where it looks like we |
| 2300 | // 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] | 2301 | if (newPointerIdBits == 0) { |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2302 | return false; |
| 2303 | } |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 2304 | |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2305 | // If the number of pointers is the same and we don't need to perform any fancy |
| 2306 | // irreversible transformations, then we can reuse the motion event for this |
| 2307 | // 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] | 2308 | // Otherwise we need to make a copy. |
| 2309 | final MotionEvent transformedEvent; |
| 2310 | if (newPointerIdBits == oldPointerIdBits) { |
| 2311 | if (child == null || child.hasIdentityMatrix()) { |
| 2312 | if (child == null) { |
| 2313 | handled = super.dispatchTouchEvent(event); |
| 2314 | } else { |
| 2315 | final float offsetX = mScrollX - child.mLeft; |
| 2316 | final float offsetY = mScrollY - child.mTop; |
| 2317 | event.offsetLocation(offsetX, offsetY); |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 2318 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 2319 | handled = child.dispatchTouchEvent(event); |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2320 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 2321 | event.offsetLocation(-offsetX, -offsetY); |
| 2322 | } |
| 2323 | return handled; |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2324 | } |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2325 | transformedEvent = MotionEvent.obtain(event); |
| 2326 | } else { |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 2327 | transformedEvent = event.split(newPointerIdBits); |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 2328 | } |
| 2329 | |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2330 | // Perform any necessary transformations and dispatch. |
| 2331 | if (child == null) { |
| 2332 | handled = super.dispatchTouchEvent(transformedEvent); |
| 2333 | } else { |
| 2334 | final float offsetX = mScrollX - child.mLeft; |
| 2335 | final float offsetY = mScrollY - child.mTop; |
| 2336 | transformedEvent.offsetLocation(offsetX, offsetY); |
| 2337 | if (! child.hasIdentityMatrix()) { |
| 2338 | transformedEvent.transform(child.getInverseMatrix()); |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 2339 | } |
| 2340 | |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2341 | handled = child.dispatchTouchEvent(transformedEvent); |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 2342 | } |
| 2343 | |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2344 | // Done. |
| 2345 | transformedEvent.recycle(); |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 2346 | return handled; |
| 2347 | } |
| 2348 | |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 2349 | /** |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 2350 | * 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] | 2351 | * dispatch. This behavior is enabled by default for applications that target an |
| 2352 | * SDK version of {@link Build.VERSION_CODES#HONEYCOMB} or newer. |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 2353 | * |
| 2354 | * <p>When this option is enabled MotionEvents may be split and dispatched to different child |
| 2355 | * views depending on where each pointer initially went down. This allows for user interactions |
| 2356 | * such as scrolling two panes of content independently, chording of buttons, and performing |
| 2357 | * independent gestures on different pieces of content. |
| 2358 | * |
| 2359 | * @param split <code>true</code> to allow MotionEvents to be split and dispatched to multiple |
| 2360 | * child views. <code>false</code> to only allow one child view to be the target of |
| 2361 | * any MotionEvent received by this ViewGroup. |
Scott Main | 27a8508 | 2013-06-10 10:39:48 -0700 | [diff] [blame] | 2362 | * @attr ref android.R.styleable#ViewGroup_splitMotionEvents |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 2363 | */ |
| 2364 | public void setMotionEventSplittingEnabled(boolean split) { |
| 2365 | // TODO Applications really shouldn't change this setting mid-touch event, |
| 2366 | // but perhaps this should handle that case and send ACTION_CANCELs to any child views |
| 2367 | // with gestures in progress when this is changed. |
| 2368 | if (split) { |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 2369 | mGroupFlags |= FLAG_SPLIT_MOTION_EVENTS; |
| 2370 | } else { |
| 2371 | mGroupFlags &= ~FLAG_SPLIT_MOTION_EVENTS; |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 2372 | } |
| 2373 | } |
| 2374 | |
| 2375 | /** |
Jeff Brown | 995e774 | 2010-12-22 16:59:36 -0800 | [diff] [blame] | 2376 | * 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] | 2377 | * @return true if MotionEvents dispatched to this ViewGroup can be split to multiple children. |
| 2378 | */ |
| 2379 | public boolean isMotionEventSplittingEnabled() { |
| 2380 | return (mGroupFlags & FLAG_SPLIT_MOTION_EVENTS) == FLAG_SPLIT_MOTION_EVENTS; |
| 2381 | } |
| 2382 | |
| 2383 | /** |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 2384 | * Returns true if this ViewGroup should be considered as a single entity for removal |
| 2385 | * when executing an Activity transition. If this is false, child elements will move |
| 2386 | * individually during the transition. |
| 2387 | * @return True if the ViewGroup should be acted on together during an Activity transition. |
| 2388 | * The default value is false when the background is null and true when the background |
George Mount | 0a2ae00 | 2014-06-23 14:57:27 +0000 | [diff] [blame] | 2389 | * is not null or if {@link #getTransitionName()} is not null. |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 2390 | */ |
| 2391 | public boolean isTransitionGroup() { |
| 2392 | if ((mGroupFlags & FLAG_IS_TRANSITION_GROUP_SET) != 0) { |
| 2393 | return ((mGroupFlags & FLAG_IS_TRANSITION_GROUP) != 0); |
| 2394 | } else { |
George Mount | 0a2ae00 | 2014-06-23 14:57:27 +0000 | [diff] [blame] | 2395 | return getBackground() != null || getTransitionName() != null; |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 2396 | } |
| 2397 | } |
| 2398 | |
| 2399 | /** |
| 2400 | * 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] | 2401 | * Activity Transitions. |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 2402 | * @param isTransitionGroup Whether or not the ViewGroup should be treated as a unit |
| 2403 | * in Activity transitions. If false, the ViewGroup won't transition, |
| 2404 | * only its children. If true, the entire ViewGroup will transition |
| 2405 | * together. |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 2406 | * @see android.app.ActivityOptions#makeSceneTransitionAnimation(android.app.Activity, |
| 2407 | * android.util.Pair[]) |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 2408 | */ |
| 2409 | public void setTransitionGroup(boolean isTransitionGroup) { |
| 2410 | mGroupFlags |= FLAG_IS_TRANSITION_GROUP_SET; |
| 2411 | if (isTransitionGroup) { |
| 2412 | mGroupFlags |= FLAG_IS_TRANSITION_GROUP; |
| 2413 | } else { |
| 2414 | mGroupFlags &= ~FLAG_IS_TRANSITION_GROUP; |
| 2415 | } |
| 2416 | } |
| 2417 | |
| 2418 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2419 | * {@inheritDoc} |
| 2420 | */ |
| 2421 | public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) { |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 2422 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2423 | if (disallowIntercept == ((mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0)) { |
| 2424 | // We're already in this state, assume our ancestors are too |
| 2425 | return; |
| 2426 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 2427 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2428 | if (disallowIntercept) { |
| 2429 | mGroupFlags |= FLAG_DISALLOW_INTERCEPT; |
| 2430 | } else { |
| 2431 | mGroupFlags &= ~FLAG_DISALLOW_INTERCEPT; |
| 2432 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 2433 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2434 | // Pass it up to our parent |
| 2435 | if (mParent != null) { |
| 2436 | mParent.requestDisallowInterceptTouchEvent(disallowIntercept); |
| 2437 | } |
| 2438 | } |
| 2439 | |
| 2440 | /** |
| 2441 | * Implement this method to intercept all touch screen motion events. This |
| 2442 | * allows you to watch events as they are dispatched to your children, and |
| 2443 | * take ownership of the current gesture at any point. |
| 2444 | * |
| 2445 | * <p>Using this function takes some care, as it has a fairly complicated |
| 2446 | * interaction with {@link View#onTouchEvent(MotionEvent) |
| 2447 | * View.onTouchEvent(MotionEvent)}, and using it requires implementing |
| 2448 | * that method as well as this one in the correct way. Events will be |
| 2449 | * received in the following order: |
| 2450 | * |
| 2451 | * <ol> |
| 2452 | * <li> You will receive the down event here. |
| 2453 | * <li> The down event will be handled either by a child of this view |
| 2454 | * group, or given to your own onTouchEvent() method to handle; this means |
| 2455 | * you should implement onTouchEvent() to return true, so you will |
| 2456 | * continue to see the rest of the gesture (instead of looking for |
| 2457 | * a parent view to handle it). Also, by returning true from |
| 2458 | * onTouchEvent(), you will not receive any following |
| 2459 | * events in onInterceptTouchEvent() and all touch processing must |
| 2460 | * happen in onTouchEvent() like normal. |
| 2461 | * <li> For as long as you return false from this function, each following |
| 2462 | * event (up to and including the final up) will be delivered first here |
| 2463 | * and then to the target's onTouchEvent(). |
| 2464 | * <li> If you return true from here, you will not receive any |
| 2465 | * following events: the target view will receive the same event but |
| 2466 | * with the action {@link MotionEvent#ACTION_CANCEL}, and all further |
| 2467 | * events will be delivered to your onTouchEvent() method and no longer |
| 2468 | * appear here. |
| 2469 | * </ol> |
| 2470 | * |
| 2471 | * @param ev The motion event being dispatched down the hierarchy. |
| 2472 | * @return Return true to steal motion events from the children and have |
| 2473 | * them dispatched to this ViewGroup through onTouchEvent(). |
| 2474 | * The current target will receive an ACTION_CANCEL event, and no further |
| 2475 | * messages will be delivered here. |
| 2476 | */ |
| 2477 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
| 2478 | return false; |
| 2479 | } |
| 2480 | |
| 2481 | /** |
| 2482 | * {@inheritDoc} |
| 2483 | * |
| 2484 | * Looks for a view to give focus to respecting the setting specified by |
| 2485 | * {@link #getDescendantFocusability()}. |
| 2486 | * |
| 2487 | * Uses {@link #onRequestFocusInDescendants(int, android.graphics.Rect)} to |
| 2488 | * find focus within the children of this group when appropriate. |
| 2489 | * |
| 2490 | * @see #FOCUS_BEFORE_DESCENDANTS |
| 2491 | * @see #FOCUS_AFTER_DESCENDANTS |
| 2492 | * @see #FOCUS_BLOCK_DESCENDANTS |
Romain Guy | 02739a8 | 2011-05-16 11:43:18 -0700 | [diff] [blame] | 2493 | * @see #onRequestFocusInDescendants(int, android.graphics.Rect) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2494 | */ |
| 2495 | @Override |
| 2496 | public boolean requestFocus(int direction, Rect previouslyFocusedRect) { |
| 2497 | if (DBG) { |
| 2498 | System.out.println(this + " ViewGroup.requestFocus direction=" |
| 2499 | + direction); |
| 2500 | } |
| 2501 | int descendantFocusability = getDescendantFocusability(); |
| 2502 | |
| 2503 | switch (descendantFocusability) { |
| 2504 | case FOCUS_BLOCK_DESCENDANTS: |
| 2505 | return super.requestFocus(direction, previouslyFocusedRect); |
| 2506 | case FOCUS_BEFORE_DESCENDANTS: { |
| 2507 | final boolean took = super.requestFocus(direction, previouslyFocusedRect); |
| 2508 | return took ? took : onRequestFocusInDescendants(direction, previouslyFocusedRect); |
| 2509 | } |
| 2510 | case FOCUS_AFTER_DESCENDANTS: { |
| 2511 | final boolean took = onRequestFocusInDescendants(direction, previouslyFocusedRect); |
| 2512 | return took ? took : super.requestFocus(direction, previouslyFocusedRect); |
| 2513 | } |
| 2514 | default: |
| 2515 | throw new IllegalStateException("descendant focusability must be " |
| 2516 | + "one of FOCUS_BEFORE_DESCENDANTS, FOCUS_AFTER_DESCENDANTS, FOCUS_BLOCK_DESCENDANTS " |
| 2517 | + "but is " + descendantFocusability); |
| 2518 | } |
| 2519 | } |
| 2520 | |
| 2521 | /** |
| 2522 | * Look for a descendant to call {@link View#requestFocus} on. |
| 2523 | * Called by {@link ViewGroup#requestFocus(int, android.graphics.Rect)} |
| 2524 | * when it wants to request focus within its children. Override this to |
| 2525 | * customize how your {@link ViewGroup} requests focus within its children. |
| 2526 | * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT |
| 2527 | * @param previouslyFocusedRect The rectangle (in this View's coordinate system) |
| 2528 | * to give a finer grained hint about where focus is coming from. May be null |
| 2529 | * if there is no hint. |
| 2530 | * @return Whether focus was taken. |
| 2531 | */ |
| 2532 | @SuppressWarnings({"ConstantConditions"}) |
| 2533 | protected boolean onRequestFocusInDescendants(int direction, |
| 2534 | Rect previouslyFocusedRect) { |
| 2535 | int index; |
| 2536 | int increment; |
| 2537 | int end; |
| 2538 | int count = mChildrenCount; |
| 2539 | if ((direction & FOCUS_FORWARD) != 0) { |
| 2540 | index = 0; |
| 2541 | increment = 1; |
| 2542 | end = count; |
| 2543 | } else { |
| 2544 | index = count - 1; |
| 2545 | increment = -1; |
| 2546 | end = -1; |
| 2547 | } |
| 2548 | final View[] children = mChildren; |
| 2549 | for (int i = index; i != end; i += increment) { |
| 2550 | View child = children[i]; |
| 2551 | if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) { |
| 2552 | if (child.requestFocus(direction, previouslyFocusedRect)) { |
| 2553 | return true; |
| 2554 | } |
| 2555 | } |
| 2556 | } |
| 2557 | return false; |
| 2558 | } |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 2559 | |
Romain Guy | a440b00 | 2010-02-24 15:57:54 -0800 | [diff] [blame] | 2560 | /** |
| 2561 | * {@inheritDoc} |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 2562 | * |
Romain Guy | dcc490f | 2010-02-24 17:59:35 -0800 | [diff] [blame] | 2563 | * @hide |
Romain Guy | a440b00 | 2010-02-24 15:57:54 -0800 | [diff] [blame] | 2564 | */ |
| 2565 | @Override |
| 2566 | public void dispatchStartTemporaryDetach() { |
| 2567 | super.dispatchStartTemporaryDetach(); |
| 2568 | final int count = mChildrenCount; |
| 2569 | final View[] children = mChildren; |
| 2570 | for (int i = 0; i < count; i++) { |
| 2571 | children[i].dispatchStartTemporaryDetach(); |
| 2572 | } |
| 2573 | } |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 2574 | |
Romain Guy | a440b00 | 2010-02-24 15:57:54 -0800 | [diff] [blame] | 2575 | /** |
| 2576 | * {@inheritDoc} |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 2577 | * |
Romain Guy | dcc490f | 2010-02-24 17:59:35 -0800 | [diff] [blame] | 2578 | * @hide |
Romain Guy | a440b00 | 2010-02-24 15:57:54 -0800 | [diff] [blame] | 2579 | */ |
| 2580 | @Override |
| 2581 | public void dispatchFinishTemporaryDetach() { |
| 2582 | super.dispatchFinishTemporaryDetach(); |
| 2583 | final int count = mChildrenCount; |
| 2584 | final View[] children = mChildren; |
| 2585 | for (int i = 0; i < count; i++) { |
| 2586 | children[i].dispatchFinishTemporaryDetach(); |
| 2587 | } |
| 2588 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2589 | |
| 2590 | /** |
| 2591 | * {@inheritDoc} |
| 2592 | */ |
| 2593 | @Override |
| 2594 | void dispatchAttachedToWindow(AttachInfo info, int visibility) { |
Adam Powell | 4b86788 | 2011-09-16 12:59:46 -0700 | [diff] [blame] | 2595 | mGroupFlags |= FLAG_PREVENT_DISPATCH_ATTACHED_TO_WINDOW; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2596 | super.dispatchAttachedToWindow(info, visibility); |
Adam Powell | 4b86788 | 2011-09-16 12:59:46 -0700 | [diff] [blame] | 2597 | mGroupFlags &= ~FLAG_PREVENT_DISPATCH_ATTACHED_TO_WINDOW; |
| 2598 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2599 | final int count = mChildrenCount; |
| 2600 | final View[] children = mChildren; |
| 2601 | for (int i = 0; i < count; i++) { |
Dianne Hackborn | 3a3a6cf | 2012-03-26 10:24:04 -0700 | [diff] [blame] | 2602 | final View child = children[i]; |
| 2603 | child.dispatchAttachedToWindow(info, |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 2604 | visibility | (child.mViewFlags & VISIBILITY_MASK)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2605 | } |
| 2606 | } |
| 2607 | |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 2608 | @Override |
Romain Guy | bb9908b | 2012-03-08 11:14:07 -0800 | [diff] [blame] | 2609 | void dispatchScreenStateChanged(int screenState) { |
| 2610 | super.dispatchScreenStateChanged(screenState); |
| 2611 | |
| 2612 | final int count = mChildrenCount; |
| 2613 | final View[] children = mChildren; |
| 2614 | for (int i = 0; i < count; i++) { |
| 2615 | children[i].dispatchScreenStateChanged(screenState); |
| 2616 | } |
| 2617 | } |
| 2618 | |
| 2619 | @Override |
Svetoslav Ganov | 031d9c1 | 2011-09-09 16:41:13 -0700 | [diff] [blame] | 2620 | boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) { |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 2621 | boolean handled = false; |
| 2622 | if (includeForAccessibility()) { |
| 2623 | handled = super.dispatchPopulateAccessibilityEventInternal(event); |
| 2624 | if (handled) { |
| 2625 | return handled; |
| 2626 | } |
Svetoslav Ganov | b84b94e | 2011-09-22 19:26:56 -0700 | [diff] [blame] | 2627 | } |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 2628 | // Let our children have a shot in populating the event. |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 2629 | ChildListForAccessibility children = ChildListForAccessibility.obtain(this, true); |
Svetoslav Ganov | 76f287e | 2012-04-23 11:02:36 -0700 | [diff] [blame] | 2630 | try { |
| 2631 | final int childCount = children.getChildCount(); |
| 2632 | for (int i = 0; i < childCount; i++) { |
| 2633 | View child = children.getChildAt(i); |
| 2634 | if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) { |
| 2635 | handled = child.dispatchPopulateAccessibilityEvent(event); |
| 2636 | if (handled) { |
Svetoslav Ganov | 76f287e | 2012-04-23 11:02:36 -0700 | [diff] [blame] | 2637 | return handled; |
| 2638 | } |
Svetoslav Ganov | 6179ea3 | 2011-06-28 01:12:41 -0700 | [diff] [blame] | 2639 | } |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 2640 | } |
Svetoslav Ganov | 76f287e | 2012-04-23 11:02:36 -0700 | [diff] [blame] | 2641 | } finally { |
| 2642 | children.recycle(); |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 2643 | } |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 2644 | return false; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 2645 | } |
| 2646 | |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 2647 | @Override |
Svetoslav Ganov | 031d9c1 | 2011-09-09 16:41:13 -0700 | [diff] [blame] | 2648 | void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) { |
| 2649 | super.onInitializeAccessibilityNodeInfoInternal(info); |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 2650 | if (mAttachInfo != null) { |
Alan Viverette | f0aed09 | 2013-11-06 15:33:03 -0800 | [diff] [blame] | 2651 | final ArrayList<View> childrenForAccessibility = mAttachInfo.mTempArrayList; |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 2652 | childrenForAccessibility.clear(); |
| 2653 | addChildrenForAccessibility(childrenForAccessibility); |
| 2654 | final int childrenForAccessibilityCount = childrenForAccessibility.size(); |
| 2655 | for (int i = 0; i < childrenForAccessibilityCount; i++) { |
Alan Viverette | f0aed09 | 2013-11-06 15:33:03 -0800 | [diff] [blame] | 2656 | final View child = childrenForAccessibility.get(i); |
| 2657 | info.addChildUnchecked(child); |
Svetoslav Ganov | ea1da3d | 2011-06-15 17:16:02 -0700 | [diff] [blame] | 2658 | } |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 2659 | childrenForAccessibility.clear(); |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 2660 | } |
| 2661 | } |
| 2662 | |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 2663 | @Override |
| 2664 | void onInitializeAccessibilityEventInternal(AccessibilityEvent event) { |
| 2665 | super.onInitializeAccessibilityEventInternal(event); |
| 2666 | event.setClassName(ViewGroup.class.getName()); |
| 2667 | } |
| 2668 | |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 2669 | @Override |
Alan Viverette | 77e9a28 | 2013-09-12 17:16:09 -0700 | [diff] [blame] | 2670 | public void notifySubtreeAccessibilityStateChanged(View child, View source, int changeType) { |
| 2671 | // If this is a live region, we should send a subtree change event |
| 2672 | // from this view. Otherwise, we can let it propagate up. |
| 2673 | if (getAccessibilityLiveRegion() != ACCESSIBILITY_LIVE_REGION_NONE) { |
| 2674 | notifyViewAccessibilityStateChangedIfNeeded(changeType); |
| 2675 | } else if (mParent != null) { |
Adam Powell | 504a10f | 2013-07-11 15:25:59 -0700 | [diff] [blame] | 2676 | try { |
Alan Viverette | 77e9a28 | 2013-09-12 17:16:09 -0700 | [diff] [blame] | 2677 | mParent.notifySubtreeAccessibilityStateChanged(this, source, changeType); |
Adam Powell | 504a10f | 2013-07-11 15:25:59 -0700 | [diff] [blame] | 2678 | } catch (AbstractMethodError e) { |
| 2679 | Log.e(VIEW_LOG_TAG, mParent.getClass().getSimpleName() + |
| 2680 | " does not fully implement ViewParent", e); |
| 2681 | } |
Svetoslav | 6254f48 | 2013-06-04 17:22:14 -0700 | [diff] [blame] | 2682 | } |
| 2683 | } |
| 2684 | |
| 2685 | @Override |
| 2686 | void resetSubtreeAccessibilityStateChanged() { |
| 2687 | super.resetSubtreeAccessibilityStateChanged(); |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 2688 | View[] children = mChildren; |
| 2689 | final int childCount = mChildrenCount; |
| 2690 | for (int i = 0; i < childCount; i++) { |
Svetoslav | 6254f48 | 2013-06-04 17:22:14 -0700 | [diff] [blame] | 2691 | children[i].resetSubtreeAccessibilityStateChanged(); |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 2692 | } |
| 2693 | } |
| 2694 | |
| 2695 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2696 | * {@inheritDoc} |
| 2697 | */ |
| 2698 | @Override |
| 2699 | void dispatchDetachedFromWindow() { |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2700 | // 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] | 2701 | // dispatching motion events to a child; we need to get rid of that |
| 2702 | // child to avoid dispatching events to it after the window is torn |
| 2703 | // down. To make sure we keep the child in a consistent state, we |
| 2704 | // first send it an ACTION_CANCEL motion event. |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2705 | cancelAndClearTouchTargets(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2706 | |
Jeff Brown | 59a422e | 2012-04-19 15:19:19 -0700 | [diff] [blame] | 2707 | // Similarly, set ACTION_EXIT to all hover targets and clear them. |
| 2708 | exitHoverTargets(); |
| 2709 | |
Chet Haase | 9c08744 | 2011-01-12 16:20:16 -0800 | [diff] [blame] | 2710 | // In case view is detached while transition is running |
Chet Haase | b989502 | 2013-04-02 15:10:58 -0700 | [diff] [blame] | 2711 | mLayoutCalledWhileSuppressed = false; |
Chet Haase | 9c08744 | 2011-01-12 16:20:16 -0800 | [diff] [blame] | 2712 | |
Christopher Tate | 86cab1b | 2011-01-13 20:28:55 -0800 | [diff] [blame] | 2713 | // Tear down our drag tracking |
| 2714 | mDragNotifiedChildren = null; |
| 2715 | if (mCurrentDrag != null) { |
| 2716 | mCurrentDrag.recycle(); |
| 2717 | mCurrentDrag = null; |
| 2718 | } |
| 2719 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2720 | final int count = mChildrenCount; |
| 2721 | final View[] children = mChildren; |
| 2722 | for (int i = 0; i < count; i++) { |
| 2723 | children[i].dispatchDetachedFromWindow(); |
| 2724 | } |
John Reck | ca7a9da | 2014-03-05 16:29:07 -0800 | [diff] [blame] | 2725 | clearDisappearingChildren(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2726 | super.dispatchDetachedFromWindow(); |
| 2727 | } |
| 2728 | |
Fabrice Di Meglio | 23c89fd | 2012-08-13 12:17:42 -0700 | [diff] [blame] | 2729 | /** |
| 2730 | * @hide |
| 2731 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2732 | @Override |
Fabrice Di Meglio | 23c89fd | 2012-08-13 12:17:42 -0700 | [diff] [blame] | 2733 | protected void internalSetPadding(int left, int top, int right, int bottom) { |
Romain Guy | 2440e67 | 2012-08-07 14:43:43 -0700 | [diff] [blame] | 2734 | super.internalSetPadding(left, top, right, bottom); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2735 | |
Romain Guy | 13f35f3 | 2011-03-24 12:03:17 -0700 | [diff] [blame] | 2736 | if ((mPaddingLeft | mPaddingTop | mPaddingRight | mPaddingBottom) != 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2737 | mGroupFlags |= FLAG_PADDING_NOT_NULL; |
| 2738 | } else { |
| 2739 | mGroupFlags &= ~FLAG_PADDING_NOT_NULL; |
| 2740 | } |
| 2741 | } |
| 2742 | |
| 2743 | /** |
| 2744 | * {@inheritDoc} |
| 2745 | */ |
| 2746 | @Override |
| 2747 | protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) { |
| 2748 | super.dispatchSaveInstanceState(container); |
| 2749 | final int count = mChildrenCount; |
| 2750 | final View[] children = mChildren; |
| 2751 | for (int i = 0; i < count; i++) { |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 2752 | View c = children[i]; |
| 2753 | if ((c.mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED) { |
| 2754 | c.dispatchSaveInstanceState(container); |
| 2755 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2756 | } |
| 2757 | } |
| 2758 | |
| 2759 | /** |
Romain Guy | 9fc2781 | 2011-04-27 14:21:41 -0700 | [diff] [blame] | 2760 | * Perform dispatching of a {@link #saveHierarchyState(android.util.SparseArray)} freeze()} |
| 2761 | * to only this view, not to its children. For use when overriding |
| 2762 | * {@link #dispatchSaveInstanceState(android.util.SparseArray)} dispatchFreeze()} to allow |
| 2763 | * 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] | 2764 | * |
| 2765 | * @param container the container |
| 2766 | */ |
| 2767 | protected void dispatchFreezeSelfOnly(SparseArray<Parcelable> container) { |
| 2768 | super.dispatchSaveInstanceState(container); |
| 2769 | } |
| 2770 | |
| 2771 | /** |
| 2772 | * {@inheritDoc} |
| 2773 | */ |
| 2774 | @Override |
| 2775 | protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) { |
| 2776 | super.dispatchRestoreInstanceState(container); |
| 2777 | final int count = mChildrenCount; |
| 2778 | final View[] children = mChildren; |
| 2779 | for (int i = 0; i < count; i++) { |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 2780 | View c = children[i]; |
| 2781 | if ((c.mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED) { |
| 2782 | c.dispatchRestoreInstanceState(container); |
| 2783 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2784 | } |
| 2785 | } |
| 2786 | |
| 2787 | /** |
Romain Guy | 02739a8 | 2011-05-16 11:43:18 -0700 | [diff] [blame] | 2788 | * Perform dispatching of a {@link #restoreHierarchyState(android.util.SparseArray)} |
| 2789 | * to only this view, not to its children. For use when overriding |
| 2790 | * {@link #dispatchRestoreInstanceState(android.util.SparseArray)} to allow |
| 2791 | * 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] | 2792 | * |
| 2793 | * @param container the container |
| 2794 | */ |
| 2795 | protected void dispatchThawSelfOnly(SparseArray<Parcelable> container) { |
| 2796 | super.dispatchRestoreInstanceState(container); |
| 2797 | } |
| 2798 | |
| 2799 | /** |
| 2800 | * Enables or disables the drawing cache for each child of this view group. |
| 2801 | * |
| 2802 | * @param enabled true to enable the cache, false to dispose of it |
| 2803 | */ |
| 2804 | protected void setChildrenDrawingCacheEnabled(boolean enabled) { |
| 2805 | if (enabled || (mPersistentDrawingCache & PERSISTENT_ALL_CACHES) != PERSISTENT_ALL_CACHES) { |
| 2806 | final View[] children = mChildren; |
| 2807 | final int count = mChildrenCount; |
| 2808 | for (int i = 0; i < count; i++) { |
| 2809 | children[i].setDrawingCacheEnabled(enabled); |
| 2810 | } |
| 2811 | } |
| 2812 | } |
| 2813 | |
| 2814 | @Override |
| 2815 | protected void onAnimationStart() { |
| 2816 | super.onAnimationStart(); |
| 2817 | |
| 2818 | // When this ViewGroup's animation starts, build the cache for the children |
| 2819 | if ((mGroupFlags & FLAG_ANIMATION_CACHE) == FLAG_ANIMATION_CACHE) { |
| 2820 | final int count = mChildrenCount; |
| 2821 | final View[] children = mChildren; |
Romain Guy | 0d9275e | 2010-10-26 14:22:30 -0700 | [diff] [blame] | 2822 | final boolean buildCache = !isHardwareAccelerated(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2823 | |
| 2824 | for (int i = 0; i < count; i++) { |
| 2825 | final View child = children[i]; |
| 2826 | if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) { |
| 2827 | child.setDrawingCacheEnabled(true); |
Romain Guy | 0d9275e | 2010-10-26 14:22:30 -0700 | [diff] [blame] | 2828 | if (buildCache) { |
| 2829 | child.buildDrawingCache(true); |
| 2830 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2831 | } |
| 2832 | } |
| 2833 | |
| 2834 | mGroupFlags |= FLAG_CHILDREN_DRAWN_WITH_CACHE; |
| 2835 | } |
| 2836 | } |
| 2837 | |
| 2838 | @Override |
| 2839 | protected void onAnimationEnd() { |
| 2840 | super.onAnimationEnd(); |
| 2841 | |
| 2842 | // When this ViewGroup's animation ends, destroy the cache of the children |
| 2843 | if ((mGroupFlags & FLAG_ANIMATION_CACHE) == FLAG_ANIMATION_CACHE) { |
| 2844 | mGroupFlags &= ~FLAG_CHILDREN_DRAWN_WITH_CACHE; |
| 2845 | |
| 2846 | if ((mPersistentDrawingCache & PERSISTENT_ANIMATION_CACHE) == 0) { |
| 2847 | setChildrenDrawingCacheEnabled(false); |
| 2848 | } |
| 2849 | } |
| 2850 | } |
| 2851 | |
Romain Guy | 223ff5c | 2010-03-02 17:07:47 -0800 | [diff] [blame] | 2852 | @Override |
| 2853 | Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) { |
Romain Guy | 65554f2 | 2010-03-22 18:58:21 -0700 | [diff] [blame] | 2854 | int count = mChildrenCount; |
| 2855 | int[] visibilities = null; |
| 2856 | |
Romain Guy | 223ff5c | 2010-03-02 17:07:47 -0800 | [diff] [blame] | 2857 | if (skipChildren) { |
Romain Guy | 65554f2 | 2010-03-22 18:58:21 -0700 | [diff] [blame] | 2858 | visibilities = new int[count]; |
| 2859 | for (int i = 0; i < count; i++) { |
| 2860 | View child = getChildAt(i); |
| 2861 | visibilities[i] = child.getVisibility(); |
| 2862 | if (visibilities[i] == View.VISIBLE) { |
| 2863 | child.setVisibility(INVISIBLE); |
| 2864 | } |
| 2865 | } |
Romain Guy | 223ff5c | 2010-03-02 17:07:47 -0800 | [diff] [blame] | 2866 | } |
| 2867 | |
| 2868 | Bitmap b = super.createSnapshot(quality, backgroundColor, skipChildren); |
Romain Guy | 65554f2 | 2010-03-22 18:58:21 -0700 | [diff] [blame] | 2869 | |
| 2870 | if (skipChildren) { |
| 2871 | for (int i = 0; i < count; i++) { |
| 2872 | getChildAt(i).setVisibility(visibilities[i]); |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 2873 | } |
Romain Guy | 65554f2 | 2010-03-22 18:58:21 -0700 | [diff] [blame] | 2874 | } |
Romain Guy | 223ff5c | 2010-03-02 17:07:47 -0800 | [diff] [blame] | 2875 | |
| 2876 | return b; |
| 2877 | } |
| 2878 | |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 2879 | /** Return true if this ViewGroup is laying out using optical bounds. */ |
| 2880 | boolean isLayoutModeOptical() { |
| 2881 | return mLayoutMode == LAYOUT_MODE_OPTICAL_BOUNDS; |
| 2882 | } |
Romain Guy | cbc6774 | 2012-04-27 16:12:57 -0700 | [diff] [blame] | 2883 | |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 2884 | Insets computeOpticalInsets() { |
| 2885 | if (isLayoutModeOptical()) { |
| 2886 | int left = 0; |
| 2887 | int top = 0; |
| 2888 | int right = 0; |
| 2889 | int bottom = 0; |
| 2890 | for (int i = 0; i < mChildrenCount; i++) { |
| 2891 | View child = getChildAt(i); |
| 2892 | if (child.getVisibility() == VISIBLE) { |
| 2893 | Insets insets = child.getOpticalInsets(); |
| 2894 | left = Math.max(left, insets.left); |
| 2895 | top = Math.max(top, insets.top); |
| 2896 | right = Math.max(right, insets.right); |
| 2897 | bottom = Math.max(bottom, insets.bottom); |
| 2898 | } |
| 2899 | } |
| 2900 | return Insets.of(left, top, right, bottom); |
| 2901 | } else { |
| 2902 | return Insets.NONE; |
| 2903 | } |
| 2904 | } |
| 2905 | |
| 2906 | private static void fillRect(Canvas canvas, Paint paint, int x1, int y1, int x2, int y2) { |
| 2907 | if (x1 != x2 && y1 != y2) { |
| 2908 | if (x1 > x2) { |
| 2909 | int tmp = x1; x1 = x2; x2 = tmp; |
| 2910 | } |
| 2911 | if (y1 > y2) { |
| 2912 | int tmp = y1; y1 = y2; y2 = tmp; |
| 2913 | } |
| 2914 | canvas.drawRect(x1, y1, x2, y2, paint); |
| 2915 | } |
| 2916 | } |
| 2917 | |
| 2918 | private static int sign(int x) { |
| 2919 | return (x >= 0) ? 1 : -1; |
| 2920 | } |
| 2921 | |
| 2922 | private static void drawCorner(Canvas c, Paint paint, int x1, int y1, int dx, int dy, int lw) { |
| 2923 | fillRect(c, paint, x1, y1, x1 + dx, y1 + lw * sign(dy)); |
| 2924 | fillRect(c, paint, x1, y1, x1 + lw * sign(dx), y1 + dy); |
| 2925 | } |
| 2926 | |
| 2927 | private int dipsToPixels(int dips) { |
| 2928 | float scale = getContext().getResources().getDisplayMetrics().density; |
| 2929 | return (int) (dips * scale + 0.5f); |
| 2930 | } |
| 2931 | |
Romain Guy | 6410c0a | 2013-06-17 11:21:58 -0700 | [diff] [blame] | 2932 | private static void drawRectCorners(Canvas canvas, int x1, int y1, int x2, int y2, Paint paint, |
| 2933 | int lineLength, int lineWidth) { |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 2934 | drawCorner(canvas, paint, x1, y1, lineLength, lineLength, lineWidth); |
| 2935 | drawCorner(canvas, paint, x1, y2, lineLength, -lineLength, lineWidth); |
| 2936 | drawCorner(canvas, paint, x2, y1, -lineLength, lineLength, lineWidth); |
| 2937 | drawCorner(canvas, paint, x2, y2, -lineLength, -lineLength, lineWidth); |
| 2938 | } |
| 2939 | |
| 2940 | private static void fillDifference(Canvas canvas, |
| 2941 | int x2, int y2, int x3, int y3, |
| 2942 | int dx1, int dy1, int dx2, int dy2, Paint paint) { |
| 2943 | int x1 = x2 - dx1; |
| 2944 | int y1 = y2 - dy1; |
| 2945 | |
| 2946 | int x4 = x3 + dx2; |
| 2947 | int y4 = y3 + dy2; |
| 2948 | |
| 2949 | fillRect(canvas, paint, x1, y1, x4, y2); |
| 2950 | fillRect(canvas, paint, x1, y2, x2, y3); |
| 2951 | fillRect(canvas, paint, x3, y2, x4, y3); |
| 2952 | fillRect(canvas, paint, x1, y3, x4, y4); |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 2953 | } |
| 2954 | |
| 2955 | /** |
| 2956 | * @hide |
| 2957 | */ |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 2958 | protected void onDebugDrawMargins(Canvas canvas, Paint paint) { |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 2959 | for (int i = 0; i < getChildCount(); i++) { |
| 2960 | View c = getChildAt(i); |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 2961 | c.getLayoutParams().onDebugDraw(c, canvas, paint); |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 2962 | } |
| 2963 | } |
| 2964 | |
| 2965 | /** |
| 2966 | * @hide |
| 2967 | */ |
| 2968 | protected void onDebugDraw(Canvas canvas) { |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 2969 | Paint paint = getDebugPaint(); |
| 2970 | |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 2971 | // Draw optical bounds |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 2972 | { |
| 2973 | paint.setColor(Color.RED); |
| 2974 | paint.setStyle(Paint.Style.STROKE); |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 2975 | |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 2976 | for (int i = 0; i < getChildCount(); i++) { |
| 2977 | View c = getChildAt(i); |
Philip Milne | 7a23b49 | 2012-04-24 22:12:36 -0700 | [diff] [blame] | 2978 | Insets insets = c.getOpticalInsets(); |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 2979 | |
| 2980 | drawRect(canvas, paint, |
| 2981 | c.getLeft() + insets.left, |
| 2982 | c.getTop() + insets.top, |
| 2983 | c.getRight() - insets.right - 1, |
| 2984 | c.getBottom() - insets.bottom - 1); |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 2985 | } |
| 2986 | } |
| 2987 | |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 2988 | // Draw margins |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 2989 | { |
| 2990 | paint.setColor(Color.argb(63, 255, 0, 255)); |
| 2991 | paint.setStyle(Paint.Style.FILL); |
Philip Milne | 604f440 | 2012-04-24 19:27:11 -0700 | [diff] [blame] | 2992 | |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 2993 | onDebugDrawMargins(canvas, paint); |
| 2994 | } |
| 2995 | |
| 2996 | // Draw clip bounds |
| 2997 | { |
| 2998 | paint.setColor(Color.rgb(63, 127, 255)); |
| 2999 | paint.setStyle(Paint.Style.FILL); |
| 3000 | |
| 3001 | int lineLength = dipsToPixels(8); |
| 3002 | int lineWidth = dipsToPixels(1); |
| 3003 | for (int i = 0; i < getChildCount(); i++) { |
| 3004 | View c = getChildAt(i); |
| 3005 | drawRectCorners(canvas, c.getLeft(), c.getTop(), c.getRight(), c.getBottom(), |
| 3006 | paint, lineLength, lineWidth); |
| 3007 | } |
Philip Milne | 604f440 | 2012-04-24 19:27:11 -0700 | [diff] [blame] | 3008 | } |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 3009 | } |
| 3010 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3011 | /** |
| 3012 | * {@inheritDoc} |
| 3013 | */ |
| 3014 | @Override |
| 3015 | protected void dispatchDraw(Canvas canvas) { |
Chris Craik | a753f4c | 2014-07-24 12:39:17 -0700 | [diff] [blame] | 3016 | boolean usingRenderNodeProperties = canvas.isRecordingFor(mRenderNode); |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 3017 | final int childrenCount = mChildrenCount; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3018 | final View[] children = mChildren; |
| 3019 | int flags = mGroupFlags; |
| 3020 | |
| 3021 | if ((flags & FLAG_RUN_ANIMATION) != 0 && canAnimate()) { |
| 3022 | final boolean cache = (mGroupFlags & FLAG_ANIMATION_CACHE) == FLAG_ANIMATION_CACHE; |
| 3023 | |
Romain Guy | 0d9275e | 2010-10-26 14:22:30 -0700 | [diff] [blame] | 3024 | final boolean buildCache = !isHardwareAccelerated(); |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 3025 | for (int i = 0; i < childrenCount; i++) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3026 | final View child = children[i]; |
| 3027 | if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) { |
| 3028 | final LayoutParams params = child.getLayoutParams(); |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 3029 | attachLayoutAnimationParameters(child, params, i, childrenCount); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3030 | bindLayoutAnimation(child); |
| 3031 | if (cache) { |
| 3032 | child.setDrawingCacheEnabled(true); |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 3033 | if (buildCache) { |
Romain Guy | 0d9275e | 2010-10-26 14:22:30 -0700 | [diff] [blame] | 3034 | child.buildDrawingCache(true); |
| 3035 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3036 | } |
| 3037 | } |
| 3038 | } |
| 3039 | |
| 3040 | final LayoutAnimationController controller = mLayoutAnimationController; |
| 3041 | if (controller.willOverlap()) { |
| 3042 | mGroupFlags |= FLAG_OPTIMIZE_INVALIDATE; |
| 3043 | } |
| 3044 | |
| 3045 | controller.start(); |
| 3046 | |
| 3047 | mGroupFlags &= ~FLAG_RUN_ANIMATION; |
| 3048 | mGroupFlags &= ~FLAG_ANIMATION_DONE; |
| 3049 | |
| 3050 | if (cache) { |
| 3051 | mGroupFlags |= FLAG_CHILDREN_DRAWN_WITH_CACHE; |
| 3052 | } |
| 3053 | |
| 3054 | if (mAnimationListener != null) { |
| 3055 | mAnimationListener.onAnimationStart(controller.getAnimation()); |
| 3056 | } |
| 3057 | } |
| 3058 | |
Selim Cinek | 19cadc2 | 2014-04-16 17:27:19 +0200 | [diff] [blame] | 3059 | int clipSaveCount = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3060 | final boolean clipToPadding = (flags & CLIP_TO_PADDING_MASK) == CLIP_TO_PADDING_MASK; |
| 3061 | if (clipToPadding) { |
Chris Craik | e4cf152 | 2014-08-04 17:55:22 -0700 | [diff] [blame] | 3062 | clipSaveCount = canvas.save(); |
Romain Guy | 8f2d94f | 2009-03-25 18:04:42 -0700 | [diff] [blame] | 3063 | canvas.clipRect(mScrollX + mPaddingLeft, mScrollY + mPaddingTop, |
| 3064 | mScrollX + mRight - mLeft - mPaddingRight, |
| 3065 | mScrollY + mBottom - mTop - mPaddingBottom); |
Selim Cinek | 19cadc2 | 2014-04-16 17:27:19 +0200 | [diff] [blame] | 3066 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3067 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3068 | // We will draw our child's animation, let's reset the flag |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 3069 | mPrivateFlags &= ~PFLAG_DRAW_ANIMATION; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3070 | mGroupFlags &= ~FLAG_INVALIDATE_REQUIRED; |
| 3071 | |
| 3072 | boolean more = false; |
| 3073 | final long drawingTime = getDrawingTime(); |
| 3074 | |
Chris Craik | 8afd0f2 | 2014-08-21 17:41:57 -0700 | [diff] [blame] | 3075 | if (usingRenderNodeProperties) canvas.insertReorderBarrier(); |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 3076 | // Only use the preordered list if not HW accelerated, since the HW pipeline will do the |
| 3077 | // draw reordering internally |
Chris Craik | a753f4c | 2014-07-24 12:39:17 -0700 | [diff] [blame] | 3078 | final ArrayList<View> preorderedList = usingRenderNodeProperties |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 3079 | ? null : buildOrderedChildList(); |
| 3080 | final boolean customOrder = preorderedList == null |
| 3081 | && isChildrenDrawingOrderEnabled(); |
| 3082 | for (int i = 0; i < childrenCount; i++) { |
| 3083 | int childIndex = customOrder ? getChildDrawingOrder(childrenCount, i) : i; |
| 3084 | final View child = (preorderedList == null) |
| 3085 | ? children[childIndex] : preorderedList.get(childIndex); |
| 3086 | if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null) { |
| 3087 | more |= drawChild(canvas, child, drawingTime); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3088 | } |
| 3089 | } |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 3090 | if (preorderedList != null) preorderedList.clear(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3091 | |
| 3092 | // Draw any disappearing views that have animations |
| 3093 | if (mDisappearingChildren != null) { |
| 3094 | final ArrayList<View> disappearingChildren = mDisappearingChildren; |
| 3095 | final int disappearingCount = disappearingChildren.size() - 1; |
| 3096 | // Go backwards -- we may delete as animations finish |
| 3097 | for (int i = disappearingCount; i >= 0; i--) { |
| 3098 | final View child = disappearingChildren.get(i); |
| 3099 | more |= drawChild(canvas, child, drawingTime); |
| 3100 | } |
| 3101 | } |
Chris Craik | 8afd0f2 | 2014-08-21 17:41:57 -0700 | [diff] [blame] | 3102 | if (usingRenderNodeProperties) canvas.insertInorderBarrier(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3103 | |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 3104 | if (debugDraw()) { |
| 3105 | onDebugDraw(canvas); |
| 3106 | } |
| 3107 | |
Chris Craik | e4cf152 | 2014-08-04 17:55:22 -0700 | [diff] [blame] | 3108 | if (clipToPadding) { |
Selim Cinek | 19cadc2 | 2014-04-16 17:27:19 +0200 | [diff] [blame] | 3109 | canvas.restoreToCount(clipSaveCount); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3110 | } |
| 3111 | |
| 3112 | // mGroupFlags might have been updated by drawChild() |
| 3113 | flags = mGroupFlags; |
| 3114 | |
| 3115 | if ((flags & FLAG_INVALIDATE_REQUIRED) == FLAG_INVALIDATE_REQUIRED) { |
Romain Guy | 849d0a3 | 2011-02-01 17:20:48 -0800 | [diff] [blame] | 3116 | invalidate(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3117 | } |
| 3118 | |
| 3119 | if ((flags & FLAG_ANIMATION_DONE) == 0 && (flags & FLAG_NOTIFY_ANIMATION_LISTENER) == 0 && |
| 3120 | mLayoutAnimationController.isDone() && !more) { |
| 3121 | // We want to erase the drawing cache and notify the listener after the |
| 3122 | // next frame is drawn because one extra invalidate() is caused by |
| 3123 | // drawChild() after the animation is over |
| 3124 | mGroupFlags |= FLAG_NOTIFY_ANIMATION_LISTENER; |
| 3125 | final Runnable end = new Runnable() { |
| 3126 | public void run() { |
| 3127 | notifyAnimationListener(); |
| 3128 | } |
| 3129 | }; |
| 3130 | post(end); |
| 3131 | } |
| 3132 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 3133 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3134 | /** |
Chet Haase | edf6f4b | 2013-03-26 07:55:30 -0700 | [diff] [blame] | 3135 | * Returns the ViewGroupOverlay for this view group, creating it if it does |
| 3136 | * not yet exist. In addition to {@link ViewOverlay}'s support for drawables, |
| 3137 | * {@link ViewGroupOverlay} allows views to be added to the overlay. These |
| 3138 | * views, like overlay drawables, are visual-only; they do not receive input |
| 3139 | * events and should not be used as anything other than a temporary |
| 3140 | * representation of a view in a parent container, such as might be used |
| 3141 | * by an animation effect. |
| 3142 | * |
Chet Haase | 9539949 | 2013-04-08 14:30:31 -0700 | [diff] [blame] | 3143 | * <p>Note: Overlays do not currently work correctly with {@link |
| 3144 | * SurfaceView} or {@link TextureView}; contents in overlays for these |
| 3145 | * types of views may not display correctly.</p> |
| 3146 | * |
Chet Haase | edf6f4b | 2013-03-26 07:55:30 -0700 | [diff] [blame] | 3147 | * @return The ViewGroupOverlay object for this view. |
| 3148 | * @see ViewGroupOverlay |
| 3149 | */ |
| 3150 | @Override |
| 3151 | public ViewGroupOverlay getOverlay() { |
| 3152 | if (mOverlay == null) { |
| 3153 | mOverlay = new ViewGroupOverlay(mContext, this); |
| 3154 | } |
| 3155 | return (ViewGroupOverlay) mOverlay; |
| 3156 | } |
| 3157 | |
| 3158 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3159 | * Returns the index of the child to draw for this iteration. Override this |
| 3160 | * if you want to change the drawing order of children. By default, it |
| 3161 | * returns i. |
| 3162 | * <p> |
Romain Guy | 293451e | 2009-11-04 13:59:48 -0800 | [diff] [blame] | 3163 | * NOTE: In order for this method to be called, you must enable child ordering |
| 3164 | * first by calling {@link #setChildrenDrawingOrderEnabled(boolean)}. |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 3165 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3166 | * @param i The current iteration. |
| 3167 | * @return The index of the child to draw this iteration. |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 3168 | * |
Romain Guy | 293451e | 2009-11-04 13:59:48 -0800 | [diff] [blame] | 3169 | * @see #setChildrenDrawingOrderEnabled(boolean) |
| 3170 | * @see #isChildrenDrawingOrderEnabled() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3171 | */ |
| 3172 | protected int getChildDrawingOrder(int childCount, int i) { |
| 3173 | return i; |
| 3174 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 3175 | |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 3176 | private boolean hasChildWithZ() { |
| 3177 | for (int i = 0; i < mChildrenCount; i++) { |
| 3178 | if (mChildren[i].getZ() != 0) return true; |
| 3179 | } |
| 3180 | return false; |
| 3181 | } |
| 3182 | |
| 3183 | /** |
| 3184 | * Populates (and returns) mPreSortedChildren with a pre-ordered list of the View's children, |
| 3185 | * sorted first by Z, then by child drawing order (if applicable). |
| 3186 | * |
| 3187 | * Uses a stable, insertion sort which is commonly O(n) for ViewGroups with very few elevated |
| 3188 | * children. |
| 3189 | */ |
| 3190 | private ArrayList<View> buildOrderedChildList() { |
| 3191 | final int count = mChildrenCount; |
| 3192 | if (count <= 1 || !hasChildWithZ()) return null; |
| 3193 | |
| 3194 | if (mPreSortedChildren == null) { |
| 3195 | mPreSortedChildren = new ArrayList<View>(count); |
| 3196 | } else { |
| 3197 | mPreSortedChildren.ensureCapacity(count); |
| 3198 | } |
| 3199 | |
| 3200 | final boolean useCustomOrder = isChildrenDrawingOrderEnabled(); |
| 3201 | for (int i = 0; i < mChildrenCount; i++) { |
| 3202 | // add next child (in child order) to end of list |
| 3203 | int childIndex = useCustomOrder ? getChildDrawingOrder(mChildrenCount, i) : i; |
| 3204 | View nextChild = mChildren[childIndex]; |
| 3205 | float currentZ = nextChild.getZ(); |
| 3206 | |
| 3207 | // insert ahead of any Views with greater Z |
| 3208 | int insertIndex = i; |
| 3209 | while (insertIndex > 0 && mPreSortedChildren.get(insertIndex - 1).getZ() > currentZ) { |
| 3210 | insertIndex--; |
| 3211 | } |
| 3212 | mPreSortedChildren.add(insertIndex, nextChild); |
| 3213 | } |
| 3214 | return mPreSortedChildren; |
| 3215 | } |
| 3216 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3217 | private void notifyAnimationListener() { |
| 3218 | mGroupFlags &= ~FLAG_NOTIFY_ANIMATION_LISTENER; |
| 3219 | mGroupFlags |= FLAG_ANIMATION_DONE; |
| 3220 | |
| 3221 | if (mAnimationListener != null) { |
| 3222 | final Runnable end = new Runnable() { |
| 3223 | public void run() { |
| 3224 | mAnimationListener.onAnimationEnd(mLayoutAnimationController.getAnimation()); |
| 3225 | } |
| 3226 | }; |
| 3227 | post(end); |
| 3228 | } |
| 3229 | |
| 3230 | if ((mGroupFlags & FLAG_ANIMATION_CACHE) == FLAG_ANIMATION_CACHE) { |
| 3231 | mGroupFlags &= ~FLAG_CHILDREN_DRAWN_WITH_CACHE; |
| 3232 | if ((mPersistentDrawingCache & PERSISTENT_ANIMATION_CACHE) == 0) { |
| 3233 | setChildrenDrawingCacheEnabled(false); |
| 3234 | } |
| 3235 | } |
| 3236 | |
Romain Guy | 849d0a3 | 2011-02-01 17:20:48 -0800 | [diff] [blame] | 3237 | invalidate(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3238 | } |
| 3239 | |
| 3240 | /** |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 3241 | * This method is used to cause children of this ViewGroup to restore or recreate their |
| 3242 | * display lists. It is called by getDisplayList() when the parent ViewGroup does not need |
| 3243 | * to recreate its own display list, which would happen if it went through the normal |
| 3244 | * draw/dispatchDraw mechanisms. |
| 3245 | * |
| 3246 | * @hide |
| 3247 | */ |
| 3248 | @Override |
| 3249 | protected void dispatchGetDisplayList() { |
| 3250 | final int count = mChildrenCount; |
| 3251 | final View[] children = mChildren; |
| 3252 | for (int i = 0; i < count; i++) { |
| 3253 | final View child = children[i]; |
Romain Guy | 59c7f80 | 2011-09-29 17:21:45 -0700 | [diff] [blame] | 3254 | if (((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null) && |
| 3255 | child.hasStaticLayer()) { |
Chet Haase | 6c0665f | 2014-08-01 13:32:27 -0700 | [diff] [blame] | 3256 | recreateChildDisplayList(child); |
Romain Guy | 2f57ba5 | 2011-02-03 18:03:29 -0800 | [diff] [blame] | 3257 | } |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 3258 | } |
Chet Haase | 91cedf1 | 2013-03-11 07:56:30 -0700 | [diff] [blame] | 3259 | if (mOverlay != null) { |
Chet Haase | edf6f4b | 2013-03-26 07:55:30 -0700 | [diff] [blame] | 3260 | View overlayView = mOverlay.getOverlayView(); |
Chet Haase | 6c0665f | 2014-08-01 13:32:27 -0700 | [diff] [blame] | 3261 | recreateChildDisplayList(overlayView); |
Chet Haase | 91cedf1 | 2013-03-11 07:56:30 -0700 | [diff] [blame] | 3262 | } |
Chet Haase | 6c0665f | 2014-08-01 13:32:27 -0700 | [diff] [blame] | 3263 | if (mDisappearingChildren != null) { |
| 3264 | final ArrayList<View> disappearingChildren = mDisappearingChildren; |
| 3265 | final int disappearingCount = disappearingChildren.size(); |
| 3266 | for (int i = 0; i < disappearingCount; ++i) { |
| 3267 | final View child = disappearingChildren.get(i); |
| 3268 | recreateChildDisplayList(child); |
| 3269 | } |
| 3270 | } |
| 3271 | } |
| 3272 | |
| 3273 | private void recreateChildDisplayList(View child) { |
| 3274 | child.mRecreateDisplayList = (child.mPrivateFlags & PFLAG_INVALIDATED) |
| 3275 | == PFLAG_INVALIDATED; |
| 3276 | child.mPrivateFlags &= ~PFLAG_INVALIDATED; |
| 3277 | child.getDisplayList(); |
| 3278 | child.mRecreateDisplayList = false; |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 3279 | } |
| 3280 | |
| 3281 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3282 | * Draw one child of this View Group. This method is responsible for getting |
| 3283 | * the canvas in the right state. This includes clipping, translating so |
| 3284 | * that the child's scrolled origin is at 0, 0, and applying any animation |
| 3285 | * transformations. |
| 3286 | * |
| 3287 | * @param canvas The canvas on which to draw the child |
| 3288 | * @param child Who to draw |
Chet Haase | bcca79a | 2012-02-14 08:45:14 -0800 | [diff] [blame] | 3289 | * @param drawingTime The time at which draw is occurring |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3290 | * @return True if an invalidate() was issued |
| 3291 | */ |
| 3292 | protected boolean drawChild(Canvas canvas, View child, long drawingTime) { |
Chet Haase | 64a48c1 | 2012-02-13 16:33:29 -0800 | [diff] [blame] | 3293 | return child.draw(canvas, this, drawingTime); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3294 | } |
| 3295 | |
| 3296 | /** |
Chris Craik | d863a10 | 2013-12-19 13:31:15 -0800 | [diff] [blame] | 3297 | * 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] | 3298 | * The default value is true. |
| 3299 | * @see #setClipChildren(boolean) |
| 3300 | * |
| 3301 | * @return True if the group's children will be clipped to their bounds, |
| 3302 | * false otherwise. |
| 3303 | */ |
| 3304 | public boolean getClipChildren() { |
| 3305 | return ((mGroupFlags & FLAG_CLIP_CHILDREN) != 0); |
| 3306 | } |
| 3307 | |
| 3308 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3309 | * By default, children are clipped to their bounds before drawing. This |
| 3310 | * allows view groups to override this behavior for animations, etc. |
| 3311 | * |
| 3312 | * @param clipChildren true to clip children to their bounds, |
| 3313 | * false otherwise |
| 3314 | * @attr ref android.R.styleable#ViewGroup_clipChildren |
| 3315 | */ |
| 3316 | public void setClipChildren(boolean clipChildren) { |
Chet Haase | a1cff50 | 2012-02-21 13:43:44 -0800 | [diff] [blame] | 3317 | boolean previousValue = (mGroupFlags & FLAG_CLIP_CHILDREN) == FLAG_CLIP_CHILDREN; |
| 3318 | if (clipChildren != previousValue) { |
| 3319 | setBooleanFlag(FLAG_CLIP_CHILDREN, clipChildren); |
Chet Haase | 1271e2c | 2012-04-20 09:54:27 -0700 | [diff] [blame] | 3320 | for (int i = 0; i < mChildrenCount; ++i) { |
| 3321 | View child = getChildAt(i); |
Chris Craik | 64a12e1 | 2014-03-28 18:12:12 -0700 | [diff] [blame] | 3322 | if (child.mRenderNode != null) { |
| 3323 | child.mRenderNode.setClipToBounds(clipChildren); |
Chet Haase | a1cff50 | 2012-02-21 13:43:44 -0800 | [diff] [blame] | 3324 | } |
| 3325 | } |
John Reck | aae9f3b | 2014-07-28 09:30:36 -0700 | [diff] [blame] | 3326 | invalidate(true); |
Chet Haase | a1cff50 | 2012-02-21 13:43:44 -0800 | [diff] [blame] | 3327 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3328 | } |
| 3329 | |
| 3330 | /** |
| 3331 | * By default, children are clipped to the padding of the ViewGroup. This |
| 3332 | * allows view groups to override this behavior |
| 3333 | * |
| 3334 | * @param clipToPadding true to clip children to the padding of the |
| 3335 | * group, false otherwise |
| 3336 | * @attr ref android.R.styleable#ViewGroup_clipToPadding |
| 3337 | */ |
| 3338 | public void setClipToPadding(boolean clipToPadding) { |
John Reck | 9fa3a24 | 2014-06-27 15:57:19 -0700 | [diff] [blame] | 3339 | if (hasBooleanFlag(FLAG_CLIP_TO_PADDING) != clipToPadding) { |
| 3340 | setBooleanFlag(FLAG_CLIP_TO_PADDING, clipToPadding); |
John Reck | aae9f3b | 2014-07-28 09:30:36 -0700 | [diff] [blame] | 3341 | invalidate(true); |
John Reck | 9fa3a24 | 2014-06-27 15:57:19 -0700 | [diff] [blame] | 3342 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3343 | } |
| 3344 | |
| 3345 | /** |
Adam Powell | 1c35b08 | 2014-07-11 15:37:15 -0700 | [diff] [blame] | 3346 | * Check if this ViewGroup is configured to clip child views to its padding. |
| 3347 | * |
| 3348 | * @return true if this ViewGroup clips children to its padding, false otherwise |
| 3349 | * |
| 3350 | * @attr ref android.R.styleable#ViewGroup_clipToPadding |
| 3351 | */ |
| 3352 | public boolean getClipToPadding() { |
| 3353 | return hasBooleanFlag(FLAG_CLIP_TO_PADDING); |
| 3354 | } |
| 3355 | |
| 3356 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3357 | * {@inheritDoc} |
| 3358 | */ |
| 3359 | @Override |
| 3360 | public void dispatchSetSelected(boolean selected) { |
| 3361 | final View[] children = mChildren; |
| 3362 | final int count = mChildrenCount; |
| 3363 | for (int i = 0; i < count; i++) { |
| 3364 | children[i].setSelected(selected); |
| 3365 | } |
| 3366 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 3367 | |
Dianne Hackborn | d0fa371 | 2010-09-14 18:57:14 -0700 | [diff] [blame] | 3368 | /** |
| 3369 | * {@inheritDoc} |
| 3370 | */ |
| 3371 | @Override |
| 3372 | public void dispatchSetActivated(boolean activated) { |
| 3373 | final View[] children = mChildren; |
| 3374 | final int count = mChildrenCount; |
| 3375 | for (int i = 0; i < count; i++) { |
Dianne Hackborn | d0fa371 | 2010-09-14 18:57:14 -0700 | [diff] [blame] | 3376 | children[i].setActivated(activated); |
| 3377 | } |
| 3378 | } |
| 3379 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3380 | @Override |
| 3381 | protected void dispatchSetPressed(boolean pressed) { |
| 3382 | final View[] children = mChildren; |
| 3383 | final int count = mChildrenCount; |
| 3384 | for (int i = 0; i < count; i++) { |
Adam Powell | 035a1fc | 2012-02-27 15:23:50 -0800 | [diff] [blame] | 3385 | final View child = children[i]; |
| 3386 | // Children that are clickable on their own should not |
| 3387 | // show a pressed state when their parent view does. |
| 3388 | // Clearing a pressed state always propagates. |
| 3389 | if (!pressed || (!child.isClickable() && !child.isLongClickable())) { |
| 3390 | child.setPressed(pressed); |
| 3391 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3392 | } |
| 3393 | } |
| 3394 | |
Adam Powell | 1487466 | 2013-07-18 19:42:41 -0700 | [diff] [blame] | 3395 | @Override |
| 3396 | void dispatchCancelPendingInputEvents() { |
| 3397 | super.dispatchCancelPendingInputEvents(); |
| 3398 | |
| 3399 | final View[] children = mChildren; |
| 3400 | final int count = mChildrenCount; |
| 3401 | for (int i = 0; i < count; i++) { |
| 3402 | children[i].dispatchCancelPendingInputEvents(); |
| 3403 | } |
| 3404 | } |
| 3405 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3406 | /** |
| 3407 | * When this property is set to true, this ViewGroup supports static transformations on |
| 3408 | * children; this causes |
| 3409 | * {@link #getChildStaticTransformation(View, android.view.animation.Transformation)} to be |
| 3410 | * invoked when a child is drawn. |
| 3411 | * |
| 3412 | * Any subclass overriding |
| 3413 | * {@link #getChildStaticTransformation(View, android.view.animation.Transformation)} should |
| 3414 | * set this property to true. |
| 3415 | * |
| 3416 | * @param enabled True to enable static transformations on children, false otherwise. |
| 3417 | * |
Chet Haase | 599913d | 2012-07-23 16:22:05 -0700 | [diff] [blame] | 3418 | * @see #getChildStaticTransformation(View, android.view.animation.Transformation) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3419 | */ |
| 3420 | protected void setStaticTransformationsEnabled(boolean enabled) { |
| 3421 | setBooleanFlag(FLAG_SUPPORT_STATIC_TRANSFORMATIONS, enabled); |
| 3422 | } |
| 3423 | |
| 3424 | /** |
Chet Haase | 2d46fcc | 2011-12-19 18:01:05 -0800 | [diff] [blame] | 3425 | * Sets <code>t</code> to be the static transformation of the child, if set, returning a |
| 3426 | * boolean to indicate whether a static transform was set. The default implementation |
| 3427 | * simply returns <code>false</code>; subclasses may override this method for different |
Chet Haase | 599913d | 2012-07-23 16:22:05 -0700 | [diff] [blame] | 3428 | * behavior. {@link #setStaticTransformationsEnabled(boolean)} must be set to true |
| 3429 | * for this method to be called. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3430 | * |
Chet Haase | 2d46fcc | 2011-12-19 18:01:05 -0800 | [diff] [blame] | 3431 | * @param child The child view whose static transform is being requested |
| 3432 | * @param t The Transformation which will hold the result |
| 3433 | * @return true if the transformation was set, false otherwise |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 3434 | * @see #setStaticTransformationsEnabled(boolean) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3435 | */ |
| 3436 | protected boolean getChildStaticTransformation(View child, Transformation t) { |
| 3437 | return false; |
| 3438 | } |
| 3439 | |
Romain Guy | f699130 | 2013-06-05 17:19:01 -0700 | [diff] [blame] | 3440 | Transformation getChildTransformation() { |
| 3441 | if (mChildTransformation == null) { |
| 3442 | mChildTransformation = new Transformation(); |
| 3443 | } |
| 3444 | return mChildTransformation; |
| 3445 | } |
| 3446 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3447 | /** |
| 3448 | * {@hide} |
| 3449 | */ |
| 3450 | @Override |
| 3451 | protected View findViewTraversal(int id) { |
| 3452 | if (id == mID) { |
| 3453 | return this; |
| 3454 | } |
| 3455 | |
| 3456 | final View[] where = mChildren; |
| 3457 | final int len = mChildrenCount; |
| 3458 | |
| 3459 | for (int i = 0; i < len; i++) { |
| 3460 | View v = where[i]; |
| 3461 | |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 3462 | if ((v.mPrivateFlags & PFLAG_IS_ROOT_NAMESPACE) == 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3463 | v = v.findViewById(id); |
| 3464 | |
| 3465 | if (v != null) { |
| 3466 | return v; |
| 3467 | } |
| 3468 | } |
| 3469 | } |
| 3470 | |
| 3471 | return null; |
| 3472 | } |
| 3473 | |
| 3474 | /** |
| 3475 | * {@hide} |
| 3476 | */ |
| 3477 | @Override |
| 3478 | protected View findViewWithTagTraversal(Object tag) { |
| 3479 | if (tag != null && tag.equals(mTag)) { |
| 3480 | return this; |
| 3481 | } |
| 3482 | |
| 3483 | final View[] where = mChildren; |
| 3484 | final int len = mChildrenCount; |
| 3485 | |
| 3486 | for (int i = 0; i < len; i++) { |
| 3487 | View v = where[i]; |
| 3488 | |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 3489 | if ((v.mPrivateFlags & PFLAG_IS_ROOT_NAMESPACE) == 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3490 | v = v.findViewWithTag(tag); |
| 3491 | |
| 3492 | if (v != null) { |
| 3493 | return v; |
| 3494 | } |
| 3495 | } |
| 3496 | } |
| 3497 | |
| 3498 | return null; |
| 3499 | } |
| 3500 | |
| 3501 | /** |
Jeff Brown | 4e6319b | 2010-12-13 10:36:51 -0800 | [diff] [blame] | 3502 | * {@hide} |
| 3503 | */ |
| 3504 | @Override |
Jeff Brown | 4dfbec2 | 2011-08-15 14:55:37 -0700 | [diff] [blame] | 3505 | protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) { |
Jeff Brown | 4e6319b | 2010-12-13 10:36:51 -0800 | [diff] [blame] | 3506 | if (predicate.apply(this)) { |
| 3507 | return this; |
| 3508 | } |
| 3509 | |
| 3510 | final View[] where = mChildren; |
| 3511 | final int len = mChildrenCount; |
| 3512 | |
| 3513 | for (int i = 0; i < len; i++) { |
| 3514 | View v = where[i]; |
| 3515 | |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 3516 | if (v != childToSkip && (v.mPrivateFlags & PFLAG_IS_ROOT_NAMESPACE) == 0) { |
Jeff Brown | 4e6319b | 2010-12-13 10:36:51 -0800 | [diff] [blame] | 3517 | v = v.findViewByPredicate(predicate); |
| 3518 | |
| 3519 | if (v != null) { |
| 3520 | return v; |
| 3521 | } |
| 3522 | } |
| 3523 | } |
| 3524 | |
| 3525 | return null; |
| 3526 | } |
| 3527 | |
| 3528 | /** |
Romain Guy | 393a52c | 2012-05-22 20:21:08 -0700 | [diff] [blame] | 3529 | * <p>Adds a child view. If no layout parameters are already set on the child, the |
| 3530 | * default parameters for this ViewGroup are set on the child.</p> |
| 3531 | * |
| 3532 | * <p><strong>Note:</strong> do not invoke this method from |
| 3533 | * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)}, |
| 3534 | * {@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] | 3535 | * |
| 3536 | * @param child the child view to add |
| 3537 | * |
| 3538 | * @see #generateDefaultLayoutParams() |
| 3539 | */ |
| 3540 | public void addView(View child) { |
| 3541 | addView(child, -1); |
| 3542 | } |
| 3543 | |
| 3544 | /** |
| 3545 | * Adds a child view. If no layout parameters are already set on the child, the |
| 3546 | * default parameters for this ViewGroup are set on the child. |
Romain Guy | 393a52c | 2012-05-22 20:21:08 -0700 | [diff] [blame] | 3547 | * |
| 3548 | * <p><strong>Note:</strong> do not invoke this method from |
| 3549 | * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)}, |
| 3550 | * {@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] | 3551 | * |
| 3552 | * @param child the child view to add |
| 3553 | * @param index the position at which to add the child |
| 3554 | * |
| 3555 | * @see #generateDefaultLayoutParams() |
| 3556 | */ |
| 3557 | public void addView(View child, int index) { |
| 3558 | LayoutParams params = child.getLayoutParams(); |
| 3559 | if (params == null) { |
| 3560 | params = generateDefaultLayoutParams(); |
| 3561 | if (params == null) { |
| 3562 | throw new IllegalArgumentException("generateDefaultLayoutParams() cannot return null"); |
| 3563 | } |
| 3564 | } |
| 3565 | addView(child, index, params); |
| 3566 | } |
| 3567 | |
| 3568 | /** |
| 3569 | * Adds a child view with this ViewGroup's default layout parameters and the |
| 3570 | * specified width and height. |
| 3571 | * |
Romain Guy | 393a52c | 2012-05-22 20:21:08 -0700 | [diff] [blame] | 3572 | * <p><strong>Note:</strong> do not invoke this method from |
| 3573 | * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)}, |
| 3574 | * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p> |
| 3575 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3576 | * @param child the child view to add |
| 3577 | */ |
| 3578 | public void addView(View child, int width, int height) { |
| 3579 | final LayoutParams params = generateDefaultLayoutParams(); |
| 3580 | params.width = width; |
| 3581 | params.height = height; |
| 3582 | addView(child, -1, params); |
| 3583 | } |
| 3584 | |
| 3585 | /** |
| 3586 | * Adds a child view with the specified layout parameters. |
| 3587 | * |
Romain Guy | 393a52c | 2012-05-22 20:21:08 -0700 | [diff] [blame] | 3588 | * <p><strong>Note:</strong> do not invoke this method from |
| 3589 | * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)}, |
| 3590 | * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p> |
| 3591 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3592 | * @param child the child view to add |
| 3593 | * @param params the layout parameters to set on the child |
| 3594 | */ |
| 3595 | public void addView(View child, LayoutParams params) { |
| 3596 | addView(child, -1, params); |
| 3597 | } |
| 3598 | |
| 3599 | /** |
| 3600 | * Adds a child view with the specified layout parameters. |
| 3601 | * |
Romain Guy | 393a52c | 2012-05-22 20:21:08 -0700 | [diff] [blame] | 3602 | * <p><strong>Note:</strong> do not invoke this method from |
| 3603 | * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)}, |
| 3604 | * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p> |
| 3605 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3606 | * @param child the child view to add |
| 3607 | * @param index the position at which to add the child |
| 3608 | * @param params the layout parameters to set on the child |
| 3609 | */ |
| 3610 | public void addView(View child, int index, LayoutParams params) { |
| 3611 | if (DBG) { |
| 3612 | System.out.println(this + " addView"); |
| 3613 | } |
| 3614 | |
| 3615 | // addViewInner() will call child.requestLayout() when setting the new LayoutParams |
| 3616 | // therefore, we call requestLayout() on ourselves before, so that the child's request |
| 3617 | // will be blocked at our level |
| 3618 | requestLayout(); |
Romain Guy | 849d0a3 | 2011-02-01 17:20:48 -0800 | [diff] [blame] | 3619 | invalidate(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3620 | addViewInner(child, index, params, false); |
| 3621 | } |
| 3622 | |
| 3623 | /** |
| 3624 | * {@inheritDoc} |
| 3625 | */ |
| 3626 | public void updateViewLayout(View view, ViewGroup.LayoutParams params) { |
| 3627 | if (!checkLayoutParams(params)) { |
| 3628 | throw new IllegalArgumentException("Invalid LayoutParams supplied to " + this); |
| 3629 | } |
| 3630 | if (view.mParent != this) { |
| 3631 | throw new IllegalArgumentException("Given view not a child of " + this); |
| 3632 | } |
| 3633 | view.setLayoutParams(params); |
| 3634 | } |
| 3635 | |
| 3636 | /** |
| 3637 | * {@inheritDoc} |
| 3638 | */ |
| 3639 | protected boolean checkLayoutParams(ViewGroup.LayoutParams p) { |
| 3640 | return p != null; |
| 3641 | } |
| 3642 | |
| 3643 | /** |
| 3644 | * Interface definition for a callback to be invoked when the hierarchy |
| 3645 | * within this view changed. The hierarchy changes whenever a child is added |
| 3646 | * to or removed from this view. |
| 3647 | */ |
| 3648 | public interface OnHierarchyChangeListener { |
| 3649 | /** |
| 3650 | * Called when a new child is added to a parent view. |
| 3651 | * |
| 3652 | * @param parent the view in which a child was added |
| 3653 | * @param child the new child view added in the hierarchy |
| 3654 | */ |
| 3655 | void onChildViewAdded(View parent, View child); |
| 3656 | |
| 3657 | /** |
| 3658 | * Called when a child is removed from a parent view. |
| 3659 | * |
| 3660 | * @param parent the view from which the child was removed |
| 3661 | * @param child the child removed from the hierarchy |
| 3662 | */ |
| 3663 | void onChildViewRemoved(View parent, View child); |
| 3664 | } |
| 3665 | |
| 3666 | /** |
| 3667 | * Register a callback to be invoked when a child is added to or removed |
| 3668 | * from this view. |
| 3669 | * |
| 3670 | * @param listener the callback to invoke on hierarchy change |
| 3671 | */ |
| 3672 | public void setOnHierarchyChangeListener(OnHierarchyChangeListener listener) { |
| 3673 | mOnHierarchyChangeListener = listener; |
| 3674 | } |
| 3675 | |
| 3676 | /** |
Philip Milne | f51d91c | 2011-07-18 16:12:19 -0700 | [diff] [blame] | 3677 | * @hide |
| 3678 | */ |
| 3679 | protected void onViewAdded(View child) { |
| 3680 | if (mOnHierarchyChangeListener != null) { |
| 3681 | mOnHierarchyChangeListener.onChildViewAdded(this, child); |
| 3682 | } |
| 3683 | } |
| 3684 | |
| 3685 | /** |
| 3686 | * @hide |
| 3687 | */ |
| 3688 | protected void onViewRemoved(View child) { |
| 3689 | if (mOnHierarchyChangeListener != null) { |
| 3690 | mOnHierarchyChangeListener.onChildViewRemoved(this, child); |
| 3691 | } |
| 3692 | } |
| 3693 | |
Philip Milne | cfb631b | 2012-10-26 10:51:46 -0700 | [diff] [blame] | 3694 | private void clearCachedLayoutMode() { |
Svetoslav | 6254f48 | 2013-06-04 17:22:14 -0700 | [diff] [blame] | 3695 | if (!hasBooleanFlag(FLAG_LAYOUT_MODE_WAS_EXPLICITLY_SET)) { |
Philip Milne | cfb631b | 2012-10-26 10:51:46 -0700 | [diff] [blame] | 3696 | mLayoutMode = LAYOUT_MODE_UNDEFINED; |
| 3697 | } |
| 3698 | } |
| 3699 | |
| 3700 | @Override |
| 3701 | protected void onAttachedToWindow() { |
| 3702 | super.onAttachedToWindow(); |
| 3703 | clearCachedLayoutMode(); |
| 3704 | } |
| 3705 | |
| 3706 | @Override |
| 3707 | protected void onDetachedFromWindow() { |
| 3708 | super.onDetachedFromWindow(); |
| 3709 | clearCachedLayoutMode(); |
| 3710 | } |
| 3711 | |
Philip Milne | f51d91c | 2011-07-18 16:12:19 -0700 | [diff] [blame] | 3712 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3713 | * Adds a view during layout. This is useful if in your onLayout() method, |
| 3714 | * you need to add more views (as does the list view for example). |
| 3715 | * |
| 3716 | * If index is negative, it means put it at the end of the list. |
| 3717 | * |
| 3718 | * @param child the view to add to the group |
| 3719 | * @param index the index at which the child must be added |
| 3720 | * @param params the layout parameters to associate with the child |
| 3721 | * @return true if the child was added, false otherwise |
| 3722 | */ |
| 3723 | protected boolean addViewInLayout(View child, int index, LayoutParams params) { |
| 3724 | return addViewInLayout(child, index, params, false); |
| 3725 | } |
| 3726 | |
| 3727 | /** |
| 3728 | * Adds a view during layout. This is useful if in your onLayout() method, |
| 3729 | * you need to add more views (as does the list view for example). |
| 3730 | * |
| 3731 | * If index is negative, it means put it at the end of the list. |
| 3732 | * |
| 3733 | * @param child the view to add to the group |
| 3734 | * @param index the index at which the child must be added |
| 3735 | * @param params the layout parameters to associate with the child |
| 3736 | * @param preventRequestLayout if true, calling this method will not trigger a |
| 3737 | * layout request on child |
| 3738 | * @return true if the child was added, false otherwise |
| 3739 | */ |
| 3740 | protected boolean addViewInLayout(View child, int index, LayoutParams params, |
| 3741 | boolean preventRequestLayout) { |
| 3742 | child.mParent = null; |
| 3743 | addViewInner(child, index, params, preventRequestLayout); |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 3744 | child.mPrivateFlags = (child.mPrivateFlags & ~PFLAG_DIRTY_MASK) | PFLAG_DRAWN; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3745 | return true; |
| 3746 | } |
| 3747 | |
| 3748 | /** |
| 3749 | * Prevents the specified child to be laid out during the next layout pass. |
| 3750 | * |
| 3751 | * @param child the child on which to perform the cleanup |
| 3752 | */ |
| 3753 | protected void cleanupLayoutState(View child) { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 3754 | child.mPrivateFlags &= ~View.PFLAG_FORCE_LAYOUT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3755 | } |
| 3756 | |
| 3757 | private void addViewInner(View child, int index, LayoutParams params, |
| 3758 | boolean preventRequestLayout) { |
| 3759 | |
Chet Haase | e8e45d3 | 2011-03-02 17:07:35 -0800 | [diff] [blame] | 3760 | if (mTransition != null) { |
| 3761 | // Don't prevent other add transitions from completing, but cancel remove |
| 3762 | // transitions to let them complete the process before we add to the container |
| 3763 | mTransition.cancel(LayoutTransition.DISAPPEARING); |
Chet Haase | add6577 | 2011-02-09 16:47:29 -0800 | [diff] [blame] | 3764 | } |
| 3765 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3766 | if (child.getParent() != null) { |
| 3767 | throw new IllegalStateException("The specified child already has a parent. " + |
| 3768 | "You must call removeView() on the child's parent first."); |
| 3769 | } |
| 3770 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 3771 | if (mTransition != null) { |
Chet Haase | 5e25c2c | 2010-09-16 11:15:56 -0700 | [diff] [blame] | 3772 | mTransition.addChild(this, child); |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 3773 | } |
| 3774 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3775 | if (!checkLayoutParams(params)) { |
| 3776 | params = generateLayoutParams(params); |
| 3777 | } |
| 3778 | |
| 3779 | if (preventRequestLayout) { |
| 3780 | child.mLayoutParams = params; |
| 3781 | } else { |
| 3782 | child.setLayoutParams(params); |
| 3783 | } |
| 3784 | |
| 3785 | if (index < 0) { |
| 3786 | index = mChildrenCount; |
| 3787 | } |
| 3788 | |
| 3789 | addInArray(child, index); |
| 3790 | |
| 3791 | // tell our children |
| 3792 | if (preventRequestLayout) { |
| 3793 | child.assignParent(this); |
| 3794 | } else { |
| 3795 | child.mParent = this; |
| 3796 | } |
| 3797 | |
| 3798 | if (child.hasFocus()) { |
| 3799 | requestChildFocus(child, child.findFocus()); |
| 3800 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 3801 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3802 | AttachInfo ai = mAttachInfo; |
Adam Powell | 4b86788 | 2011-09-16 12:59:46 -0700 | [diff] [blame] | 3803 | if (ai != null && (mGroupFlags & FLAG_PREVENT_DISPATCH_ATTACHED_TO_WINDOW) == 0) { |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 3804 | boolean lastKeepOn = ai.mKeepScreenOn; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3805 | ai.mKeepScreenOn = false; |
| 3806 | child.dispatchAttachedToWindow(mAttachInfo, (mViewFlags&VISIBILITY_MASK)); |
| 3807 | if (ai.mKeepScreenOn) { |
| 3808 | needGlobalAttributesUpdate(true); |
| 3809 | } |
| 3810 | ai.mKeepScreenOn = lastKeepOn; |
| 3811 | } |
| 3812 | |
Fabrice Di Meglio | 9a04856 | 2012-09-26 14:55:56 -0700 | [diff] [blame] | 3813 | if (child.isLayoutDirectionInherited()) { |
Fabrice Di Meglio | a7e0bcd | 2012-10-16 19:55:01 -0700 | [diff] [blame] | 3814 | child.resetRtlProperties(); |
Fabrice Di Meglio | 9a04856 | 2012-09-26 14:55:56 -0700 | [diff] [blame] | 3815 | } |
| 3816 | |
Philip Milne | f51d91c | 2011-07-18 16:12:19 -0700 | [diff] [blame] | 3817 | onViewAdded(child); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3818 | |
| 3819 | if ((child.mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE) { |
| 3820 | mGroupFlags |= FLAG_NOTIFY_CHILDREN_ON_DRAWABLE_STATE_CHANGE; |
| 3821 | } |
Adam Powell | 539ee87 | 2012-02-03 19:00:49 -0800 | [diff] [blame] | 3822 | |
| 3823 | if (child.hasTransientState()) { |
| 3824 | childHasTransientStateChanged(child, true); |
| 3825 | } |
Svetoslav | 6254f48 | 2013-06-04 17:22:14 -0700 | [diff] [blame] | 3826 | |
Svetoslav | 8e3feb1 | 2014-02-24 13:46:47 -0800 | [diff] [blame] | 3827 | if (child.getVisibility() != View.GONE) { |
Svetoslav | 00dbe81 | 2013-06-10 12:51:09 -0700 | [diff] [blame] | 3828 | notifySubtreeAccessibilityStateChangedIfNeeded(); |
Svetoslav | 6254f48 | 2013-06-04 17:22:14 -0700 | [diff] [blame] | 3829 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3830 | } |
| 3831 | |
| 3832 | private void addInArray(View child, int index) { |
| 3833 | View[] children = mChildren; |
| 3834 | final int count = mChildrenCount; |
| 3835 | final int size = children.length; |
| 3836 | if (index == count) { |
| 3837 | if (size == count) { |
| 3838 | mChildren = new View[size + ARRAY_CAPACITY_INCREMENT]; |
| 3839 | System.arraycopy(children, 0, mChildren, 0, size); |
| 3840 | children = mChildren; |
| 3841 | } |
| 3842 | children[mChildrenCount++] = child; |
| 3843 | } else if (index < count) { |
| 3844 | if (size == count) { |
| 3845 | mChildren = new View[size + ARRAY_CAPACITY_INCREMENT]; |
| 3846 | System.arraycopy(children, 0, mChildren, 0, index); |
| 3847 | System.arraycopy(children, index, mChildren, index + 1, count - index); |
| 3848 | children = mChildren; |
| 3849 | } else { |
| 3850 | System.arraycopy(children, index, children, index + 1, count - index); |
| 3851 | } |
| 3852 | children[index] = child; |
| 3853 | mChildrenCount++; |
Joe Onorato | 03ab0c7 | 2011-01-06 15:46:27 -0800 | [diff] [blame] | 3854 | if (mLastTouchDownIndex >= index) { |
| 3855 | mLastTouchDownIndex++; |
| 3856 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3857 | } else { |
| 3858 | throw new IndexOutOfBoundsException("index=" + index + " count=" + count); |
| 3859 | } |
| 3860 | } |
| 3861 | |
| 3862 | // This method also sets the child's mParent to null |
| 3863 | private void removeFromArray(int index) { |
| 3864 | final View[] children = mChildren; |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 3865 | if (!(mTransitioningViews != null && mTransitioningViews.contains(children[index]))) { |
| 3866 | children[index].mParent = null; |
| 3867 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3868 | final int count = mChildrenCount; |
| 3869 | if (index == count - 1) { |
| 3870 | children[--mChildrenCount] = null; |
| 3871 | } else if (index >= 0 && index < count) { |
| 3872 | System.arraycopy(children, index + 1, children, index, count - index - 1); |
| 3873 | children[--mChildrenCount] = null; |
| 3874 | } else { |
| 3875 | throw new IndexOutOfBoundsException(); |
| 3876 | } |
Joe Onorato | 03ab0c7 | 2011-01-06 15:46:27 -0800 | [diff] [blame] | 3877 | if (mLastTouchDownIndex == index) { |
| 3878 | mLastTouchDownTime = 0; |
| 3879 | mLastTouchDownIndex = -1; |
| 3880 | } else if (mLastTouchDownIndex > index) { |
| 3881 | mLastTouchDownIndex--; |
| 3882 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3883 | } |
| 3884 | |
| 3885 | // This method also sets the children's mParent to null |
| 3886 | private void removeFromArray(int start, int count) { |
| 3887 | final View[] children = mChildren; |
| 3888 | final int childrenCount = mChildrenCount; |
| 3889 | |
| 3890 | start = Math.max(0, start); |
| 3891 | final int end = Math.min(childrenCount, start + count); |
| 3892 | |
| 3893 | if (start == end) { |
| 3894 | return; |
| 3895 | } |
| 3896 | |
| 3897 | if (end == childrenCount) { |
| 3898 | for (int i = start; i < end; i++) { |
| 3899 | children[i].mParent = null; |
| 3900 | children[i] = null; |
| 3901 | } |
| 3902 | } else { |
| 3903 | for (int i = start; i < end; i++) { |
| 3904 | children[i].mParent = null; |
| 3905 | } |
| 3906 | |
| 3907 | // Since we're looping above, we might as well do the copy, but is arraycopy() |
| 3908 | // faster than the extra 2 bounds checks we would do in the loop? |
| 3909 | System.arraycopy(children, end, children, start, childrenCount - end); |
| 3910 | |
| 3911 | for (int i = childrenCount - (end - start); i < childrenCount; i++) { |
| 3912 | children[i] = null; |
| 3913 | } |
| 3914 | } |
| 3915 | |
| 3916 | mChildrenCount -= (end - start); |
| 3917 | } |
| 3918 | |
| 3919 | private void bindLayoutAnimation(View child) { |
| 3920 | Animation a = mLayoutAnimationController.getAnimationForView(child); |
| 3921 | child.setAnimation(a); |
| 3922 | } |
| 3923 | |
| 3924 | /** |
| 3925 | * Subclasses should override this method to set layout animation |
| 3926 | * parameters on the supplied child. |
| 3927 | * |
| 3928 | * @param child the child to associate with animation parameters |
| 3929 | * @param params the child's layout parameters which hold the animation |
| 3930 | * parameters |
| 3931 | * @param index the index of the child in the view group |
| 3932 | * @param count the number of children in the view group |
| 3933 | */ |
| 3934 | protected void attachLayoutAnimationParameters(View child, |
| 3935 | LayoutParams params, int index, int count) { |
| 3936 | LayoutAnimationController.AnimationParameters animationParams = |
| 3937 | params.layoutAnimationParameters; |
| 3938 | if (animationParams == null) { |
| 3939 | animationParams = new LayoutAnimationController.AnimationParameters(); |
| 3940 | params.layoutAnimationParameters = animationParams; |
| 3941 | } |
| 3942 | |
| 3943 | animationParams.count = count; |
| 3944 | animationParams.index = index; |
| 3945 | } |
| 3946 | |
| 3947 | /** |
| 3948 | * {@inheritDoc} |
Romain Guy | 393a52c | 2012-05-22 20:21:08 -0700 | [diff] [blame] | 3949 | * |
| 3950 | * <p><strong>Note:</strong> do not invoke this method from |
| 3951 | * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)}, |
| 3952 | * {@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] | 3953 | */ |
| 3954 | public void removeView(View view) { |
| 3955 | removeViewInternal(view); |
| 3956 | requestLayout(); |
Romain Guy | 849d0a3 | 2011-02-01 17:20:48 -0800 | [diff] [blame] | 3957 | invalidate(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3958 | } |
| 3959 | |
| 3960 | /** |
| 3961 | * Removes a view during layout. This is useful if in your onLayout() method, |
| 3962 | * you need to remove more views. |
| 3963 | * |
Romain Guy | 393a52c | 2012-05-22 20:21:08 -0700 | [diff] [blame] | 3964 | * <p><strong>Note:</strong> do not invoke this method from |
| 3965 | * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)}, |
| 3966 | * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p> |
| 3967 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3968 | * @param view the view to remove from the group |
| 3969 | */ |
| 3970 | public void removeViewInLayout(View view) { |
| 3971 | removeViewInternal(view); |
| 3972 | } |
| 3973 | |
| 3974 | /** |
| 3975 | * Removes a range of views during layout. This is useful if in your onLayout() method, |
| 3976 | * you need to remove more views. |
| 3977 | * |
Romain Guy | 393a52c | 2012-05-22 20:21:08 -0700 | [diff] [blame] | 3978 | * <p><strong>Note:</strong> do not invoke this method from |
| 3979 | * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)}, |
| 3980 | * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p> |
| 3981 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3982 | * @param start the index of the first view to remove from the group |
| 3983 | * @param count the number of views to remove from the group |
| 3984 | */ |
| 3985 | public void removeViewsInLayout(int start, int count) { |
| 3986 | removeViewsInternal(start, count); |
| 3987 | } |
| 3988 | |
| 3989 | /** |
| 3990 | * Removes the view at the specified position in the group. |
| 3991 | * |
Romain Guy | 393a52c | 2012-05-22 20:21:08 -0700 | [diff] [blame] | 3992 | * <p><strong>Note:</strong> do not invoke this method from |
| 3993 | * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)}, |
| 3994 | * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p> |
| 3995 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3996 | * @param index the position in the group of the view to remove |
| 3997 | */ |
| 3998 | public void removeViewAt(int index) { |
| 3999 | removeViewInternal(index, getChildAt(index)); |
| 4000 | requestLayout(); |
Romain Guy | 849d0a3 | 2011-02-01 17:20:48 -0800 | [diff] [blame] | 4001 | invalidate(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4002 | } |
| 4003 | |
| 4004 | /** |
| 4005 | * Removes the specified range of views from the group. |
| 4006 | * |
Romain Guy | 393a52c | 2012-05-22 20:21:08 -0700 | [diff] [blame] | 4007 | * <p><strong>Note:</strong> do not invoke this method from |
| 4008 | * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)}, |
| 4009 | * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p> |
| 4010 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4011 | * @param start the first position in the group of the range of views to remove |
| 4012 | * @param count the number of views to remove |
| 4013 | */ |
| 4014 | public void removeViews(int start, int count) { |
| 4015 | removeViewsInternal(start, count); |
| 4016 | requestLayout(); |
Romain Guy | 849d0a3 | 2011-02-01 17:20:48 -0800 | [diff] [blame] | 4017 | invalidate(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4018 | } |
| 4019 | |
| 4020 | private void removeViewInternal(View view) { |
| 4021 | final int index = indexOfChild(view); |
| 4022 | if (index >= 0) { |
| 4023 | removeViewInternal(index, view); |
| 4024 | } |
| 4025 | } |
| 4026 | |
| 4027 | private void removeViewInternal(int index, View view) { |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4028 | |
| 4029 | if (mTransition != null) { |
Chet Haase | 5e25c2c | 2010-09-16 11:15:56 -0700 | [diff] [blame] | 4030 | mTransition.removeChild(this, view); |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4031 | } |
| 4032 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4033 | boolean clearChildFocus = false; |
| 4034 | if (view == mFocused) { |
Alan Viverette | 223622a | 2013-12-17 13:29:02 -0800 | [diff] [blame] | 4035 | view.unFocus(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4036 | clearChildFocus = true; |
| 4037 | } |
| 4038 | |
Svetoslav Ganov | 149567f | 2013-01-08 15:23:34 -0800 | [diff] [blame] | 4039 | if (view.isAccessibilityFocused()) { |
| 4040 | view.clearAccessibilityFocus(); |
| 4041 | } |
Svetoslav Ganov | 961bf0e | 2012-05-08 09:40:03 -0700 | [diff] [blame] | 4042 | |
Jeff Brown | 59a422e | 2012-04-19 15:19:19 -0700 | [diff] [blame] | 4043 | cancelTouchTarget(view); |
| 4044 | cancelHoverTarget(view); |
| 4045 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4046 | if (view.getAnimation() != null || |
| 4047 | (mTransitioningViews != null && mTransitioningViews.contains(view))) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4048 | addDisappearingView(view); |
| 4049 | } else if (view.mAttachInfo != null) { |
| 4050 | view.dispatchDetachedFromWindow(); |
| 4051 | } |
| 4052 | |
Adam Powell | 539ee87 | 2012-02-03 19:00:49 -0800 | [diff] [blame] | 4053 | if (view.hasTransientState()) { |
| 4054 | childHasTransientStateChanged(view, false); |
| 4055 | } |
| 4056 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4057 | needGlobalAttributesUpdate(false); |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 4058 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4059 | removeFromArray(index); |
| 4060 | |
| 4061 | if (clearChildFocus) { |
| 4062 | clearChildFocus(view); |
Svetoslav Ganov | 149567f | 2013-01-08 15:23:34 -0800 | [diff] [blame] | 4063 | if (!rootViewRequestFocus()) { |
| 4064 | notifyGlobalFocusCleared(this); |
| 4065 | } |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 4066 | } |
Romain Guy | 6fb0563 | 2012-11-29 10:50:33 -0800 | [diff] [blame] | 4067 | |
| 4068 | onViewRemoved(view); |
Svetoslav | 6254f48 | 2013-06-04 17:22:14 -0700 | [diff] [blame] | 4069 | |
Svetoslav | 8e3feb1 | 2014-02-24 13:46:47 -0800 | [diff] [blame] | 4070 | if (view.getVisibility() != View.GONE) { |
Svetoslav | 00dbe81 | 2013-06-10 12:51:09 -0700 | [diff] [blame] | 4071 | notifySubtreeAccessibilityStateChangedIfNeeded(); |
Svetoslav | 6254f48 | 2013-06-04 17:22:14 -0700 | [diff] [blame] | 4072 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4073 | } |
| 4074 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4075 | /** |
| 4076 | * Sets the LayoutTransition object for this ViewGroup. If the LayoutTransition object is |
| 4077 | * not null, changes in layout which occur because of children being added to or removed from |
| 4078 | * the ViewGroup will be animated according to the animations defined in that LayoutTransition |
| 4079 | * object. By default, the transition object is null (so layout changes are not animated). |
| 4080 | * |
Chet Haase | ef3cbfd | 2013-08-21 14:01:02 -0700 | [diff] [blame] | 4081 | * <p>Replacing a non-null transition will cause that previous transition to be |
| 4082 | * canceled, if it is currently running, to restore this container to |
| 4083 | * its correct post-transition state.</p> |
| 4084 | * |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4085 | * @param transition The LayoutTransition object that will animated changes in layout. A value |
| 4086 | * of <code>null</code> means no transition will run on layout changes. |
Chet Haase | 13cc120 | 2010-09-03 15:39:20 -0700 | [diff] [blame] | 4087 | * @attr ref android.R.styleable#ViewGroup_animateLayoutChanges |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4088 | */ |
| 4089 | public void setLayoutTransition(LayoutTransition transition) { |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 4090 | if (mTransition != null) { |
Chet Haase | fee6f2b | 2013-08-27 12:22:29 -0700 | [diff] [blame] | 4091 | LayoutTransition previousTransition = mTransition; |
| 4092 | previousTransition.cancel(); |
| 4093 | previousTransition.removeTransitionListener(mLayoutTransitionListener); |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 4094 | } |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4095 | mTransition = transition; |
Chet Haase | 13cc120 | 2010-09-03 15:39:20 -0700 | [diff] [blame] | 4096 | if (mTransition != null) { |
| 4097 | mTransition.addTransitionListener(mLayoutTransitionListener); |
| 4098 | } |
| 4099 | } |
| 4100 | |
| 4101 | /** |
| 4102 | * Gets the LayoutTransition object for this ViewGroup. If the LayoutTransition object is |
| 4103 | * not null, changes in layout which occur because of children being added to or removed from |
| 4104 | * the ViewGroup will be animated according to the animations defined in that LayoutTransition |
| 4105 | * object. By default, the transition object is null (so layout changes are not animated). |
| 4106 | * |
| 4107 | * @return LayoutTranstion The LayoutTransition object that will animated changes in layout. |
| 4108 | * A value of <code>null</code> means no transition will run on layout changes. |
| 4109 | */ |
| 4110 | public LayoutTransition getLayoutTransition() { |
| 4111 | return mTransition; |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4112 | } |
| 4113 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4114 | private void removeViewsInternal(int start, int count) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4115 | final View focused = mFocused; |
| 4116 | final boolean detach = mAttachInfo != null; |
Svetoslav Ganov | 149567f | 2013-01-08 15:23:34 -0800 | [diff] [blame] | 4117 | boolean clearChildFocus = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4118 | |
| 4119 | final View[] children = mChildren; |
| 4120 | final int end = start + count; |
| 4121 | |
| 4122 | for (int i = start; i < end; i++) { |
| 4123 | final View view = children[i]; |
| 4124 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4125 | if (mTransition != null) { |
Chet Haase | 5e25c2c | 2010-09-16 11:15:56 -0700 | [diff] [blame] | 4126 | mTransition.removeChild(this, view); |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4127 | } |
| 4128 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4129 | if (view == focused) { |
Alan Viverette | 223622a | 2013-12-17 13:29:02 -0800 | [diff] [blame] | 4130 | view.unFocus(null); |
Svetoslav Ganov | 149567f | 2013-01-08 15:23:34 -0800 | [diff] [blame] | 4131 | clearChildFocus = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4132 | } |
| 4133 | |
Svetoslav Ganov | 149567f | 2013-01-08 15:23:34 -0800 | [diff] [blame] | 4134 | if (view.isAccessibilityFocused()) { |
| 4135 | view.clearAccessibilityFocus(); |
| 4136 | } |
Svetoslav Ganov | 961bf0e | 2012-05-08 09:40:03 -0700 | [diff] [blame] | 4137 | |
Jeff Brown | 59a422e | 2012-04-19 15:19:19 -0700 | [diff] [blame] | 4138 | cancelTouchTarget(view); |
| 4139 | cancelHoverTarget(view); |
| 4140 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4141 | if (view.getAnimation() != null || |
| 4142 | (mTransitioningViews != null && mTransitioningViews.contains(view))) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4143 | addDisappearingView(view); |
| 4144 | } else if (detach) { |
| 4145 | view.dispatchDetachedFromWindow(); |
| 4146 | } |
| 4147 | |
Adam Powell | 539ee87 | 2012-02-03 19:00:49 -0800 | [diff] [blame] | 4148 | if (view.hasTransientState()) { |
| 4149 | childHasTransientStateChanged(view, false); |
| 4150 | } |
| 4151 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4152 | needGlobalAttributesUpdate(false); |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 4153 | |
Philip Milne | f51d91c | 2011-07-18 16:12:19 -0700 | [diff] [blame] | 4154 | onViewRemoved(view); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4155 | } |
| 4156 | |
| 4157 | removeFromArray(start, count); |
| 4158 | |
Svetoslav Ganov | 149567f | 2013-01-08 15:23:34 -0800 | [diff] [blame] | 4159 | if (clearChildFocus) { |
| 4160 | clearChildFocus(focused); |
| 4161 | if (!rootViewRequestFocus()) { |
| 4162 | notifyGlobalFocusCleared(focused); |
| 4163 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4164 | } |
| 4165 | } |
| 4166 | |
| 4167 | /** |
| 4168 | * Call this method to remove all child views from the |
| 4169 | * ViewGroup. |
Romain Guy | 393a52c | 2012-05-22 20:21:08 -0700 | [diff] [blame] | 4170 | * |
| 4171 | * <p><strong>Note:</strong> do not invoke this method from |
| 4172 | * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)}, |
| 4173 | * {@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] | 4174 | */ |
| 4175 | public void removeAllViews() { |
| 4176 | removeAllViewsInLayout(); |
| 4177 | requestLayout(); |
Romain Guy | 849d0a3 | 2011-02-01 17:20:48 -0800 | [diff] [blame] | 4178 | invalidate(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4179 | } |
| 4180 | |
| 4181 | /** |
| 4182 | * Called by a ViewGroup subclass to remove child views from itself, |
| 4183 | * when it must first know its size on screen before it can calculate how many |
| 4184 | * child views it will render. An example is a Gallery or a ListView, which |
| 4185 | * may "have" 50 children, but actually only render the number of children |
| 4186 | * that can currently fit inside the object on screen. Do not call |
| 4187 | * this method unless you are extending ViewGroup and understand the |
| 4188 | * view measuring and layout pipeline. |
Romain Guy | 393a52c | 2012-05-22 20:21:08 -0700 | [diff] [blame] | 4189 | * |
| 4190 | * <p><strong>Note:</strong> do not invoke this method from |
| 4191 | * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)}, |
| 4192 | * {@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] | 4193 | */ |
| 4194 | public void removeAllViewsInLayout() { |
| 4195 | final int count = mChildrenCount; |
| 4196 | if (count <= 0) { |
| 4197 | return; |
| 4198 | } |
| 4199 | |
| 4200 | final View[] children = mChildren; |
| 4201 | mChildrenCount = 0; |
| 4202 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4203 | final View focused = mFocused; |
| 4204 | final boolean detach = mAttachInfo != null; |
Svetoslav Ganov | 149567f | 2013-01-08 15:23:34 -0800 | [diff] [blame] | 4205 | boolean clearChildFocus = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4206 | |
| 4207 | needGlobalAttributesUpdate(false); |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 4208 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4209 | for (int i = count - 1; i >= 0; i--) { |
| 4210 | final View view = children[i]; |
| 4211 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4212 | if (mTransition != null) { |
Chet Haase | 5e25c2c | 2010-09-16 11:15:56 -0700 | [diff] [blame] | 4213 | mTransition.removeChild(this, view); |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4214 | } |
| 4215 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4216 | if (view == focused) { |
Alan Viverette | 223622a | 2013-12-17 13:29:02 -0800 | [diff] [blame] | 4217 | view.unFocus(null); |
Svetoslav Ganov | 149567f | 2013-01-08 15:23:34 -0800 | [diff] [blame] | 4218 | clearChildFocus = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4219 | } |
| 4220 | |
Svetoslav Ganov | 149567f | 2013-01-08 15:23:34 -0800 | [diff] [blame] | 4221 | if (view.isAccessibilityFocused()) { |
| 4222 | view.clearAccessibilityFocus(); |
| 4223 | } |
Svetoslav Ganov | 961bf0e | 2012-05-08 09:40:03 -0700 | [diff] [blame] | 4224 | |
Jeff Brown | 59a422e | 2012-04-19 15:19:19 -0700 | [diff] [blame] | 4225 | cancelTouchTarget(view); |
| 4226 | cancelHoverTarget(view); |
| 4227 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4228 | if (view.getAnimation() != null || |
| 4229 | (mTransitioningViews != null && mTransitioningViews.contains(view))) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4230 | addDisappearingView(view); |
| 4231 | } else if (detach) { |
| 4232 | view.dispatchDetachedFromWindow(); |
| 4233 | } |
| 4234 | |
Adam Powell | 539ee87 | 2012-02-03 19:00:49 -0800 | [diff] [blame] | 4235 | if (view.hasTransientState()) { |
| 4236 | childHasTransientStateChanged(view, false); |
| 4237 | } |
| 4238 | |
Philip Milne | f51d91c | 2011-07-18 16:12:19 -0700 | [diff] [blame] | 4239 | onViewRemoved(view); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4240 | |
| 4241 | view.mParent = null; |
| 4242 | children[i] = null; |
| 4243 | } |
| 4244 | |
Svetoslav Ganov | 149567f | 2013-01-08 15:23:34 -0800 | [diff] [blame] | 4245 | if (clearChildFocus) { |
| 4246 | clearChildFocus(focused); |
| 4247 | if (!rootViewRequestFocus()) { |
| 4248 | notifyGlobalFocusCleared(focused); |
| 4249 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4250 | } |
| 4251 | } |
| 4252 | |
| 4253 | /** |
| 4254 | * Finishes the removal of a detached view. This method will dispatch the detached from |
| 4255 | * window event and notify the hierarchy change listener. |
Chet Haase | ca479d4 | 2012-08-30 17:20:08 -0700 | [diff] [blame] | 4256 | * <p> |
| 4257 | * This method is intended to be lightweight and makes no assumptions about whether the |
| 4258 | * parent or child should be redrawn. Proper use of this method will include also making |
| 4259 | * any appropriate {@link #requestLayout()} or {@link #invalidate()} calls. |
| 4260 | * For example, callers can {@link #post(Runnable) post} a {@link Runnable} |
| 4261 | * which performs a {@link #requestLayout()} on the next frame, after all detach/remove |
| 4262 | * 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] | 4263 | * |
| 4264 | * @param child the child to be definitely removed from the view hierarchy |
| 4265 | * @param animate if true and the view has an animation, the view is placed in the |
| 4266 | * disappearing views list, otherwise, it is detached from the window |
| 4267 | * |
| 4268 | * @see #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams) |
| 4269 | * @see #detachAllViewsFromParent() |
| 4270 | * @see #detachViewFromParent(View) |
| 4271 | * @see #detachViewFromParent(int) |
| 4272 | */ |
| 4273 | protected void removeDetachedView(View child, boolean animate) { |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4274 | if (mTransition != null) { |
Chet Haase | 5e25c2c | 2010-09-16 11:15:56 -0700 | [diff] [blame] | 4275 | mTransition.removeChild(this, child); |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4276 | } |
| 4277 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4278 | if (child == mFocused) { |
| 4279 | child.clearFocus(); |
| 4280 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 4281 | |
Svetoslav Ganov | 961bf0e | 2012-05-08 09:40:03 -0700 | [diff] [blame] | 4282 | child.clearAccessibilityFocus(); |
| 4283 | |
Jeff Brown | 59a422e | 2012-04-19 15:19:19 -0700 | [diff] [blame] | 4284 | cancelTouchTarget(child); |
| 4285 | cancelHoverTarget(child); |
| 4286 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4287 | if ((animate && child.getAnimation() != null) || |
| 4288 | (mTransitioningViews != null && mTransitioningViews.contains(child))) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4289 | addDisappearingView(child); |
| 4290 | } else if (child.mAttachInfo != null) { |
| 4291 | child.dispatchDetachedFromWindow(); |
| 4292 | } |
| 4293 | |
Adam Powell | 539ee87 | 2012-02-03 19:00:49 -0800 | [diff] [blame] | 4294 | if (child.hasTransientState()) { |
| 4295 | childHasTransientStateChanged(child, false); |
| 4296 | } |
| 4297 | |
Philip Milne | f51d91c | 2011-07-18 16:12:19 -0700 | [diff] [blame] | 4298 | onViewRemoved(child); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4299 | } |
| 4300 | |
| 4301 | /** |
| 4302 | * 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] | 4303 | * sets the layout parameters and puts the view in the list of children so that |
| 4304 | * it can be retrieved by calling {@link #getChildAt(int)}. |
| 4305 | * <p> |
| 4306 | * This method is intended to be lightweight and makes no assumptions about whether the |
| 4307 | * parent or child should be redrawn. Proper use of this method will include also making |
| 4308 | * any appropriate {@link #requestLayout()} or {@link #invalidate()} calls. |
| 4309 | * For example, callers can {@link #post(Runnable) post} a {@link Runnable} |
| 4310 | * which performs a {@link #requestLayout()} on the next frame, after all detach/attach |
| 4311 | * calls are finished, causing layout to be run prior to redrawing the view hierarchy. |
| 4312 | * <p> |
| 4313 | * 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] | 4314 | * |
| 4315 | * @param child the child to attach |
| 4316 | * @param index the index at which the child should be attached |
| 4317 | * @param params the layout parameters of the child |
| 4318 | * |
| 4319 | * @see #removeDetachedView(View, boolean) |
| 4320 | * @see #detachAllViewsFromParent() |
| 4321 | * @see #detachViewFromParent(View) |
| 4322 | * @see #detachViewFromParent(int) |
| 4323 | */ |
| 4324 | protected void attachViewToParent(View child, int index, LayoutParams params) { |
| 4325 | child.mLayoutParams = params; |
| 4326 | |
| 4327 | if (index < 0) { |
| 4328 | index = mChildrenCount; |
| 4329 | } |
| 4330 | |
| 4331 | addInArray(child, index); |
| 4332 | |
| 4333 | child.mParent = this; |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 4334 | child.mPrivateFlags = (child.mPrivateFlags & ~PFLAG_DIRTY_MASK |
| 4335 | & ~PFLAG_DRAWING_CACHE_VALID) |
| 4336 | | PFLAG_DRAWN | PFLAG_INVALIDATED; |
| 4337 | this.mPrivateFlags |= PFLAG_INVALIDATED; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4338 | |
| 4339 | if (child.hasFocus()) { |
| 4340 | requestChildFocus(child, child.findFocus()); |
| 4341 | } |
| 4342 | } |
| 4343 | |
| 4344 | /** |
Chet Haase | ca479d4 | 2012-08-30 17:20:08 -0700 | [diff] [blame] | 4345 | * Detaches a view from its parent. Detaching a view should be followed |
| 4346 | * either by a call to |
| 4347 | * {@link #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)} |
| 4348 | * or a call to {@link #removeDetachedView(View, boolean)}. Detachment should only be |
| 4349 | * temporary; reattachment or removal should happen within the same drawing cycle as |
| 4350 | * detachment. When a view is detached, its parent is null and cannot be retrieved by a |
| 4351 | * call to {@link #getChildAt(int)}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4352 | * |
| 4353 | * @param child the child to detach |
| 4354 | * |
| 4355 | * @see #detachViewFromParent(int) |
| 4356 | * @see #detachViewsFromParent(int, int) |
| 4357 | * @see #detachAllViewsFromParent() |
| 4358 | * @see #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams) |
| 4359 | * @see #removeDetachedView(View, boolean) |
| 4360 | */ |
| 4361 | protected void detachViewFromParent(View child) { |
| 4362 | removeFromArray(indexOfChild(child)); |
| 4363 | } |
| 4364 | |
| 4365 | /** |
Chet Haase | ca479d4 | 2012-08-30 17:20:08 -0700 | [diff] [blame] | 4366 | * Detaches a view from its parent. Detaching a view should be followed |
| 4367 | * either by a call to |
| 4368 | * {@link #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)} |
| 4369 | * or a call to {@link #removeDetachedView(View, boolean)}. Detachment should only be |
| 4370 | * temporary; reattachment or removal should happen within the same drawing cycle as |
| 4371 | * detachment. When a view is detached, its parent is null and cannot be retrieved by a |
| 4372 | * call to {@link #getChildAt(int)}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4373 | * |
| 4374 | * @param index the index of the child to detach |
| 4375 | * |
| 4376 | * @see #detachViewFromParent(View) |
| 4377 | * @see #detachAllViewsFromParent() |
| 4378 | * @see #detachViewsFromParent(int, int) |
| 4379 | * @see #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams) |
| 4380 | * @see #removeDetachedView(View, boolean) |
| 4381 | */ |
| 4382 | protected void detachViewFromParent(int index) { |
| 4383 | removeFromArray(index); |
| 4384 | } |
| 4385 | |
| 4386 | /** |
Chet Haase | ca479d4 | 2012-08-30 17:20:08 -0700 | [diff] [blame] | 4387 | * Detaches a range of views from their parents. Detaching a view should be followed |
| 4388 | * either by a call to |
| 4389 | * {@link #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)} |
| 4390 | * or a call to {@link #removeDetachedView(View, boolean)}. Detachment should only be |
| 4391 | * temporary; reattachment or removal should happen within the same drawing cycle as |
| 4392 | * detachment. When a view is detached, its parent is null and cannot be retrieved by a |
| 4393 | * call to {@link #getChildAt(int)}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4394 | * |
| 4395 | * @param start the first index of the childrend range to detach |
| 4396 | * @param count the number of children to detach |
| 4397 | * |
| 4398 | * @see #detachViewFromParent(View) |
| 4399 | * @see #detachViewFromParent(int) |
| 4400 | * @see #detachAllViewsFromParent() |
| 4401 | * @see #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams) |
| 4402 | * @see #removeDetachedView(View, boolean) |
| 4403 | */ |
| 4404 | protected void detachViewsFromParent(int start, int count) { |
| 4405 | removeFromArray(start, count); |
| 4406 | } |
| 4407 | |
| 4408 | /** |
Chet Haase | ca479d4 | 2012-08-30 17:20:08 -0700 | [diff] [blame] | 4409 | * Detaches all views from the parent. Detaching a view should be followed |
| 4410 | * either by a call to |
| 4411 | * {@link #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)} |
| 4412 | * or a call to {@link #removeDetachedView(View, boolean)}. Detachment should only be |
| 4413 | * temporary; reattachment or removal should happen within the same drawing cycle as |
| 4414 | * detachment. When a view is detached, its parent is null and cannot be retrieved by a |
| 4415 | * call to {@link #getChildAt(int)}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4416 | * |
| 4417 | * @see #detachViewFromParent(View) |
| 4418 | * @see #detachViewFromParent(int) |
| 4419 | * @see #detachViewsFromParent(int, int) |
| 4420 | * @see #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams) |
| 4421 | * @see #removeDetachedView(View, boolean) |
| 4422 | */ |
| 4423 | protected void detachAllViewsFromParent() { |
| 4424 | final int count = mChildrenCount; |
| 4425 | if (count <= 0) { |
| 4426 | return; |
| 4427 | } |
| 4428 | |
| 4429 | final View[] children = mChildren; |
| 4430 | mChildrenCount = 0; |
| 4431 | |
| 4432 | for (int i = count - 1; i >= 0; i--) { |
| 4433 | children[i].mParent = null; |
| 4434 | children[i] = null; |
| 4435 | } |
| 4436 | } |
| 4437 | |
| 4438 | /** |
| 4439 | * Don't call or override this method. It is used for the implementation of |
| 4440 | * the view hierarchy. |
| 4441 | */ |
| 4442 | public final void invalidateChild(View child, final Rect dirty) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4443 | ViewParent parent = this; |
| 4444 | |
| 4445 | final AttachInfo attachInfo = mAttachInfo; |
| 4446 | if (attachInfo != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4447 | // If the child is drawing an animation, we want to copy this flag onto |
| 4448 | // ourselves and the parent to make sure the invalidate request goes |
| 4449 | // through |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 4450 | final boolean drawAnimation = (child.mPrivateFlags & PFLAG_DRAW_ANIMATION) |
| 4451 | == PFLAG_DRAW_ANIMATION; |
Romain Guy | 24443ea | 2009-05-11 11:56:30 -0700 | [diff] [blame] | 4452 | |
Romain Guy | fe455af | 2012-02-15 16:40:20 -0800 | [diff] [blame] | 4453 | // Check whether the child that requests the invalidate is fully opaque |
| 4454 | // Views being animated or transformed are not considered opaque because we may |
| 4455 | // be invalidating their old position and need the parent to paint behind them. |
| 4456 | Matrix childMatrix = child.getMatrix(); |
| 4457 | final boolean isOpaque = child.isOpaque() && !drawAnimation && |
| 4458 | child.getAnimation() == null && childMatrix.isIdentity(); |
| 4459 | // Mark the child as dirty, using the appropriate flag |
| 4460 | // Make sure we do not set both flags at the same time |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 4461 | int opaqueFlag = isOpaque ? PFLAG_DIRTY_OPAQUE : PFLAG_DIRTY; |
Romain Guy | fe455af | 2012-02-15 16:40:20 -0800 | [diff] [blame] | 4462 | |
John Reck | 96bb8ad | 2014-06-19 10:53:03 -0700 | [diff] [blame] | 4463 | if (child.mLayerType != LAYER_TYPE_NONE) { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 4464 | mPrivateFlags |= PFLAG_INVALIDATED; |
| 4465 | mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID; |
Romain Guy | fe455af | 2012-02-15 16:40:20 -0800 | [diff] [blame] | 4466 | } |
| 4467 | |
| 4468 | final int[] location = attachInfo.mInvalidateChildLocation; |
| 4469 | location[CHILD_LEFT_INDEX] = child.mLeft; |
| 4470 | location[CHILD_TOP_INDEX] = child.mTop; |
Chet Haase | 599913d | 2012-07-23 16:22:05 -0700 | [diff] [blame] | 4471 | if (!childMatrix.isIdentity() || |
| 4472 | (mGroupFlags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) { |
Romain Guy | fe455af | 2012-02-15 16:40:20 -0800 | [diff] [blame] | 4473 | RectF boundingRect = attachInfo.mTmpTransformRect; |
| 4474 | boundingRect.set(dirty); |
Chet Haase | 599913d | 2012-07-23 16:22:05 -0700 | [diff] [blame] | 4475 | Matrix transformMatrix; |
| 4476 | if ((mGroupFlags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) { |
| 4477 | Transformation t = attachInfo.mTmpTransformation; |
| 4478 | boolean transformed = getChildStaticTransformation(child, t); |
| 4479 | if (transformed) { |
| 4480 | transformMatrix = attachInfo.mTmpMatrix; |
| 4481 | transformMatrix.set(t.getMatrix()); |
| 4482 | if (!childMatrix.isIdentity()) { |
| 4483 | transformMatrix.preConcat(childMatrix); |
| 4484 | } |
| 4485 | } else { |
| 4486 | transformMatrix = childMatrix; |
| 4487 | } |
| 4488 | } else { |
| 4489 | transformMatrix = childMatrix; |
| 4490 | } |
| 4491 | transformMatrix.mapRect(boundingRect); |
Romain Guy | fe455af | 2012-02-15 16:40:20 -0800 | [diff] [blame] | 4492 | dirty.set((int) (boundingRect.left - 0.5f), |
| 4493 | (int) (boundingRect.top - 0.5f), |
| 4494 | (int) (boundingRect.right + 0.5f), |
| 4495 | (int) (boundingRect.bottom + 0.5f)); |
| 4496 | } |
| 4497 | |
| 4498 | do { |
| 4499 | View view = null; |
| 4500 | if (parent instanceof View) { |
| 4501 | view = (View) parent; |
Romain Guy | fe455af | 2012-02-15 16:40:20 -0800 | [diff] [blame] | 4502 | } |
| 4503 | |
| 4504 | if (drawAnimation) { |
| 4505 | if (view != null) { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 4506 | view.mPrivateFlags |= PFLAG_DRAW_ANIMATION; |
Romain Guy | fe455af | 2012-02-15 16:40:20 -0800 | [diff] [blame] | 4507 | } else if (parent instanceof ViewRootImpl) { |
| 4508 | ((ViewRootImpl) parent).mIsAnimating = true; |
| 4509 | } |
| 4510 | } |
| 4511 | |
| 4512 | // If the parent is dirty opaque or not dirty, mark it dirty with the opaque |
| 4513 | // flag coming from the child that initiated the invalidate |
| 4514 | if (view != null) { |
| 4515 | if ((view.mViewFlags & FADING_EDGE_MASK) != 0 && |
| 4516 | view.getSolidColor() == 0) { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 4517 | opaqueFlag = PFLAG_DIRTY; |
Romain Guy | fe455af | 2012-02-15 16:40:20 -0800 | [diff] [blame] | 4518 | } |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 4519 | if ((view.mPrivateFlags & PFLAG_DIRTY_MASK) != PFLAG_DIRTY) { |
| 4520 | view.mPrivateFlags = (view.mPrivateFlags & ~PFLAG_DIRTY_MASK) | opaqueFlag; |
Romain Guy | fe455af | 2012-02-15 16:40:20 -0800 | [diff] [blame] | 4521 | } |
| 4522 | } |
| 4523 | |
| 4524 | parent = parent.invalidateChildInParent(location, dirty); |
| 4525 | if (view != null) { |
| 4526 | // Account for transform on current parent |
| 4527 | Matrix m = view.getMatrix(); |
| 4528 | if (!m.isIdentity()) { |
| 4529 | RectF boundingRect = attachInfo.mTmpTransformRect; |
| 4530 | boundingRect.set(dirty); |
| 4531 | m.mapRect(boundingRect); |
Romain Guy | e8585b1 | 2012-02-17 18:28:47 -0800 | [diff] [blame] | 4532 | dirty.set((int) (boundingRect.left - 0.5f), |
| 4533 | (int) (boundingRect.top - 0.5f), |
Romain Guy | fe455af | 2012-02-15 16:40:20 -0800 | [diff] [blame] | 4534 | (int) (boundingRect.right + 0.5f), |
| 4535 | (int) (boundingRect.bottom + 0.5f)); |
| 4536 | } |
| 4537 | } |
| 4538 | } while (parent != null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4539 | } |
| 4540 | } |
| 4541 | |
| 4542 | /** |
| 4543 | * Don't call or override this method. It is used for the implementation of |
| 4544 | * the view hierarchy. |
| 4545 | * |
| 4546 | * This implementation returns null if this ViewGroup does not have a parent, |
| 4547 | * if this ViewGroup is already fully invalidated or if the dirty rectangle |
| 4548 | * does not intersect with this ViewGroup's bounds. |
| 4549 | */ |
| 4550 | public ViewParent invalidateChildInParent(final int[] location, final Rect dirty) { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 4551 | if ((mPrivateFlags & PFLAG_DRAWN) == PFLAG_DRAWN || |
| 4552 | (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == PFLAG_DRAWING_CACHE_VALID) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4553 | if ((mGroupFlags & (FLAG_OPTIMIZE_INVALIDATE | FLAG_ANIMATION_DONE)) != |
| 4554 | FLAG_OPTIMIZE_INVALIDATE) { |
| 4555 | dirty.offset(location[CHILD_LEFT_INDEX] - mScrollX, |
| 4556 | location[CHILD_TOP_INDEX] - mScrollY); |
Chet Haase | a4f14eb | 2013-04-22 11:11:39 -0700 | [diff] [blame] | 4557 | if ((mGroupFlags & FLAG_CLIP_CHILDREN) == 0) { |
| 4558 | dirty.union(0, 0, mRight - mLeft, mBottom - mTop); |
| 4559 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4560 | |
| 4561 | final int left = mLeft; |
| 4562 | final int top = mTop; |
| 4563 | |
Chet Haase | 05e91ed | 2012-07-03 14:17:57 -0700 | [diff] [blame] | 4564 | if ((mGroupFlags & FLAG_CLIP_CHILDREN) == FLAG_CLIP_CHILDREN) { |
| 4565 | if (!dirty.intersect(0, 0, mRight - left, mBottom - top)) { |
| 4566 | dirty.setEmpty(); |
Romain Guy | 3a3133d | 2011-02-01 22:59:58 -0800 | [diff] [blame] | 4567 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4568 | } |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 4569 | mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID; |
Chet Haase | 05e91ed | 2012-07-03 14:17:57 -0700 | [diff] [blame] | 4570 | |
| 4571 | location[CHILD_LEFT_INDEX] = left; |
| 4572 | location[CHILD_TOP_INDEX] = top; |
| 4573 | |
John Reck | 96bb8ad | 2014-06-19 10:53:03 -0700 | [diff] [blame] | 4574 | if (mLayerType != LAYER_TYPE_NONE) { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 4575 | mPrivateFlags |= PFLAG_INVALIDATED; |
Chet Haase | 05e91ed | 2012-07-03 14:17:57 -0700 | [diff] [blame] | 4576 | } |
| 4577 | |
| 4578 | return mParent; |
| 4579 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4580 | } else { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 4581 | mPrivateFlags &= ~PFLAG_DRAWN & ~PFLAG_DRAWING_CACHE_VALID; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4582 | |
| 4583 | location[CHILD_LEFT_INDEX] = mLeft; |
| 4584 | location[CHILD_TOP_INDEX] = mTop; |
Chet Haase | a3db866 | 2011-07-19 10:36:05 -0700 | [diff] [blame] | 4585 | if ((mGroupFlags & FLAG_CLIP_CHILDREN) == FLAG_CLIP_CHILDREN) { |
| 4586 | dirty.set(0, 0, mRight - mLeft, mBottom - mTop); |
| 4587 | } else { |
| 4588 | // in case the dirty rect extends outside the bounds of this container |
| 4589 | dirty.union(0, 0, mRight - mLeft, mBottom - mTop); |
| 4590 | } |
Romain Guy | 3a3133d | 2011-02-01 22:59:58 -0800 | [diff] [blame] | 4591 | |
John Reck | 96bb8ad | 2014-06-19 10:53:03 -0700 | [diff] [blame] | 4592 | if (mLayerType != LAYER_TYPE_NONE) { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 4593 | mPrivateFlags |= PFLAG_INVALIDATED; |
Romain Guy | 3a3133d | 2011-02-01 22:59:58 -0800 | [diff] [blame] | 4594 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4595 | |
| 4596 | return mParent; |
| 4597 | } |
| 4598 | } |
| 4599 | |
| 4600 | return null; |
| 4601 | } |
| 4602 | |
| 4603 | /** |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 4604 | * Native-calculated damage path |
| 4605 | * Returns false if this path was unable to complete successfully. This means |
| 4606 | * it hit a ViewParent it doesn't recognize and needs to fall back to calculating |
| 4607 | * damage area |
| 4608 | * @hide |
| 4609 | */ |
| 4610 | public boolean damageChildDeferred(View child) { |
| 4611 | ViewParent parent = getParent(); |
| 4612 | while (parent != null) { |
| 4613 | if (parent instanceof ViewGroup) { |
| 4614 | parent = parent.getParent(); |
| 4615 | } else if (parent instanceof ViewRootImpl) { |
| 4616 | ((ViewRootImpl) parent).invalidate(); |
| 4617 | return true; |
| 4618 | } else { |
| 4619 | parent = null; |
| 4620 | } |
| 4621 | } |
| 4622 | return false; |
| 4623 | } |
| 4624 | |
| 4625 | /** |
Chet Haase | 9d1992d | 2012-03-13 11:03:25 -0700 | [diff] [blame] | 4626 | * Quick invalidation method called by View.invalidateViewProperty. This doesn't set the |
| 4627 | * DRAWN flags and doesn't handle the Animation logic that the default invalidation methods |
| 4628 | * do; all we want to do here is schedule a traversal with the appropriate dirty rect. |
| 4629 | * |
| 4630 | * @hide |
| 4631 | */ |
Chris Craik | 49e6c73 | 2014-03-31 12:34:11 -0700 | [diff] [blame] | 4632 | public void damageChild(View child, final Rect dirty) { |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 4633 | if (damageChildDeferred(child)) { |
| 4634 | return; |
| 4635 | } |
| 4636 | |
Chet Haase | 9d1992d | 2012-03-13 11:03:25 -0700 | [diff] [blame] | 4637 | ViewParent parent = this; |
| 4638 | |
| 4639 | final AttachInfo attachInfo = mAttachInfo; |
| 4640 | if (attachInfo != null) { |
Chet Haase | 9d1992d | 2012-03-13 11:03:25 -0700 | [diff] [blame] | 4641 | int left = child.mLeft; |
| 4642 | int top = child.mTop; |
| 4643 | if (!child.getMatrix().isIdentity()) { |
| 4644 | child.transformRect(dirty); |
| 4645 | } |
| 4646 | |
| 4647 | do { |
| 4648 | if (parent instanceof ViewGroup) { |
| 4649 | ViewGroup parentVG = (ViewGroup) parent; |
Chet Haase | b85967b | 2012-03-26 14:37:51 -0700 | [diff] [blame] | 4650 | if (parentVG.mLayerType != LAYER_TYPE_NONE) { |
| 4651 | // Layered parents should be recreated, not just re-issued |
| 4652 | parentVG.invalidate(); |
| 4653 | parent = null; |
| 4654 | } else { |
Chris Craik | 49e6c73 | 2014-03-31 12:34:11 -0700 | [diff] [blame] | 4655 | parent = parentVG.damageChildInParent(left, top, dirty); |
Chet Haase | b85967b | 2012-03-26 14:37:51 -0700 | [diff] [blame] | 4656 | left = parentVG.mLeft; |
| 4657 | top = parentVG.mTop; |
| 4658 | } |
Chet Haase | 9d1992d | 2012-03-13 11:03:25 -0700 | [diff] [blame] | 4659 | } else { |
| 4660 | // Reached the top; this calls into the usual invalidate method in |
| 4661 | // ViewRootImpl, which schedules a traversal |
| 4662 | final int[] location = attachInfo.mInvalidateChildLocation; |
| 4663 | location[0] = left; |
| 4664 | location[1] = top; |
| 4665 | parent = parent.invalidateChildInParent(location, dirty); |
| 4666 | } |
| 4667 | } while (parent != null); |
| 4668 | } |
| 4669 | } |
| 4670 | |
| 4671 | /** |
| 4672 | * Quick invalidation method that simply transforms the dirty rect into the parent's |
| 4673 | * coordinate system, pruning the invalidation if the parent has already been invalidated. |
Chet Haase | e4a2d7c | 2013-06-21 17:49:36 -0700 | [diff] [blame] | 4674 | * |
| 4675 | * @hide |
Chet Haase | 9d1992d | 2012-03-13 11:03:25 -0700 | [diff] [blame] | 4676 | */ |
Chris Craik | 49e6c73 | 2014-03-31 12:34:11 -0700 | [diff] [blame] | 4677 | protected ViewParent damageChildInParent(int left, int top, final Rect dirty) { |
| 4678 | if ((mPrivateFlags & PFLAG_DRAWN) != 0 |
| 4679 | || (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) != 0) { |
Chet Haase | 9d1992d | 2012-03-13 11:03:25 -0700 | [diff] [blame] | 4680 | dirty.offset(left - mScrollX, top - mScrollY); |
Chet Haase | a4f14eb | 2013-04-22 11:11:39 -0700 | [diff] [blame] | 4681 | if ((mGroupFlags & FLAG_CLIP_CHILDREN) == 0) { |
| 4682 | dirty.union(0, 0, mRight - mLeft, mBottom - mTop); |
| 4683 | } |
Chet Haase | 9d1992d | 2012-03-13 11:03:25 -0700 | [diff] [blame] | 4684 | |
| 4685 | if ((mGroupFlags & FLAG_CLIP_CHILDREN) == 0 || |
| 4686 | dirty.intersect(0, 0, mRight - mLeft, mBottom - mTop)) { |
| 4687 | |
Chet Haase | 9d1992d | 2012-03-13 11:03:25 -0700 | [diff] [blame] | 4688 | if (!getMatrix().isIdentity()) { |
| 4689 | transformRect(dirty); |
| 4690 | } |
| 4691 | |
| 4692 | return mParent; |
| 4693 | } |
| 4694 | } |
| 4695 | |
| 4696 | return null; |
| 4697 | } |
| 4698 | |
| 4699 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4700 | * Offset a rectangle that is in a descendant's coordinate |
| 4701 | * space into our coordinate space. |
| 4702 | * @param descendant A descendant of this view |
| 4703 | * @param rect A rectangle defined in descendant's coordinate space. |
| 4704 | */ |
| 4705 | public final void offsetDescendantRectToMyCoords(View descendant, Rect rect) { |
| 4706 | offsetRectBetweenParentAndChild(descendant, rect, true, false); |
| 4707 | } |
| 4708 | |
| 4709 | /** |
| 4710 | * Offset a rectangle that is in our coordinate space into an ancestor's |
| 4711 | * coordinate space. |
| 4712 | * @param descendant A descendant of this view |
| 4713 | * @param rect A rectangle defined in descendant's coordinate space. |
| 4714 | */ |
| 4715 | public final void offsetRectIntoDescendantCoords(View descendant, Rect rect) { |
| 4716 | offsetRectBetweenParentAndChild(descendant, rect, false, false); |
| 4717 | } |
| 4718 | |
| 4719 | /** |
| 4720 | * Helper method that offsets a rect either from parent to descendant or |
| 4721 | * descendant to parent. |
| 4722 | */ |
| 4723 | void offsetRectBetweenParentAndChild(View descendant, Rect rect, |
| 4724 | boolean offsetFromChildToParent, boolean clipToBounds) { |
| 4725 | |
| 4726 | // already in the same coord system :) |
| 4727 | if (descendant == this) { |
| 4728 | return; |
| 4729 | } |
| 4730 | |
| 4731 | ViewParent theParent = descendant.mParent; |
| 4732 | |
| 4733 | // search and offset up to the parent |
| 4734 | while ((theParent != null) |
| 4735 | && (theParent instanceof View) |
| 4736 | && (theParent != this)) { |
| 4737 | |
| 4738 | if (offsetFromChildToParent) { |
| 4739 | rect.offset(descendant.mLeft - descendant.mScrollX, |
| 4740 | descendant.mTop - descendant.mScrollY); |
| 4741 | if (clipToBounds) { |
| 4742 | View p = (View) theParent; |
| 4743 | rect.intersect(0, 0, p.mRight - p.mLeft, p.mBottom - p.mTop); |
| 4744 | } |
| 4745 | } else { |
| 4746 | if (clipToBounds) { |
| 4747 | View p = (View) theParent; |
| 4748 | rect.intersect(0, 0, p.mRight - p.mLeft, p.mBottom - p.mTop); |
| 4749 | } |
| 4750 | rect.offset(descendant.mScrollX - descendant.mLeft, |
| 4751 | descendant.mScrollY - descendant.mTop); |
| 4752 | } |
| 4753 | |
| 4754 | descendant = (View) theParent; |
| 4755 | theParent = descendant.mParent; |
| 4756 | } |
| 4757 | |
| 4758 | // now that we are up to this view, need to offset one more time |
| 4759 | // to get into our coordinate space |
| 4760 | if (theParent == this) { |
| 4761 | if (offsetFromChildToParent) { |
| 4762 | rect.offset(descendant.mLeft - descendant.mScrollX, |
| 4763 | descendant.mTop - descendant.mScrollY); |
| 4764 | } else { |
| 4765 | rect.offset(descendant.mScrollX - descendant.mLeft, |
| 4766 | descendant.mScrollY - descendant.mTop); |
| 4767 | } |
| 4768 | } else { |
| 4769 | throw new IllegalArgumentException("parameter must be a descendant of this view"); |
| 4770 | } |
| 4771 | } |
| 4772 | |
| 4773 | /** |
| 4774 | * Offset the vertical location of all children of this view by the specified number of pixels. |
| 4775 | * |
| 4776 | * @param offset the number of pixels to offset |
| 4777 | * |
| 4778 | * @hide |
| 4779 | */ |
| 4780 | public void offsetChildrenTopAndBottom(int offset) { |
| 4781 | final int count = mChildrenCount; |
| 4782 | final View[] children = mChildren; |
Romain Guy | 5549cb5 | 2013-05-06 18:42:08 -0700 | [diff] [blame] | 4783 | boolean invalidate = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4784 | |
| 4785 | for (int i = 0; i < count; i++) { |
| 4786 | final View v = children[i]; |
| 4787 | v.mTop += offset; |
| 4788 | v.mBottom += offset; |
Chris Craik | 64a12e1 | 2014-03-28 18:12:12 -0700 | [diff] [blame] | 4789 | if (v.mRenderNode != null) { |
Romain Guy | 5549cb5 | 2013-05-06 18:42:08 -0700 | [diff] [blame] | 4790 | invalidate = true; |
Chris Craik | 64a12e1 | 2014-03-28 18:12:12 -0700 | [diff] [blame] | 4791 | v.mRenderNode.offsetTopAndBottom(offset); |
Chet Haase | a1cff50 | 2012-02-21 13:43:44 -0800 | [diff] [blame] | 4792 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4793 | } |
Romain Guy | 5549cb5 | 2013-05-06 18:42:08 -0700 | [diff] [blame] | 4794 | |
| 4795 | if (invalidate) { |
| 4796 | invalidateViewProperty(false, false); |
| 4797 | } |
Guang Zhu | 84e2509 | 2014-05-01 21:12:55 -0700 | [diff] [blame] | 4798 | notifySubtreeAccessibilityStateChangedIfNeeded(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4799 | } |
| 4800 | |
| 4801 | /** |
| 4802 | * {@inheritDoc} |
| 4803 | */ |
| 4804 | public boolean getChildVisibleRect(View child, Rect r, android.graphics.Point offset) { |
Adam Powell | f93bb6d | 2011-12-12 15:21:57 -0800 | [diff] [blame] | 4805 | // It doesn't make a whole lot of sense to call this on a view that isn't attached, |
| 4806 | // but for some simple tests it can be useful. If we don't have attach info this |
| 4807 | // will allocate memory. |
| 4808 | final RectF rect = mAttachInfo != null ? mAttachInfo.mTmpTransformRect : new RectF(); |
Gilles Debunne | cea4513 | 2011-11-24 02:19:27 +0100 | [diff] [blame] | 4809 | rect.set(r); |
| 4810 | |
| 4811 | if (!child.hasIdentityMatrix()) { |
| 4812 | child.getMatrix().mapRect(rect); |
| 4813 | } |
| 4814 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4815 | int dx = child.mLeft - mScrollX; |
| 4816 | int dy = child.mTop - mScrollY; |
Gilles Debunne | cea4513 | 2011-11-24 02:19:27 +0100 | [diff] [blame] | 4817 | |
| 4818 | rect.offset(dx, dy); |
| 4819 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4820 | if (offset != null) { |
Gilles Debunne | cea4513 | 2011-11-24 02:19:27 +0100 | [diff] [blame] | 4821 | if (!child.hasIdentityMatrix()) { |
Adam Powell | f93bb6d | 2011-12-12 15:21:57 -0800 | [diff] [blame] | 4822 | float[] position = mAttachInfo != null ? mAttachInfo.mTmpTransformLocation |
| 4823 | : new float[2]; |
Gilles Debunne | cea4513 | 2011-11-24 02:19:27 +0100 | [diff] [blame] | 4824 | position[0] = offset.x; |
| 4825 | position[1] = offset.y; |
| 4826 | child.getMatrix().mapPoints(position); |
| 4827 | offset.x = (int) (position[0] + 0.5f); |
| 4828 | offset.y = (int) (position[1] + 0.5f); |
| 4829 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4830 | offset.x += dx; |
| 4831 | offset.y += dy; |
| 4832 | } |
Gilles Debunne | cea4513 | 2011-11-24 02:19:27 +0100 | [diff] [blame] | 4833 | |
| 4834 | if (rect.intersect(0, 0, mRight - mLeft, mBottom - mTop)) { |
| 4835 | if (mParent == null) return true; |
| 4836 | r.set((int) (rect.left + 0.5f), (int) (rect.top + 0.5f), |
| 4837 | (int) (rect.right + 0.5f), (int) (rect.bottom + 0.5f)); |
| 4838 | return mParent.getChildVisibleRect(this, r, offset); |
| 4839 | } |
| 4840 | |
| 4841 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4842 | } |
| 4843 | |
| 4844 | /** |
| 4845 | * {@inheritDoc} |
| 4846 | */ |
| 4847 | @Override |
Chet Haase | 9c08744 | 2011-01-12 16:20:16 -0800 | [diff] [blame] | 4848 | public final void layout(int l, int t, int r, int b) { |
Chet Haase | 430742f | 2013-04-12 11:18:36 -0700 | [diff] [blame] | 4849 | if (!mSuppressLayout && (mTransition == null || !mTransition.isChangingLayout())) { |
Chet Haase | 7dd4a53 | 2012-04-16 13:35:09 -0700 | [diff] [blame] | 4850 | if (mTransition != null) { |
| 4851 | mTransition.layoutChange(this); |
| 4852 | } |
Chet Haase | 9c08744 | 2011-01-12 16:20:16 -0800 | [diff] [blame] | 4853 | super.layout(l, t, r, b); |
| 4854 | } else { |
| 4855 | // 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] | 4856 | mLayoutCalledWhileSuppressed = true; |
Chet Haase | 9c08744 | 2011-01-12 16:20:16 -0800 | [diff] [blame] | 4857 | } |
| 4858 | } |
| 4859 | |
| 4860 | /** |
| 4861 | * {@inheritDoc} |
| 4862 | */ |
| 4863 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4864 | protected abstract void onLayout(boolean changed, |
| 4865 | int l, int t, int r, int b); |
| 4866 | |
| 4867 | /** |
| 4868 | * Indicates whether the view group has the ability to animate its children |
| 4869 | * after the first layout. |
| 4870 | * |
| 4871 | * @return true if the children can be animated, false otherwise |
| 4872 | */ |
| 4873 | protected boolean canAnimate() { |
| 4874 | return mLayoutAnimationController != null; |
| 4875 | } |
| 4876 | |
| 4877 | /** |
| 4878 | * Runs the layout animation. Calling this method triggers a relayout of |
| 4879 | * this view group. |
| 4880 | */ |
| 4881 | public void startLayoutAnimation() { |
| 4882 | if (mLayoutAnimationController != null) { |
| 4883 | mGroupFlags |= FLAG_RUN_ANIMATION; |
| 4884 | requestLayout(); |
| 4885 | } |
| 4886 | } |
| 4887 | |
| 4888 | /** |
| 4889 | * Schedules the layout animation to be played after the next layout pass |
| 4890 | * of this view group. This can be used to restart the layout animation |
| 4891 | * when the content of the view group changes or when the activity is |
| 4892 | * paused and resumed. |
| 4893 | */ |
| 4894 | public void scheduleLayoutAnimation() { |
| 4895 | mGroupFlags |= FLAG_RUN_ANIMATION; |
| 4896 | } |
| 4897 | |
| 4898 | /** |
| 4899 | * Sets the layout animation controller used to animate the group's |
| 4900 | * children after the first layout. |
| 4901 | * |
| 4902 | * @param controller the animation controller |
| 4903 | */ |
| 4904 | public void setLayoutAnimation(LayoutAnimationController controller) { |
| 4905 | mLayoutAnimationController = controller; |
| 4906 | if (mLayoutAnimationController != null) { |
| 4907 | mGroupFlags |= FLAG_RUN_ANIMATION; |
| 4908 | } |
| 4909 | } |
| 4910 | |
| 4911 | /** |
| 4912 | * Returns the layout animation controller used to animate the group's |
| 4913 | * children. |
| 4914 | * |
| 4915 | * @return the current animation controller |
| 4916 | */ |
| 4917 | public LayoutAnimationController getLayoutAnimation() { |
| 4918 | return mLayoutAnimationController; |
| 4919 | } |
| 4920 | |
| 4921 | /** |
| 4922 | * Indicates whether the children's drawing cache is used during a layout |
| 4923 | * animation. By default, the drawing cache is enabled but this will prevent |
| 4924 | * nested layout animations from working. To nest animations, you must disable |
| 4925 | * the cache. |
| 4926 | * |
| 4927 | * @return true if the animation cache is enabled, false otherwise |
| 4928 | * |
| 4929 | * @see #setAnimationCacheEnabled(boolean) |
| 4930 | * @see View#setDrawingCacheEnabled(boolean) |
| 4931 | */ |
| 4932 | @ViewDebug.ExportedProperty |
| 4933 | public boolean isAnimationCacheEnabled() { |
| 4934 | return (mGroupFlags & FLAG_ANIMATION_CACHE) == FLAG_ANIMATION_CACHE; |
| 4935 | } |
| 4936 | |
| 4937 | /** |
| 4938 | * Enables or disables the children's drawing cache during a layout animation. |
| 4939 | * By default, the drawing cache is enabled but this will prevent nested |
| 4940 | * layout animations from working. To nest animations, you must disable the |
| 4941 | * cache. |
| 4942 | * |
| 4943 | * @param enabled true to enable the animation cache, false otherwise |
| 4944 | * |
| 4945 | * @see #isAnimationCacheEnabled() |
| 4946 | * @see View#setDrawingCacheEnabled(boolean) |
| 4947 | */ |
| 4948 | public void setAnimationCacheEnabled(boolean enabled) { |
| 4949 | setBooleanFlag(FLAG_ANIMATION_CACHE, enabled); |
| 4950 | } |
| 4951 | |
| 4952 | /** |
| 4953 | * Indicates whether this ViewGroup will always try to draw its children using their |
| 4954 | * drawing cache. By default this property is enabled. |
| 4955 | * |
| 4956 | * @return true if the animation cache is enabled, false otherwise |
| 4957 | * |
| 4958 | * @see #setAlwaysDrawnWithCacheEnabled(boolean) |
| 4959 | * @see #setChildrenDrawnWithCacheEnabled(boolean) |
| 4960 | * @see View#setDrawingCacheEnabled(boolean) |
| 4961 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 4962 | @ViewDebug.ExportedProperty(category = "drawing") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4963 | public boolean isAlwaysDrawnWithCacheEnabled() { |
| 4964 | return (mGroupFlags & FLAG_ALWAYS_DRAWN_WITH_CACHE) == FLAG_ALWAYS_DRAWN_WITH_CACHE; |
| 4965 | } |
| 4966 | |
| 4967 | /** |
| 4968 | * Indicates whether this ViewGroup will always try to draw its children using their |
| 4969 | * drawing cache. This property can be set to true when the cache rendering is |
| 4970 | * slightly different from the children's normal rendering. Renderings can be different, |
| 4971 | * for instance, when the cache's quality is set to low. |
| 4972 | * |
| 4973 | * When this property is disabled, the ViewGroup will use the drawing cache of its |
| 4974 | * children only when asked to. It's usually the task of subclasses to tell ViewGroup |
| 4975 | * when to start using the drawing cache and when to stop using it. |
| 4976 | * |
| 4977 | * @param always true to always draw with the drawing cache, false otherwise |
| 4978 | * |
| 4979 | * @see #isAlwaysDrawnWithCacheEnabled() |
| 4980 | * @see #setChildrenDrawnWithCacheEnabled(boolean) |
| 4981 | * @see View#setDrawingCacheEnabled(boolean) |
| 4982 | * @see View#setDrawingCacheQuality(int) |
| 4983 | */ |
| 4984 | public void setAlwaysDrawnWithCacheEnabled(boolean always) { |
| 4985 | setBooleanFlag(FLAG_ALWAYS_DRAWN_WITH_CACHE, always); |
| 4986 | } |
| 4987 | |
| 4988 | /** |
| 4989 | * Indicates whether the ViewGroup is currently drawing its children using |
| 4990 | * their drawing cache. |
| 4991 | * |
| 4992 | * @return true if children should be drawn with their cache, false otherwise |
| 4993 | * |
| 4994 | * @see #setAlwaysDrawnWithCacheEnabled(boolean) |
| 4995 | * @see #setChildrenDrawnWithCacheEnabled(boolean) |
| 4996 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 4997 | @ViewDebug.ExportedProperty(category = "drawing") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4998 | protected boolean isChildrenDrawnWithCacheEnabled() { |
| 4999 | return (mGroupFlags & FLAG_CHILDREN_DRAWN_WITH_CACHE) == FLAG_CHILDREN_DRAWN_WITH_CACHE; |
| 5000 | } |
| 5001 | |
| 5002 | /** |
| 5003 | * Tells the ViewGroup to draw its children using their drawing cache. This property |
| 5004 | * is ignored when {@link #isAlwaysDrawnWithCacheEnabled()} is true. A child's drawing cache |
| 5005 | * will be used only if it has been enabled. |
| 5006 | * |
| 5007 | * Subclasses should call this method to start and stop using the drawing cache when |
| 5008 | * they perform performance sensitive operations, like scrolling or animating. |
| 5009 | * |
| 5010 | * @param enabled true if children should be drawn with their cache, false otherwise |
| 5011 | * |
| 5012 | * @see #setAlwaysDrawnWithCacheEnabled(boolean) |
| 5013 | * @see #isChildrenDrawnWithCacheEnabled() |
| 5014 | */ |
| 5015 | protected void setChildrenDrawnWithCacheEnabled(boolean enabled) { |
| 5016 | setBooleanFlag(FLAG_CHILDREN_DRAWN_WITH_CACHE, enabled); |
| 5017 | } |
| 5018 | |
Romain Guy | 293451e | 2009-11-04 13:59:48 -0800 | [diff] [blame] | 5019 | /** |
| 5020 | * Indicates whether the ViewGroup is drawing its children in the order defined by |
| 5021 | * {@link #getChildDrawingOrder(int, int)}. |
| 5022 | * |
| 5023 | * @return true if children drawing order is defined by {@link #getChildDrawingOrder(int, int)}, |
| 5024 | * false otherwise |
| 5025 | * |
| 5026 | * @see #setChildrenDrawingOrderEnabled(boolean) |
| 5027 | * @see #getChildDrawingOrder(int, int) |
| 5028 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 5029 | @ViewDebug.ExportedProperty(category = "drawing") |
Romain Guy | 293451e | 2009-11-04 13:59:48 -0800 | [diff] [blame] | 5030 | protected boolean isChildrenDrawingOrderEnabled() { |
| 5031 | return (mGroupFlags & FLAG_USE_CHILD_DRAWING_ORDER) == FLAG_USE_CHILD_DRAWING_ORDER; |
| 5032 | } |
| 5033 | |
| 5034 | /** |
| 5035 | * Tells the ViewGroup whether to draw its children in the order defined by the method |
| 5036 | * {@link #getChildDrawingOrder(int, int)}. |
| 5037 | * |
| 5038 | * @param enabled true if the order of the children when drawing is determined by |
| 5039 | * {@link #getChildDrawingOrder(int, int)}, false otherwise |
| 5040 | * |
| 5041 | * @see #isChildrenDrawingOrderEnabled() |
| 5042 | * @see #getChildDrawingOrder(int, int) |
| 5043 | */ |
| 5044 | protected void setChildrenDrawingOrderEnabled(boolean enabled) { |
| 5045 | setBooleanFlag(FLAG_USE_CHILD_DRAWING_ORDER, enabled); |
| 5046 | } |
| 5047 | |
Svetoslav | 6254f48 | 2013-06-04 17:22:14 -0700 | [diff] [blame] | 5048 | private boolean hasBooleanFlag(int flag) { |
Philip Milne | f091b66 | 2013-02-27 11:15:21 -0800 | [diff] [blame] | 5049 | return (mGroupFlags & flag) == flag; |
| 5050 | } |
| 5051 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5052 | private void setBooleanFlag(int flag, boolean value) { |
| 5053 | if (value) { |
| 5054 | mGroupFlags |= flag; |
| 5055 | } else { |
| 5056 | mGroupFlags &= ~flag; |
| 5057 | } |
| 5058 | } |
| 5059 | |
| 5060 | /** |
| 5061 | * Returns an integer indicating what types of drawing caches are kept in memory. |
| 5062 | * |
| 5063 | * @see #setPersistentDrawingCache(int) |
| 5064 | * @see #setAnimationCacheEnabled(boolean) |
| 5065 | * |
| 5066 | * @return one or a combination of {@link #PERSISTENT_NO_CACHE}, |
| 5067 | * {@link #PERSISTENT_ANIMATION_CACHE}, {@link #PERSISTENT_SCROLLING_CACHE} |
| 5068 | * and {@link #PERSISTENT_ALL_CACHES} |
| 5069 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 5070 | @ViewDebug.ExportedProperty(category = "drawing", mapping = { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5071 | @ViewDebug.IntToString(from = PERSISTENT_NO_CACHE, to = "NONE"), |
Romain Guy | 203688c | 2010-05-12 15:41:32 -0700 | [diff] [blame] | 5072 | @ViewDebug.IntToString(from = PERSISTENT_ANIMATION_CACHE, to = "ANIMATION"), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5073 | @ViewDebug.IntToString(from = PERSISTENT_SCROLLING_CACHE, to = "SCROLLING"), |
| 5074 | @ViewDebug.IntToString(from = PERSISTENT_ALL_CACHES, to = "ALL") |
| 5075 | }) |
| 5076 | public int getPersistentDrawingCache() { |
| 5077 | return mPersistentDrawingCache; |
| 5078 | } |
| 5079 | |
| 5080 | /** |
| 5081 | * Indicates what types of drawing caches should be kept in memory after |
| 5082 | * they have been created. |
| 5083 | * |
| 5084 | * @see #getPersistentDrawingCache() |
| 5085 | * @see #setAnimationCacheEnabled(boolean) |
| 5086 | * |
| 5087 | * @param drawingCacheToKeep one or a combination of {@link #PERSISTENT_NO_CACHE}, |
| 5088 | * {@link #PERSISTENT_ANIMATION_CACHE}, {@link #PERSISTENT_SCROLLING_CACHE} |
| 5089 | * and {@link #PERSISTENT_ALL_CACHES} |
| 5090 | */ |
| 5091 | public void setPersistentDrawingCache(int drawingCacheToKeep) { |
| 5092 | mPersistentDrawingCache = drawingCacheToKeep & PERSISTENT_ALL_CACHES; |
| 5093 | } |
| 5094 | |
Philip Milne | f091b66 | 2013-02-27 11:15:21 -0800 | [diff] [blame] | 5095 | private void setLayoutMode(int layoutMode, boolean explicitly) { |
| 5096 | mLayoutMode = layoutMode; |
| 5097 | setBooleanFlag(FLAG_LAYOUT_MODE_WAS_EXPLICITLY_SET, explicitly); |
| 5098 | } |
| 5099 | |
| 5100 | /** |
| 5101 | * Recursively traverse the view hierarchy, resetting the layoutMode of any |
| 5102 | * descendants that had inherited a different layoutMode from a previous parent. |
| 5103 | * Recursion terminates when a descendant's mode is: |
| 5104 | * <ul> |
| 5105 | * <li>Undefined</li> |
| 5106 | * <li>The same as the root node's</li> |
| 5107 | * <li>A mode that had been explicitly set</li> |
| 5108 | * <ul/> |
| 5109 | * The first two clauses are optimizations. |
| 5110 | * @param layoutModeOfRoot |
| 5111 | */ |
| 5112 | @Override |
| 5113 | void invalidateInheritedLayoutMode(int layoutModeOfRoot) { |
| 5114 | if (mLayoutMode == LAYOUT_MODE_UNDEFINED || |
| 5115 | mLayoutMode == layoutModeOfRoot || |
Svetoslav | 6254f48 | 2013-06-04 17:22:14 -0700 | [diff] [blame] | 5116 | hasBooleanFlag(FLAG_LAYOUT_MODE_WAS_EXPLICITLY_SET)) { |
Philip Milne | f091b66 | 2013-02-27 11:15:21 -0800 | [diff] [blame] | 5117 | return; |
| 5118 | } |
| 5119 | setLayoutMode(LAYOUT_MODE_UNDEFINED, false); |
| 5120 | |
| 5121 | // apply recursively |
| 5122 | for (int i = 0, N = getChildCount(); i < N; i++) { |
| 5123 | getChildAt(i).invalidateInheritedLayoutMode(layoutModeOfRoot); |
| 5124 | } |
| 5125 | } |
| 5126 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5127 | /** |
Philip Milne | cfb631b | 2012-10-26 10:51:46 -0700 | [diff] [blame] | 5128 | * Returns the basis of alignment during layout operations on this ViewGroup: |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 5129 | * either {@link #LAYOUT_MODE_CLIP_BOUNDS} or {@link #LAYOUT_MODE_OPTICAL_BOUNDS}. |
Philip Milne | cfb631b | 2012-10-26 10:51:46 -0700 | [diff] [blame] | 5130 | * <p> |
| 5131 | * If no layoutMode was explicitly set, either programmatically or in an XML resource, |
| 5132 | * the method returns the layoutMode of the view's parent ViewGroup if such a parent exists, |
| 5133 | * 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] | 5134 | * |
Philip Milne | fcc6a0f | 2012-04-16 16:12:19 -0700 | [diff] [blame] | 5135 | * @return the layout mode to use during layout operations |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 5136 | * |
| 5137 | * @see #setLayoutMode(int) |
| 5138 | */ |
| 5139 | public int getLayoutMode() { |
Philip Milne | cfb631b | 2012-10-26 10:51:46 -0700 | [diff] [blame] | 5140 | if (mLayoutMode == LAYOUT_MODE_UNDEFINED) { |
Philip Milne | f091b66 | 2013-02-27 11:15:21 -0800 | [diff] [blame] | 5141 | int inheritedLayoutMode = (mParent instanceof ViewGroup) ? |
| 5142 | ((ViewGroup) mParent).getLayoutMode() : LAYOUT_MODE_DEFAULT; |
| 5143 | setLayoutMode(inheritedLayoutMode, false); |
Philip Milne | cfb631b | 2012-10-26 10:51:46 -0700 | [diff] [blame] | 5144 | } |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 5145 | return mLayoutMode; |
| 5146 | } |
| 5147 | |
| 5148 | /** |
Philip Milne | cfb631b | 2012-10-26 10:51:46 -0700 | [diff] [blame] | 5149 | * Sets the basis of alignment during the layout of this ViewGroup. |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 5150 | * Valid values are either {@link #LAYOUT_MODE_CLIP_BOUNDS} or |
| 5151 | * {@link #LAYOUT_MODE_OPTICAL_BOUNDS}. |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 5152 | * |
Philip Milne | fcc6a0f | 2012-04-16 16:12:19 -0700 | [diff] [blame] | 5153 | * @param layoutMode the layout mode to use during layout operations |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 5154 | * |
| 5155 | * @see #getLayoutMode() |
Scott Main | 27a8508 | 2013-06-10 10:39:48 -0700 | [diff] [blame] | 5156 | * @attr ref android.R.styleable#ViewGroup_layoutMode |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 5157 | */ |
| 5158 | public void setLayoutMode(int layoutMode) { |
| 5159 | if (mLayoutMode != layoutMode) { |
Philip Milne | f091b66 | 2013-02-27 11:15:21 -0800 | [diff] [blame] | 5160 | invalidateInheritedLayoutMode(layoutMode); |
| 5161 | setLayoutMode(layoutMode, layoutMode != LAYOUT_MODE_UNDEFINED); |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 5162 | requestLayout(); |
| 5163 | } |
| 5164 | } |
| 5165 | |
| 5166 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5167 | * Returns a new set of layout parameters based on the supplied attributes set. |
| 5168 | * |
| 5169 | * @param attrs the attributes to build the layout parameters from |
| 5170 | * |
| 5171 | * @return an instance of {@link android.view.ViewGroup.LayoutParams} or one |
| 5172 | * of its descendants |
| 5173 | */ |
| 5174 | public LayoutParams generateLayoutParams(AttributeSet attrs) { |
| 5175 | return new LayoutParams(getContext(), attrs); |
| 5176 | } |
| 5177 | |
| 5178 | /** |
| 5179 | * Returns a safe set of layout parameters based on the supplied layout params. |
| 5180 | * When a ViewGroup is passed a View whose layout params do not pass the test of |
| 5181 | * {@link #checkLayoutParams(android.view.ViewGroup.LayoutParams)}, this method |
| 5182 | * is invoked. This method should return a new set of layout params suitable for |
| 5183 | * this ViewGroup, possibly by copying the appropriate attributes from the |
| 5184 | * specified set of layout params. |
| 5185 | * |
| 5186 | * @param p The layout parameters to convert into a suitable set of layout parameters |
| 5187 | * for this ViewGroup. |
| 5188 | * |
| 5189 | * @return an instance of {@link android.view.ViewGroup.LayoutParams} or one |
| 5190 | * of its descendants |
| 5191 | */ |
| 5192 | protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) { |
| 5193 | return p; |
| 5194 | } |
| 5195 | |
| 5196 | /** |
| 5197 | * Returns a set of default layout parameters. These parameters are requested |
| 5198 | * when the View passed to {@link #addView(View)} has no layout parameters |
| 5199 | * already set. If null is returned, an exception is thrown from addView. |
| 5200 | * |
| 5201 | * @return a set of default layout parameters or null |
| 5202 | */ |
| 5203 | protected LayoutParams generateDefaultLayoutParams() { |
| 5204 | return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); |
| 5205 | } |
| 5206 | |
| 5207 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5208 | * {@inheritDoc} |
| 5209 | */ |
| 5210 | @Override |
| 5211 | protected void debug(int depth) { |
| 5212 | super.debug(depth); |
| 5213 | String output; |
| 5214 | |
| 5215 | if (mFocused != null) { |
| 5216 | output = debugIndent(depth); |
| 5217 | output += "mFocused"; |
| 5218 | Log.d(VIEW_LOG_TAG, output); |
| 5219 | } |
| 5220 | if (mChildrenCount != 0) { |
| 5221 | output = debugIndent(depth); |
| 5222 | output += "{"; |
| 5223 | Log.d(VIEW_LOG_TAG, output); |
| 5224 | } |
| 5225 | int count = mChildrenCount; |
| 5226 | for (int i = 0; i < count; i++) { |
| 5227 | View child = mChildren[i]; |
| 5228 | child.debug(depth + 1); |
| 5229 | } |
| 5230 | |
| 5231 | if (mChildrenCount != 0) { |
| 5232 | output = debugIndent(depth); |
| 5233 | output += "}"; |
| 5234 | Log.d(VIEW_LOG_TAG, output); |
| 5235 | } |
| 5236 | } |
| 5237 | |
| 5238 | /** |
| 5239 | * Returns the position in the group of the specified child view. |
| 5240 | * |
| 5241 | * @param child the view for which to get the position |
| 5242 | * @return a positive integer representing the position of the view in the |
| 5243 | * group, or -1 if the view does not exist in the group |
| 5244 | */ |
| 5245 | public int indexOfChild(View child) { |
| 5246 | final int count = mChildrenCount; |
| 5247 | final View[] children = mChildren; |
| 5248 | for (int i = 0; i < count; i++) { |
| 5249 | if (children[i] == child) { |
| 5250 | return i; |
| 5251 | } |
| 5252 | } |
| 5253 | return -1; |
| 5254 | } |
| 5255 | |
| 5256 | /** |
| 5257 | * Returns the number of children in the group. |
| 5258 | * |
| 5259 | * @return a positive integer representing the number of children in |
| 5260 | * the group |
| 5261 | */ |
| 5262 | public int getChildCount() { |
| 5263 | return mChildrenCount; |
| 5264 | } |
| 5265 | |
| 5266 | /** |
| 5267 | * Returns the view at the specified position in the group. |
| 5268 | * |
| 5269 | * @param index the position at which to get the view from |
| 5270 | * @return the view at the specified position or null if the position |
| 5271 | * does not exist within the group |
| 5272 | */ |
| 5273 | public View getChildAt(int index) { |
Adam Powell | 3ba8f5d6 | 2011-03-07 15:36:33 -0800 | [diff] [blame] | 5274 | if (index < 0 || index >= mChildrenCount) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5275 | return null; |
| 5276 | } |
Adam Powell | 3ba8f5d6 | 2011-03-07 15:36:33 -0800 | [diff] [blame] | 5277 | return mChildren[index]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5278 | } |
| 5279 | |
| 5280 | /** |
| 5281 | * Ask all of the children of this view to measure themselves, taking into |
| 5282 | * account both the MeasureSpec requirements for this view and its padding. |
| 5283 | * We skip children that are in the GONE state The heavy lifting is done in |
| 5284 | * getChildMeasureSpec. |
| 5285 | * |
| 5286 | * @param widthMeasureSpec The width requirements for this view |
| 5287 | * @param heightMeasureSpec The height requirements for this view |
| 5288 | */ |
| 5289 | protected void measureChildren(int widthMeasureSpec, int heightMeasureSpec) { |
| 5290 | final int size = mChildrenCount; |
| 5291 | final View[] children = mChildren; |
| 5292 | for (int i = 0; i < size; ++i) { |
| 5293 | final View child = children[i]; |
| 5294 | if ((child.mViewFlags & VISIBILITY_MASK) != GONE) { |
| 5295 | measureChild(child, widthMeasureSpec, heightMeasureSpec); |
| 5296 | } |
| 5297 | } |
| 5298 | } |
| 5299 | |
| 5300 | /** |
| 5301 | * Ask one of the children of this view to measure itself, taking into |
| 5302 | * account both the MeasureSpec requirements for this view and its padding. |
| 5303 | * The heavy lifting is done in getChildMeasureSpec. |
| 5304 | * |
| 5305 | * @param child The child to measure |
| 5306 | * @param parentWidthMeasureSpec The width requirements for this view |
| 5307 | * @param parentHeightMeasureSpec The height requirements for this view |
| 5308 | */ |
| 5309 | protected void measureChild(View child, int parentWidthMeasureSpec, |
| 5310 | int parentHeightMeasureSpec) { |
| 5311 | final LayoutParams lp = child.getLayoutParams(); |
| 5312 | |
| 5313 | final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec, |
| 5314 | mPaddingLeft + mPaddingRight, lp.width); |
| 5315 | final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec, |
| 5316 | mPaddingTop + mPaddingBottom, lp.height); |
| 5317 | |
| 5318 | child.measure(childWidthMeasureSpec, childHeightMeasureSpec); |
| 5319 | } |
| 5320 | |
| 5321 | /** |
| 5322 | * Ask one of the children of this view to measure itself, taking into |
| 5323 | * account both the MeasureSpec requirements for this view and its padding |
| 5324 | * and margins. The child must have MarginLayoutParams The heavy lifting is |
| 5325 | * done in getChildMeasureSpec. |
| 5326 | * |
| 5327 | * @param child The child to measure |
| 5328 | * @param parentWidthMeasureSpec The width requirements for this view |
| 5329 | * @param widthUsed Extra space that has been used up by the parent |
| 5330 | * horizontally (possibly by other children of the parent) |
| 5331 | * @param parentHeightMeasureSpec The height requirements for this view |
| 5332 | * @param heightUsed Extra space that has been used up by the parent |
| 5333 | * vertically (possibly by other children of the parent) |
| 5334 | */ |
| 5335 | protected void measureChildWithMargins(View child, |
| 5336 | int parentWidthMeasureSpec, int widthUsed, |
| 5337 | int parentHeightMeasureSpec, int heightUsed) { |
| 5338 | final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams(); |
| 5339 | |
| 5340 | final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec, |
| 5341 | mPaddingLeft + mPaddingRight + lp.leftMargin + lp.rightMargin |
| 5342 | + widthUsed, lp.width); |
| 5343 | final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec, |
| 5344 | mPaddingTop + mPaddingBottom + lp.topMargin + lp.bottomMargin |
| 5345 | + heightUsed, lp.height); |
| 5346 | |
| 5347 | child.measure(childWidthMeasureSpec, childHeightMeasureSpec); |
| 5348 | } |
| 5349 | |
| 5350 | /** |
| 5351 | * Does the hard part of measureChildren: figuring out the MeasureSpec to |
| 5352 | * pass to a particular child. This method figures out the right MeasureSpec |
| 5353 | * for one dimension (height or width) of one child view. |
| 5354 | * |
| 5355 | * The goal is to combine information from our MeasureSpec with the |
| 5356 | * LayoutParams of the child to get the best possible results. For example, |
| 5357 | * if the this view knows its size (because its MeasureSpec has a mode of |
| 5358 | * EXACTLY), and the child has indicated in its LayoutParams that it wants |
| 5359 | * to be the same size as the parent, the parent should ask the child to |
| 5360 | * layout given an exact size. |
| 5361 | * |
| 5362 | * @param spec The requirements for this view |
| 5363 | * @param padding The padding of this view for the current dimension and |
| 5364 | * margins, if applicable |
| 5365 | * @param childDimension How big the child wants to be in the current |
| 5366 | * dimension |
| 5367 | * @return a MeasureSpec integer for the child |
| 5368 | */ |
| 5369 | public static int getChildMeasureSpec(int spec, int padding, int childDimension) { |
| 5370 | int specMode = MeasureSpec.getMode(spec); |
| 5371 | int specSize = MeasureSpec.getSize(spec); |
| 5372 | |
| 5373 | int size = Math.max(0, specSize - padding); |
| 5374 | |
| 5375 | int resultSize = 0; |
| 5376 | int resultMode = 0; |
| 5377 | |
| 5378 | switch (specMode) { |
| 5379 | // Parent has imposed an exact size on us |
| 5380 | case MeasureSpec.EXACTLY: |
| 5381 | if (childDimension >= 0) { |
| 5382 | resultSize = childDimension; |
| 5383 | resultMode = MeasureSpec.EXACTLY; |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 5384 | } else if (childDimension == LayoutParams.MATCH_PARENT) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5385 | // Child wants to be our size. So be it. |
| 5386 | resultSize = size; |
| 5387 | resultMode = MeasureSpec.EXACTLY; |
| 5388 | } else if (childDimension == LayoutParams.WRAP_CONTENT) { |
| 5389 | // Child wants to determine its own size. It can't be |
| 5390 | // bigger than us. |
| 5391 | resultSize = size; |
| 5392 | resultMode = MeasureSpec.AT_MOST; |
| 5393 | } |
| 5394 | break; |
| 5395 | |
| 5396 | // Parent has imposed a maximum size on us |
| 5397 | case MeasureSpec.AT_MOST: |
| 5398 | if (childDimension >= 0) { |
| 5399 | // Child wants a specific size... so be it |
| 5400 | resultSize = childDimension; |
| 5401 | resultMode = MeasureSpec.EXACTLY; |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 5402 | } else if (childDimension == LayoutParams.MATCH_PARENT) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5403 | // Child wants to be our size, but our size is not fixed. |
| 5404 | // Constrain child to not be bigger than us. |
| 5405 | resultSize = size; |
| 5406 | resultMode = MeasureSpec.AT_MOST; |
| 5407 | } else if (childDimension == LayoutParams.WRAP_CONTENT) { |
| 5408 | // Child wants to determine its own size. It can't be |
| 5409 | // bigger than us. |
| 5410 | resultSize = size; |
| 5411 | resultMode = MeasureSpec.AT_MOST; |
| 5412 | } |
| 5413 | break; |
| 5414 | |
| 5415 | // Parent asked to see how big we want to be |
| 5416 | case MeasureSpec.UNSPECIFIED: |
| 5417 | if (childDimension >= 0) { |
| 5418 | // Child wants a specific size... let him have it |
| 5419 | resultSize = childDimension; |
| 5420 | resultMode = MeasureSpec.EXACTLY; |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 5421 | } else if (childDimension == LayoutParams.MATCH_PARENT) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5422 | // Child wants to be our size... find out how big it should |
| 5423 | // be |
| 5424 | resultSize = 0; |
| 5425 | resultMode = MeasureSpec.UNSPECIFIED; |
| 5426 | } else if (childDimension == LayoutParams.WRAP_CONTENT) { |
| 5427 | // Child wants to determine its own size.... find out how |
| 5428 | // big it should be |
| 5429 | resultSize = 0; |
| 5430 | resultMode = MeasureSpec.UNSPECIFIED; |
| 5431 | } |
| 5432 | break; |
| 5433 | } |
| 5434 | return MeasureSpec.makeMeasureSpec(resultSize, resultMode); |
| 5435 | } |
| 5436 | |
| 5437 | |
| 5438 | /** |
| 5439 | * Removes any pending animations for views that have been removed. Call |
| 5440 | * this if you don't want animations for exiting views to stack up. |
| 5441 | */ |
| 5442 | public void clearDisappearingChildren() { |
John Reck | ca7a9da | 2014-03-05 16:29:07 -0800 | [diff] [blame] | 5443 | final ArrayList<View> disappearingChildren = mDisappearingChildren; |
| 5444 | if (disappearingChildren != null) { |
| 5445 | final int count = disappearingChildren.size(); |
| 5446 | for (int i = 0; i < count; i++) { |
| 5447 | final View view = disappearingChildren.get(i); |
| 5448 | if (view.mAttachInfo != null) { |
| 5449 | view.dispatchDetachedFromWindow(); |
| 5450 | } |
| 5451 | view.clearAnimation(); |
| 5452 | } |
| 5453 | disappearingChildren.clear(); |
Chet Haase | b85967b | 2012-03-26 14:37:51 -0700 | [diff] [blame] | 5454 | invalidate(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5455 | } |
| 5456 | } |
| 5457 | |
| 5458 | /** |
| 5459 | * Add a view which is removed from mChildren but still needs animation |
| 5460 | * |
| 5461 | * @param v View to add |
| 5462 | */ |
| 5463 | private void addDisappearingView(View v) { |
| 5464 | ArrayList<View> disappearingChildren = mDisappearingChildren; |
| 5465 | |
| 5466 | if (disappearingChildren == null) { |
| 5467 | disappearingChildren = mDisappearingChildren = new ArrayList<View>(); |
| 5468 | } |
| 5469 | |
| 5470 | disappearingChildren.add(v); |
| 5471 | } |
| 5472 | |
| 5473 | /** |
| 5474 | * Cleanup a view when its animation is done. This may mean removing it from |
| 5475 | * the list of disappearing views. |
| 5476 | * |
| 5477 | * @param view The view whose animation has finished |
| 5478 | * @param animation The animation, cannot be null |
| 5479 | */ |
Chet Haase | 64a48c1 | 2012-02-13 16:33:29 -0800 | [diff] [blame] | 5480 | void finishAnimatingView(final View view, Animation animation) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5481 | final ArrayList<View> disappearingChildren = mDisappearingChildren; |
| 5482 | if (disappearingChildren != null) { |
| 5483 | if (disappearingChildren.contains(view)) { |
| 5484 | disappearingChildren.remove(view); |
| 5485 | |
| 5486 | if (view.mAttachInfo != null) { |
| 5487 | view.dispatchDetachedFromWindow(); |
| 5488 | } |
| 5489 | |
| 5490 | view.clearAnimation(); |
| 5491 | mGroupFlags |= FLAG_INVALIDATE_REQUIRED; |
| 5492 | } |
| 5493 | } |
| 5494 | |
| 5495 | if (animation != null && !animation.getFillAfter()) { |
| 5496 | view.clearAnimation(); |
| 5497 | } |
| 5498 | |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 5499 | if ((view.mPrivateFlags & PFLAG_ANIMATION_STARTED) == PFLAG_ANIMATION_STARTED) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5500 | view.onAnimationEnd(); |
| 5501 | // Should be performed by onAnimationEnd() but this avoid an infinite loop, |
| 5502 | // so we'd rather be safe than sorry |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 5503 | view.mPrivateFlags &= ~PFLAG_ANIMATION_STARTED; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5504 | // Draw one more frame after the animation is done |
| 5505 | mGroupFlags |= FLAG_INVALIDATE_REQUIRED; |
| 5506 | } |
| 5507 | } |
| 5508 | |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 5509 | /** |
Chet Haase | aceafe6 | 2011-08-26 15:44:33 -0700 | [diff] [blame] | 5510 | * Utility function called by View during invalidation to determine whether a view that |
| 5511 | * is invisible or gone should still be invalidated because it is being transitioned (and |
| 5512 | * therefore still needs to be drawn). |
| 5513 | */ |
| 5514 | boolean isViewTransitioning(View view) { |
| 5515 | return (mTransitioningViews != null && mTransitioningViews.contains(view)); |
| 5516 | } |
| 5517 | |
| 5518 | /** |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 5519 | * This method tells the ViewGroup that the given View object, which should have this |
| 5520 | * ViewGroup as its parent, |
| 5521 | * should be kept around (re-displayed when the ViewGroup draws its children) even if it |
| 5522 | * is removed from its parent. This allows animations, such as those used by |
| 5523 | * {@link android.app.Fragment} and {@link android.animation.LayoutTransition} to animate |
| 5524 | * the removal of views. A call to this method should always be accompanied by a later call |
| 5525 | * to {@link #endViewTransition(View)}, such as after an animation on the View has finished, |
| 5526 | * so that the View finally gets removed. |
| 5527 | * |
| 5528 | * @param view The View object to be kept visible even if it gets removed from its parent. |
| 5529 | */ |
| 5530 | public void startViewTransition(View view) { |
| 5531 | if (view.mParent == this) { |
| 5532 | if (mTransitioningViews == null) { |
| 5533 | mTransitioningViews = new ArrayList<View>(); |
| 5534 | } |
| 5535 | mTransitioningViews.add(view); |
| 5536 | } |
| 5537 | } |
| 5538 | |
| 5539 | /** |
| 5540 | * This method should always be called following an earlier call to |
| 5541 | * {@link #startViewTransition(View)}. The given View is finally removed from its parent |
| 5542 | * and will no longer be displayed. Note that this method does not perform the functionality |
| 5543 | * of removing a view from its parent; it just discontinues the display of a View that |
| 5544 | * has previously been removed. |
| 5545 | * |
| 5546 | * @return view The View object that has been removed but is being kept around in the visible |
| 5547 | * hierarchy by an earlier call to {@link #startViewTransition(View)}. |
| 5548 | */ |
| 5549 | public void endViewTransition(View view) { |
| 5550 | if (mTransitioningViews != null) { |
| 5551 | mTransitioningViews.remove(view); |
| 5552 | final ArrayList<View> disappearingChildren = mDisappearingChildren; |
| 5553 | if (disappearingChildren != null && disappearingChildren.contains(view)) { |
| 5554 | disappearingChildren.remove(view); |
Chet Haase | 5e25c2c | 2010-09-16 11:15:56 -0700 | [diff] [blame] | 5555 | if (mVisibilityChangingChildren != null && |
| 5556 | mVisibilityChangingChildren.contains(view)) { |
| 5557 | mVisibilityChangingChildren.remove(view); |
| 5558 | } else { |
| 5559 | if (view.mAttachInfo != null) { |
| 5560 | view.dispatchDetachedFromWindow(); |
| 5561 | } |
| 5562 | if (view.mParent != null) { |
| 5563 | view.mParent = null; |
| 5564 | } |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 5565 | } |
Chet Haase | b85967b | 2012-03-26 14:37:51 -0700 | [diff] [blame] | 5566 | invalidate(); |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 5567 | } |
| 5568 | } |
| 5569 | } |
| 5570 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 5571 | private LayoutTransition.TransitionListener mLayoutTransitionListener = |
| 5572 | new LayoutTransition.TransitionListener() { |
| 5573 | @Override |
| 5574 | public void startTransition(LayoutTransition transition, ViewGroup container, |
| 5575 | View view, int transitionType) { |
| 5576 | // We only care about disappearing items, since we need special logic to keep |
| 5577 | // those items visible after they've been 'removed' |
| 5578 | if (transitionType == LayoutTransition.DISAPPEARING) { |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 5579 | startViewTransition(view); |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 5580 | } |
| 5581 | } |
| 5582 | |
| 5583 | @Override |
| 5584 | public void endTransition(LayoutTransition transition, ViewGroup container, |
| 5585 | View view, int transitionType) { |
Chet Haase | b989502 | 2013-04-02 15:10:58 -0700 | [diff] [blame] | 5586 | if (mLayoutCalledWhileSuppressed && !transition.isChangingLayout()) { |
Chet Haase | 9c08744 | 2011-01-12 16:20:16 -0800 | [diff] [blame] | 5587 | requestLayout(); |
Chet Haase | b989502 | 2013-04-02 15:10:58 -0700 | [diff] [blame] | 5588 | mLayoutCalledWhileSuppressed = false; |
Chet Haase | 9c08744 | 2011-01-12 16:20:16 -0800 | [diff] [blame] | 5589 | } |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 5590 | if (transitionType == LayoutTransition.DISAPPEARING && mTransitioningViews != null) { |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 5591 | endViewTransition(view); |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 5592 | } |
| 5593 | } |
| 5594 | }; |
| 5595 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5596 | /** |
Chet Haase | b989502 | 2013-04-02 15:10:58 -0700 | [diff] [blame] | 5597 | * Tells this ViewGroup to suppress all layout() calls until layout |
| 5598 | * suppression is disabled with a later call to suppressLayout(false). |
| 5599 | * When layout suppression is disabled, a requestLayout() call is sent |
| 5600 | * if layout() was attempted while layout was being suppressed. |
| 5601 | * |
| 5602 | * @hide |
| 5603 | */ |
| 5604 | public void suppressLayout(boolean suppress) { |
| 5605 | mSuppressLayout = suppress; |
| 5606 | if (!suppress) { |
| 5607 | if (mLayoutCalledWhileSuppressed) { |
| 5608 | requestLayout(); |
| 5609 | mLayoutCalledWhileSuppressed = false; |
| 5610 | } |
| 5611 | } |
| 5612 | } |
| 5613 | |
| 5614 | /** |
Chet Haase | 199acdf | 2013-07-24 18:40:55 -0700 | [diff] [blame] | 5615 | * Returns whether layout calls on this container are currently being |
| 5616 | * suppressed, due to an earlier call to {@link #suppressLayout(boolean)}. |
| 5617 | * |
| 5618 | * @return true if layout calls are currently suppressed, false otherwise. |
| 5619 | * |
| 5620 | * @hide |
| 5621 | */ |
| 5622 | public boolean isLayoutSuppressed() { |
| 5623 | return mSuppressLayout; |
| 5624 | } |
| 5625 | |
| 5626 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5627 | * {@inheritDoc} |
| 5628 | */ |
| 5629 | @Override |
| 5630 | public boolean gatherTransparentRegion(Region region) { |
| 5631 | // If no transparent regions requested, we are always opaque. |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 5632 | 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] | 5633 | if (meOpaque && region == null) { |
| 5634 | // The caller doesn't care about the region, so stop now. |
| 5635 | return true; |
| 5636 | } |
| 5637 | super.gatherTransparentRegion(region); |
| 5638 | final View[] children = mChildren; |
| 5639 | final int count = mChildrenCount; |
| 5640 | boolean noneOfTheChildrenAreTransparent = true; |
| 5641 | for (int i = 0; i < count; i++) { |
| 5642 | final View child = children[i]; |
Mathias Agopian | e338115 | 2010-12-02 15:19:36 -0800 | [diff] [blame] | 5643 | if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5644 | if (!child.gatherTransparentRegion(region)) { |
| 5645 | noneOfTheChildrenAreTransparent = false; |
| 5646 | } |
| 5647 | } |
| 5648 | } |
| 5649 | return meOpaque || noneOfTheChildrenAreTransparent; |
| 5650 | } |
| 5651 | |
| 5652 | /** |
| 5653 | * {@inheritDoc} |
| 5654 | */ |
| 5655 | public void requestTransparentRegion(View child) { |
| 5656 | if (child != null) { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 5657 | child.mPrivateFlags |= View.PFLAG_REQUEST_TRANSPARENT_REGIONS; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5658 | if (mParent != null) { |
| 5659 | mParent.requestTransparentRegion(this); |
| 5660 | } |
| 5661 | } |
| 5662 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 5663 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5664 | @Override |
Adam Powell | 46e38fd | 2014-02-03 10:16:49 -0800 | [diff] [blame] | 5665 | public WindowInsets dispatchApplyWindowInsets(WindowInsets insets) { |
| 5666 | insets = super.dispatchApplyWindowInsets(insets); |
Adam Powell | 0d9fdba | 2014-06-11 15:33:08 -0700 | [diff] [blame] | 5667 | if (!insets.isConsumed()) { |
Adam Powell | 46e38fd | 2014-02-03 10:16:49 -0800 | [diff] [blame] | 5668 | final int count = getChildCount(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5669 | for (int i = 0; i < count; i++) { |
Adam Powell | 46e38fd | 2014-02-03 10:16:49 -0800 | [diff] [blame] | 5670 | insets = getChildAt(i).dispatchApplyWindowInsets(insets); |
Adam Powell | 0d9fdba | 2014-06-11 15:33:08 -0700 | [diff] [blame] | 5671 | if (insets.isConsumed()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5672 | break; |
| 5673 | } |
| 5674 | } |
| 5675 | } |
Adam Powell | 46e38fd | 2014-02-03 10:16:49 -0800 | [diff] [blame] | 5676 | return insets; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5677 | } |
| 5678 | |
| 5679 | /** |
| 5680 | * Returns the animation listener to which layout animation events are |
| 5681 | * sent. |
| 5682 | * |
| 5683 | * @return an {@link android.view.animation.Animation.AnimationListener} |
| 5684 | */ |
| 5685 | public Animation.AnimationListener getLayoutAnimationListener() { |
| 5686 | return mAnimationListener; |
| 5687 | } |
| 5688 | |
| 5689 | @Override |
| 5690 | protected void drawableStateChanged() { |
| 5691 | super.drawableStateChanged(); |
| 5692 | |
| 5693 | if ((mGroupFlags & FLAG_NOTIFY_CHILDREN_ON_DRAWABLE_STATE_CHANGE) != 0) { |
| 5694 | if ((mGroupFlags & FLAG_ADD_STATES_FROM_CHILDREN) != 0) { |
| 5695 | throw new IllegalStateException("addStateFromChildren cannot be enabled if a" |
| 5696 | + " child has duplicateParentState set to true"); |
| 5697 | } |
| 5698 | |
| 5699 | final View[] children = mChildren; |
| 5700 | final int count = mChildrenCount; |
| 5701 | |
| 5702 | for (int i = 0; i < count; i++) { |
| 5703 | final View child = children[i]; |
| 5704 | if ((child.mViewFlags & DUPLICATE_PARENT_STATE) != 0) { |
| 5705 | child.refreshDrawableState(); |
| 5706 | } |
| 5707 | } |
| 5708 | } |
| 5709 | } |
| 5710 | |
| 5711 | @Override |
Dianne Hackborn | e213677 | 2010-11-04 15:08:59 -0700 | [diff] [blame] | 5712 | public void jumpDrawablesToCurrentState() { |
| 5713 | super.jumpDrawablesToCurrentState(); |
| 5714 | final View[] children = mChildren; |
| 5715 | final int count = mChildrenCount; |
| 5716 | for (int i = 0; i < count; i++) { |
| 5717 | children[i].jumpDrawablesToCurrentState(); |
| 5718 | } |
| 5719 | } |
| 5720 | |
| 5721 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5722 | protected int[] onCreateDrawableState(int extraSpace) { |
| 5723 | if ((mGroupFlags & FLAG_ADD_STATES_FROM_CHILDREN) == 0) { |
| 5724 | return super.onCreateDrawableState(extraSpace); |
| 5725 | } |
| 5726 | |
| 5727 | int need = 0; |
| 5728 | int n = getChildCount(); |
| 5729 | for (int i = 0; i < n; i++) { |
| 5730 | int[] childState = getChildAt(i).getDrawableState(); |
| 5731 | |
| 5732 | if (childState != null) { |
| 5733 | need += childState.length; |
| 5734 | } |
| 5735 | } |
| 5736 | |
| 5737 | int[] state = super.onCreateDrawableState(extraSpace + need); |
| 5738 | |
| 5739 | for (int i = 0; i < n; i++) { |
| 5740 | int[] childState = getChildAt(i).getDrawableState(); |
| 5741 | |
| 5742 | if (childState != null) { |
| 5743 | state = mergeDrawableStates(state, childState); |
| 5744 | } |
| 5745 | } |
| 5746 | |
| 5747 | return state; |
| 5748 | } |
| 5749 | |
| 5750 | /** |
| 5751 | * Sets whether this ViewGroup's drawable states also include |
| 5752 | * its children's drawable states. This is used, for example, to |
| 5753 | * make a group appear to be focused when its child EditText or button |
| 5754 | * is focused. |
| 5755 | */ |
| 5756 | public void setAddStatesFromChildren(boolean addsStates) { |
| 5757 | if (addsStates) { |
| 5758 | mGroupFlags |= FLAG_ADD_STATES_FROM_CHILDREN; |
| 5759 | } else { |
| 5760 | mGroupFlags &= ~FLAG_ADD_STATES_FROM_CHILDREN; |
| 5761 | } |
| 5762 | |
| 5763 | refreshDrawableState(); |
| 5764 | } |
| 5765 | |
| 5766 | /** |
| 5767 | * Returns whether this ViewGroup's drawable states also include |
| 5768 | * its children's drawable states. This is used, for example, to |
| 5769 | * make a group appear to be focused when its child EditText or button |
| 5770 | * is focused. |
| 5771 | */ |
| 5772 | public boolean addStatesFromChildren() { |
| 5773 | return (mGroupFlags & FLAG_ADD_STATES_FROM_CHILDREN) != 0; |
| 5774 | } |
| 5775 | |
| 5776 | /** |
Jeff Smith | a45746e | 2012-07-19 14:19:24 -0500 | [diff] [blame] | 5777 | * If {@link #addStatesFromChildren} is true, refreshes this group's |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5778 | * drawable state (to include the states from its children). |
| 5779 | */ |
| 5780 | public void childDrawableStateChanged(View child) { |
| 5781 | if ((mGroupFlags & FLAG_ADD_STATES_FROM_CHILDREN) != 0) { |
| 5782 | refreshDrawableState(); |
| 5783 | } |
| 5784 | } |
| 5785 | |
| 5786 | /** |
| 5787 | * Specifies the animation listener to which layout animation events must |
| 5788 | * be sent. Only |
| 5789 | * {@link android.view.animation.Animation.AnimationListener#onAnimationStart(Animation)} |
| 5790 | * and |
| 5791 | * {@link android.view.animation.Animation.AnimationListener#onAnimationEnd(Animation)} |
| 5792 | * are invoked. |
| 5793 | * |
| 5794 | * @param animationListener the layout animation listener |
| 5795 | */ |
| 5796 | public void setLayoutAnimationListener(Animation.AnimationListener animationListener) { |
| 5797 | mAnimationListener = animationListener; |
| 5798 | } |
| 5799 | |
| 5800 | /** |
Chet Haase | cca2c98 | 2011-05-20 14:34:18 -0700 | [diff] [blame] | 5801 | * This method is called by LayoutTransition when there are 'changing' animations that need |
| 5802 | * to start after the layout/setup phase. The request is forwarded to the ViewAncestor, who |
| 5803 | * starts all pending transitions prior to the drawing phase in the current traversal. |
| 5804 | * |
| 5805 | * @param transition The LayoutTransition to be started on the next traversal. |
| 5806 | * |
| 5807 | * @hide |
| 5808 | */ |
| 5809 | public void requestTransitionStart(LayoutTransition transition) { |
Dianne Hackborn | 6dd005b | 2011-07-18 13:22:50 -0700 | [diff] [blame] | 5810 | ViewRootImpl viewAncestor = getViewRootImpl(); |
Chet Haase | 1abf7fa | 2011-08-17 18:31:56 -0700 | [diff] [blame] | 5811 | if (viewAncestor != null) { |
| 5812 | viewAncestor.requestTransitionStart(transition); |
| 5813 | } |
Chet Haase | cca2c98 | 2011-05-20 14:34:18 -0700 | [diff] [blame] | 5814 | } |
| 5815 | |
Fabrice Di Meglio | 4457e85 | 2012-09-18 19:23:12 -0700 | [diff] [blame] | 5816 | /** |
| 5817 | * @hide |
| 5818 | */ |
Fabrice Di Meglio | 80dc53d | 2011-06-21 18:36:33 -0700 | [diff] [blame] | 5819 | @Override |
Fabrice Di Meglio | 09ecb25 | 2013-05-03 16:51:55 -0700 | [diff] [blame] | 5820 | public boolean resolveRtlPropertiesIfNeeded() { |
| 5821 | final boolean result = super.resolveRtlPropertiesIfNeeded(); |
| 5822 | // We dont need to resolve the children RTL properties if nothing has changed for the parent |
| 5823 | if (result) { |
| 5824 | int count = getChildCount(); |
| 5825 | for (int i = 0; i < count; i++) { |
| 5826 | final View child = getChildAt(i); |
| 5827 | if (child.isLayoutDirectionInherited()) { |
| 5828 | child.resolveRtlPropertiesIfNeeded(); |
| 5829 | } |
Fabrice Di Meglio | 84ebb35 | 2012-10-11 16:27:37 -0700 | [diff] [blame] | 5830 | } |
| 5831 | } |
Fabrice Di Meglio | 09ecb25 | 2013-05-03 16:51:55 -0700 | [diff] [blame] | 5832 | return result; |
Fabrice Di Meglio | 84ebb35 | 2012-10-11 16:27:37 -0700 | [diff] [blame] | 5833 | } |
| 5834 | |
| 5835 | /** |
| 5836 | * @hide |
| 5837 | */ |
| 5838 | @Override |
Fabrice Di Meglio | 9a04856 | 2012-09-26 14:55:56 -0700 | [diff] [blame] | 5839 | public boolean resolveLayoutDirection() { |
| 5840 | final boolean result = super.resolveLayoutDirection(); |
| 5841 | if (result) { |
| 5842 | int count = getChildCount(); |
| 5843 | for (int i = 0; i < count; i++) { |
| 5844 | final View child = getChildAt(i); |
| 5845 | if (child.isLayoutDirectionInherited()) { |
| 5846 | child.resolveLayoutDirection(); |
| 5847 | } |
Fabrice Di Meglio | 1f88ba8 | 2012-09-24 14:56:49 -0700 | [diff] [blame] | 5848 | } |
| 5849 | } |
Fabrice Di Meglio | 9a04856 | 2012-09-26 14:55:56 -0700 | [diff] [blame] | 5850 | return result; |
Fabrice Di Meglio | 1f88ba8 | 2012-09-24 14:56:49 -0700 | [diff] [blame] | 5851 | } |
| 5852 | |
| 5853 | /** |
| 5854 | * @hide |
| 5855 | */ |
| 5856 | @Override |
Fabrice Di Meglio | 9a04856 | 2012-09-26 14:55:56 -0700 | [diff] [blame] | 5857 | public boolean resolveTextDirection() { |
| 5858 | final boolean result = super.resolveTextDirection(); |
| 5859 | if (result) { |
| 5860 | int count = getChildCount(); |
| 5861 | for (int i = 0; i < count; i++) { |
| 5862 | final View child = getChildAt(i); |
| 5863 | if (child.isTextDirectionInherited()) { |
| 5864 | child.resolveTextDirection(); |
| 5865 | } |
Fabrice Di Meglio | 1f88ba8 | 2012-09-24 14:56:49 -0700 | [diff] [blame] | 5866 | } |
| 5867 | } |
Fabrice Di Meglio | 9a04856 | 2012-09-26 14:55:56 -0700 | [diff] [blame] | 5868 | return result; |
Fabrice Di Meglio | 1f88ba8 | 2012-09-24 14:56:49 -0700 | [diff] [blame] | 5869 | } |
| 5870 | |
| 5871 | /** |
| 5872 | * @hide |
| 5873 | */ |
| 5874 | @Override |
Fabrice Di Meglio | 9a04856 | 2012-09-26 14:55:56 -0700 | [diff] [blame] | 5875 | public boolean resolveTextAlignment() { |
| 5876 | final boolean result = super.resolveTextAlignment(); |
| 5877 | if (result) { |
| 5878 | int count = getChildCount(); |
| 5879 | for (int i = 0; i < count; i++) { |
| 5880 | final View child = getChildAt(i); |
| 5881 | if (child.isTextAlignmentInherited()) { |
| 5882 | child.resolveTextAlignment(); |
| 5883 | } |
Fabrice Di Meglio | 1f88ba8 | 2012-09-24 14:56:49 -0700 | [diff] [blame] | 5884 | } |
| 5885 | } |
Fabrice Di Meglio | 9a04856 | 2012-09-26 14:55:56 -0700 | [diff] [blame] | 5886 | return result; |
Fabrice Di Meglio | 1f88ba8 | 2012-09-24 14:56:49 -0700 | [diff] [blame] | 5887 | } |
| 5888 | |
| 5889 | /** |
| 5890 | * @hide |
| 5891 | */ |
| 5892 | @Override |
Fabrice Di Meglio | 84ebb35 | 2012-10-11 16:27:37 -0700 | [diff] [blame] | 5893 | public void resolvePadding() { |
| 5894 | super.resolvePadding(); |
| 5895 | int count = getChildCount(); |
| 5896 | for (int i = 0; i < count; i++) { |
| 5897 | final View child = getChildAt(i); |
| 5898 | if (child.isLayoutDirectionInherited()) { |
| 5899 | child.resolvePadding(); |
| 5900 | } |
| 5901 | } |
| 5902 | } |
| 5903 | |
| 5904 | /** |
| 5905 | * @hide |
| 5906 | */ |
| 5907 | @Override |
| 5908 | protected void resolveDrawables() { |
| 5909 | super.resolveDrawables(); |
| 5910 | int count = getChildCount(); |
| 5911 | for (int i = 0; i < count; i++) { |
| 5912 | final View child = getChildAt(i); |
| 5913 | if (child.isLayoutDirectionInherited()) { |
| 5914 | child.resolveDrawables(); |
| 5915 | } |
| 5916 | } |
| 5917 | } |
| 5918 | |
Fabrice Di Meglio | 1e0ed6b | 2012-10-18 16:06:52 -0700 | [diff] [blame] | 5919 | /** |
| 5920 | * @hide |
| 5921 | */ |
Fabrice Di Meglio | fcc3348 | 2012-10-18 11:11:51 -0700 | [diff] [blame] | 5922 | @Override |
| 5923 | public void resolveLayoutParams() { |
| 5924 | super.resolveLayoutParams(); |
| 5925 | int count = getChildCount(); |
| 5926 | for (int i = 0; i < count; i++) { |
| 5927 | final View child = getChildAt(i); |
| 5928 | child.resolveLayoutParams(); |
| 5929 | } |
| 5930 | } |
| 5931 | |
Fabrice Di Meglio | 84ebb35 | 2012-10-11 16:27:37 -0700 | [diff] [blame] | 5932 | /** |
| 5933 | * @hide |
| 5934 | */ |
| 5935 | @Override |
Fabrice Di Meglio | 4457e85 | 2012-09-18 19:23:12 -0700 | [diff] [blame] | 5936 | public void resetResolvedLayoutDirection() { |
| 5937 | super.resetResolvedLayoutDirection(); |
| 5938 | |
Fabrice Di Meglio | 4457e85 | 2012-09-18 19:23:12 -0700 | [diff] [blame] | 5939 | int count = getChildCount(); |
Fabrice Di Meglio | 80dc53d | 2011-06-21 18:36:33 -0700 | [diff] [blame] | 5940 | for (int i = 0; i < count; i++) { |
| 5941 | final View child = getChildAt(i); |
Fabrice Di Meglio | e56ffdc | 2012-09-23 14:51:16 -0700 | [diff] [blame] | 5942 | if (child.isLayoutDirectionInherited()) { |
Fabrice Di Meglio | 7f86c80 | 2011-07-01 15:09:24 -0700 | [diff] [blame] | 5943 | child.resetResolvedLayoutDirection(); |
Fabrice Di Meglio | 80dc53d | 2011-06-21 18:36:33 -0700 | [diff] [blame] | 5944 | } |
Fabrice Di Meglio | 1f88ba8 | 2012-09-24 14:56:49 -0700 | [diff] [blame] | 5945 | } |
| 5946 | } |
| 5947 | |
| 5948 | /** |
| 5949 | * @hide |
| 5950 | */ |
| 5951 | @Override |
| 5952 | public void resetResolvedTextDirection() { |
| 5953 | super.resetResolvedTextDirection(); |
| 5954 | |
| 5955 | int count = getChildCount(); |
| 5956 | for (int i = 0; i < count; i++) { |
| 5957 | final View child = getChildAt(i); |
Fabrice Di Meglio | 97e146c | 2012-09-23 15:45:16 -0700 | [diff] [blame] | 5958 | if (child.isTextDirectionInherited()) { |
Fabrice Di Meglio | 2226886 | 2011-06-27 18:13:18 -0700 | [diff] [blame] | 5959 | child.resetResolvedTextDirection(); |
| 5960 | } |
Fabrice Di Meglio | 1f88ba8 | 2012-09-24 14:56:49 -0700 | [diff] [blame] | 5961 | } |
| 5962 | } |
| 5963 | |
| 5964 | /** |
| 5965 | * @hide |
| 5966 | */ |
| 5967 | @Override |
| 5968 | public void resetResolvedTextAlignment() { |
| 5969 | super.resetResolvedTextAlignment(); |
| 5970 | |
| 5971 | int count = getChildCount(); |
| 5972 | for (int i = 0; i < count; i++) { |
| 5973 | final View child = getChildAt(i); |
Fabrice Di Meglio | 1a7d487 | 2012-09-23 16:19:58 -0700 | [diff] [blame] | 5974 | if (child.isTextAlignmentInherited()) { |
Fabrice Di Meglio | 9da0f8a | 2012-03-13 19:37:57 -0700 | [diff] [blame] | 5975 | child.resetResolvedTextAlignment(); |
| 5976 | } |
| 5977 | } |
| 5978 | } |
| 5979 | |
Fabrice Di Meglio | 2226886 | 2011-06-27 18:13:18 -0700 | [diff] [blame] | 5980 | /** |
Fabrice Di Meglio | 84ebb35 | 2012-10-11 16:27:37 -0700 | [diff] [blame] | 5981 | * @hide |
| 5982 | */ |
| 5983 | @Override |
| 5984 | public void resetResolvedPadding() { |
| 5985 | super.resetResolvedPadding(); |
| 5986 | |
| 5987 | int count = getChildCount(); |
| 5988 | for (int i = 0; i < count; i++) { |
| 5989 | final View child = getChildAt(i); |
| 5990 | if (child.isLayoutDirectionInherited()) { |
| 5991 | child.resetResolvedPadding(); |
| 5992 | } |
| 5993 | } |
| 5994 | } |
| 5995 | |
| 5996 | /** |
| 5997 | * @hide |
| 5998 | */ |
| 5999 | @Override |
| 6000 | protected void resetResolvedDrawables() { |
| 6001 | super.resetResolvedDrawables(); |
| 6002 | |
| 6003 | int count = getChildCount(); |
| 6004 | for (int i = 0; i < count; i++) { |
| 6005 | final View child = getChildAt(i); |
| 6006 | if (child.isLayoutDirectionInherited()) { |
| 6007 | child.resetResolvedDrawables(); |
| 6008 | } |
| 6009 | } |
| 6010 | } |
| 6011 | |
| 6012 | /** |
Patrick Dubroy | e0a799a | 2011-05-04 16:19:22 -0700 | [diff] [blame] | 6013 | * Return true if the pressed state should be delayed for children or descendants of this |
| 6014 | * ViewGroup. Generally, this should be done for containers that can scroll, such as a List. |
| 6015 | * This prevents the pressed state from appearing when the user is actually trying to scroll |
| 6016 | * the content. |
| 6017 | * |
| 6018 | * The default implementation returns true for compatibility reasons. Subclasses that do |
| 6019 | * not scroll should generally override this method and return false. |
| 6020 | */ |
| 6021 | public boolean shouldDelayChildPressedState() { |
| 6022 | return true; |
| 6023 | } |
| 6024 | |
Adam Powell | 10ba277 | 2014-04-15 09:46:51 -0700 | [diff] [blame] | 6025 | /** |
| 6026 | * @inheritDoc |
| 6027 | */ |
| 6028 | @Override |
| 6029 | public boolean onStartNestedScroll(View child, View target, int nestedScrollAxes) { |
| 6030 | return false; |
| 6031 | } |
| 6032 | |
| 6033 | /** |
| 6034 | * @inheritDoc |
| 6035 | */ |
| 6036 | @Override |
| 6037 | public void onNestedScrollAccepted(View child, View target, int axes) { |
| 6038 | mNestedScrollAxes = axes; |
| 6039 | } |
| 6040 | |
| 6041 | /** |
| 6042 | * @inheritDoc |
| 6043 | * |
| 6044 | * <p>The default implementation of onStopNestedScroll calls |
| 6045 | * {@link #stopNestedScroll()} to halt any recursive nested scrolling in progress.</p> |
| 6046 | */ |
| 6047 | @Override |
| 6048 | public void onStopNestedScroll(View child) { |
| 6049 | // Stop any recursive nested scrolling. |
| 6050 | stopNestedScroll(); |
| 6051 | } |
| 6052 | |
| 6053 | /** |
| 6054 | * @inheritDoc |
| 6055 | */ |
| 6056 | @Override |
| 6057 | public void onNestedScroll(View target, int dxConsumed, int dyConsumed, |
| 6058 | int dxUnconsumed, int dyUnconsumed) { |
| 6059 | // Do nothing |
| 6060 | } |
| 6061 | |
| 6062 | /** |
| 6063 | * @inheritDoc |
| 6064 | */ |
| 6065 | @Override |
| 6066 | public void onNestedPreScroll(View target, int dx, int dy, int[] consumed) { |
| 6067 | // Do nothing |
| 6068 | } |
| 6069 | |
| 6070 | /** |
| 6071 | * @inheritDoc |
| 6072 | */ |
| 6073 | @Override |
Adam Powell | b36e4f9 | 2014-05-01 10:23:33 -0700 | [diff] [blame] | 6074 | public boolean onNestedFling(View target, float velocityX, float velocityY, boolean consumed) { |
Adam Powell | 10ba277 | 2014-04-15 09:46:51 -0700 | [diff] [blame] | 6075 | return false; |
| 6076 | } |
| 6077 | |
| 6078 | /** |
Adam Powell | b72be59 | 2014-07-16 21:41:31 -0700 | [diff] [blame] | 6079 | * @inheritDoc |
| 6080 | */ |
| 6081 | @Override |
| 6082 | public boolean onNestedPreFling(View target, float velocityX, float velocityY) { |
| 6083 | return false; |
| 6084 | } |
| 6085 | |
| 6086 | /** |
Adam Powell | 10ba277 | 2014-04-15 09:46:51 -0700 | [diff] [blame] | 6087 | * Return the current axes of nested scrolling for this ViewGroup. |
| 6088 | * |
| 6089 | * <p>A ViewGroup returning something other than {@link #SCROLL_AXIS_NONE} is currently |
| 6090 | * acting as a nested scrolling parent for one or more descendant views in the hierarchy.</p> |
| 6091 | * |
| 6092 | * @return Flags indicating the current axes of nested scrolling |
| 6093 | * @see #SCROLL_AXIS_HORIZONTAL |
| 6094 | * @see #SCROLL_AXIS_VERTICAL |
| 6095 | * @see #SCROLL_AXIS_NONE |
| 6096 | */ |
| 6097 | public int getNestedScrollAxes() { |
| 6098 | return mNestedScrollAxes; |
| 6099 | } |
| 6100 | |
Philip Milne | d7dd890 | 2012-01-26 16:55:30 -0800 | [diff] [blame] | 6101 | /** @hide */ |
| 6102 | protected void onSetLayoutParams(View child, LayoutParams layoutParams) { |
| 6103 | } |
| 6104 | |
George Mount | e180337 | 2014-02-26 19:00:52 +0000 | [diff] [blame] | 6105 | /** @hide */ |
| 6106 | @Override |
| 6107 | public void captureTransitioningViews(List<View> transitioningViews) { |
| 6108 | if (getVisibility() != View.VISIBLE) { |
| 6109 | return; |
| 6110 | } |
| 6111 | if (isTransitionGroup()) { |
| 6112 | transitioningViews.add(this); |
| 6113 | } else { |
| 6114 | int count = getChildCount(); |
| 6115 | for (int i = 0; i < count; i++) { |
| 6116 | View child = getChildAt(i); |
| 6117 | child.captureTransitioningViews(transitioningViews); |
| 6118 | } |
| 6119 | } |
| 6120 | } |
| 6121 | |
| 6122 | /** @hide */ |
| 6123 | @Override |
George Mount | abb352a | 2014-05-09 10:27:20 -0700 | [diff] [blame] | 6124 | public void findNamedViews(Map<String, View> namedElements) { |
George Mount | fe361d2 | 2014-07-08 17:25:25 -0700 | [diff] [blame] | 6125 | if (getVisibility() != VISIBLE && mGhostView == null) { |
George Mount | e180337 | 2014-02-26 19:00:52 +0000 | [diff] [blame] | 6126 | return; |
| 6127 | } |
George Mount | abb352a | 2014-05-09 10:27:20 -0700 | [diff] [blame] | 6128 | super.findNamedViews(namedElements); |
George Mount | e180337 | 2014-02-26 19:00:52 +0000 | [diff] [blame] | 6129 | int count = getChildCount(); |
| 6130 | for (int i = 0; i < count; i++) { |
| 6131 | View child = getChildAt(i); |
George Mount | abb352a | 2014-05-09 10:27:20 -0700 | [diff] [blame] | 6132 | child.findNamedViews(namedElements); |
George Mount | e180337 | 2014-02-26 19:00:52 +0000 | [diff] [blame] | 6133 | } |
| 6134 | } |
| 6135 | |
Patrick Dubroy | e0a799a | 2011-05-04 16:19:22 -0700 | [diff] [blame] | 6136 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6137 | * LayoutParams are used by views to tell their parents how they want to be |
| 6138 | * laid out. See |
| 6139 | * {@link android.R.styleable#ViewGroup_Layout ViewGroup Layout Attributes} |
| 6140 | * for a list of all child view attributes that this class supports. |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 6141 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6142 | * <p> |
| 6143 | * The base LayoutParams class just describes how big the view wants to be |
| 6144 | * for both width and height. For each dimension, it can specify one of: |
| 6145 | * <ul> |
Dirk Dougherty | 75c66da | 2010-03-25 16:33:33 -0700 | [diff] [blame] | 6146 | * <li>FILL_PARENT (renamed MATCH_PARENT in API Level 8 and higher), which |
| 6147 | * 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] | 6148 | * <li> WRAP_CONTENT, which means that the view wants to be just big enough |
| 6149 | * to enclose its content (plus padding) |
Dirk Dougherty | 75c66da | 2010-03-25 16:33:33 -0700 | [diff] [blame] | 6150 | * <li> an exact number |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6151 | * </ul> |
| 6152 | * There are subclasses of LayoutParams for different subclasses of |
| 6153 | * ViewGroup. For example, AbsoluteLayout has its own subclass of |
Joe Fernandez | 558459f | 2011-10-13 16:47:36 -0700 | [diff] [blame] | 6154 | * LayoutParams which adds an X and Y value.</p> |
| 6155 | * |
| 6156 | * <div class="special reference"> |
| 6157 | * <h3>Developer Guides</h3> |
| 6158 | * <p>For more information about creating user interface layouts, read the |
| 6159 | * <a href="{@docRoot}guide/topics/ui/declaring-layout.html">XML Layouts</a> developer |
| 6160 | * guide.</p></div> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6161 | * |
| 6162 | * @attr ref android.R.styleable#ViewGroup_Layout_layout_height |
| 6163 | * @attr ref android.R.styleable#ViewGroup_Layout_layout_width |
| 6164 | */ |
| 6165 | public static class LayoutParams { |
| 6166 | /** |
Dirk Dougherty | 75c66da | 2010-03-25 16:33:33 -0700 | [diff] [blame] | 6167 | * Special value for the height or width requested by a View. |
| 6168 | * FILL_PARENT means that the view wants to be as big as its parent, |
| 6169 | * minus the parent's padding, if any. This value is deprecated |
| 6170 | * 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] | 6171 | */ |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 6172 | @SuppressWarnings({"UnusedDeclaration"}) |
| 6173 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6174 | public static final int FILL_PARENT = -1; |
| 6175 | |
| 6176 | /** |
| 6177 | * Special value for the height or width requested by a View. |
Gilles Debunne | f5c6eff | 2010-02-09 19:08:36 -0800 | [diff] [blame] | 6178 | * 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] | 6179 | * minus the parent's padding, if any. Introduced in API Level 8. |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 6180 | */ |
| 6181 | public static final int MATCH_PARENT = -1; |
| 6182 | |
| 6183 | /** |
| 6184 | * 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] | 6185 | * WRAP_CONTENT means that the view wants to be just large enough to fit |
| 6186 | * its own internal content, taking its own padding into account. |
| 6187 | */ |
| 6188 | public static final int WRAP_CONTENT = -2; |
| 6189 | |
| 6190 | /** |
Dirk Dougherty | 75c66da | 2010-03-25 16:33:33 -0700 | [diff] [blame] | 6191 | * Information about how wide the view wants to be. Can be one of the |
| 6192 | * constants FILL_PARENT (replaced by MATCH_PARENT , |
| 6193 | * 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] | 6194 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 6195 | @ViewDebug.ExportedProperty(category = "layout", mapping = { |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 6196 | @ViewDebug.IntToString(from = MATCH_PARENT, to = "MATCH_PARENT"), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6197 | @ViewDebug.IntToString(from = WRAP_CONTENT, to = "WRAP_CONTENT") |
| 6198 | }) |
| 6199 | public int width; |
| 6200 | |
| 6201 | /** |
Dirk Dougherty | 75c66da | 2010-03-25 16:33:33 -0700 | [diff] [blame] | 6202 | * Information about how tall the view wants to be. Can be one of the |
| 6203 | * constants FILL_PARENT (replaced by MATCH_PARENT , |
| 6204 | * 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] | 6205 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 6206 | @ViewDebug.ExportedProperty(category = "layout", mapping = { |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 6207 | @ViewDebug.IntToString(from = MATCH_PARENT, to = "MATCH_PARENT"), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6208 | @ViewDebug.IntToString(from = WRAP_CONTENT, to = "WRAP_CONTENT") |
| 6209 | }) |
| 6210 | public int height; |
| 6211 | |
| 6212 | /** |
| 6213 | * Used to animate layouts. |
| 6214 | */ |
| 6215 | public LayoutAnimationController.AnimationParameters layoutAnimationParameters; |
| 6216 | |
| 6217 | /** |
| 6218 | * Creates a new set of layout parameters. The values are extracted from |
| 6219 | * the supplied attributes set and context. The XML attributes mapped |
| 6220 | * to this set of layout parameters are: |
| 6221 | * |
| 6222 | * <ul> |
| 6223 | * <li><code>layout_width</code>: the width, either an exact value, |
Dirk Dougherty | 75c66da | 2010-03-25 16:33:33 -0700 | [diff] [blame] | 6224 | * {@link #WRAP_CONTENT}, or {@link #FILL_PARENT} (replaced by |
| 6225 | * {@link #MATCH_PARENT} in API Level 8)</li> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6226 | * <li><code>layout_height</code>: the height, either an exact value, |
Dirk Dougherty | 75c66da | 2010-03-25 16:33:33 -0700 | [diff] [blame] | 6227 | * {@link #WRAP_CONTENT}, or {@link #FILL_PARENT} (replaced by |
| 6228 | * {@link #MATCH_PARENT} in API Level 8)</li> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6229 | * </ul> |
| 6230 | * |
| 6231 | * @param c the application environment |
| 6232 | * @param attrs the set of attributes from which to extract the layout |
| 6233 | * parameters' values |
| 6234 | */ |
| 6235 | public LayoutParams(Context c, AttributeSet attrs) { |
| 6236 | TypedArray a = c.obtainStyledAttributes(attrs, R.styleable.ViewGroup_Layout); |
| 6237 | setBaseAttributes(a, |
| 6238 | R.styleable.ViewGroup_Layout_layout_width, |
| 6239 | R.styleable.ViewGroup_Layout_layout_height); |
| 6240 | a.recycle(); |
| 6241 | } |
| 6242 | |
| 6243 | /** |
| 6244 | * Creates a new set of layout parameters with the specified width |
| 6245 | * and height. |
| 6246 | * |
Dirk Dougherty | 75c66da | 2010-03-25 16:33:33 -0700 | [diff] [blame] | 6247 | * @param width the width, either {@link #WRAP_CONTENT}, |
| 6248 | * {@link #FILL_PARENT} (replaced by {@link #MATCH_PARENT} in |
| 6249 | * API Level 8), or a fixed size in pixels |
| 6250 | * @param height the height, either {@link #WRAP_CONTENT}, |
| 6251 | * {@link #FILL_PARENT} (replaced by {@link #MATCH_PARENT} in |
| 6252 | * API Level 8), or a fixed size in pixels |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6253 | */ |
| 6254 | public LayoutParams(int width, int height) { |
| 6255 | this.width = width; |
| 6256 | this.height = height; |
| 6257 | } |
| 6258 | |
| 6259 | /** |
| 6260 | * Copy constructor. Clones the width and height values of the source. |
| 6261 | * |
| 6262 | * @param source The layout params to copy from. |
| 6263 | */ |
| 6264 | public LayoutParams(LayoutParams source) { |
| 6265 | this.width = source.width; |
| 6266 | this.height = source.height; |
| 6267 | } |
| 6268 | |
| 6269 | /** |
| 6270 | * Used internally by MarginLayoutParams. |
| 6271 | * @hide |
| 6272 | */ |
| 6273 | LayoutParams() { |
| 6274 | } |
| 6275 | |
| 6276 | /** |
Dave Burke | 579e140 | 2012-10-18 20:41:55 -0700 | [diff] [blame] | 6277 | * Extracts the layout parameters from the supplied attributes. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6278 | * |
| 6279 | * @param a the style attributes to extract the parameters from |
| 6280 | * @param widthAttr the identifier of the width attribute |
| 6281 | * @param heightAttr the identifier of the height attribute |
| 6282 | */ |
| 6283 | protected void setBaseAttributes(TypedArray a, int widthAttr, int heightAttr) { |
Dave Burke | 579e140 | 2012-10-18 20:41:55 -0700 | [diff] [blame] | 6284 | width = a.getLayoutDimension(widthAttr, "layout_width"); |
| 6285 | height = a.getLayoutDimension(heightAttr, "layout_height"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6286 | } |
| 6287 | |
| 6288 | /** |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 6289 | * Resolve layout parameters depending on the layout direction. Subclasses that care about |
| 6290 | * layoutDirection changes should override this method. The default implementation does |
| 6291 | * nothing. |
| 6292 | * |
| 6293 | * @param layoutDirection the direction of the layout |
| 6294 | * |
| 6295 | * {@link View#LAYOUT_DIRECTION_LTR} |
| 6296 | * {@link View#LAYOUT_DIRECTION_RTL} |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 6297 | */ |
Fabrice Di Meglio | 2918ab6 | 2012-10-10 16:39:25 -0700 | [diff] [blame] | 6298 | public void resolveLayoutDirection(int layoutDirection) { |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 6299 | } |
| 6300 | |
| 6301 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6302 | * Returns a String representation of this set of layout parameters. |
| 6303 | * |
| 6304 | * @param output the String to prepend to the internal representation |
| 6305 | * @return a String with the following format: output + |
| 6306 | * "ViewGroup.LayoutParams={ width=WIDTH, height=HEIGHT }" |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 6307 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6308 | * @hide |
| 6309 | */ |
| 6310 | public String debug(String output) { |
| 6311 | return output + "ViewGroup.LayoutParams={ width=" |
| 6312 | + sizeToString(width) + ", height=" + sizeToString(height) + " }"; |
| 6313 | } |
| 6314 | |
| 6315 | /** |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 6316 | * Use {@code canvas} to draw suitable debugging annotations for these LayoutParameters. |
| 6317 | * |
| 6318 | * @param view the view that contains these layout parameters |
| 6319 | * @param canvas the canvas on which to draw |
| 6320 | * |
| 6321 | * @hide |
| 6322 | */ |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 6323 | public void onDebugDraw(View view, Canvas canvas, Paint paint) { |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 6324 | } |
| 6325 | |
| 6326 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6327 | * Converts the specified size to a readable String. |
| 6328 | * |
| 6329 | * @param size the size to convert |
| 6330 | * @return a String instance representing the supplied size |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 6331 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6332 | * @hide |
| 6333 | */ |
| 6334 | protected static String sizeToString(int size) { |
| 6335 | if (size == WRAP_CONTENT) { |
| 6336 | return "wrap-content"; |
| 6337 | } |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 6338 | if (size == MATCH_PARENT) { |
| 6339 | return "match-parent"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6340 | } |
| 6341 | return String.valueOf(size); |
| 6342 | } |
| 6343 | } |
| 6344 | |
| 6345 | /** |
| 6346 | * Per-child layout information for layouts that support margins. |
| 6347 | * See |
| 6348 | * {@link android.R.styleable#ViewGroup_MarginLayout ViewGroup Margin Layout Attributes} |
| 6349 | * for a list of all child view attributes that this class supports. |
| 6350 | */ |
| 6351 | public static class MarginLayoutParams extends ViewGroup.LayoutParams { |
| 6352 | /** |
Philip Milne | d7dd890 | 2012-01-26 16:55:30 -0800 | [diff] [blame] | 6353 | * The left margin in pixels of the child. |
| 6354 | * Call {@link ViewGroup#setLayoutParams(LayoutParams)} after reassigning a new value |
| 6355 | * to this field. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6356 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 6357 | @ViewDebug.ExportedProperty(category = "layout") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6358 | public int leftMargin; |
| 6359 | |
| 6360 | /** |
Philip Milne | d7dd890 | 2012-01-26 16:55:30 -0800 | [diff] [blame] | 6361 | * The top margin in pixels of the child. |
| 6362 | * Call {@link ViewGroup#setLayoutParams(LayoutParams)} after reassigning a new value |
| 6363 | * to this field. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6364 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 6365 | @ViewDebug.ExportedProperty(category = "layout") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6366 | public int topMargin; |
| 6367 | |
| 6368 | /** |
Philip Milne | d7dd890 | 2012-01-26 16:55:30 -0800 | [diff] [blame] | 6369 | * The right margin in pixels of the child. |
| 6370 | * Call {@link ViewGroup#setLayoutParams(LayoutParams)} after reassigning a new value |
| 6371 | * to this field. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6372 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 6373 | @ViewDebug.ExportedProperty(category = "layout") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6374 | public int rightMargin; |
| 6375 | |
| 6376 | /** |
Philip Milne | d7dd890 | 2012-01-26 16:55:30 -0800 | [diff] [blame] | 6377 | * The bottom margin in pixels of the child. |
| 6378 | * Call {@link ViewGroup#setLayoutParams(LayoutParams)} after reassigning a new value |
| 6379 | * to this field. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6380 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 6381 | @ViewDebug.ExportedProperty(category = "layout") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6382 | public int bottomMargin; |
| 6383 | |
| 6384 | /** |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 6385 | * The start margin in pixels of the child. |
Fabrice Di Meglio | 54546f2 | 2012-02-14 16:26:16 -0800 | [diff] [blame] | 6386 | * Call {@link ViewGroup#setLayoutParams(LayoutParams)} after reassigning a new value |
| 6387 | * to this field. |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 6388 | */ |
| 6389 | @ViewDebug.ExportedProperty(category = "layout") |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 6390 | private int startMargin = DEFAULT_MARGIN_RELATIVE; |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 6391 | |
| 6392 | /** |
| 6393 | * The end margin in pixels of the child. |
Fabrice Di Meglio | 54546f2 | 2012-02-14 16:26:16 -0800 | [diff] [blame] | 6394 | * Call {@link ViewGroup#setLayoutParams(LayoutParams)} after reassigning a new value |
| 6395 | * to this field. |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 6396 | */ |
| 6397 | @ViewDebug.ExportedProperty(category = "layout") |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 6398 | private int endMargin = DEFAULT_MARGIN_RELATIVE; |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 6399 | |
| 6400 | /** |
| 6401 | * The default start and end margin. |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 6402 | * @hide |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 6403 | */ |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 6404 | public static final int DEFAULT_MARGIN_RELATIVE = Integer.MIN_VALUE; |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 6405 | |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 6406 | /** |
| 6407 | * Bit 0: layout direction |
| 6408 | * Bit 1: layout direction |
| 6409 | * Bit 2: left margin undefined |
| 6410 | * Bit 3: right margin undefined |
| 6411 | * Bit 4: is RTL compatibility mode |
| 6412 | * Bit 5: need resolution |
| 6413 | * |
| 6414 | * Bit 6 to 7 not used |
| 6415 | * |
| 6416 | * @hide |
| 6417 | */ |
| 6418 | @ViewDebug.ExportedProperty(category = "layout", flagMapping = { |
| 6419 | @ViewDebug.FlagToString(mask = LAYOUT_DIRECTION_MASK, |
| 6420 | equals = LAYOUT_DIRECTION_MASK, name = "LAYOUT_DIRECTION"), |
| 6421 | @ViewDebug.FlagToString(mask = LEFT_MARGIN_UNDEFINED_MASK, |
| 6422 | equals = LEFT_MARGIN_UNDEFINED_MASK, name = "LEFT_MARGIN_UNDEFINED_MASK"), |
| 6423 | @ViewDebug.FlagToString(mask = RIGHT_MARGIN_UNDEFINED_MASK, |
| 6424 | equals = RIGHT_MARGIN_UNDEFINED_MASK, name = "RIGHT_MARGIN_UNDEFINED_MASK"), |
| 6425 | @ViewDebug.FlagToString(mask = RTL_COMPATIBILITY_MODE_MASK, |
| 6426 | equals = RTL_COMPATIBILITY_MODE_MASK, name = "RTL_COMPATIBILITY_MODE_MASK"), |
| 6427 | @ViewDebug.FlagToString(mask = NEED_RESOLUTION_MASK, |
| 6428 | equals = NEED_RESOLUTION_MASK, name = "NEED_RESOLUTION_MASK") |
Jon Miranda | 4597e98 | 2014-07-29 07:25:49 -0700 | [diff] [blame] | 6429 | }, formatToHexString = true) |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 6430 | byte mMarginFlags; |
Fabrice Di Meglio | 69bd558 | 2012-07-02 13:17:24 -0700 | [diff] [blame] | 6431 | |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 6432 | private static final int LAYOUT_DIRECTION_MASK = 0x00000003; |
| 6433 | private static final int LEFT_MARGIN_UNDEFINED_MASK = 0x00000004; |
| 6434 | private static final int RIGHT_MARGIN_UNDEFINED_MASK = 0x00000008; |
| 6435 | private static final int RTL_COMPATIBILITY_MODE_MASK = 0x00000010; |
| 6436 | private static final int NEED_RESOLUTION_MASK = 0x00000020; |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 6437 | |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 6438 | private static final int DEFAULT_MARGIN_RESOLVED = 0; |
| 6439 | private static final int UNDEFINED_MARGIN = DEFAULT_MARGIN_RELATIVE; |
Fabrice Di Meglio | 02a7d56 | 2013-03-27 12:41:22 -0700 | [diff] [blame] | 6440 | |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 6441 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6442 | * Creates a new set of layout parameters. The values are extracted from |
| 6443 | * the supplied attributes set and context. |
| 6444 | * |
| 6445 | * @param c the application environment |
| 6446 | * @param attrs the set of attributes from which to extract the layout |
| 6447 | * parameters' values |
| 6448 | */ |
| 6449 | public MarginLayoutParams(Context c, AttributeSet attrs) { |
| 6450 | super(); |
| 6451 | |
| 6452 | TypedArray a = c.obtainStyledAttributes(attrs, R.styleable.ViewGroup_MarginLayout); |
| 6453 | setBaseAttributes(a, |
| 6454 | R.styleable.ViewGroup_MarginLayout_layout_width, |
| 6455 | R.styleable.ViewGroup_MarginLayout_layout_height); |
| 6456 | |
| 6457 | int margin = a.getDimensionPixelSize( |
| 6458 | com.android.internal.R.styleable.ViewGroup_MarginLayout_layout_margin, -1); |
| 6459 | if (margin >= 0) { |
| 6460 | leftMargin = margin; |
| 6461 | topMargin = margin; |
| 6462 | rightMargin= margin; |
| 6463 | bottomMargin = margin; |
| 6464 | } else { |
| 6465 | leftMargin = a.getDimensionPixelSize( |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 6466 | R.styleable.ViewGroup_MarginLayout_layout_marginLeft, |
Fabrice Di Meglio | 02a7d56 | 2013-03-27 12:41:22 -0700 | [diff] [blame] | 6467 | UNDEFINED_MARGIN); |
| 6468 | if (leftMargin == UNDEFINED_MARGIN) { |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 6469 | mMarginFlags |= LEFT_MARGIN_UNDEFINED_MASK; |
Fabrice Di Meglio | 02a7d56 | 2013-03-27 12:41:22 -0700 | [diff] [blame] | 6470 | leftMargin = DEFAULT_MARGIN_RESOLVED; |
| 6471 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6472 | rightMargin = a.getDimensionPixelSize( |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 6473 | R.styleable.ViewGroup_MarginLayout_layout_marginRight, |
Fabrice Di Meglio | 02a7d56 | 2013-03-27 12:41:22 -0700 | [diff] [blame] | 6474 | UNDEFINED_MARGIN); |
| 6475 | if (rightMargin == UNDEFINED_MARGIN) { |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 6476 | mMarginFlags |= RIGHT_MARGIN_UNDEFINED_MASK; |
Fabrice Di Meglio | 02a7d56 | 2013-03-27 12:41:22 -0700 | [diff] [blame] | 6477 | rightMargin = DEFAULT_MARGIN_RESOLVED; |
| 6478 | } |
| 6479 | |
| 6480 | topMargin = a.getDimensionPixelSize( |
| 6481 | R.styleable.ViewGroup_MarginLayout_layout_marginTop, |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 6482 | DEFAULT_MARGIN_RESOLVED); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6483 | bottomMargin = a.getDimensionPixelSize( |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 6484 | R.styleable.ViewGroup_MarginLayout_layout_marginBottom, |
| 6485 | DEFAULT_MARGIN_RESOLVED); |
Fabrice Di Meglio | 02a7d56 | 2013-03-27 12:41:22 -0700 | [diff] [blame] | 6486 | |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 6487 | startMargin = a.getDimensionPixelSize( |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 6488 | R.styleable.ViewGroup_MarginLayout_layout_marginStart, |
| 6489 | DEFAULT_MARGIN_RELATIVE); |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 6490 | endMargin = a.getDimensionPixelSize( |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 6491 | R.styleable.ViewGroup_MarginLayout_layout_marginEnd, |
| 6492 | DEFAULT_MARGIN_RELATIVE); |
| 6493 | |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 6494 | if (isMarginRelative()) { |
| 6495 | mMarginFlags |= NEED_RESOLUTION_MASK; |
| 6496 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6497 | } |
| 6498 | |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 6499 | final boolean hasRtlSupport = c.getApplicationInfo().hasRtlSupport(); |
| 6500 | final int targetSdkVersion = c.getApplicationInfo().targetSdkVersion; |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 6501 | if (targetSdkVersion < JELLY_BEAN_MR1 || !hasRtlSupport) { |
| 6502 | mMarginFlags |= RTL_COMPATIBILITY_MODE_MASK; |
| 6503 | } |
| 6504 | |
| 6505 | // Layout direction is LTR by default |
| 6506 | mMarginFlags |= LAYOUT_DIRECTION_LTR; |
Fabrice Di Meglio | 69bd558 | 2012-07-02 13:17:24 -0700 | [diff] [blame] | 6507 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6508 | a.recycle(); |
| 6509 | } |
| 6510 | |
| 6511 | /** |
| 6512 | * {@inheritDoc} |
| 6513 | */ |
| 6514 | public MarginLayoutParams(int width, int height) { |
| 6515 | super(width, height); |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 6516 | |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 6517 | mMarginFlags |= LEFT_MARGIN_UNDEFINED_MASK; |
| 6518 | mMarginFlags |= RIGHT_MARGIN_UNDEFINED_MASK; |
Fabrice Di Meglio | 02a7d56 | 2013-03-27 12:41:22 -0700 | [diff] [blame] | 6519 | |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 6520 | mMarginFlags &= ~NEED_RESOLUTION_MASK; |
| 6521 | mMarginFlags &= ~RTL_COMPATIBILITY_MODE_MASK; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6522 | } |
| 6523 | |
| 6524 | /** |
| 6525 | * Copy constructor. Clones the width, height and margin values of the source. |
| 6526 | * |
| 6527 | * @param source The layout params to copy from. |
| 6528 | */ |
| 6529 | public MarginLayoutParams(MarginLayoutParams source) { |
| 6530 | this.width = source.width; |
| 6531 | this.height = source.height; |
| 6532 | |
| 6533 | this.leftMargin = source.leftMargin; |
| 6534 | this.topMargin = source.topMargin; |
| 6535 | this.rightMargin = source.rightMargin; |
| 6536 | this.bottomMargin = source.bottomMargin; |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 6537 | this.startMargin = source.startMargin; |
| 6538 | this.endMargin = source.endMargin; |
Fabrice Di Meglio | 69bd558 | 2012-07-02 13:17:24 -0700 | [diff] [blame] | 6539 | |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 6540 | this.mMarginFlags = source.mMarginFlags; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6541 | } |
| 6542 | |
| 6543 | /** |
| 6544 | * {@inheritDoc} |
| 6545 | */ |
| 6546 | public MarginLayoutParams(LayoutParams source) { |
| 6547 | super(source); |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 6548 | |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 6549 | mMarginFlags |= LEFT_MARGIN_UNDEFINED_MASK; |
| 6550 | mMarginFlags |= RIGHT_MARGIN_UNDEFINED_MASK; |
Fabrice Di Meglio | 02a7d56 | 2013-03-27 12:41:22 -0700 | [diff] [blame] | 6551 | |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 6552 | mMarginFlags &= ~NEED_RESOLUTION_MASK; |
| 6553 | mMarginFlags &= ~RTL_COMPATIBILITY_MODE_MASK; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6554 | } |
| 6555 | |
| 6556 | /** |
Adam Powell | d760083 | 2014-07-01 15:22:50 -0700 | [diff] [blame] | 6557 | * @hide Used internally. |
| 6558 | */ |
| 6559 | public final void copyMarginsFrom(MarginLayoutParams source) { |
| 6560 | this.leftMargin = source.leftMargin; |
| 6561 | this.topMargin = source.topMargin; |
| 6562 | this.rightMargin = source.rightMargin; |
| 6563 | this.bottomMargin = source.bottomMargin; |
| 6564 | this.startMargin = source.startMargin; |
| 6565 | this.endMargin = source.endMargin; |
| 6566 | |
| 6567 | this.mMarginFlags = source.mMarginFlags; |
| 6568 | } |
| 6569 | |
| 6570 | /** |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 6571 | * Sets the margins, in pixels. A call to {@link android.view.View#requestLayout()} needs |
| 6572 | * to be done so that the new margins are taken into account. Left and right margins may be |
| 6573 | * overriden by {@link android.view.View#requestLayout()} depending on layout direction. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6574 | * |
| 6575 | * @param left the left margin size |
| 6576 | * @param top the top margin size |
| 6577 | * @param right the right margin size |
| 6578 | * @param bottom the bottom margin size |
| 6579 | * |
| 6580 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginLeft |
| 6581 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginTop |
| 6582 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginRight |
| 6583 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginBottom |
| 6584 | */ |
| 6585 | public void setMargins(int left, int top, int right, int bottom) { |
| 6586 | leftMargin = left; |
| 6587 | topMargin = top; |
| 6588 | rightMargin = right; |
| 6589 | bottomMargin = bottom; |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 6590 | mMarginFlags &= ~LEFT_MARGIN_UNDEFINED_MASK; |
| 6591 | mMarginFlags &= ~RIGHT_MARGIN_UNDEFINED_MASK; |
| 6592 | if (isMarginRelative()) { |
| 6593 | mMarginFlags |= NEED_RESOLUTION_MASK; |
| 6594 | } else { |
| 6595 | mMarginFlags &= ~NEED_RESOLUTION_MASK; |
| 6596 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6597 | } |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 6598 | |
| 6599 | /** |
| 6600 | * Sets the relative margins, in pixels. A call to {@link android.view.View#requestLayout()} |
| 6601 | * needs to be done so that the new relative margins are taken into account. Left and right |
| 6602 | * margins may be overriden by {@link android.view.View#requestLayout()} depending on layout |
| 6603 | * direction. |
| 6604 | * |
| 6605 | * @param start the start margin size |
| 6606 | * @param top the top margin size |
| 6607 | * @param end the right margin size |
| 6608 | * @param bottom the bottom margin size |
| 6609 | * |
| 6610 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginStart |
| 6611 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginTop |
| 6612 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginEnd |
| 6613 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginBottom |
| 6614 | * |
| 6615 | * @hide |
| 6616 | */ |
| 6617 | public void setMarginsRelative(int start, int top, int end, int bottom) { |
| 6618 | startMargin = start; |
| 6619 | topMargin = top; |
| 6620 | endMargin = end; |
| 6621 | bottomMargin = bottom; |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 6622 | mMarginFlags |= NEED_RESOLUTION_MASK; |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 6623 | } |
| 6624 | |
| 6625 | /** |
Fabrice Di Meglio | a40627d | 2012-09-11 16:47:21 -0700 | [diff] [blame] | 6626 | * Sets the relative start margin. |
| 6627 | * |
Fabrice Di Meglio | 61a2177 | 2012-09-12 16:33:13 -0700 | [diff] [blame] | 6628 | * @param start the start margin size |
Fabrice Di Meglio | a40627d | 2012-09-11 16:47:21 -0700 | [diff] [blame] | 6629 | * |
| 6630 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginStart |
| 6631 | */ |
| 6632 | public void setMarginStart(int start) { |
| 6633 | startMargin = start; |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 6634 | mMarginFlags |= NEED_RESOLUTION_MASK; |
Fabrice Di Meglio | a40627d | 2012-09-11 16:47:21 -0700 | [diff] [blame] | 6635 | } |
| 6636 | |
| 6637 | /** |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 6638 | * Returns the start margin in pixels. |
| 6639 | * |
| 6640 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginStart |
| 6641 | * |
| 6642 | * @return the start margin in pixels. |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 6643 | */ |
| 6644 | public int getMarginStart() { |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 6645 | if (startMargin != DEFAULT_MARGIN_RELATIVE) return startMargin; |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 6646 | if ((mMarginFlags & NEED_RESOLUTION_MASK) == NEED_RESOLUTION_MASK) { |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 6647 | doResolveMargins(); |
| 6648 | } |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 6649 | switch(mMarginFlags & LAYOUT_DIRECTION_MASK) { |
Fabrice Di Meglio | 69bd558 | 2012-07-02 13:17:24 -0700 | [diff] [blame] | 6650 | case View.LAYOUT_DIRECTION_RTL: |
| 6651 | return rightMargin; |
| 6652 | case View.LAYOUT_DIRECTION_LTR: |
| 6653 | default: |
| 6654 | return leftMargin; |
| 6655 | } |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 6656 | } |
| 6657 | |
| 6658 | /** |
Fabrice Di Meglio | a40627d | 2012-09-11 16:47:21 -0700 | [diff] [blame] | 6659 | * Sets the relative end margin. |
| 6660 | * |
Fabrice Di Meglio | 61a2177 | 2012-09-12 16:33:13 -0700 | [diff] [blame] | 6661 | * @param end the end margin size |
Fabrice Di Meglio | a40627d | 2012-09-11 16:47:21 -0700 | [diff] [blame] | 6662 | * |
| 6663 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginEnd |
| 6664 | */ |
| 6665 | public void setMarginEnd(int end) { |
| 6666 | endMargin = end; |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 6667 | mMarginFlags |= NEED_RESOLUTION_MASK; |
Fabrice Di Meglio | a40627d | 2012-09-11 16:47:21 -0700 | [diff] [blame] | 6668 | } |
| 6669 | |
| 6670 | /** |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 6671 | * Returns the end margin in pixels. |
| 6672 | * |
| 6673 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginEnd |
| 6674 | * |
| 6675 | * @return the end margin in pixels. |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 6676 | */ |
| 6677 | public int getMarginEnd() { |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 6678 | if (endMargin != DEFAULT_MARGIN_RELATIVE) return endMargin; |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 6679 | if ((mMarginFlags & NEED_RESOLUTION_MASK) == NEED_RESOLUTION_MASK) { |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 6680 | doResolveMargins(); |
| 6681 | } |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 6682 | switch(mMarginFlags & LAYOUT_DIRECTION_MASK) { |
Fabrice Di Meglio | 69bd558 | 2012-07-02 13:17:24 -0700 | [diff] [blame] | 6683 | case View.LAYOUT_DIRECTION_RTL: |
| 6684 | return leftMargin; |
| 6685 | case View.LAYOUT_DIRECTION_LTR: |
| 6686 | default: |
| 6687 | return rightMargin; |
| 6688 | } |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 6689 | } |
| 6690 | |
| 6691 | /** |
| 6692 | * Check if margins are relative. |
| 6693 | * |
| 6694 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginStart |
| 6695 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginEnd |
| 6696 | * |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 6697 | * @return true if either marginStart or marginEnd has been set. |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 6698 | */ |
| 6699 | public boolean isMarginRelative() { |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 6700 | return (startMargin != DEFAULT_MARGIN_RELATIVE || endMargin != DEFAULT_MARGIN_RELATIVE); |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 6701 | } |
| 6702 | |
| 6703 | /** |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 6704 | * Set the layout direction |
| 6705 | * @param layoutDirection the layout direction. |
| 6706 | * Should be either {@link View#LAYOUT_DIRECTION_LTR} |
| 6707 | * or {@link View#LAYOUT_DIRECTION_RTL}. |
| 6708 | */ |
| 6709 | public void setLayoutDirection(int layoutDirection) { |
| 6710 | if (layoutDirection != View.LAYOUT_DIRECTION_LTR && |
| 6711 | layoutDirection != View.LAYOUT_DIRECTION_RTL) return; |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 6712 | if (layoutDirection != (mMarginFlags & LAYOUT_DIRECTION_MASK)) { |
| 6713 | mMarginFlags &= ~LAYOUT_DIRECTION_MASK; |
| 6714 | mMarginFlags |= (layoutDirection & LAYOUT_DIRECTION_MASK); |
| 6715 | if (isMarginRelative()) { |
| 6716 | mMarginFlags |= NEED_RESOLUTION_MASK; |
| 6717 | } else { |
| 6718 | mMarginFlags &= ~NEED_RESOLUTION_MASK; |
| 6719 | } |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 6720 | } |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 6721 | } |
| 6722 | |
| 6723 | /** |
| 6724 | * Retuns the layout direction. Can be either {@link View#LAYOUT_DIRECTION_LTR} or |
| 6725 | * {@link View#LAYOUT_DIRECTION_RTL}. |
| 6726 | * |
| 6727 | * @return the layout direction. |
| 6728 | */ |
| 6729 | public int getLayoutDirection() { |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 6730 | return (mMarginFlags & LAYOUT_DIRECTION_MASK); |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 6731 | } |
| 6732 | |
| 6733 | /** |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 6734 | * 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] | 6735 | * may be overridden depending on layout direction. |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 6736 | */ |
| 6737 | @Override |
Fabrice Di Meglio | 2918ab6 | 2012-10-10 16:39:25 -0700 | [diff] [blame] | 6738 | public void resolveLayoutDirection(int layoutDirection) { |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 6739 | setLayoutDirection(layoutDirection); |
Fabrice Di Meglio | 69bd558 | 2012-07-02 13:17:24 -0700 | [diff] [blame] | 6740 | |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 6741 | // No relative margin or pre JB-MR1 case or no need to resolve, just dont do anything |
| 6742 | // 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] | 6743 | if (!isMarginRelative() || |
| 6744 | (mMarginFlags & NEED_RESOLUTION_MASK) != NEED_RESOLUTION_MASK) return; |
Fabrice Di Meglio | 69bd558 | 2012-07-02 13:17:24 -0700 | [diff] [blame] | 6745 | |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 6746 | // Proceed with resolution |
| 6747 | doResolveMargins(); |
| 6748 | } |
| 6749 | |
| 6750 | private void doResolveMargins() { |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 6751 | if ((mMarginFlags & RTL_COMPATIBILITY_MODE_MASK) == RTL_COMPATIBILITY_MODE_MASK) { |
Fabrice Di Meglio | 02a7d56 | 2013-03-27 12:41:22 -0700 | [diff] [blame] | 6752 | // if left or right margins are not defined and if we have some start or end margin |
| 6753 | // defined then use those start and end margins. |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 6754 | if ((mMarginFlags & LEFT_MARGIN_UNDEFINED_MASK) == LEFT_MARGIN_UNDEFINED_MASK |
| 6755 | && startMargin > DEFAULT_MARGIN_RELATIVE) { |
Fabrice Di Meglio | 02a7d56 | 2013-03-27 12:41:22 -0700 | [diff] [blame] | 6756 | leftMargin = startMargin; |
| 6757 | } |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 6758 | if ((mMarginFlags & RIGHT_MARGIN_UNDEFINED_MASK) == RIGHT_MARGIN_UNDEFINED_MASK |
| 6759 | && endMargin > DEFAULT_MARGIN_RELATIVE) { |
Fabrice Di Meglio | 02a7d56 | 2013-03-27 12:41:22 -0700 | [diff] [blame] | 6760 | rightMargin = endMargin; |
| 6761 | } |
| 6762 | } else { |
| 6763 | // We have some relative margins (either the start one or the end one or both). So use |
| 6764 | // them and override what has been defined for left and right margins. If either start |
| 6765 | // 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] | 6766 | switch(mMarginFlags & LAYOUT_DIRECTION_MASK) { |
Fabrice Di Meglio | 02a7d56 | 2013-03-27 12:41:22 -0700 | [diff] [blame] | 6767 | case View.LAYOUT_DIRECTION_RTL: |
| 6768 | leftMargin = (endMargin > DEFAULT_MARGIN_RELATIVE) ? |
| 6769 | endMargin : DEFAULT_MARGIN_RESOLVED; |
| 6770 | rightMargin = (startMargin > DEFAULT_MARGIN_RELATIVE) ? |
| 6771 | startMargin : DEFAULT_MARGIN_RESOLVED; |
| 6772 | break; |
| 6773 | case View.LAYOUT_DIRECTION_LTR: |
| 6774 | default: |
| 6775 | leftMargin = (startMargin > DEFAULT_MARGIN_RELATIVE) ? |
| 6776 | startMargin : DEFAULT_MARGIN_RESOLVED; |
| 6777 | rightMargin = (endMargin > DEFAULT_MARGIN_RELATIVE) ? |
| 6778 | endMargin : DEFAULT_MARGIN_RESOLVED; |
| 6779 | break; |
| 6780 | } |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 6781 | } |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 6782 | mMarginFlags &= ~NEED_RESOLUTION_MASK; |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 6783 | } |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 6784 | |
Fabrice Di Meglio | 03b8d3a | 2012-09-27 17:05:27 -0700 | [diff] [blame] | 6785 | /** |
| 6786 | * @hide |
| 6787 | */ |
| 6788 | public boolean isLayoutRtl() { |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 6789 | return ((mMarginFlags & LAYOUT_DIRECTION_MASK) == View.LAYOUT_DIRECTION_RTL); |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 6790 | } |
| 6791 | |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 6792 | /** |
| 6793 | * @hide |
| 6794 | */ |
| 6795 | @Override |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 6796 | public void onDebugDraw(View view, Canvas canvas, Paint paint) { |
| 6797 | Insets oi = isLayoutModeOptical(view.mParent) ? view.getOpticalInsets() : Insets.NONE; |
| 6798 | |
| 6799 | fillDifference(canvas, |
| 6800 | view.getLeft() + oi.left, |
| 6801 | view.getTop() + oi.top, |
| 6802 | view.getRight() - oi.right, |
| 6803 | view.getBottom() - oi.bottom, |
| 6804 | leftMargin, |
| 6805 | topMargin, |
| 6806 | rightMargin, |
| 6807 | bottomMargin, |
| 6808 | paint); |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 6809 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6810 | } |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 6811 | |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 6812 | /* Describes a touched view and the ids of the pointers that it has captured. |
| 6813 | * |
| 6814 | * This code assumes that pointer ids are always in the range 0..31 such that |
| 6815 | * it can use a bitfield to track which pointer ids are present. |
| 6816 | * As it happens, the lower layers of the input dispatch pipeline also use the |
| 6817 | * same trick so the assumption should be safe here... |
| 6818 | */ |
| 6819 | private static final class TouchTarget { |
| 6820 | private static final int MAX_RECYCLED = 32; |
Romain Guy | 6410c0a | 2013-06-17 11:21:58 -0700 | [diff] [blame] | 6821 | private static final Object sRecycleLock = new Object[0]; |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 6822 | private static TouchTarget sRecycleBin; |
| 6823 | private static int sRecycledCount; |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 6824 | |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 6825 | public static final int ALL_POINTER_IDS = -1; // all ones |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 6826 | |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 6827 | // The touched child view. |
| 6828 | public View child; |
| 6829 | |
| 6830 | // The combined bit mask of pointer ids for all pointers captured by the target. |
| 6831 | public int pointerIdBits; |
| 6832 | |
| 6833 | // The next target in the target list. |
| 6834 | public TouchTarget next; |
| 6835 | |
| 6836 | private TouchTarget() { |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 6837 | } |
| 6838 | |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 6839 | public static TouchTarget obtain(View child, int pointerIdBits) { |
| 6840 | final TouchTarget target; |
| 6841 | synchronized (sRecycleLock) { |
Adam Powell | 816c3be | 2010-08-23 18:00:05 -0700 | [diff] [blame] | 6842 | if (sRecycleBin == null) { |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 6843 | target = new TouchTarget(); |
Adam Powell | 816c3be | 2010-08-23 18:00:05 -0700 | [diff] [blame] | 6844 | } else { |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 6845 | target = sRecycleBin; |
| 6846 | sRecycleBin = target.next; |
| 6847 | sRecycledCount--; |
| 6848 | target.next = null; |
Adam Powell | 816c3be | 2010-08-23 18:00:05 -0700 | [diff] [blame] | 6849 | } |
Adam Powell | 816c3be | 2010-08-23 18:00:05 -0700 | [diff] [blame] | 6850 | } |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 6851 | target.child = child; |
| 6852 | target.pointerIdBits = pointerIdBits; |
| 6853 | return target; |
| 6854 | } |
Adam Powell | 816c3be | 2010-08-23 18:00:05 -0700 | [diff] [blame] | 6855 | |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 6856 | public void recycle() { |
| 6857 | synchronized (sRecycleLock) { |
| 6858 | if (sRecycledCount < MAX_RECYCLED) { |
| 6859 | next = sRecycleBin; |
| 6860 | sRecycleBin = this; |
| 6861 | sRecycledCount += 1; |
Patrick Dubroy | fb0547d2 | 2010-10-19 17:36:18 -0700 | [diff] [blame] | 6862 | } else { |
| 6863 | next = null; |
Adam Powell | 816c3be | 2010-08-23 18:00:05 -0700 | [diff] [blame] | 6864 | } |
Patrick Dubroy | fb0547d2 | 2010-10-19 17:36:18 -0700 | [diff] [blame] | 6865 | child = null; |
Adam Powell | 816c3be | 2010-08-23 18:00:05 -0700 | [diff] [blame] | 6866 | } |
| 6867 | } |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 6868 | } |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 6869 | |
| 6870 | /* Describes a hovered view. */ |
| 6871 | private static final class HoverTarget { |
| 6872 | private static final int MAX_RECYCLED = 32; |
Romain Guy | 6410c0a | 2013-06-17 11:21:58 -0700 | [diff] [blame] | 6873 | private static final Object sRecycleLock = new Object[0]; |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 6874 | private static HoverTarget sRecycleBin; |
| 6875 | private static int sRecycledCount; |
| 6876 | |
| 6877 | // The hovered child view. |
| 6878 | public View child; |
| 6879 | |
| 6880 | // The next target in the target list. |
| 6881 | public HoverTarget next; |
| 6882 | |
| 6883 | private HoverTarget() { |
| 6884 | } |
| 6885 | |
| 6886 | public static HoverTarget obtain(View child) { |
| 6887 | final HoverTarget target; |
| 6888 | synchronized (sRecycleLock) { |
| 6889 | if (sRecycleBin == null) { |
| 6890 | target = new HoverTarget(); |
| 6891 | } else { |
| 6892 | target = sRecycleBin; |
| 6893 | sRecycleBin = target.next; |
| 6894 | sRecycledCount--; |
| 6895 | target.next = null; |
| 6896 | } |
| 6897 | } |
| 6898 | target.child = child; |
| 6899 | return target; |
| 6900 | } |
| 6901 | |
| 6902 | public void recycle() { |
| 6903 | synchronized (sRecycleLock) { |
| 6904 | if (sRecycledCount < MAX_RECYCLED) { |
| 6905 | next = sRecycleBin; |
| 6906 | sRecycleBin = this; |
| 6907 | sRecycledCount += 1; |
| 6908 | } else { |
| 6909 | next = null; |
| 6910 | } |
| 6911 | child = null; |
| 6912 | } |
| 6913 | } |
| 6914 | } |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 6915 | |
| 6916 | /** |
| 6917 | * Pooled class that orderes the children of a ViewGroup from start |
| 6918 | * to end based on how they are laid out and the layout direction. |
| 6919 | */ |
| 6920 | static class ChildListForAccessibility { |
| 6921 | |
| 6922 | private static final int MAX_POOL_SIZE = 32; |
| 6923 | |
Svetoslav Ganov | be922dc | 2012-11-30 16:46:26 -0800 | [diff] [blame] | 6924 | private static final SynchronizedPool<ChildListForAccessibility> sPool = |
| 6925 | new SynchronizedPool<ChildListForAccessibility>(MAX_POOL_SIZE); |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 6926 | |
| 6927 | private final ArrayList<View> mChildren = new ArrayList<View>(); |
| 6928 | |
| 6929 | private final ArrayList<ViewLocationHolder> mHolders = new ArrayList<ViewLocationHolder>(); |
| 6930 | |
| 6931 | public static ChildListForAccessibility obtain(ViewGroup parent, boolean sort) { |
Svetoslav Ganov | be922dc | 2012-11-30 16:46:26 -0800 | [diff] [blame] | 6932 | ChildListForAccessibility list = sPool.acquire(); |
| 6933 | if (list == null) { |
| 6934 | list = new ChildListForAccessibility(); |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 6935 | } |
Svetoslav Ganov | be922dc | 2012-11-30 16:46:26 -0800 | [diff] [blame] | 6936 | list.init(parent, sort); |
| 6937 | return list; |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 6938 | } |
| 6939 | |
| 6940 | public void recycle() { |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 6941 | clear(); |
Svetoslav Ganov | be922dc | 2012-11-30 16:46:26 -0800 | [diff] [blame] | 6942 | sPool.release(this); |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 6943 | } |
| 6944 | |
| 6945 | public int getChildCount() { |
| 6946 | return mChildren.size(); |
| 6947 | } |
| 6948 | |
| 6949 | public View getChildAt(int index) { |
| 6950 | return mChildren.get(index); |
| 6951 | } |
| 6952 | |
| 6953 | public int getChildIndex(View child) { |
| 6954 | return mChildren.indexOf(child); |
| 6955 | } |
| 6956 | |
| 6957 | private void init(ViewGroup parent, boolean sort) { |
| 6958 | ArrayList<View> children = mChildren; |
| 6959 | final int childCount = parent.getChildCount(); |
| 6960 | for (int i = 0; i < childCount; i++) { |
| 6961 | View child = parent.getChildAt(i); |
| 6962 | children.add(child); |
| 6963 | } |
| 6964 | if (sort) { |
| 6965 | ArrayList<ViewLocationHolder> holders = mHolders; |
| 6966 | for (int i = 0; i < childCount; i++) { |
| 6967 | View child = children.get(i); |
| 6968 | ViewLocationHolder holder = ViewLocationHolder.obtain(parent, child); |
| 6969 | holders.add(holder); |
| 6970 | } |
| 6971 | Collections.sort(holders); |
| 6972 | for (int i = 0; i < childCount; i++) { |
| 6973 | ViewLocationHolder holder = holders.get(i); |
| 6974 | children.set(i, holder.mView); |
| 6975 | holder.recycle(); |
| 6976 | } |
| 6977 | holders.clear(); |
| 6978 | } |
| 6979 | } |
| 6980 | |
| 6981 | private void clear() { |
| 6982 | mChildren.clear(); |
| 6983 | } |
| 6984 | } |
| 6985 | |
| 6986 | /** |
| 6987 | * Pooled class that holds a View and its location with respect to |
| 6988 | * a specified root. This enables sorting of views based on their |
| 6989 | * coordinates without recomputing the position relative to the root |
| 6990 | * on every comparison. |
| 6991 | */ |
| 6992 | static class ViewLocationHolder implements Comparable<ViewLocationHolder> { |
| 6993 | |
| 6994 | private static final int MAX_POOL_SIZE = 32; |
| 6995 | |
Svetoslav Ganov | be922dc | 2012-11-30 16:46:26 -0800 | [diff] [blame] | 6996 | private static final SynchronizedPool<ViewLocationHolder> sPool = |
| 6997 | new SynchronizedPool<ViewLocationHolder>(MAX_POOL_SIZE); |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 6998 | |
| 6999 | private final Rect mLocation = new Rect(); |
| 7000 | |
| 7001 | public View mView; |
| 7002 | |
| 7003 | private int mLayoutDirection; |
| 7004 | |
| 7005 | public static ViewLocationHolder obtain(ViewGroup root, View view) { |
Svetoslav Ganov | be922dc | 2012-11-30 16:46:26 -0800 | [diff] [blame] | 7006 | ViewLocationHolder holder = sPool.acquire(); |
| 7007 | if (holder == null) { |
| 7008 | holder = new ViewLocationHolder(); |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 7009 | } |
Svetoslav Ganov | be922dc | 2012-11-30 16:46:26 -0800 | [diff] [blame] | 7010 | holder.init(root, view); |
| 7011 | return holder; |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 7012 | } |
| 7013 | |
| 7014 | public void recycle() { |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 7015 | clear(); |
Svetoslav Ganov | be922dc | 2012-11-30 16:46:26 -0800 | [diff] [blame] | 7016 | sPool.release(this); |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 7017 | } |
| 7018 | |
| 7019 | @Override |
| 7020 | public int compareTo(ViewLocationHolder another) { |
| 7021 | // This instance is greater than an invalid argument. |
| 7022 | if (another == null) { |
| 7023 | return 1; |
| 7024 | } |
Svetoslav | 04cab1b | 2014-08-25 18:35:57 -0700 | [diff] [blame] | 7025 | // We are ordering left-to-right, top-to-bottom. |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 7026 | if (mLayoutDirection == LAYOUT_DIRECTION_LTR) { |
| 7027 | final int leftDifference = mLocation.left - another.mLocation.left; |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 7028 | if (leftDifference != 0) { |
| 7029 | return leftDifference; |
| 7030 | } |
| 7031 | } else { // RTL |
| 7032 | final int rightDifference = mLocation.right - another.mLocation.right; |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 7033 | if (rightDifference != 0) { |
| 7034 | return -rightDifference; |
| 7035 | } |
| 7036 | } |
Svetoslav | 04cab1b | 2014-08-25 18:35:57 -0700 | [diff] [blame] | 7037 | // We are ordering left-to-right, top-to-bottom. |
| 7038 | final int topDifference = mLocation.top - another.mLocation.top; |
| 7039 | if (topDifference != 0) { |
| 7040 | return topDifference; |
| 7041 | } |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 7042 | // Break tie by height. |
| 7043 | final int heightDiference = mLocation.height() - another.mLocation.height(); |
| 7044 | if (heightDiference != 0) { |
| 7045 | return -heightDiference; |
| 7046 | } |
| 7047 | // Break tie by width. |
| 7048 | final int widthDiference = mLocation.width() - another.mLocation.width(); |
| 7049 | if (widthDiference != 0) { |
| 7050 | return -widthDiference; |
| 7051 | } |
Svetoslav Ganov | 005b83b | 2012-04-16 18:17:17 -0700 | [diff] [blame] | 7052 | // Just break the tie somehow. The accessibliity ids are unique |
| 7053 | // and stable, hence this is deterministic tie breaking. |
| 7054 | return mView.getAccessibilityViewId() - another.mView.getAccessibilityViewId(); |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 7055 | } |
| 7056 | |
| 7057 | private void init(ViewGroup root, View view) { |
| 7058 | Rect viewLocation = mLocation; |
| 7059 | view.getDrawingRect(viewLocation); |
| 7060 | root.offsetDescendantRectToMyCoords(view, viewLocation); |
| 7061 | mView = view; |
Fabrice Di Meglio | e56ffdc | 2012-09-23 14:51:16 -0700 | [diff] [blame] | 7062 | mLayoutDirection = root.getLayoutDirection(); |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 7063 | } |
| 7064 | |
| 7065 | private void clear() { |
| 7066 | mView = null; |
| 7067 | mLocation.set(0, 0, 0, 0); |
| 7068 | } |
| 7069 | } |
Romain Guy | cbc6774 | 2012-04-27 16:12:57 -0700 | [diff] [blame] | 7070 | |
| 7071 | private static Paint getDebugPaint() { |
| 7072 | if (sDebugPaint == null) { |
| 7073 | sDebugPaint = new Paint(); |
| 7074 | sDebugPaint.setAntiAlias(false); |
| 7075 | } |
| 7076 | return sDebugPaint; |
| 7077 | } |
| 7078 | |
Romain Guy | 6410c0a | 2013-06-17 11:21:58 -0700 | [diff] [blame] | 7079 | 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] | 7080 | if (sDebugLines== null) { |
Romain Guy | 6410c0a | 2013-06-17 11:21:58 -0700 | [diff] [blame] | 7081 | // 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] | 7082 | sDebugLines = new float[16]; |
| 7083 | } |
| 7084 | |
Romain Guy | cbc6774 | 2012-04-27 16:12:57 -0700 | [diff] [blame] | 7085 | sDebugLines[0] = x1; |
| 7086 | sDebugLines[1] = y1; |
| 7087 | sDebugLines[2] = x2; |
| 7088 | sDebugLines[3] = y1; |
| 7089 | |
| 7090 | sDebugLines[4] = x2; |
| 7091 | sDebugLines[5] = y1; |
| 7092 | sDebugLines[6] = x2; |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 7093 | sDebugLines[7] = y2; |
Romain Guy | cbc6774 | 2012-04-27 16:12:57 -0700 | [diff] [blame] | 7094 | |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 7095 | sDebugLines[8] = x2; |
Romain Guy | cbc6774 | 2012-04-27 16:12:57 -0700 | [diff] [blame] | 7096 | sDebugLines[9] = y2; |
| 7097 | sDebugLines[10] = x1; |
| 7098 | sDebugLines[11] = y2; |
| 7099 | |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 7100 | sDebugLines[12] = x1; |
| 7101 | sDebugLines[13] = y2; |
Romain Guy | cbc6774 | 2012-04-27 16:12:57 -0700 | [diff] [blame] | 7102 | sDebugLines[14] = x1; |
| 7103 | sDebugLines[15] = y1; |
| 7104 | |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 7105 | canvas.drawLines(sDebugLines, paint); |
Romain Guy | cbc6774 | 2012-04-27 16:12:57 -0700 | [diff] [blame] | 7106 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7107 | } |