The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.view; |
| 18 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 19 | import android.animation.LayoutTransition; |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 20 | import android.annotation.IdRes; |
Alan Viverette | 922e1c6 | 2015-05-05 17:18:27 -0700 | [diff] [blame] | 21 | import android.annotation.NonNull; |
Tor Norbye | 83c6896 | 2015-03-10 20:55:31 -0700 | [diff] [blame] | 22 | import android.annotation.UiThread; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | import android.content.Context; |
Clara Bayarri | d5bf3ed | 2015-03-27 17:32:45 +0000 | [diff] [blame] | 24 | import android.content.Intent; |
Adam Powell | ff0d298 | 2014-07-10 20:34:14 -0700 | [diff] [blame] | 25 | import android.content.pm.PackageManager; |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 26 | import android.content.res.Configuration; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | import android.content.res.TypedArray; |
| 28 | import android.graphics.Bitmap; |
| 29 | import android.graphics.Canvas; |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 30 | import android.graphics.Color; |
| 31 | import android.graphics.Insets; |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 32 | import android.graphics.Matrix; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | import android.graphics.Paint; |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 34 | import android.graphics.PointF; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | import android.graphics.Rect; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | import android.graphics.RectF; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 37 | import android.graphics.Region; |
Jeff Brown | 995e774 | 2010-12-22 16:59:36 -0800 | [diff] [blame] | 38 | import android.os.Build; |
Adam Powell | b6ab098 | 2015-01-07 17:00:12 -0800 | [diff] [blame] | 39 | import android.os.Bundle; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | import android.os.Parcelable; |
| 41 | import android.os.SystemClock; |
| 42 | import android.util.AttributeSet; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | import android.util.Log; |
Svetoslav Ganov | be922dc | 2012-11-30 16:46:26 -0800 | [diff] [blame] | 44 | import android.util.Pools.SynchronizedPool; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | import android.util.SparseArray; |
Dianne Hackborn | 6ff55fc | 2015-08-05 18:07:31 -0700 | [diff] [blame] | 46 | import android.util.SparseBooleanArray; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 47 | import android.view.accessibility.AccessibilityEvent; |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 48 | import android.view.accessibility.AccessibilityNodeInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | import android.view.animation.Animation; |
| 50 | import android.view.animation.AnimationUtils; |
| 51 | import android.view.animation.LayoutAnimationController; |
| 52 | import android.view.animation.Transformation; |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 53 | |
Romain Guy | 0211a0a | 2011-02-14 16:34:59 -0800 | [diff] [blame] | 54 | import com.android.internal.R; |
| 55 | import com.android.internal.util.Predicate; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | |
| 57 | import java.util.ArrayList; |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 58 | import java.util.Collections; |
Christopher Tate | 86cab1b | 2011-01-13 20:28:55 -0800 | [diff] [blame] | 59 | import java.util.HashSet; |
George Mount | e180337 | 2014-02-26 19:00:52 +0000 | [diff] [blame] | 60 | import java.util.List; |
| 61 | import java.util.Map; |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 62 | import static android.os.Build.VERSION_CODES.JELLY_BEAN_MR1; |
| 63 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | /** |
| 65 | * <p> |
| 66 | * A <code>ViewGroup</code> is a special view that can contain other views |
| 67 | * (called children.) The view group is the base class for layouts and views |
| 68 | * containers. This class also defines the |
| 69 | * {@link android.view.ViewGroup.LayoutParams} class which serves as the base |
| 70 | * class for layouts parameters. |
| 71 | * </p> |
| 72 | * |
| 73 | * <p> |
| 74 | * Also see {@link LayoutParams} for layout attributes. |
| 75 | * </p> |
Romain Guy | d6a463a | 2009-05-21 23:10:10 -0700 | [diff] [blame] | 76 | * |
Joe Fernandez | 558459f | 2011-10-13 16:47:36 -0700 | [diff] [blame] | 77 | * <div class="special reference"> |
| 78 | * <h3>Developer Guides</h3> |
| 79 | * <p>For more information about creating user interface layouts, read the |
| 80 | * <a href="{@docRoot}guide/topics/ui/declaring-layout.html">XML Layouts</a> developer |
| 81 | * guide.</p></div> |
| 82 | * |
Dianne Hackborn | 7caab0f | 2013-03-06 13:47:04 -0800 | [diff] [blame] | 83 | * <p>Here is a complete implementation of a custom ViewGroup that implements |
| 84 | * a simple {@link android.widget.FrameLayout} along with the ability to stack |
| 85 | * children in left and right gutters.</p> |
| 86 | * |
| 87 | * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/CustomLayout.java |
| 88 | * Complete} |
| 89 | * |
| 90 | * <p>If you are implementing XML layout attributes as shown in the example, this is the |
| 91 | * corresponding definition for them that would go in <code>res/values/attrs.xml</code>:</p> |
| 92 | * |
| 93 | * {@sample development/samples/ApiDemos/res/values/attrs.xml CustomLayout} |
| 94 | * |
| 95 | * <p>Finally the layout manager can be used in an XML layout like so:</p> |
| 96 | * |
| 97 | * {@sample development/samples/ApiDemos/res/layout/custom_layout.xml Complete} |
| 98 | * |
Romain Guy | d6a463a | 2009-05-21 23:10:10 -0700 | [diff] [blame] | 99 | * @attr ref android.R.styleable#ViewGroup_clipChildren |
| 100 | * @attr ref android.R.styleable#ViewGroup_clipToPadding |
| 101 | * @attr ref android.R.styleable#ViewGroup_layoutAnimation |
| 102 | * @attr ref android.R.styleable#ViewGroup_animationCache |
| 103 | * @attr ref android.R.styleable#ViewGroup_persistentDrawingCache |
| 104 | * @attr ref android.R.styleable#ViewGroup_alwaysDrawnWithCache |
| 105 | * @attr ref android.R.styleable#ViewGroup_addStatesFromChildren |
| 106 | * @attr ref android.R.styleable#ViewGroup_descendantFocusability |
Chet Haase | 13cc120 | 2010-09-03 15:39:20 -0700 | [diff] [blame] | 107 | * @attr ref android.R.styleable#ViewGroup_animateLayoutChanges |
Scott Main | 27a8508 | 2013-06-10 10:39:48 -0700 | [diff] [blame] | 108 | * @attr ref android.R.styleable#ViewGroup_splitMotionEvents |
| 109 | * @attr ref android.R.styleable#ViewGroup_layoutMode |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 110 | */ |
Tor Norbye | 83c6896 | 2015-03-10 20:55:31 -0700 | [diff] [blame] | 111 | @UiThread |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 112 | public abstract class ViewGroup extends View implements ViewParent, ViewManager { |
Adam Powell | 539ee87 | 2012-02-03 19:00:49 -0800 | [diff] [blame] | 113 | private static final String TAG = "ViewGroup"; |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 114 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 115 | private static final boolean DBG = false; |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 116 | /** @hide */ |
| 117 | public static boolean DEBUG_DRAW = false; |
Gilles Debunne | cea4513 | 2011-11-24 02:19:27 +0100 | [diff] [blame] | 118 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 119 | /** |
| 120 | * Views which have been hidden or removed which need to be animated on |
| 121 | * their way out. |
| 122 | * This field should be made private, so it is hidden from the SDK. |
| 123 | * {@hide} |
| 124 | */ |
| 125 | protected ArrayList<View> mDisappearingChildren; |
| 126 | |
| 127 | /** |
| 128 | * Listener used to propagate events indicating when children are added |
| 129 | * and/or removed from a view group. |
| 130 | * This field should be made private, so it is hidden from the SDK. |
| 131 | * {@hide} |
| 132 | */ |
| 133 | protected OnHierarchyChangeListener mOnHierarchyChangeListener; |
| 134 | |
| 135 | // The view contained within this ViewGroup that has or contains focus. |
| 136 | private View mFocused; |
| 137 | |
Chet Haase | 4846032 | 2010-06-11 14:22:25 -0700 | [diff] [blame] | 138 | /** |
| 139 | * A Transformation used when drawing children, to |
| 140 | * apply on the child being drawn. |
| 141 | */ |
Romain Guy | f699130 | 2013-06-05 17:19:01 -0700 | [diff] [blame] | 142 | private Transformation mChildTransformation; |
Chet Haase | 4846032 | 2010-06-11 14:22:25 -0700 | [diff] [blame] | 143 | |
| 144 | /** |
| 145 | * Used to track the current invalidation region. |
| 146 | */ |
Chet Haase | 64a48c1 | 2012-02-13 16:33:29 -0800 | [diff] [blame] | 147 | RectF mInvalidateRegion; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 148 | |
Chet Haase | 4846032 | 2010-06-11 14:22:25 -0700 | [diff] [blame] | 149 | /** |
| 150 | * A Transformation used to calculate a correct |
| 151 | * invalidation area when the application is autoscaled. |
| 152 | */ |
Chet Haase | 64a48c1 | 2012-02-13 16:33:29 -0800 | [diff] [blame] | 153 | Transformation mInvalidationTransformation; |
Chet Haase | 4846032 | 2010-06-11 14:22:25 -0700 | [diff] [blame] | 154 | |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 155 | // View currently under an ongoing drag. Can be null, a child or this window. |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 156 | private View mCurrentDragView; |
| 157 | |
Christopher Tate | 86cab1b | 2011-01-13 20:28:55 -0800 | [diff] [blame] | 158 | // Metadata about the ongoing drag |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 159 | private DragEvent mCurrentDragStartEvent; |
| 160 | private boolean mIsInterestedInDrag; |
| 161 | private HashSet<View> mChildrenInterestedInDrag; |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 162 | |
| 163 | // Used during drag dispatch |
Romain Guy | 6410c0a | 2013-06-17 11:21:58 -0700 | [diff] [blame] | 164 | private PointF mLocalPoint; |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 165 | |
Alan Viverette | b942b6f | 2014-12-08 10:37:39 -0800 | [diff] [blame] | 166 | // Lazily-created holder for point computations. |
| 167 | private float[] mTempPoint; |
| 168 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 169 | // Layout animation |
| 170 | private LayoutAnimationController mLayoutAnimationController; |
| 171 | private Animation.AnimationListener mAnimationListener; |
| 172 | |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 173 | // First touch target in the linked list of touch targets. |
| 174 | private TouchTarget mFirstTouchTarget; |
| 175 | |
Joe Onorato | 03ab0c7 | 2011-01-06 15:46:27 -0800 | [diff] [blame] | 176 | // For debugging only. You can see these in hierarchyviewer. |
Romain Guy | e95003e | 2011-01-09 13:53:06 -0800 | [diff] [blame] | 177 | @SuppressWarnings({"FieldCanBeLocal", "UnusedDeclaration"}) |
Joe Onorato | 03ab0c7 | 2011-01-06 15:46:27 -0800 | [diff] [blame] | 178 | @ViewDebug.ExportedProperty(category = "events") |
| 179 | private long mLastTouchDownTime; |
| 180 | @ViewDebug.ExportedProperty(category = "events") |
| 181 | private int mLastTouchDownIndex = -1; |
Romain Guy | e95003e | 2011-01-09 13:53:06 -0800 | [diff] [blame] | 182 | @SuppressWarnings({"FieldCanBeLocal", "UnusedDeclaration"}) |
Joe Onorato | 03ab0c7 | 2011-01-06 15:46:27 -0800 | [diff] [blame] | 183 | @ViewDebug.ExportedProperty(category = "events") |
| 184 | private float mLastTouchDownX; |
Romain Guy | e95003e | 2011-01-09 13:53:06 -0800 | [diff] [blame] | 185 | @SuppressWarnings({"FieldCanBeLocal", "UnusedDeclaration"}) |
Joe Onorato | 03ab0c7 | 2011-01-06 15:46:27 -0800 | [diff] [blame] | 186 | @ViewDebug.ExportedProperty(category = "events") |
| 187 | private float mLastTouchDownY; |
| 188 | |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 189 | // First hover target in the linked list of hover targets. |
| 190 | // The hover targets are children which have received ACTION_HOVER_ENTER. |
| 191 | // They might not have actually handled the hover event, but we will |
| 192 | // continue sending hover events to them as long as the pointer remains over |
| 193 | // their bounds and the view group does not intercept hover. |
| 194 | private HoverTarget mFirstHoverTarget; |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 195 | |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 196 | // True if the view group itself received a hover event. |
| 197 | // It might not have actually handled the hover event. |
| 198 | private boolean mHoveredSelf; |
| 199 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 200 | /** |
| 201 | * Internal flags. |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 202 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 203 | * This field should be made private, so it is hidden from the SDK. |
| 204 | * {@hide} |
| 205 | */ |
Romain Guy | 2440e67 | 2012-08-07 14:43:43 -0700 | [diff] [blame] | 206 | @ViewDebug.ExportedProperty(flagMapping = { |
| 207 | @ViewDebug.FlagToString(mask = FLAG_CLIP_CHILDREN, equals = FLAG_CLIP_CHILDREN, |
| 208 | name = "CLIP_CHILDREN"), |
| 209 | @ViewDebug.FlagToString(mask = FLAG_CLIP_TO_PADDING, equals = FLAG_CLIP_TO_PADDING, |
| 210 | name = "CLIP_TO_PADDING"), |
| 211 | @ViewDebug.FlagToString(mask = FLAG_PADDING_NOT_NULL, equals = FLAG_PADDING_NOT_NULL, |
| 212 | name = "PADDING_NOT_NULL") |
Jon Miranda | 4597e98 | 2014-07-29 07:25:49 -0700 | [diff] [blame] | 213 | }, formatToHexString = true) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 214 | protected int mGroupFlags; |
| 215 | |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 216 | /** |
| 217 | * Either {@link #LAYOUT_MODE_CLIP_BOUNDS} or {@link #LAYOUT_MODE_OPTICAL_BOUNDS}. |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 218 | */ |
Philip Milne | cfb631b | 2012-10-26 10:51:46 -0700 | [diff] [blame] | 219 | private int mLayoutMode = LAYOUT_MODE_UNDEFINED; |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 220 | |
Romain Guy | 33f6beb | 2012-02-16 19:24:51 -0800 | [diff] [blame] | 221 | /** |
| 222 | * NOTE: If you change the flags below make sure to reflect the changes |
| 223 | * the DisplayList class |
| 224 | */ |
Filip Gruszczynski | a33bdf3 | 2015-11-19 18:22:16 -0800 | [diff] [blame] | 225 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 226 | // When set, ViewGroup invalidates only the child's rectangle |
| 227 | // Set by default |
Chet Haase | 64a48c1 | 2012-02-13 16:33:29 -0800 | [diff] [blame] | 228 | static final int FLAG_CLIP_CHILDREN = 0x1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 229 | |
| 230 | // When set, ViewGroup excludes the padding area from the invalidate rectangle |
| 231 | // Set by default |
| 232 | private static final int FLAG_CLIP_TO_PADDING = 0x2; |
| 233 | |
| 234 | // When set, dispatchDraw() will invoke invalidate(); this is set by drawChild() when |
| 235 | // a child needs to be invalidated and FLAG_OPTIMIZE_INVALIDATE is set |
Chet Haase | 64a48c1 | 2012-02-13 16:33:29 -0800 | [diff] [blame] | 236 | static final int FLAG_INVALIDATE_REQUIRED = 0x4; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 237 | |
| 238 | // When set, dispatchDraw() will run the layout animation and unset the flag |
| 239 | private static final int FLAG_RUN_ANIMATION = 0x8; |
| 240 | |
| 241 | // When set, there is either no layout animation on the ViewGroup or the layout |
| 242 | // animation is over |
| 243 | // Set by default |
Chet Haase | 64a48c1 | 2012-02-13 16:33:29 -0800 | [diff] [blame] | 244 | static final int FLAG_ANIMATION_DONE = 0x10; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 245 | |
| 246 | // If set, this ViewGroup has padding; if unset there is no padding and we don't need |
| 247 | // to clip it, even if FLAG_CLIP_TO_PADDING is set |
| 248 | private static final int FLAG_PADDING_NOT_NULL = 0x20; |
| 249 | |
Chris Craik | 5a6bbae | 2015-04-10 17:41:34 -0700 | [diff] [blame] | 250 | /** @deprecated - functionality removed */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 251 | private static final int FLAG_ANIMATION_CACHE = 0x40; |
| 252 | |
| 253 | // When set, this ViewGroup converts calls to invalidate(Rect) to invalidate() during a |
| 254 | // layout animation; this avoid clobbering the hierarchy |
| 255 | // Automatically set when the layout animation starts, depending on the animation's |
| 256 | // characteristics |
Chet Haase | 64a48c1 | 2012-02-13 16:33:29 -0800 | [diff] [blame] | 257 | static final int FLAG_OPTIMIZE_INVALIDATE = 0x80; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 258 | |
| 259 | // When set, the next call to drawChild() will clear mChildTransformation's matrix |
Chet Haase | 64a48c1 | 2012-02-13 16:33:29 -0800 | [diff] [blame] | 260 | static final int FLAG_CLEAR_TRANSFORMATION = 0x100; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 261 | |
| 262 | // When set, this ViewGroup invokes mAnimationListener.onAnimationEnd() and removes |
| 263 | // the children's Bitmap caches if necessary |
| 264 | // This flag is set when the layout animation is over (after FLAG_ANIMATION_DONE is set) |
| 265 | private static final int FLAG_NOTIFY_ANIMATION_LISTENER = 0x200; |
| 266 | |
| 267 | /** |
| 268 | * When set, the drawing method will call {@link #getChildDrawingOrder(int, int)} |
| 269 | * to get the index of the child to draw for that iteration. |
Filip Gruszczynski | a33bdf3 | 2015-11-19 18:22:16 -0800 | [diff] [blame] | 270 | * |
Romain Guy | 293451e | 2009-11-04 13:59:48 -0800 | [diff] [blame] | 271 | * @hide |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 272 | */ |
| 273 | protected static final int FLAG_USE_CHILD_DRAWING_ORDER = 0x400; |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 274 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 275 | /** |
| 276 | * When set, this ViewGroup supports static transformations on children; this causes |
| 277 | * {@link #getChildStaticTransformation(View, android.view.animation.Transformation)} to be |
| 278 | * invoked when a child is drawn. |
| 279 | * |
| 280 | * Any subclass overriding |
| 281 | * {@link #getChildStaticTransformation(View, android.view.animation.Transformation)} should |
| 282 | * set this flags in {@link #mGroupFlags}. |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 283 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 284 | * {@hide} |
| 285 | */ |
| 286 | protected static final int FLAG_SUPPORT_STATIC_TRANSFORMATIONS = 0x800; |
| 287 | |
John Reck | fb5899d | 2014-08-15 18:51:27 -0700 | [diff] [blame] | 288 | // UNUSED FLAG VALUE: 0x1000; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 289 | |
| 290 | /** |
| 291 | * When set, this ViewGroup's drawable states also include those |
| 292 | * of its children. |
| 293 | */ |
| 294 | private static final int FLAG_ADD_STATES_FROM_CHILDREN = 0x2000; |
| 295 | |
Chris Craik | 5a6bbae | 2015-04-10 17:41:34 -0700 | [diff] [blame] | 296 | /** @deprecated functionality removed */ |
| 297 | private static final int FLAG_ALWAYS_DRAWN_WITH_CACHE = 0x4000; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 298 | |
Chris Craik | 5a6bbae | 2015-04-10 17:41:34 -0700 | [diff] [blame] | 299 | /** @deprecated functionality removed */ |
| 300 | private static final int FLAG_CHILDREN_DRAWN_WITH_CACHE = 0x8000; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 301 | |
| 302 | /** |
| 303 | * When set, this group will go through its list of children to notify them of |
| 304 | * any drawable state change. |
| 305 | */ |
| 306 | private static final int FLAG_NOTIFY_CHILDREN_ON_DRAWABLE_STATE_CHANGE = 0x10000; |
| 307 | |
| 308 | private static final int FLAG_MASK_FOCUSABILITY = 0x60000; |
| 309 | |
| 310 | /** |
| 311 | * This view will get focus before any of its descendants. |
| 312 | */ |
| 313 | public static final int FOCUS_BEFORE_DESCENDANTS = 0x20000; |
| 314 | |
| 315 | /** |
| 316 | * This view will get focus only if none of its descendants want it. |
| 317 | */ |
| 318 | public static final int FOCUS_AFTER_DESCENDANTS = 0x40000; |
| 319 | |
| 320 | /** |
| 321 | * This view will block any of its descendants from getting focus, even |
| 322 | * if they are focusable. |
| 323 | */ |
| 324 | public static final int FOCUS_BLOCK_DESCENDANTS = 0x60000; |
| 325 | |
| 326 | /** |
| 327 | * Used to map between enum in attrubutes and flag values. |
| 328 | */ |
| 329 | private static final int[] DESCENDANT_FOCUSABILITY_FLAGS = |
| 330 | {FOCUS_BEFORE_DESCENDANTS, FOCUS_AFTER_DESCENDANTS, |
| 331 | FOCUS_BLOCK_DESCENDANTS}; |
| 332 | |
| 333 | /** |
| 334 | * When set, this ViewGroup should not intercept touch events. |
Adam Powell | 110486f | 2010-06-22 17:14:44 -0700 | [diff] [blame] | 335 | * {@hide} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 336 | */ |
Adam Powell | 110486f | 2010-06-22 17:14:44 -0700 | [diff] [blame] | 337 | protected static final int FLAG_DISALLOW_INTERCEPT = 0x80000; |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 338 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 339 | /** |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 340 | * When set, this ViewGroup will split MotionEvents to multiple child Views when appropriate. |
| 341 | */ |
Adam Powell | f37df07 | 2010-09-17 16:22:49 -0700 | [diff] [blame] | 342 | private static final int FLAG_SPLIT_MOTION_EVENTS = 0x200000; |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 343 | |
| 344 | /** |
Adam Powell | 4b86788 | 2011-09-16 12:59:46 -0700 | [diff] [blame] | 345 | * When set, this ViewGroup will not dispatch onAttachedToWindow calls |
| 346 | * to children when adding new views. This is used to prevent multiple |
| 347 | * onAttached calls when a ViewGroup adds children in its own onAttached method. |
| 348 | */ |
| 349 | private static final int FLAG_PREVENT_DISPATCH_ATTACHED_TO_WINDOW = 0x400000; |
| 350 | |
| 351 | /** |
Philip Milne | cfb631b | 2012-10-26 10:51:46 -0700 | [diff] [blame] | 352 | * When true, indicates that a layoutMode has been explicitly set, either with |
| 353 | * an explicit call to {@link #setLayoutMode(int)} in code or from an XML resource. |
| 354 | * This distinguishes the situation in which a layout mode was inherited from |
| 355 | * one of the ViewGroup's ancestors and cached locally. |
| 356 | */ |
| 357 | private static final int FLAG_LAYOUT_MODE_WAS_EXPLICITLY_SET = 0x800000; |
| 358 | |
Chris Craik | b49f446 | 2014-03-20 12:44:20 -0700 | [diff] [blame] | 359 | static final int FLAG_IS_TRANSITION_GROUP = 0x1000000; |
Chris Craik | d863a10 | 2013-12-19 13:31:15 -0800 | [diff] [blame] | 360 | |
Chris Craik | b49f446 | 2014-03-20 12:44:20 -0700 | [diff] [blame] | 361 | static final int FLAG_IS_TRANSITION_GROUP_SET = 0x2000000; |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 362 | |
Chris Craik | d863a10 | 2013-12-19 13:31:15 -0800 | [diff] [blame] | 363 | /** |
Adam Powell | ff0d298 | 2014-07-10 20:34:14 -0700 | [diff] [blame] | 364 | * When set, focus will not be permitted to enter this group if a touchscreen is present. |
| 365 | */ |
| 366 | static final int FLAG_TOUCHSCREEN_BLOCKS_FOCUS = 0x4000000; |
| 367 | |
| 368 | /** |
Clara Bayarri | 4423d91 | 2015-03-02 19:42:48 +0000 | [diff] [blame] | 369 | * When true, indicates that a call to startActionModeForChild was made with the type parameter |
| 370 | * and should not be ignored. This helps in backwards compatibility with the existing method |
| 371 | * without a type. |
| 372 | * |
| 373 | * @see #startActionModeForChild(View, android.view.ActionMode.Callback) |
| 374 | * @see #startActionModeForChild(View, android.view.ActionMode.Callback, int) |
| 375 | */ |
| 376 | private static final int FLAG_START_ACTION_MODE_FOR_CHILD_IS_TYPED = 0x8000000; |
| 377 | |
| 378 | /** |
| 379 | * When true, indicates that a call to startActionModeForChild was made without the type |
| 380 | * parameter. This helps in backwards compatibility with the existing method |
| 381 | * without a type. |
| 382 | * |
| 383 | * @see #startActionModeForChild(View, android.view.ActionMode.Callback) |
| 384 | * @see #startActionModeForChild(View, android.view.ActionMode.Callback, int) |
| 385 | */ |
| 386 | private static final int FLAG_START_ACTION_MODE_FOR_CHILD_IS_NOT_TYPED = 0x10000000; |
| 387 | |
| 388 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 389 | * Indicates which types of drawing caches are to be kept in memory. |
| 390 | * This field should be made private, so it is hidden from the SDK. |
| 391 | * {@hide} |
| 392 | */ |
| 393 | protected int mPersistentDrawingCache; |
| 394 | |
| 395 | /** |
| 396 | * Used to indicate that no drawing cache should be kept in memory. |
| 397 | */ |
| 398 | public static final int PERSISTENT_NO_CACHE = 0x0; |
| 399 | |
| 400 | /** |
| 401 | * Used to indicate that the animation drawing cache should be kept in memory. |
| 402 | */ |
| 403 | public static final int PERSISTENT_ANIMATION_CACHE = 0x1; |
| 404 | |
| 405 | /** |
| 406 | * Used to indicate that the scrolling drawing cache should be kept in memory. |
| 407 | */ |
| 408 | public static final int PERSISTENT_SCROLLING_CACHE = 0x2; |
| 409 | |
| 410 | /** |
| 411 | * Used to indicate that all drawing caches should be kept in memory. |
| 412 | */ |
| 413 | public static final int PERSISTENT_ALL_CACHES = 0x3; |
| 414 | |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 415 | // Layout Modes |
| 416 | |
Philip Milne | cfb631b | 2012-10-26 10:51:46 -0700 | [diff] [blame] | 417 | private static final int LAYOUT_MODE_UNDEFINED = -1; |
| 418 | |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 419 | /** |
| 420 | * This constant is a {@link #setLayoutMode(int) layoutMode}. |
Philip Milne | 7a23b49 | 2012-04-24 22:12:36 -0700 | [diff] [blame] | 421 | * 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] | 422 | * {@link #getRight() right} and {@link #getBottom() bottom}. |
| 423 | */ |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 424 | public static final int LAYOUT_MODE_CLIP_BOUNDS = 0; |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 425 | |
| 426 | /** |
| 427 | * This constant is a {@link #setLayoutMode(int) layoutMode}. |
Philip Milne | 7a23b49 | 2012-04-24 22:12:36 -0700 | [diff] [blame] | 428 | * Optical bounds describe where a widget appears to be. They sit inside the clip |
| 429 | * bounds which need to cover a larger area to allow other effects, |
| 430 | * such as shadows and glows, to be drawn. |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 431 | */ |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 432 | public static final int LAYOUT_MODE_OPTICAL_BOUNDS = 1; |
| 433 | |
| 434 | /** @hide */ |
Philip Milne | cfb631b | 2012-10-26 10:51:46 -0700 | [diff] [blame] | 435 | public static int LAYOUT_MODE_DEFAULT = LAYOUT_MODE_CLIP_BOUNDS; |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 436 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 437 | /** |
| 438 | * We clip to padding when FLAG_CLIP_TO_PADDING and FLAG_PADDING_NOT_NULL |
| 439 | * are set at the same time. |
| 440 | */ |
| 441 | protected static final int CLIP_TO_PADDING_MASK = FLAG_CLIP_TO_PADDING | FLAG_PADDING_NOT_NULL; |
| 442 | |
| 443 | // Index of the child's left position in the mLocation array |
| 444 | private static final int CHILD_LEFT_INDEX = 0; |
| 445 | // Index of the child's top position in the mLocation array |
| 446 | private static final int CHILD_TOP_INDEX = 1; |
| 447 | |
| 448 | // Child views of this ViewGroup |
| 449 | private View[] mChildren; |
| 450 | // Number of valid children in the mChildren array, the rest should be null or not |
| 451 | // considered as children |
| 452 | private int mChildrenCount; |
| 453 | |
Chet Haase | b989502 | 2013-04-02 15:10:58 -0700 | [diff] [blame] | 454 | // Whether layout calls are currently being suppressed, controlled by calls to |
| 455 | // suppressLayout() |
| 456 | boolean mSuppressLayout = false; |
| 457 | |
| 458 | // Whether any layout calls have actually been suppressed while mSuppressLayout |
| 459 | // has been true. This tracks whether we need to issue a requestLayout() when |
| 460 | // layout is later re-enabled. |
| 461 | private boolean mLayoutCalledWhileSuppressed = false; |
| 462 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 463 | private static final int ARRAY_INITIAL_CAPACITY = 12; |
| 464 | private static final int ARRAY_CAPACITY_INCREMENT = 12; |
| 465 | |
Romain Guy | cbc6774 | 2012-04-27 16:12:57 -0700 | [diff] [blame] | 466 | private static Paint sDebugPaint; |
| 467 | private static float[] sDebugLines; |
Philip Milne | 604f440 | 2012-04-24 19:27:11 -0700 | [diff] [blame] | 468 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 469 | // Used to draw cached views |
Chet Haase | 64a48c1 | 2012-02-13 16:33:29 -0800 | [diff] [blame] | 470 | Paint mCachePaint; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 471 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 472 | // Used to animate add/remove changes in layout |
| 473 | private LayoutTransition mTransition; |
| 474 | |
| 475 | // The set of views that are currently being transitioned. This list is used to track views |
| 476 | // being removed that should not actually be removed from the parent yet because they are |
| 477 | // being animated. |
| 478 | private ArrayList<View> mTransitioningViews; |
| 479 | |
Chet Haase | 5e25c2c | 2010-09-16 11:15:56 -0700 | [diff] [blame] | 480 | // List of children changing visibility. This is used to potentially keep rendering |
| 481 | // views during a transition when they otherwise would have become gone/invisible |
| 482 | private ArrayList<View> mVisibilityChangingChildren; |
| 483 | |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 484 | // Temporary holder of presorted children, only used for |
| 485 | // input/software draw dispatch for correctly Z ordering. |
| 486 | private ArrayList<View> mPreSortedChildren; |
| 487 | |
Adam Powell | 539ee87 | 2012-02-03 19:00:49 -0800 | [diff] [blame] | 488 | // Indicates how many of this container's child subtrees contain transient state |
| 489 | @ViewDebug.ExportedProperty(category = "layout") |
| 490 | private int mChildCountWithTransientState = 0; |
| 491 | |
Adam Powell | 10ba277 | 2014-04-15 09:46:51 -0700 | [diff] [blame] | 492 | /** |
| 493 | * Currently registered axes for nested scrolling. Flag set consisting of |
| 494 | * {@link #SCROLL_AXIS_HORIZONTAL} {@link #SCROLL_AXIS_VERTICAL} or {@link #SCROLL_AXIS_NONE} |
| 495 | * for null. |
| 496 | */ |
| 497 | private int mNestedScrollAxes; |
| 498 | |
Chet Haase | c633d2f | 2015-04-07 10:29:39 -0700 | [diff] [blame] | 499 | // Used to manage the list of transient views, added by addTransientView() |
| 500 | private List<Integer> mTransientIndices = null; |
| 501 | private List<View> mTransientViews = null; |
| 502 | |
| 503 | |
Clara Bayarri | 4423d91 | 2015-03-02 19:42:48 +0000 | [diff] [blame] | 504 | /** |
| 505 | * Empty ActionMode used as a sentinel in recursive entries to startActionModeForChild. |
| 506 | * |
| 507 | * @see #startActionModeForChild(View, android.view.ActionMode.Callback) |
| 508 | * @see #startActionModeForChild(View, android.view.ActionMode.Callback, int) |
| 509 | */ |
| 510 | private static final ActionMode SENTINEL_ACTION_MODE = new ActionMode() { |
| 511 | @Override |
| 512 | public void setTitle(CharSequence title) {} |
| 513 | |
| 514 | @Override |
| 515 | public void setTitle(int resId) {} |
| 516 | |
| 517 | @Override |
| 518 | public void setSubtitle(CharSequence subtitle) {} |
| 519 | |
| 520 | @Override |
| 521 | public void setSubtitle(int resId) {} |
| 522 | |
| 523 | @Override |
| 524 | public void setCustomView(View view) {} |
| 525 | |
| 526 | @Override |
| 527 | public void invalidate() {} |
| 528 | |
| 529 | @Override |
| 530 | public void finish() {} |
| 531 | |
| 532 | @Override |
| 533 | public Menu getMenu() { |
| 534 | return null; |
| 535 | } |
| 536 | |
| 537 | @Override |
| 538 | public CharSequence getTitle() { |
| 539 | return null; |
| 540 | } |
| 541 | |
| 542 | @Override |
| 543 | public CharSequence getSubtitle() { |
| 544 | return null; |
| 545 | } |
| 546 | |
| 547 | @Override |
| 548 | public View getCustomView() { |
| 549 | return null; |
| 550 | } |
| 551 | |
| 552 | @Override |
| 553 | public MenuInflater getMenuInflater() { |
| 554 | return null; |
| 555 | } |
| 556 | }; |
| 557 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 558 | public ViewGroup(Context context) { |
Alan Viverette | d6479ec | 2013-09-10 17:03:02 -0700 | [diff] [blame] | 559 | this(context, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | public ViewGroup(Context context, AttributeSet attrs) { |
Alan Viverette | d6479ec | 2013-09-10 17:03:02 -0700 | [diff] [blame] | 563 | this(context, attrs, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 564 | } |
| 565 | |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 566 | public ViewGroup(Context context, AttributeSet attrs, int defStyleAttr) { |
Alan Viverette | d6479ec | 2013-09-10 17:03:02 -0700 | [diff] [blame] | 567 | this(context, attrs, defStyleAttr, 0); |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 568 | } |
| 569 | |
| 570 | public ViewGroup(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 571 | super(context, attrs, defStyleAttr, defStyleRes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 572 | initViewGroup(); |
Alan Viverette | d6479ec | 2013-09-10 17:03:02 -0700 | [diff] [blame] | 573 | initFromAttributes(context, attrs, defStyleAttr, defStyleRes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 574 | } |
| 575 | |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 576 | private boolean debugDraw() { |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 577 | return DEBUG_DRAW || mAttachInfo != null && mAttachInfo.mDebugLayout; |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 578 | } |
| 579 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 580 | private void initViewGroup() { |
| 581 | // ViewGroup doesn't draw by default |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 582 | if (!debugDraw()) { |
| 583 | setFlags(WILL_NOT_DRAW, DRAW_MASK); |
| 584 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 585 | mGroupFlags |= FLAG_CLIP_CHILDREN; |
| 586 | mGroupFlags |= FLAG_CLIP_TO_PADDING; |
| 587 | mGroupFlags |= FLAG_ANIMATION_DONE; |
Chris Craik | f6ce8fd | 2015-05-11 15:33:11 -0700 | [diff] [blame] | 588 | mGroupFlags |= FLAG_ANIMATION_CACHE; |
| 589 | mGroupFlags |= FLAG_ALWAYS_DRAWN_WITH_CACHE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 590 | |
Jeff Brown | 995e774 | 2010-12-22 16:59:36 -0800 | [diff] [blame] | 591 | if (mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.HONEYCOMB) { |
| 592 | mGroupFlags |= FLAG_SPLIT_MOTION_EVENTS; |
| 593 | } |
| 594 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 595 | setDescendantFocusability(FOCUS_BEFORE_DESCENDANTS); |
| 596 | |
| 597 | mChildren = new View[ARRAY_INITIAL_CAPACITY]; |
| 598 | mChildrenCount = 0; |
| 599 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 600 | mPersistentDrawingCache = PERSISTENT_SCROLLING_CACHE; |
| 601 | } |
| 602 | |
Alan Viverette | d6479ec | 2013-09-10 17:03:02 -0700 | [diff] [blame] | 603 | private void initFromAttributes( |
| 604 | Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
Filip Gruszczyński | b50cea0 | 2014-03-05 17:54:58 -0800 | [diff] [blame] | 605 | final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ViewGroup, defStyleAttr, |
| 606 | defStyleRes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 607 | |
| 608 | final int N = a.getIndexCount(); |
| 609 | for (int i = 0; i < N; i++) { |
| 610 | int attr = a.getIndex(i); |
| 611 | switch (attr) { |
| 612 | case R.styleable.ViewGroup_clipChildren: |
| 613 | setClipChildren(a.getBoolean(attr, true)); |
| 614 | break; |
| 615 | case R.styleable.ViewGroup_clipToPadding: |
| 616 | setClipToPadding(a.getBoolean(attr, true)); |
| 617 | break; |
| 618 | case R.styleable.ViewGroup_animationCache: |
| 619 | setAnimationCacheEnabled(a.getBoolean(attr, true)); |
| 620 | break; |
| 621 | case R.styleable.ViewGroup_persistentDrawingCache: |
| 622 | setPersistentDrawingCache(a.getInt(attr, PERSISTENT_SCROLLING_CACHE)); |
| 623 | break; |
| 624 | case R.styleable.ViewGroup_addStatesFromChildren: |
| 625 | setAddStatesFromChildren(a.getBoolean(attr, false)); |
| 626 | break; |
| 627 | case R.styleable.ViewGroup_alwaysDrawnWithCache: |
| 628 | setAlwaysDrawnWithCacheEnabled(a.getBoolean(attr, true)); |
| 629 | break; |
| 630 | case R.styleable.ViewGroup_layoutAnimation: |
| 631 | int id = a.getResourceId(attr, -1); |
| 632 | if (id > 0) { |
| 633 | setLayoutAnimation(AnimationUtils.loadLayoutAnimation(mContext, id)); |
| 634 | } |
| 635 | break; |
| 636 | case R.styleable.ViewGroup_descendantFocusability: |
| 637 | setDescendantFocusability(DESCENDANT_FOCUSABILITY_FLAGS[a.getInt(attr, 0)]); |
| 638 | break; |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 639 | case R.styleable.ViewGroup_splitMotionEvents: |
| 640 | setMotionEventSplittingEnabled(a.getBoolean(attr, false)); |
| 641 | break; |
Chet Haase | 13cc120 | 2010-09-03 15:39:20 -0700 | [diff] [blame] | 642 | case R.styleable.ViewGroup_animateLayoutChanges: |
| 643 | boolean animateLayoutChanges = a.getBoolean(attr, false); |
| 644 | if (animateLayoutChanges) { |
| 645 | setLayoutTransition(new LayoutTransition()); |
| 646 | } |
| 647 | break; |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 648 | case R.styleable.ViewGroup_layoutMode: |
Philip Milne | cfb631b | 2012-10-26 10:51:46 -0700 | [diff] [blame] | 649 | setLayoutMode(a.getInt(attr, LAYOUT_MODE_UNDEFINED)); |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 650 | break; |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 651 | case R.styleable.ViewGroup_transitionGroup: |
| 652 | setTransitionGroup(a.getBoolean(attr, false)); |
| 653 | break; |
Adam Powell | ff0d298 | 2014-07-10 20:34:14 -0700 | [diff] [blame] | 654 | case R.styleable.ViewGroup_touchscreenBlocksFocus: |
| 655 | setTouchscreenBlocksFocus(a.getBoolean(attr, false)); |
| 656 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 657 | } |
| 658 | } |
| 659 | |
| 660 | a.recycle(); |
| 661 | } |
| 662 | |
| 663 | /** |
| 664 | * Gets the descendant focusability of this view group. The descendant |
| 665 | * focusability defines the relationship between this view group and its |
| 666 | * descendants when looking for a view to take focus in |
| 667 | * {@link #requestFocus(int, android.graphics.Rect)}. |
| 668 | * |
| 669 | * @return one of {@link #FOCUS_BEFORE_DESCENDANTS}, {@link #FOCUS_AFTER_DESCENDANTS}, |
| 670 | * {@link #FOCUS_BLOCK_DESCENDANTS}. |
| 671 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 672 | @ViewDebug.ExportedProperty(category = "focus", mapping = { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 673 | @ViewDebug.IntToString(from = FOCUS_BEFORE_DESCENDANTS, to = "FOCUS_BEFORE_DESCENDANTS"), |
| 674 | @ViewDebug.IntToString(from = FOCUS_AFTER_DESCENDANTS, to = "FOCUS_AFTER_DESCENDANTS"), |
| 675 | @ViewDebug.IntToString(from = FOCUS_BLOCK_DESCENDANTS, to = "FOCUS_BLOCK_DESCENDANTS") |
| 676 | }) |
| 677 | public int getDescendantFocusability() { |
| 678 | return mGroupFlags & FLAG_MASK_FOCUSABILITY; |
| 679 | } |
| 680 | |
| 681 | /** |
| 682 | * Set the descendant focusability of this view group. This defines the relationship |
| 683 | * between this view group and its descendants when looking for a view to |
| 684 | * take focus in {@link #requestFocus(int, android.graphics.Rect)}. |
| 685 | * |
| 686 | * @param focusability one of {@link #FOCUS_BEFORE_DESCENDANTS}, {@link #FOCUS_AFTER_DESCENDANTS}, |
| 687 | * {@link #FOCUS_BLOCK_DESCENDANTS}. |
| 688 | */ |
| 689 | public void setDescendantFocusability(int focusability) { |
| 690 | switch (focusability) { |
| 691 | case FOCUS_BEFORE_DESCENDANTS: |
| 692 | case FOCUS_AFTER_DESCENDANTS: |
| 693 | case FOCUS_BLOCK_DESCENDANTS: |
| 694 | break; |
| 695 | default: |
| 696 | throw new IllegalArgumentException("must be one of FOCUS_BEFORE_DESCENDANTS, " |
| 697 | + "FOCUS_AFTER_DESCENDANTS, FOCUS_BLOCK_DESCENDANTS"); |
| 698 | } |
| 699 | mGroupFlags &= ~FLAG_MASK_FOCUSABILITY; |
| 700 | mGroupFlags |= (focusability & FLAG_MASK_FOCUSABILITY); |
| 701 | } |
| 702 | |
| 703 | /** |
| 704 | * {@inheritDoc} |
| 705 | */ |
| 706 | @Override |
| 707 | void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) { |
| 708 | if (mFocused != null) { |
Alan Viverette | 223622a | 2013-12-17 13:29:02 -0800 | [diff] [blame] | 709 | mFocused.unFocus(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 710 | mFocused = null; |
| 711 | } |
| 712 | super.handleFocusGainInternal(direction, previouslyFocusedRect); |
| 713 | } |
| 714 | |
| 715 | /** |
| 716 | * {@inheritDoc} |
| 717 | */ |
| 718 | public void requestChildFocus(View child, View focused) { |
| 719 | if (DBG) { |
| 720 | System.out.println(this + " requestChildFocus()"); |
| 721 | } |
| 722 | if (getDescendantFocusability() == FOCUS_BLOCK_DESCENDANTS) { |
| 723 | return; |
| 724 | } |
| 725 | |
| 726 | // Unfocus us, if necessary |
Alan Viverette | 223622a | 2013-12-17 13:29:02 -0800 | [diff] [blame] | 727 | super.unFocus(focused); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 728 | |
| 729 | // We had a previous notion of who had focus. Clear it. |
| 730 | if (mFocused != child) { |
| 731 | if (mFocused != null) { |
Alan Viverette | 223622a | 2013-12-17 13:29:02 -0800 | [diff] [blame] | 732 | mFocused.unFocus(focused); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | mFocused = child; |
| 736 | } |
| 737 | if (mParent != null) { |
| 738 | mParent.requestChildFocus(this, focused); |
| 739 | } |
| 740 | } |
| 741 | |
| 742 | /** |
| 743 | * {@inheritDoc} |
| 744 | */ |
| 745 | public void focusableViewAvailable(View v) { |
| 746 | if (mParent != null |
| 747 | // shortcut: don't report a new focusable view if we block our descendants from |
| 748 | // getting focus |
| 749 | && (getDescendantFocusability() != FOCUS_BLOCK_DESCENDANTS) |
Adam Powell | 88c1175 | 2014-07-21 17:19:16 -0700 | [diff] [blame] | 750 | && (isFocusableInTouchMode() || !shouldBlockFocusForTouchscreen()) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 751 | // shortcut: don't report a new focusable view if we already are focused |
| 752 | // (and we don't prefer our descendants) |
| 753 | // |
| 754 | // note: knowing that mFocused is non-null is not a good enough reason |
| 755 | // to break the traversal since in that case we'd actually have to find |
| 756 | // the focused view and make sure it wasn't FOCUS_AFTER_DESCENDANTS and |
Joe Onorato | c6cc0f8 | 2011-04-12 11:53:13 -0700 | [diff] [blame] | 757 | // an ancestor of v; this will get checked for at ViewAncestor |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 758 | && !(isFocused() && getDescendantFocusability() != FOCUS_AFTER_DESCENDANTS)) { |
| 759 | mParent.focusableViewAvailable(v); |
| 760 | } |
| 761 | } |
| 762 | |
| 763 | /** |
| 764 | * {@inheritDoc} |
| 765 | */ |
| 766 | public boolean showContextMenuForChild(View originalView) { |
| 767 | return mParent != null && mParent.showContextMenuForChild(originalView); |
| 768 | } |
| 769 | |
Oren Blasberg | ed39126 | 2015-09-01 12:12:51 -0700 | [diff] [blame] | 770 | @Override |
| 771 | public boolean showContextMenuForChild(View originalView, float x, float y) { |
| 772 | return mParent != null && mParent.showContextMenuForChild(originalView, x, y); |
| 773 | } |
| 774 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 775 | /** |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 776 | * {@inheritDoc} |
| 777 | */ |
Clara Bayarri | 4423d91 | 2015-03-02 19:42:48 +0000 | [diff] [blame] | 778 | @Override |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 779 | public ActionMode startActionModeForChild(View originalView, ActionMode.Callback callback) { |
Clara Bayarri | 4423d91 | 2015-03-02 19:42:48 +0000 | [diff] [blame] | 780 | if ((mGroupFlags & FLAG_START_ACTION_MODE_FOR_CHILD_IS_TYPED) == 0) { |
| 781 | // This is the original call. |
| 782 | try { |
| 783 | mGroupFlags |= FLAG_START_ACTION_MODE_FOR_CHILD_IS_NOT_TYPED; |
| 784 | return startActionModeForChild(originalView, callback, ActionMode.TYPE_PRIMARY); |
| 785 | } finally { |
| 786 | mGroupFlags &= ~FLAG_START_ACTION_MODE_FOR_CHILD_IS_NOT_TYPED; |
| 787 | } |
| 788 | } else { |
| 789 | // We are being called from the new method with type. |
| 790 | return SENTINEL_ACTION_MODE; |
| 791 | } |
| 792 | } |
| 793 | |
| 794 | /** |
| 795 | * {@inheritDoc} |
| 796 | */ |
| 797 | @Override |
| 798 | public ActionMode startActionModeForChild( |
| 799 | View originalView, ActionMode.Callback callback, int type) { |
Adam Powell | e9fd6d2 | 2015-06-01 11:26:32 -0700 | [diff] [blame] | 800 | if ((mGroupFlags & FLAG_START_ACTION_MODE_FOR_CHILD_IS_NOT_TYPED) == 0 |
| 801 | && type == ActionMode.TYPE_PRIMARY) { |
Clara Bayarri | 4423d91 | 2015-03-02 19:42:48 +0000 | [diff] [blame] | 802 | ActionMode mode; |
| 803 | try { |
| 804 | mGroupFlags |= FLAG_START_ACTION_MODE_FOR_CHILD_IS_TYPED; |
| 805 | mode = startActionModeForChild(originalView, callback); |
| 806 | } finally { |
| 807 | mGroupFlags &= ~FLAG_START_ACTION_MODE_FOR_CHILD_IS_TYPED; |
| 808 | } |
| 809 | if (mode != SENTINEL_ACTION_MODE) { |
| 810 | return mode; |
| 811 | } |
| 812 | } |
| 813 | if (mParent != null) { |
| 814 | try { |
| 815 | return mParent.startActionModeForChild(originalView, callback, type); |
| 816 | } catch (AbstractMethodError ame) { |
| 817 | // Custom view parents might not implement this method. |
| 818 | return mParent.startActionModeForChild(originalView, callback); |
| 819 | } |
| 820 | } |
| 821 | return null; |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 822 | } |
| 823 | |
| 824 | /** |
Clara Bayarri | d5bf3ed | 2015-03-27 17:32:45 +0000 | [diff] [blame] | 825 | * @hide |
| 826 | */ |
| 827 | @Override |
| 828 | public boolean dispatchActivityResult( |
| 829 | String who, int requestCode, int resultCode, Intent data) { |
| 830 | if (super.dispatchActivityResult(who, requestCode, resultCode, data)) { |
| 831 | return true; |
| 832 | } |
| 833 | int childCount = getChildCount(); |
| 834 | for (int i = 0; i < childCount; i++) { |
| 835 | View child = getChildAt(i); |
| 836 | if (child.dispatchActivityResult(who, requestCode, resultCode, data)) { |
| 837 | return true; |
| 838 | } |
| 839 | } |
| 840 | return false; |
| 841 | } |
| 842 | |
| 843 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 844 | * Find the nearest view in the specified direction that wants to take |
| 845 | * focus. |
| 846 | * |
| 847 | * @param focused The view that currently has focus |
| 848 | * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and |
| 849 | * FOCUS_RIGHT, or 0 for not applicable. |
| 850 | */ |
| 851 | public View focusSearch(View focused, int direction) { |
Svetoslav Ganov | 27e2da7 | 2012-07-02 18:12:00 -0700 | [diff] [blame] | 852 | if (isRootNamespace()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 853 | // root namespace means we should consider ourselves the top of the |
| 854 | // tree for focus searching; otherwise we could be focus searching |
| 855 | // into other tabs. see LocalActivityManager and TabHost for more info |
| 856 | return FocusFinder.getInstance().findNextFocus(this, focused, direction); |
| 857 | } else if (mParent != null) { |
| 858 | return mParent.focusSearch(focused, direction); |
| 859 | } |
| 860 | return null; |
| 861 | } |
| 862 | |
| 863 | /** |
| 864 | * {@inheritDoc} |
| 865 | */ |
| 866 | public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) { |
| 867 | return false; |
| 868 | } |
| 869 | |
| 870 | /** |
| 871 | * {@inheritDoc} |
| 872 | */ |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 873 | @Override |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 874 | public boolean requestSendAccessibilityEvent(View child, AccessibilityEvent event) { |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 875 | ViewParent parent = mParent; |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 876 | if (parent == null) { |
| 877 | return false; |
| 878 | } |
| 879 | final boolean propagate = onRequestSendAccessibilityEvent(child, event); |
| 880 | if (!propagate) { |
| 881 | return false; |
| 882 | } |
| 883 | return parent.requestSendAccessibilityEvent(this, event); |
| 884 | } |
| 885 | |
| 886 | /** |
| 887 | * Called when a child has requested sending an {@link AccessibilityEvent} and |
| 888 | * gives an opportunity to its parent to augment the event. |
Svetoslav Ganov | 031d9c1 | 2011-09-09 16:41:13 -0700 | [diff] [blame] | 889 | * <p> |
Adam Powell | 2fcbbd0 | 2011-09-28 18:56:43 -0700 | [diff] [blame] | 890 | * If an {@link android.view.View.AccessibilityDelegate} has been specified via calling |
| 891 | * {@link android.view.View#setAccessibilityDelegate(android.view.View.AccessibilityDelegate)} its |
| 892 | * {@link android.view.View.AccessibilityDelegate#onRequestSendAccessibilityEvent(ViewGroup, View, AccessibilityEvent)} |
Svetoslav Ganov | 031d9c1 | 2011-09-09 16:41:13 -0700 | [diff] [blame] | 893 | * is responsible for handling this call. |
| 894 | * </p> |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 895 | * |
| 896 | * @param child The child which requests sending the event. |
| 897 | * @param event The event to be sent. |
| 898 | * @return True if the event should be sent. |
| 899 | * |
| 900 | * @see #requestSendAccessibilityEvent(View, AccessibilityEvent) |
| 901 | */ |
| 902 | public boolean onRequestSendAccessibilityEvent(View child, AccessibilityEvent event) { |
Svetoslav Ganov | 031d9c1 | 2011-09-09 16:41:13 -0700 | [diff] [blame] | 903 | if (mAccessibilityDelegate != null) { |
| 904 | return mAccessibilityDelegate.onRequestSendAccessibilityEvent(this, child, event); |
| 905 | } else { |
| 906 | return onRequestSendAccessibilityEventInternal(child, event); |
| 907 | } |
| 908 | } |
| 909 | |
| 910 | /** |
| 911 | * @see #onRequestSendAccessibilityEvent(View, AccessibilityEvent) |
| 912 | * |
| 913 | * Note: Called from the default {@link View.AccessibilityDelegate}. |
Alan Viverette | a54956a | 2015-01-07 16:05:02 -0800 | [diff] [blame] | 914 | * |
| 915 | * @hide |
Svetoslav Ganov | 031d9c1 | 2011-09-09 16:41:13 -0700 | [diff] [blame] | 916 | */ |
Alan Viverette | a54956a | 2015-01-07 16:05:02 -0800 | [diff] [blame] | 917 | public boolean onRequestSendAccessibilityEventInternal(View child, AccessibilityEvent event) { |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 918 | return true; |
| 919 | } |
| 920 | |
| 921 | /** |
Adam Powell | 539ee87 | 2012-02-03 19:00:49 -0800 | [diff] [blame] | 922 | * 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] | 923 | */ |
| 924 | public void childHasTransientStateChanged(View child, boolean childHasTransientState) { |
| 925 | final boolean oldHasTransientState = hasTransientState(); |
| 926 | if (childHasTransientState) { |
| 927 | mChildCountWithTransientState++; |
| 928 | } else { |
| 929 | mChildCountWithTransientState--; |
| 930 | } |
| 931 | |
| 932 | final boolean newHasTransientState = hasTransientState(); |
| 933 | if (mParent != null && oldHasTransientState != newHasTransientState) { |
| 934 | try { |
| 935 | mParent.childHasTransientStateChanged(this, newHasTransientState); |
| 936 | } catch (AbstractMethodError e) { |
| 937 | Log.e(TAG, mParent.getClass().getSimpleName() + |
| 938 | " does not fully implement ViewParent", e); |
| 939 | } |
| 940 | } |
| 941 | } |
| 942 | |
Adam Powell | 539ee87 | 2012-02-03 19:00:49 -0800 | [diff] [blame] | 943 | @Override |
| 944 | public boolean hasTransientState() { |
| 945 | return mChildCountWithTransientState > 0 || super.hasTransientState(); |
| 946 | } |
| 947 | |
| 948 | /** |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 949 | * {@inheritDoc} |
| 950 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 951 | @Override |
| 952 | public boolean dispatchUnhandledMove(View focused, int direction) { |
| 953 | return mFocused != null && |
| 954 | mFocused.dispatchUnhandledMove(focused, direction); |
| 955 | } |
| 956 | |
| 957 | /** |
| 958 | * {@inheritDoc} |
| 959 | */ |
| 960 | public void clearChildFocus(View child) { |
| 961 | if (DBG) { |
| 962 | System.out.println(this + " clearChildFocus()"); |
| 963 | } |
| 964 | |
| 965 | mFocused = null; |
| 966 | if (mParent != null) { |
| 967 | mParent.clearChildFocus(this); |
| 968 | } |
| 969 | } |
| 970 | |
| 971 | /** |
| 972 | * {@inheritDoc} |
| 973 | */ |
| 974 | @Override |
| 975 | public void clearFocus() { |
Svetoslav Ganov | b36a0ac | 2012-02-14 17:46:47 -0800 | [diff] [blame] | 976 | if (DBG) { |
| 977 | System.out.println(this + " clearFocus()"); |
| 978 | } |
| 979 | if (mFocused == null) { |
| 980 | super.clearFocus(); |
| 981 | } else { |
Svetoslav Ganov | b552d89 | 2012-06-02 14:35:02 -0700 | [diff] [blame] | 982 | View focused = mFocused; |
Svetoslav Ganov | b36a0ac | 2012-02-14 17:46:47 -0800 | [diff] [blame] | 983 | mFocused = null; |
Svetoslav Ganov | b552d89 | 2012-06-02 14:35:02 -0700 | [diff] [blame] | 984 | focused.clearFocus(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 985 | } |
| 986 | } |
| 987 | |
| 988 | /** |
| 989 | * {@inheritDoc} |
| 990 | */ |
| 991 | @Override |
Alan Viverette | 223622a | 2013-12-17 13:29:02 -0800 | [diff] [blame] | 992 | void unFocus(View focused) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 993 | if (DBG) { |
| 994 | System.out.println(this + " unFocus()"); |
| 995 | } |
Svetoslav Ganov | b36a0ac | 2012-02-14 17:46:47 -0800 | [diff] [blame] | 996 | if (mFocused == null) { |
Alan Viverette | 223622a | 2013-12-17 13:29:02 -0800 | [diff] [blame] | 997 | super.unFocus(focused); |
Svetoslav Ganov | b36a0ac | 2012-02-14 17:46:47 -0800 | [diff] [blame] | 998 | } else { |
Alan Viverette | 223622a | 2013-12-17 13:29:02 -0800 | [diff] [blame] | 999 | mFocused.unFocus(focused); |
Svetoslav Ganov | b36a0ac | 2012-02-14 17:46:47 -0800 | [diff] [blame] | 1000 | mFocused = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1001 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1002 | } |
| 1003 | |
| 1004 | /** |
| 1005 | * Returns the focused child of this view, if any. The child may have focus |
| 1006 | * or contain focus. |
| 1007 | * |
| 1008 | * @return the focused child or null. |
| 1009 | */ |
| 1010 | public View getFocusedChild() { |
| 1011 | return mFocused; |
| 1012 | } |
| 1013 | |
Adam Powell | 88c1175 | 2014-07-21 17:19:16 -0700 | [diff] [blame] | 1014 | View getDeepestFocusedChild() { |
| 1015 | View v = this; |
| 1016 | while (v != null) { |
| 1017 | if (v.isFocused()) { |
| 1018 | return v; |
| 1019 | } |
| 1020 | v = v instanceof ViewGroup ? ((ViewGroup) v).getFocusedChild() : null; |
| 1021 | } |
| 1022 | return null; |
| 1023 | } |
| 1024 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1025 | /** |
| 1026 | * Returns true if this view has or contains focus |
| 1027 | * |
| 1028 | * @return true if this view has or contains focus |
| 1029 | */ |
| 1030 | @Override |
| 1031 | public boolean hasFocus() { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 1032 | return (mPrivateFlags & PFLAG_FOCUSED) != 0 || mFocused != null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1033 | } |
| 1034 | |
| 1035 | /* |
| 1036 | * (non-Javadoc) |
| 1037 | * |
| 1038 | * @see android.view.View#findFocus() |
| 1039 | */ |
| 1040 | @Override |
| 1041 | public View findFocus() { |
| 1042 | if (DBG) { |
| 1043 | System.out.println("Find focus in " + this + ": flags=" |
| 1044 | + isFocused() + ", child=" + mFocused); |
| 1045 | } |
| 1046 | |
| 1047 | if (isFocused()) { |
| 1048 | return this; |
| 1049 | } |
| 1050 | |
| 1051 | if (mFocused != null) { |
| 1052 | return mFocused.findFocus(); |
| 1053 | } |
| 1054 | return null; |
| 1055 | } |
| 1056 | |
| 1057 | /** |
| 1058 | * {@inheritDoc} |
| 1059 | */ |
| 1060 | @Override |
| 1061 | public boolean hasFocusable() { |
| 1062 | if ((mViewFlags & VISIBILITY_MASK) != VISIBLE) { |
| 1063 | return false; |
| 1064 | } |
| 1065 | |
| 1066 | if (isFocusable()) { |
| 1067 | return true; |
| 1068 | } |
| 1069 | |
| 1070 | final int descendantFocusability = getDescendantFocusability(); |
Adam Powell | 88c1175 | 2014-07-21 17:19:16 -0700 | [diff] [blame] | 1071 | if (descendantFocusability != FOCUS_BLOCK_DESCENDANTS) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1072 | final int count = mChildrenCount; |
| 1073 | final View[] children = mChildren; |
| 1074 | |
| 1075 | for (int i = 0; i < count; i++) { |
| 1076 | final View child = children[i]; |
| 1077 | if (child.hasFocusable()) { |
| 1078 | return true; |
| 1079 | } |
| 1080 | } |
| 1081 | } |
| 1082 | |
| 1083 | return false; |
| 1084 | } |
| 1085 | |
| 1086 | /** |
| 1087 | * {@inheritDoc} |
| 1088 | */ |
| 1089 | @Override |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 1090 | 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] | 1091 | final int focusableCount = views.size(); |
| 1092 | |
| 1093 | final int descendantFocusability = getDescendantFocusability(); |
| 1094 | |
Adam Powell | 88c1175 | 2014-07-21 17:19:16 -0700 | [diff] [blame] | 1095 | if (descendantFocusability != FOCUS_BLOCK_DESCENDANTS) { |
| 1096 | if (shouldBlockFocusForTouchscreen()) { |
| 1097 | focusableMode |= FOCUSABLES_TOUCH_MODE; |
| 1098 | } |
| 1099 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1100 | final int count = mChildrenCount; |
| 1101 | final View[] children = mChildren; |
| 1102 | |
| 1103 | for (int i = 0; i < count; i++) { |
| 1104 | final View child = children[i]; |
| 1105 | if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) { |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 1106 | child.addFocusables(views, direction, focusableMode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1107 | } |
| 1108 | } |
| 1109 | } |
| 1110 | |
| 1111 | // we add ourselves (if focusable) in all cases except for when we are |
| 1112 | // FOCUS_AFTER_DESCENDANTS and there are some descendants focusable. this is |
| 1113 | // to avoid the focus search finding layouts when a more precise search |
| 1114 | // among the focusable children would be more interesting. |
Adam Powell | ff0d298 | 2014-07-10 20:34:14 -0700 | [diff] [blame] | 1115 | if ((descendantFocusability != FOCUS_AFTER_DESCENDANTS |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1116 | // No focusable descendants |
Adam Powell | 88c1175 | 2014-07-21 17:19:16 -0700 | [diff] [blame] | 1117 | || (focusableCount == views.size())) && |
| 1118 | (isFocusableInTouchMode() || !shouldBlockFocusForTouchscreen())) { |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 1119 | super.addFocusables(views, direction, focusableMode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1120 | } |
| 1121 | } |
| 1122 | |
Adam Powell | ff0d298 | 2014-07-10 20:34:14 -0700 | [diff] [blame] | 1123 | /** |
| 1124 | * Set whether this ViewGroup should ignore focus requests for itself and its children. |
| 1125 | * If this option is enabled and the ViewGroup or a descendant currently has focus, focus |
| 1126 | * will proceed forward. |
| 1127 | * |
| 1128 | * @param touchscreenBlocksFocus true to enable blocking focus in the presence of a touchscreen |
| 1129 | */ |
| 1130 | public void setTouchscreenBlocksFocus(boolean touchscreenBlocksFocus) { |
| 1131 | if (touchscreenBlocksFocus) { |
| 1132 | mGroupFlags |= FLAG_TOUCHSCREEN_BLOCKS_FOCUS; |
| 1133 | if (hasFocus()) { |
Adam Powell | 88c1175 | 2014-07-21 17:19:16 -0700 | [diff] [blame] | 1134 | final View focusedChild = getDeepestFocusedChild(); |
| 1135 | if (!focusedChild.isFocusableInTouchMode()) { |
| 1136 | final View newFocus = focusSearch(FOCUS_FORWARD); |
| 1137 | if (newFocus != null) { |
| 1138 | newFocus.requestFocus(); |
| 1139 | } |
Adam Powell | ff0d298 | 2014-07-10 20:34:14 -0700 | [diff] [blame] | 1140 | } |
| 1141 | } |
| 1142 | } else { |
| 1143 | mGroupFlags &= ~FLAG_TOUCHSCREEN_BLOCKS_FOCUS; |
| 1144 | } |
| 1145 | } |
| 1146 | |
| 1147 | /** |
| 1148 | * Check whether this ViewGroup should ignore focus requests for itself and its children. |
| 1149 | */ |
| 1150 | public boolean getTouchscreenBlocksFocus() { |
| 1151 | return (mGroupFlags & FLAG_TOUCHSCREEN_BLOCKS_FOCUS) != 0; |
| 1152 | } |
| 1153 | |
| 1154 | boolean shouldBlockFocusForTouchscreen() { |
| 1155 | return getTouchscreenBlocksFocus() && |
| 1156 | mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_TOUCHSCREEN); |
| 1157 | } |
| 1158 | |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 1159 | @Override |
Svetoslav Ganov | ea515ae | 2011-09-14 18:15:32 -0700 | [diff] [blame] | 1160 | public void findViewsWithText(ArrayList<View> outViews, CharSequence text, int flags) { |
| 1161 | super.findViewsWithText(outViews, text, flags); |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 1162 | final int childrenCount = mChildrenCount; |
| 1163 | final View[] children = mChildren; |
| 1164 | for (int i = 0; i < childrenCount; i++) { |
| 1165 | View child = children[i]; |
Svetoslav Ganov | ea515ae | 2011-09-14 18:15:32 -0700 | [diff] [blame] | 1166 | if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 1167 | && (child.mPrivateFlags & PFLAG_IS_ROOT_NAMESPACE) == 0) { |
Svetoslav Ganov | ea515ae | 2011-09-14 18:15:32 -0700 | [diff] [blame] | 1168 | child.findViewsWithText(outViews, text, flags); |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 1169 | } |
| 1170 | } |
| 1171 | } |
| 1172 | |
Svetoslav | 5b578da | 2013-05-08 14:23:32 -0700 | [diff] [blame] | 1173 | /** @hide */ |
Svetoslav Ganov | 2cdedff | 2011-10-03 14:18:42 -0700 | [diff] [blame] | 1174 | @Override |
Svetoslav | 5b578da | 2013-05-08 14:23:32 -0700 | [diff] [blame] | 1175 | public View findViewByAccessibilityIdTraversal(int accessibilityId) { |
Svetoslav Ganov | 2cdedff | 2011-10-03 14:18:42 -0700 | [diff] [blame] | 1176 | View foundView = super.findViewByAccessibilityIdTraversal(accessibilityId); |
| 1177 | if (foundView != null) { |
| 1178 | return foundView; |
| 1179 | } |
Svetoslav | 6cc4627 | 2015-06-03 11:38:30 -0700 | [diff] [blame] | 1180 | |
| 1181 | if (getAccessibilityNodeProvider() != null) { |
| 1182 | return null; |
| 1183 | } |
| 1184 | |
Svetoslav Ganov | 2cdedff | 2011-10-03 14:18:42 -0700 | [diff] [blame] | 1185 | final int childrenCount = mChildrenCount; |
| 1186 | final View[] children = mChildren; |
| 1187 | for (int i = 0; i < childrenCount; i++) { |
| 1188 | View child = children[i]; |
| 1189 | foundView = child.findViewByAccessibilityIdTraversal(accessibilityId); |
| 1190 | if (foundView != null) { |
| 1191 | return foundView; |
| 1192 | } |
| 1193 | } |
Svetoslav | 6cc4627 | 2015-06-03 11:38:30 -0700 | [diff] [blame] | 1194 | |
Svetoslav Ganov | 2cdedff | 2011-10-03 14:18:42 -0700 | [diff] [blame] | 1195 | return null; |
| 1196 | } |
| 1197 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1198 | /** |
| 1199 | * {@inheritDoc} |
| 1200 | */ |
| 1201 | @Override |
| 1202 | public void dispatchWindowFocusChanged(boolean hasFocus) { |
| 1203 | super.dispatchWindowFocusChanged(hasFocus); |
| 1204 | final int count = mChildrenCount; |
| 1205 | final View[] children = mChildren; |
| 1206 | for (int i = 0; i < count; i++) { |
| 1207 | children[i].dispatchWindowFocusChanged(hasFocus); |
| 1208 | } |
| 1209 | } |
| 1210 | |
| 1211 | /** |
| 1212 | * {@inheritDoc} |
| 1213 | */ |
| 1214 | @Override |
| 1215 | public void addTouchables(ArrayList<View> views) { |
| 1216 | super.addTouchables(views); |
| 1217 | |
| 1218 | final int count = mChildrenCount; |
| 1219 | final View[] children = mChildren; |
| 1220 | |
| 1221 | for (int i = 0; i < count; i++) { |
| 1222 | final View child = children[i]; |
| 1223 | if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) { |
| 1224 | child.addTouchables(views); |
| 1225 | } |
| 1226 | } |
| 1227 | } |
Dianne Hackborn | 3a3a6cf | 2012-03-26 10:24:04 -0700 | [diff] [blame] | 1228 | |
| 1229 | /** |
| 1230 | * @hide |
| 1231 | */ |
| 1232 | @Override |
| 1233 | public void makeOptionalFitsSystemWindows() { |
| 1234 | super.makeOptionalFitsSystemWindows(); |
| 1235 | final int count = mChildrenCount; |
| 1236 | final View[] children = mChildren; |
| 1237 | for (int i = 0; i < count; i++) { |
| 1238 | children[i].makeOptionalFitsSystemWindows(); |
| 1239 | } |
| 1240 | } |
| 1241 | |
Romain Guy | 43c9cdf | 2010-01-27 13:53:55 -0800 | [diff] [blame] | 1242 | /** |
| 1243 | * {@inheritDoc} |
| 1244 | */ |
| 1245 | @Override |
| 1246 | public void dispatchDisplayHint(int hint) { |
| 1247 | super.dispatchDisplayHint(hint); |
| 1248 | final int count = mChildrenCount; |
| 1249 | final View[] children = mChildren; |
| 1250 | for (int i = 0; i < count; i++) { |
| 1251 | children[i].dispatchDisplayHint(hint); |
| 1252 | } |
| 1253 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1254 | |
| 1255 | /** |
Chet Haase | 0d29936 | 2012-01-26 10:51:48 -0800 | [diff] [blame] | 1256 | * Called when a view's visibility has changed. Notify the parent to take any appropriate |
| 1257 | * action. |
| 1258 | * |
| 1259 | * @param child The view whose visibility has changed |
| 1260 | * @param oldVisibility The previous visibility value (GONE, INVISIBLE, or VISIBLE). |
| 1261 | * @param newVisibility The new visibility value (GONE, INVISIBLE, or VISIBLE). |
Chet Haase | 5e25c2c | 2010-09-16 11:15:56 -0700 | [diff] [blame] | 1262 | * @hide |
Chet Haase | 5e25c2c | 2010-09-16 11:15:56 -0700 | [diff] [blame] | 1263 | */ |
Chet Haase | 0d29936 | 2012-01-26 10:51:48 -0800 | [diff] [blame] | 1264 | protected void onChildVisibilityChanged(View child, int oldVisibility, int newVisibility) { |
Chet Haase | 5e25c2c | 2010-09-16 11:15:56 -0700 | [diff] [blame] | 1265 | if (mTransition != null) { |
Chet Haase | 0d29936 | 2012-01-26 10:51:48 -0800 | [diff] [blame] | 1266 | if (newVisibility == VISIBLE) { |
| 1267 | mTransition.showChild(this, child, oldVisibility); |
Chet Haase | 5e25c2c | 2010-09-16 11:15:56 -0700 | [diff] [blame] | 1268 | } else { |
Chet Haase | 0d29936 | 2012-01-26 10:51:48 -0800 | [diff] [blame] | 1269 | mTransition.hideChild(this, child, newVisibility); |
Chet Haase | 5e25c2c | 2010-09-16 11:15:56 -0700 | [diff] [blame] | 1270 | if (mTransitioningViews != null && mTransitioningViews.contains(child)) { |
Chet Haase | ddbb346 | 2012-06-19 13:54:29 -0700 | [diff] [blame] | 1271 | // Only track this on disappearing views - appearing views are already visible |
| 1272 | // and don't need special handling during drawChild() |
| 1273 | if (mVisibilityChangingChildren == null) { |
| 1274 | mVisibilityChangingChildren = new ArrayList<View>(); |
| 1275 | } |
| 1276 | mVisibilityChangingChildren.add(child); |
Chet Haase | 5e25c2c | 2010-09-16 11:15:56 -0700 | [diff] [blame] | 1277 | addDisappearingView(child); |
| 1278 | } |
| 1279 | } |
| 1280 | } |
Christopher Tate | 86cab1b | 2011-01-13 20:28:55 -0800 | [diff] [blame] | 1281 | |
| 1282 | // in all cases, for drags |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1283 | if (newVisibility == VISIBLE && mCurrentDragStartEvent != null) { |
| 1284 | if (!mChildrenInterestedInDrag.contains(child)) { |
| 1285 | notifyChildOfDragStart(child); |
Christopher Tate | 86cab1b | 2011-01-13 20:28:55 -0800 | [diff] [blame] | 1286 | } |
| 1287 | } |
Chet Haase | 5e25c2c | 2010-09-16 11:15:56 -0700 | [diff] [blame] | 1288 | } |
| 1289 | |
| 1290 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1291 | * {@inheritDoc} |
| 1292 | */ |
| 1293 | @Override |
Adam Powell | 326d808 | 2009-12-09 15:10:07 -0800 | [diff] [blame] | 1294 | protected void dispatchVisibilityChanged(View changedView, int visibility) { |
| 1295 | super.dispatchVisibilityChanged(changedView, visibility); |
| 1296 | final int count = mChildrenCount; |
| 1297 | final View[] children = mChildren; |
| 1298 | for (int i = 0; i < count; i++) { |
| 1299 | children[i].dispatchVisibilityChanged(changedView, visibility); |
| 1300 | } |
| 1301 | } |
| 1302 | |
| 1303 | /** |
| 1304 | * {@inheritDoc} |
| 1305 | */ |
| 1306 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1307 | public void dispatchWindowVisibilityChanged(int visibility) { |
| 1308 | super.dispatchWindowVisibilityChanged(visibility); |
| 1309 | final int count = mChildrenCount; |
| 1310 | final View[] children = mChildren; |
| 1311 | for (int i = 0; i < count; i++) { |
| 1312 | children[i].dispatchWindowVisibilityChanged(visibility); |
| 1313 | } |
| 1314 | } |
| 1315 | |
| 1316 | /** |
| 1317 | * {@inheritDoc} |
| 1318 | */ |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 1319 | @Override |
| 1320 | public void dispatchConfigurationChanged(Configuration newConfig) { |
| 1321 | super.dispatchConfigurationChanged(newConfig); |
| 1322 | final int count = mChildrenCount; |
| 1323 | final View[] children = mChildren; |
| 1324 | for (int i = 0; i < count; i++) { |
| 1325 | children[i].dispatchConfigurationChanged(newConfig); |
| 1326 | } |
| 1327 | } |
Filip Gruszczynski | a33bdf3 | 2015-11-19 18:22:16 -0800 | [diff] [blame] | 1328 | |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 1329 | /** |
| 1330 | * {@inheritDoc} |
| 1331 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1332 | public void recomputeViewAttributes(View child) { |
Joe Onorato | 664644d | 2011-01-23 17:53:23 -0800 | [diff] [blame] | 1333 | if (mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) { |
| 1334 | ViewParent parent = mParent; |
| 1335 | if (parent != null) parent.recomputeViewAttributes(this); |
| 1336 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1337 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 1338 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1339 | @Override |
Dianne Hackborn | 3a3a6cf | 2012-03-26 10:24:04 -0700 | [diff] [blame] | 1340 | void dispatchCollectViewAttributes(AttachInfo attachInfo, int visibility) { |
| 1341 | if ((visibility & VISIBILITY_MASK) == VISIBLE) { |
| 1342 | super.dispatchCollectViewAttributes(attachInfo, visibility); |
| 1343 | final int count = mChildrenCount; |
| 1344 | final View[] children = mChildren; |
| 1345 | for (int i = 0; i < count; i++) { |
| 1346 | final View child = children[i]; |
| 1347 | child.dispatchCollectViewAttributes(attachInfo, |
| 1348 | visibility | (child.mViewFlags&VISIBILITY_MASK)); |
| 1349 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1350 | } |
| 1351 | } |
| 1352 | |
| 1353 | /** |
| 1354 | * {@inheritDoc} |
| 1355 | */ |
| 1356 | public void bringChildToFront(View child) { |
Alan Viverette | 77bb6f1 | 2015-02-11 17:24:33 -0800 | [diff] [blame] | 1357 | final int index = indexOfChild(child); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1358 | if (index >= 0) { |
| 1359 | removeFromArray(index); |
| 1360 | addInArray(child, mChildrenCount); |
| 1361 | child.mParent = this; |
Chet Haase | cb96db8 | 2013-09-04 10:21:46 -0700 | [diff] [blame] | 1362 | requestLayout(); |
| 1363 | invalidate(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1364 | } |
| 1365 | } |
| 1366 | |
Romain Guy | 6410c0a | 2013-06-17 11:21:58 -0700 | [diff] [blame] | 1367 | private PointF getLocalPoint() { |
| 1368 | if (mLocalPoint == null) mLocalPoint = new PointF(); |
| 1369 | return mLocalPoint; |
| 1370 | } |
| 1371 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1372 | /** |
| 1373 | * {@inheritDoc} |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1374 | */ |
Steve Block | 8a7259b | 2012-03-01 11:24:41 +0000 | [diff] [blame] | 1375 | // TODO: Write real docs |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1376 | @Override |
| 1377 | public boolean dispatchDragEvent(DragEvent event) { |
| 1378 | boolean retval = false; |
| 1379 | final float tx = event.mX; |
| 1380 | final float ty = event.mY; |
| 1381 | |
Dianne Hackborn | 6dd005b | 2011-07-18 13:22:50 -0700 | [diff] [blame] | 1382 | ViewRootImpl root = getViewRootImpl(); |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1383 | |
| 1384 | // Dispatch down the view hierarchy |
Romain Guy | 6410c0a | 2013-06-17 11:21:58 -0700 | [diff] [blame] | 1385 | final PointF localPoint = getLocalPoint(); |
| 1386 | |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1387 | switch (event.mAction) { |
| 1388 | case DragEvent.ACTION_DRAG_STARTED: { |
| 1389 | // clear state to recalculate which views we drag over |
Chris Tate | 9d1ab88 | 2010-11-02 15:55:39 -0700 | [diff] [blame] | 1390 | mCurrentDragView = null; |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1391 | |
Christopher Tate | 86cab1b | 2011-01-13 20:28:55 -0800 | [diff] [blame] | 1392 | // Set up our tracking of drag-started notifications |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1393 | mCurrentDragStartEvent = DragEvent.obtain(event); |
| 1394 | if (mChildrenInterestedInDrag == null) { |
| 1395 | mChildrenInterestedInDrag = new HashSet<View>(); |
Christopher Tate | 86cab1b | 2011-01-13 20:28:55 -0800 | [diff] [blame] | 1396 | } else { |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1397 | mChildrenInterestedInDrag.clear(); |
Christopher Tate | 86cab1b | 2011-01-13 20:28:55 -0800 | [diff] [blame] | 1398 | } |
| 1399 | |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1400 | // Now dispatch down to our children, caching the responses |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1401 | final int count = mChildrenCount; |
| 1402 | final View[] children = mChildren; |
| 1403 | for (int i = 0; i < count; i++) { |
Christopher Tate | 2c095f3 | 2010-10-04 14:13:40 -0700 | [diff] [blame] | 1404 | final View child = children[i]; |
Christopher Tate | 3d4bf17 | 2011-03-28 16:16:46 -0700 | [diff] [blame] | 1405 | child.mPrivateFlags2 &= ~View.DRAG_MASK; |
Christopher Tate | 2c095f3 | 2010-10-04 14:13:40 -0700 | [diff] [blame] | 1406 | if (child.getVisibility() == VISIBLE) { |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1407 | if (notifyChildOfDragStart(children[i])) { |
| 1408 | retval = true; |
Christopher Tate | 2c095f3 | 2010-10-04 14:13:40 -0700 | [diff] [blame] | 1409 | } |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1410 | } |
| 1411 | } |
| 1412 | |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1413 | // Notify itself of the drag start. |
| 1414 | mIsInterestedInDrag = super.dispatchDragEvent(event); |
| 1415 | if (mIsInterestedInDrag) { |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1416 | retval = true; |
| 1417 | } |
| 1418 | } break; |
| 1419 | |
| 1420 | case DragEvent.ACTION_DRAG_ENDED: { |
Christopher Tate | 86cab1b | 2011-01-13 20:28:55 -0800 | [diff] [blame] | 1421 | // Release the bookkeeping now that the drag lifecycle has ended |
Vadim Tryshev | 1517789 | 2015-12-18 08:01:00 -0800 | [diff] [blame] | 1422 | final HashSet<View> childrenInterestedInDrag = mChildrenInterestedInDrag; |
| 1423 | if (childrenInterestedInDrag != null) { |
| 1424 | for (View child : childrenInterestedInDrag) { |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1425 | // If a child was interested in the ongoing drag, it's told that it's over |
| 1426 | if (child.dispatchDragEvent(event)) { |
| 1427 | retval = true; |
| 1428 | } |
Christopher Tate | 3d4bf17 | 2011-03-28 16:16:46 -0700 | [diff] [blame] | 1429 | child.mPrivateFlags2 &= ~View.DRAG_MASK; |
| 1430 | child.refreshDrawableState(); |
Christopher Tate | 1fc014f | 2011-01-19 12:56:26 -0800 | [diff] [blame] | 1431 | } |
Vadim Tryshev | 1517789 | 2015-12-18 08:01:00 -0800 | [diff] [blame] | 1432 | childrenInterestedInDrag.clear(); |
| 1433 | } |
| 1434 | if (mCurrentDragStartEvent != null) { |
| 1435 | mCurrentDragStartEvent.recycle(); |
| 1436 | mCurrentDragStartEvent = null; |
Christopher Tate | 1fc014f | 2011-01-19 12:56:26 -0800 | [diff] [blame] | 1437 | } |
Christopher Tate | 86cab1b | 2011-01-13 20:28:55 -0800 | [diff] [blame] | 1438 | |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1439 | if (mIsInterestedInDrag) { |
| 1440 | if (super.dispatchDragEvent(event)) { |
| 1441 | retval = true; |
| 1442 | } |
| 1443 | mIsInterestedInDrag = false; |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1444 | } |
| 1445 | } break; |
| 1446 | |
| 1447 | case DragEvent.ACTION_DRAG_LOCATION: { |
| 1448 | // Find the [possibly new] drag target |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1449 | View target = findFrontmostDroppableChildAt(event.mX, event.mY, localPoint); |
| 1450 | if (target == null && mIsInterestedInDrag) { |
| 1451 | target = this; |
| 1452 | } |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1453 | |
| 1454 | // 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] | 1455 | // we're over now [for purposes of the eventual drag-recipient-changed |
| 1456 | // notifications to the framework] and tell the new target that the drag |
| 1457 | // has entered its bounds. The root will see setDragFocus() calls all |
| 1458 | // the way down to the final leaf view that is handling the LOCATION event |
| 1459 | // before reporting the new potential recipient to the framework. |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1460 | if (mCurrentDragView != target) { |
Chris Tate | 9d1ab88 | 2010-11-02 15:55:39 -0700 | [diff] [blame] | 1461 | root.setDragFocus(target); |
| 1462 | |
| 1463 | final int action = event.mAction; |
Vladislav Kaznacheev | cc010b2 | 2016-01-21 16:24:40 -0800 | [diff] [blame^] | 1464 | // Position should not be available for ACTION_DRAG_ENTERED and ACTION_DRAG_EXITED. |
| 1465 | event.mX = 0; |
| 1466 | event.mY = 0; |
| 1467 | |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1468 | // If we've dragged off of a child view or this window, send it the EXITED message |
Chris Tate | 9d1ab88 | 2010-11-02 15:55:39 -0700 | [diff] [blame] | 1469 | if (mCurrentDragView != null) { |
Christopher Tate | 3d4bf17 | 2011-03-28 16:16:46 -0700 | [diff] [blame] | 1470 | final View view = mCurrentDragView; |
Chris Tate | 9d1ab88 | 2010-11-02 15:55:39 -0700 | [diff] [blame] | 1471 | event.mAction = DragEvent.ACTION_DRAG_EXITED; |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1472 | if (view != this) { |
| 1473 | view.dispatchDragEvent(event); |
| 1474 | view.mPrivateFlags2 &= ~View.PFLAG2_DRAG_HOVERED; |
| 1475 | view.refreshDrawableState(); |
| 1476 | } else { |
| 1477 | super.dispatchDragEvent(event); |
| 1478 | } |
Chris Tate | 9d1ab88 | 2010-11-02 15:55:39 -0700 | [diff] [blame] | 1479 | } |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1480 | |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1481 | mCurrentDragView = target; |
Chris Tate | 9d1ab88 | 2010-11-02 15:55:39 -0700 | [diff] [blame] | 1482 | |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1483 | // If we've dragged over a new child view, send it the ENTERED message, otherwise |
| 1484 | // send it to this window. |
Chris Tate | 9d1ab88 | 2010-11-02 15:55:39 -0700 | [diff] [blame] | 1485 | if (target != null) { |
| 1486 | event.mAction = DragEvent.ACTION_DRAG_ENTERED; |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1487 | if (target != this) { |
| 1488 | target.dispatchDragEvent(event); |
| 1489 | target.mPrivateFlags2 |= View.PFLAG2_DRAG_HOVERED; |
| 1490 | target.refreshDrawableState(); |
| 1491 | } else { |
| 1492 | super.dispatchDragEvent(event); |
| 1493 | } |
Chris Tate | 9d1ab88 | 2010-11-02 15:55:39 -0700 | [diff] [blame] | 1494 | } |
| 1495 | event.mAction = action; // restore the event's original state |
Vladislav Kaznacheev | cc010b2 | 2016-01-21 16:24:40 -0800 | [diff] [blame^] | 1496 | event.mX = tx; |
| 1497 | event.mY = ty; |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1498 | } |
Christopher Tate | 2c095f3 | 2010-10-04 14:13:40 -0700 | [diff] [blame] | 1499 | |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1500 | // Dispatch the actual drag location notice, localized into its coordinates |
| 1501 | if (target != null) { |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1502 | if (target != this) { |
| 1503 | event.mX = localPoint.x; |
| 1504 | event.mY = localPoint.y; |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1505 | |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1506 | retval = target.dispatchDragEvent(event); |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1507 | |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1508 | event.mX = tx; |
| 1509 | event.mY = ty; |
| 1510 | } else { |
| 1511 | retval = super.dispatchDragEvent(event); |
| 1512 | } |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1513 | } |
| 1514 | } break; |
| 1515 | |
Chris Tate | 9d1ab88 | 2010-11-02 15:55:39 -0700 | [diff] [blame] | 1516 | /* Entered / exited dispatch |
| 1517 | * |
| 1518 | * DRAG_ENTERED is not dispatched downwards from ViewGroup. The reason for this is |
| 1519 | * that we're about to get the corresponding LOCATION event, which we will use to |
| 1520 | * determine which of our children is the new target; at that point we will |
| 1521 | * push a DRAG_ENTERED down to the new target child [which may itself be a ViewGroup]. |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1522 | * If no suitable child is detected, dispatch to this window. |
Chris Tate | 9d1ab88 | 2010-11-02 15:55:39 -0700 | [diff] [blame] | 1523 | * |
| 1524 | * DRAG_EXITED *is* dispatched all the way down immediately: once we know the |
| 1525 | * drag has left this ViewGroup, we know by definition that every contained subview |
| 1526 | * is also no longer under the drag point. |
| 1527 | */ |
| 1528 | |
| 1529 | case DragEvent.ACTION_DRAG_EXITED: { |
| 1530 | if (mCurrentDragView != null) { |
Christopher Tate | 3d4bf17 | 2011-03-28 16:16:46 -0700 | [diff] [blame] | 1531 | final View view = mCurrentDragView; |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1532 | if (view != this) { |
| 1533 | view.dispatchDragEvent(event); |
| 1534 | view.mPrivateFlags2 &= ~View.PFLAG2_DRAG_HOVERED; |
| 1535 | view.refreshDrawableState(); |
| 1536 | } else { |
| 1537 | super.dispatchDragEvent(event); |
| 1538 | } |
Christopher Tate | 3d4bf17 | 2011-03-28 16:16:46 -0700 | [diff] [blame] | 1539 | |
Chris Tate | 9d1ab88 | 2010-11-02 15:55:39 -0700 | [diff] [blame] | 1540 | mCurrentDragView = null; |
| 1541 | } |
| 1542 | } break; |
| 1543 | |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1544 | case DragEvent.ACTION_DROP: { |
Christopher Tate | 2c095f3 | 2010-10-04 14:13:40 -0700 | [diff] [blame] | 1545 | 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] | 1546 | View target = findFrontmostDroppableChildAt(event.mX, event.mY, localPoint); |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1547 | if (target != null) { |
Christopher Tate | 5ada6cb | 2010-10-05 14:15:29 -0700 | [diff] [blame] | 1548 | 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] | 1549 | event.mX = localPoint.x; |
| 1550 | event.mY = localPoint.y; |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1551 | retval = target.dispatchDragEvent(event); |
| 1552 | event.mX = tx; |
| 1553 | event.mY = ty; |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1554 | } else if (mIsInterestedInDrag) { |
| 1555 | retval = super.dispatchDragEvent(event); |
Christopher Tate | 5ada6cb | 2010-10-05 14:15:29 -0700 | [diff] [blame] | 1556 | } else { |
| 1557 | if (ViewDebug.DEBUG_DRAG) { |
| 1558 | Log.d(View.VIEW_LOG_TAG, " not dropped on an accepting view"); |
| 1559 | } |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1560 | } |
| 1561 | } break; |
| 1562 | } |
| 1563 | |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1564 | return retval; |
| 1565 | } |
| 1566 | |
| 1567 | // Find the frontmost child view that lies under the given point, and calculate |
| 1568 | // the position within its own local coordinate system. |
| 1569 | View findFrontmostDroppableChildAt(float x, float y, PointF outLocalPoint) { |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1570 | final int count = mChildrenCount; |
| 1571 | final View[] children = mChildren; |
| 1572 | for (int i = count - 1; i >= 0; i--) { |
| 1573 | final View child = children[i]; |
Christopher Tate | 3d4bf17 | 2011-03-28 16:16:46 -0700 | [diff] [blame] | 1574 | if (!child.canAcceptDrag()) { |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1575 | continue; |
| 1576 | } |
| 1577 | |
Christopher Tate | 2c095f3 | 2010-10-04 14:13:40 -0700 | [diff] [blame] | 1578 | if (isTransformedTouchPointInView(x, y, child, outLocalPoint)) { |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1579 | return child; |
| 1580 | } |
| 1581 | } |
| 1582 | return null; |
| 1583 | } |
| 1584 | |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1585 | boolean notifyChildOfDragStart(View child) { |
| 1586 | // The caller guarantees that the child is not in mChildrenInterestedInDrag yet. |
| 1587 | |
Christopher Tate | 86cab1b | 2011-01-13 20:28:55 -0800 | [diff] [blame] | 1588 | if (ViewDebug.DEBUG_DRAG) { |
| 1589 | Log.d(View.VIEW_LOG_TAG, "Sending drag-started to view: " + child); |
| 1590 | } |
| 1591 | |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1592 | final boolean canAccept = child.dispatchDragEvent(mCurrentDragStartEvent); |
| 1593 | if (canAccept) { |
| 1594 | mChildrenInterestedInDrag.add(child); |
| 1595 | if (!child.canAcceptDrag()) { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 1596 | child.mPrivateFlags2 |= View.PFLAG2_DRAG_CAN_ACCEPT; |
Christopher Tate | 3d4bf17 | 2011-03-28 16:16:46 -0700 | [diff] [blame] | 1597 | child.refreshDrawableState(); |
| 1598 | } |
Christopher Tate | 86cab1b | 2011-01-13 20:28:55 -0800 | [diff] [blame] | 1599 | } |
Christopher Tate | 3d4bf17 | 2011-03-28 16:16:46 -0700 | [diff] [blame] | 1600 | return canAccept; |
Christopher Tate | 86cab1b | 2011-01-13 20:28:55 -0800 | [diff] [blame] | 1601 | } |
| 1602 | |
Joe Onorato | 664644d | 2011-01-23 17:53:23 -0800 | [diff] [blame] | 1603 | @Override |
Dianne Hackborn | 3a3a6cf | 2012-03-26 10:24:04 -0700 | [diff] [blame] | 1604 | public void dispatchWindowSystemUiVisiblityChanged(int visible) { |
| 1605 | super.dispatchWindowSystemUiVisiblityChanged(visible); |
| 1606 | |
| 1607 | final int count = mChildrenCount; |
| 1608 | final View[] children = mChildren; |
| 1609 | for (int i=0; i <count; i++) { |
| 1610 | final View child = children[i]; |
| 1611 | child.dispatchWindowSystemUiVisiblityChanged(visible); |
| 1612 | } |
| 1613 | } |
| 1614 | |
| 1615 | @Override |
Joe Onorato | 664644d | 2011-01-23 17:53:23 -0800 | [diff] [blame] | 1616 | public void dispatchSystemUiVisibilityChanged(int visible) { |
| 1617 | super.dispatchSystemUiVisibilityChanged(visible); |
| 1618 | |
| 1619 | final int count = mChildrenCount; |
| 1620 | final View[] children = mChildren; |
| 1621 | for (int i=0; i <count; i++) { |
| 1622 | final View child = children[i]; |
| 1623 | child.dispatchSystemUiVisibilityChanged(visible); |
| 1624 | } |
| 1625 | } |
| 1626 | |
Dianne Hackborn | 9a230e0 | 2011-10-06 11:51:27 -0700 | [diff] [blame] | 1627 | @Override |
Dianne Hackborn | cf67578 | 2012-05-10 15:07:24 -0700 | [diff] [blame] | 1628 | boolean updateLocalSystemUiVisibility(int localValue, int localChanges) { |
| 1629 | boolean changed = super.updateLocalSystemUiVisibility(localValue, localChanges); |
Dianne Hackborn | 9a230e0 | 2011-10-06 11:51:27 -0700 | [diff] [blame] | 1630 | |
| 1631 | final int count = mChildrenCount; |
| 1632 | final View[] children = mChildren; |
| 1633 | for (int i=0; i <count; i++) { |
| 1634 | final View child = children[i]; |
Dianne Hackborn | cf67578 | 2012-05-10 15:07:24 -0700 | [diff] [blame] | 1635 | changed |= child.updateLocalSystemUiVisibility(localValue, localChanges); |
Dianne Hackborn | 9a230e0 | 2011-10-06 11:51:27 -0700 | [diff] [blame] | 1636 | } |
Dianne Hackborn | cf67578 | 2012-05-10 15:07:24 -0700 | [diff] [blame] | 1637 | return changed; |
Dianne Hackborn | 9a230e0 | 2011-10-06 11:51:27 -0700 | [diff] [blame] | 1638 | } |
| 1639 | |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1640 | /** |
| 1641 | * {@inheritDoc} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1642 | */ |
| 1643 | @Override |
| 1644 | public boolean dispatchKeyEventPreIme(KeyEvent event) { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 1645 | if ((mPrivateFlags & (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) |
| 1646 | == (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1647 | return super.dispatchKeyEventPreIme(event); |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 1648 | } else if (mFocused != null && (mFocused.mPrivateFlags & PFLAG_HAS_BOUNDS) |
| 1649 | == PFLAG_HAS_BOUNDS) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1650 | return mFocused.dispatchKeyEventPreIme(event); |
| 1651 | } |
| 1652 | return false; |
| 1653 | } |
| 1654 | |
| 1655 | /** |
| 1656 | * {@inheritDoc} |
| 1657 | */ |
| 1658 | @Override |
| 1659 | public boolean dispatchKeyEvent(KeyEvent event) { |
Jeff Brown | 21bc5c9 | 2011-02-28 18:27:14 -0800 | [diff] [blame] | 1660 | if (mInputEventConsistencyVerifier != null) { |
| 1661 | mInputEventConsistencyVerifier.onKeyEvent(event, 1); |
| 1662 | } |
| 1663 | |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 1664 | if ((mPrivateFlags & (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) |
| 1665 | == (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) { |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 1666 | if (super.dispatchKeyEvent(event)) { |
| 1667 | return true; |
| 1668 | } |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 1669 | } else if (mFocused != null && (mFocused.mPrivateFlags & PFLAG_HAS_BOUNDS) |
| 1670 | == PFLAG_HAS_BOUNDS) { |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 1671 | if (mFocused.dispatchKeyEvent(event)) { |
| 1672 | return true; |
| 1673 | } |
| 1674 | } |
| 1675 | |
| 1676 | if (mInputEventConsistencyVerifier != null) { |
| 1677 | mInputEventConsistencyVerifier.onUnhandledEvent(event, 1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1678 | } |
| 1679 | return false; |
| 1680 | } |
| 1681 | |
| 1682 | /** |
| 1683 | * {@inheritDoc} |
| 1684 | */ |
| 1685 | @Override |
| 1686 | public boolean dispatchKeyShortcutEvent(KeyEvent event) { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 1687 | if ((mPrivateFlags & (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) |
| 1688 | == (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1689 | return super.dispatchKeyShortcutEvent(event); |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 1690 | } else if (mFocused != null && (mFocused.mPrivateFlags & PFLAG_HAS_BOUNDS) |
| 1691 | == PFLAG_HAS_BOUNDS) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1692 | return mFocused.dispatchKeyShortcutEvent(event); |
| 1693 | } |
| 1694 | return false; |
| 1695 | } |
| 1696 | |
| 1697 | /** |
| 1698 | * {@inheritDoc} |
| 1699 | */ |
| 1700 | @Override |
| 1701 | public boolean dispatchTrackballEvent(MotionEvent event) { |
Jeff Brown | 21bc5c9 | 2011-02-28 18:27:14 -0800 | [diff] [blame] | 1702 | if (mInputEventConsistencyVerifier != null) { |
| 1703 | mInputEventConsistencyVerifier.onTrackballEvent(event, 1); |
| 1704 | } |
| 1705 | |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 1706 | if ((mPrivateFlags & (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) |
| 1707 | == (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) { |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 1708 | if (super.dispatchTrackballEvent(event)) { |
| 1709 | return true; |
| 1710 | } |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 1711 | } else if (mFocused != null && (mFocused.mPrivateFlags & PFLAG_HAS_BOUNDS) |
| 1712 | == PFLAG_HAS_BOUNDS) { |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 1713 | if (mFocused.dispatchTrackballEvent(event)) { |
| 1714 | return true; |
| 1715 | } |
| 1716 | } |
| 1717 | |
| 1718 | if (mInputEventConsistencyVerifier != null) { |
| 1719 | mInputEventConsistencyVerifier.onUnhandledEvent(event, 1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1720 | } |
| 1721 | return false; |
| 1722 | } |
| 1723 | |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 1724 | @Override |
Jun Mukai | d4eaef7 | 2015-10-30 15:54:33 -0700 | [diff] [blame] | 1725 | public PointerIcon getPointerIcon(MotionEvent event, float x, float y) { |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 1726 | // Check what the child under the pointer says about the pointer. |
| 1727 | final int childrenCount = mChildrenCount; |
| 1728 | if (childrenCount != 0) { |
| 1729 | final ArrayList<View> preorderedList = buildOrderedChildList(); |
| 1730 | final boolean customOrder = preorderedList == null |
| 1731 | && isChildrenDrawingOrderEnabled(); |
| 1732 | final View[] children = mChildren; |
| 1733 | for (int i = childrenCount - 1; i >= 0; i--) { |
| 1734 | final int childIndex = customOrder ? getChildDrawingOrder(childrenCount, i) : i; |
| 1735 | final View child = (preorderedList == null) |
| 1736 | ? children[childIndex] : preorderedList.get(childIndex); |
| 1737 | PointF point = getLocalPoint(); |
| 1738 | if (isTransformedTouchPointInView(x, y, child, point)) { |
Jun Mukai | d4eaef7 | 2015-10-30 15:54:33 -0700 | [diff] [blame] | 1739 | final PointerIcon pointerIcon = child.getPointerIcon(event, point.x, point.y); |
| 1740 | if (pointerIcon != null) { |
| 1741 | return pointerIcon; |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 1742 | } |
| 1743 | break; |
| 1744 | } |
| 1745 | } |
| 1746 | } |
| 1747 | |
| 1748 | // The pointer is not a child or the child has no preferences, returning the default |
| 1749 | // implementation. |
Jun Mukai | d4eaef7 | 2015-10-30 15:54:33 -0700 | [diff] [blame] | 1750 | return super.getPointerIcon(event, x, y); |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 1751 | } |
| 1752 | |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 1753 | /** |
| 1754 | * {@inheritDoc} |
| 1755 | */ |
Romain Guy | a948927 | 2011-06-22 20:58:11 -0700 | [diff] [blame] | 1756 | @SuppressWarnings({"ConstantConditions"}) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1757 | @Override |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 1758 | protected boolean dispatchHoverEvent(MotionEvent event) { |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 1759 | final int action = event.getAction(); |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 1760 | |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 1761 | // First check whether the view group wants to intercept the hover event. |
| 1762 | final boolean interceptHover = onInterceptHoverEvent(event); |
| 1763 | event.setAction(action); // restore action in case it was changed |
| 1764 | |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 1765 | MotionEvent eventNoHistory = event; |
| 1766 | boolean handled = false; |
| 1767 | |
| 1768 | // Send events to the hovered children and build a new list of hover targets until |
| 1769 | // one is found that handles the event. |
| 1770 | HoverTarget firstOldHoverTarget = mFirstHoverTarget; |
| 1771 | mFirstHoverTarget = null; |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 1772 | if (!interceptHover && action != MotionEvent.ACTION_HOVER_EXIT) { |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 1773 | final float x = event.getX(); |
| 1774 | final float y = event.getY(); |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 1775 | final int childrenCount = mChildrenCount; |
| 1776 | if (childrenCount != 0) { |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 1777 | final ArrayList<View> preorderedList = buildOrderedChildList(); |
| 1778 | final boolean customOrder = preorderedList == null |
| 1779 | && isChildrenDrawingOrderEnabled(); |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 1780 | final View[] children = mChildren; |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 1781 | HoverTarget lastHoverTarget = null; |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 1782 | for (int i = childrenCount - 1; i >= 0; i--) { |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 1783 | int childIndex = customOrder ? getChildDrawingOrder(childrenCount, i) : i; |
| 1784 | final View child = (preorderedList == null) |
| 1785 | ? children[childIndex] : preorderedList.get(childIndex); |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 1786 | if (!canViewReceivePointerEvents(child) |
| 1787 | || !isTransformedTouchPointInView(x, y, child, null)) { |
| 1788 | continue; |
| 1789 | } |
| 1790 | |
| 1791 | // Obtain a hover target for this child. Dequeue it from the |
| 1792 | // old hover target list if the child was previously hovered. |
| 1793 | HoverTarget hoverTarget = firstOldHoverTarget; |
| 1794 | final boolean wasHovered; |
| 1795 | for (HoverTarget predecessor = null; ;) { |
| 1796 | if (hoverTarget == null) { |
| 1797 | hoverTarget = HoverTarget.obtain(child); |
| 1798 | wasHovered = false; |
| 1799 | break; |
| 1800 | } |
| 1801 | |
| 1802 | if (hoverTarget.child == child) { |
| 1803 | if (predecessor != null) { |
| 1804 | predecessor.next = hoverTarget.next; |
| 1805 | } else { |
| 1806 | firstOldHoverTarget = hoverTarget.next; |
| 1807 | } |
| 1808 | hoverTarget.next = null; |
| 1809 | wasHovered = true; |
| 1810 | break; |
| 1811 | } |
| 1812 | |
| 1813 | predecessor = hoverTarget; |
| 1814 | hoverTarget = hoverTarget.next; |
| 1815 | } |
| 1816 | |
| 1817 | // Enqueue the hover target onto the new hover target list. |
| 1818 | if (lastHoverTarget != null) { |
| 1819 | lastHoverTarget.next = hoverTarget; |
| 1820 | } else { |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 1821 | mFirstHoverTarget = hoverTarget; |
| 1822 | } |
Sangkyu Lee | 8725f36 | 2013-03-13 09:38:45 +0900 | [diff] [blame] | 1823 | lastHoverTarget = hoverTarget; |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 1824 | |
| 1825 | // Dispatch the event to the child. |
| 1826 | if (action == MotionEvent.ACTION_HOVER_ENTER) { |
| 1827 | if (!wasHovered) { |
| 1828 | // Send the enter as is. |
| 1829 | handled |= dispatchTransformedGenericPointerEvent( |
| 1830 | event, child); // enter |
| 1831 | } |
| 1832 | } else if (action == MotionEvent.ACTION_HOVER_MOVE) { |
| 1833 | if (!wasHovered) { |
| 1834 | // Synthesize an enter from a move. |
| 1835 | eventNoHistory = obtainMotionEventNoHistoryOrSelf(eventNoHistory); |
| 1836 | eventNoHistory.setAction(MotionEvent.ACTION_HOVER_ENTER); |
| 1837 | handled |= dispatchTransformedGenericPointerEvent( |
| 1838 | eventNoHistory, child); // enter |
| 1839 | eventNoHistory.setAction(action); |
| 1840 | |
| 1841 | handled |= dispatchTransformedGenericPointerEvent( |
| 1842 | eventNoHistory, child); // move |
| 1843 | } else { |
| 1844 | // Send the move as is. |
| 1845 | handled |= dispatchTransformedGenericPointerEvent(event, child); |
| 1846 | } |
| 1847 | } |
| 1848 | if (handled) { |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 1849 | break; |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 1850 | } |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 1851 | } |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 1852 | if (preorderedList != null) preorderedList.clear(); |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 1853 | } |
| 1854 | } |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 1855 | |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 1856 | // Send exit events to all previously hovered children that are no longer hovered. |
| 1857 | while (firstOldHoverTarget != null) { |
| 1858 | final View child = firstOldHoverTarget.child; |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 1859 | |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 1860 | // Exit the old hovered child. |
| 1861 | if (action == MotionEvent.ACTION_HOVER_EXIT) { |
| 1862 | // Send the exit as is. |
| 1863 | handled |= dispatchTransformedGenericPointerEvent( |
| 1864 | event, child); // exit |
| 1865 | } else { |
| 1866 | // Synthesize an exit from a move or enter. |
| 1867 | // Ignore the result because hover focus has moved to a different view. |
| 1868 | if (action == MotionEvent.ACTION_HOVER_MOVE) { |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 1869 | dispatchTransformedGenericPointerEvent( |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 1870 | event, child); // move |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 1871 | } |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 1872 | eventNoHistory = obtainMotionEventNoHistoryOrSelf(eventNoHistory); |
| 1873 | eventNoHistory.setAction(MotionEvent.ACTION_HOVER_EXIT); |
| 1874 | dispatchTransformedGenericPointerEvent( |
| 1875 | eventNoHistory, child); // exit |
| 1876 | eventNoHistory.setAction(action); |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 1877 | } |
| 1878 | |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 1879 | final HoverTarget nextOldHoverTarget = firstOldHoverTarget.next; |
| 1880 | firstOldHoverTarget.recycle(); |
| 1881 | firstOldHoverTarget = nextOldHoverTarget; |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 1882 | } |
| 1883 | |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 1884 | // 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] | 1885 | boolean newHoveredSelf = !handled; |
| 1886 | if (newHoveredSelf == mHoveredSelf) { |
| 1887 | if (newHoveredSelf) { |
| 1888 | // Send event to the view group as before. |
| 1889 | handled |= super.dispatchHoverEvent(event); |
| 1890 | } |
| 1891 | } else { |
| 1892 | if (mHoveredSelf) { |
| 1893 | // Exit the view group. |
| 1894 | if (action == MotionEvent.ACTION_HOVER_EXIT) { |
| 1895 | // Send the exit as is. |
| 1896 | handled |= super.dispatchHoverEvent(event); // exit |
| 1897 | } else { |
| 1898 | // Synthesize an exit from a move or enter. |
| 1899 | // Ignore the result because hover focus is moving to a different view. |
| 1900 | if (action == MotionEvent.ACTION_HOVER_MOVE) { |
| 1901 | super.dispatchHoverEvent(event); // move |
| 1902 | } |
| 1903 | eventNoHistory = obtainMotionEventNoHistoryOrSelf(eventNoHistory); |
| 1904 | eventNoHistory.setAction(MotionEvent.ACTION_HOVER_EXIT); |
| 1905 | super.dispatchHoverEvent(eventNoHistory); // exit |
| 1906 | eventNoHistory.setAction(action); |
| 1907 | } |
| 1908 | mHoveredSelf = false; |
| 1909 | } |
| 1910 | |
| 1911 | if (newHoveredSelf) { |
| 1912 | // Enter the view group. |
| 1913 | if (action == MotionEvent.ACTION_HOVER_ENTER) { |
| 1914 | // Send the enter as is. |
| 1915 | handled |= super.dispatchHoverEvent(event); // enter |
| 1916 | mHoveredSelf = true; |
| 1917 | } else if (action == MotionEvent.ACTION_HOVER_MOVE) { |
| 1918 | // Synthesize an enter from a move. |
| 1919 | eventNoHistory = obtainMotionEventNoHistoryOrSelf(eventNoHistory); |
| 1920 | eventNoHistory.setAction(MotionEvent.ACTION_HOVER_ENTER); |
| 1921 | handled |= super.dispatchHoverEvent(eventNoHistory); // enter |
| 1922 | eventNoHistory.setAction(action); |
| 1923 | |
| 1924 | handled |= super.dispatchHoverEvent(eventNoHistory); // move |
| 1925 | mHoveredSelf = true; |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 1926 | } |
| 1927 | } |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 1928 | } |
| 1929 | |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 1930 | // Recycle the copy of the event that we made. |
| 1931 | if (eventNoHistory != event) { |
| 1932 | eventNoHistory.recycle(); |
| 1933 | } |
| 1934 | |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 1935 | // Done. |
| 1936 | return handled; |
| 1937 | } |
| 1938 | |
Jeff Brown | 59a422e | 2012-04-19 15:19:19 -0700 | [diff] [blame] | 1939 | private void exitHoverTargets() { |
| 1940 | if (mHoveredSelf || mFirstHoverTarget != null) { |
| 1941 | final long now = SystemClock.uptimeMillis(); |
| 1942 | MotionEvent event = MotionEvent.obtain(now, now, |
| 1943 | MotionEvent.ACTION_HOVER_EXIT, 0.0f, 0.0f, 0); |
| 1944 | event.setSource(InputDevice.SOURCE_TOUCHSCREEN); |
| 1945 | dispatchHoverEvent(event); |
| 1946 | event.recycle(); |
| 1947 | } |
| 1948 | } |
| 1949 | |
| 1950 | private void cancelHoverTarget(View view) { |
| 1951 | HoverTarget predecessor = null; |
| 1952 | HoverTarget target = mFirstHoverTarget; |
| 1953 | while (target != null) { |
| 1954 | final HoverTarget next = target.next; |
| 1955 | if (target.child == view) { |
| 1956 | if (predecessor == null) { |
| 1957 | mFirstHoverTarget = next; |
| 1958 | } else { |
| 1959 | predecessor.next = next; |
| 1960 | } |
| 1961 | target.recycle(); |
| 1962 | |
| 1963 | final long now = SystemClock.uptimeMillis(); |
| 1964 | MotionEvent event = MotionEvent.obtain(now, now, |
| 1965 | MotionEvent.ACTION_HOVER_EXIT, 0.0f, 0.0f, 0); |
| 1966 | event.setSource(InputDevice.SOURCE_TOUCHSCREEN); |
| 1967 | view.dispatchHoverEvent(event); |
| 1968 | event.recycle(); |
| 1969 | return; |
| 1970 | } |
| 1971 | predecessor = target; |
| 1972 | target = next; |
| 1973 | } |
| 1974 | } |
| 1975 | |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 1976 | /** @hide */ |
| 1977 | @Override |
| 1978 | protected boolean hasHoveredChild() { |
| 1979 | return mFirstHoverTarget != null; |
| 1980 | } |
| 1981 | |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 1982 | @Override |
Adam Powell | cf392d1 | 2015-06-25 14:48:45 -0700 | [diff] [blame] | 1983 | public void addChildrenForAccessibility(ArrayList<View> outChildren) { |
Svetoslav | 762621c | 2015-06-01 17:21:59 -0700 | [diff] [blame] | 1984 | if (getAccessibilityNodeProvider() != null) { |
| 1985 | return; |
| 1986 | } |
Svetoslav Ganov | 76f287e | 2012-04-23 11:02:36 -0700 | [diff] [blame] | 1987 | ChildListForAccessibility children = ChildListForAccessibility.obtain(this, true); |
| 1988 | try { |
| 1989 | final int childrenCount = children.getChildCount(); |
| 1990 | for (int i = 0; i < childrenCount; i++) { |
| 1991 | View child = children.getChildAt(i); |
Svetoslav Ganov | c406be9 | 2012-05-11 16:12:32 -0700 | [diff] [blame] | 1992 | if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) { |
Svetoslav Ganov | 76f287e | 2012-04-23 11:02:36 -0700 | [diff] [blame] | 1993 | if (child.includeForAccessibility()) { |
Adam Powell | cf392d1 | 2015-06-25 14:48:45 -0700 | [diff] [blame] | 1994 | outChildren.add(child); |
Svetoslav Ganov | 76f287e | 2012-04-23 11:02:36 -0700 | [diff] [blame] | 1995 | } else { |
Adam Powell | cf392d1 | 2015-06-25 14:48:45 -0700 | [diff] [blame] | 1996 | child.addChildrenForAccessibility(outChildren); |
Svetoslav Ganov | 76f287e | 2012-04-23 11:02:36 -0700 | [diff] [blame] | 1997 | } |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 1998 | } |
| 1999 | } |
Svetoslav Ganov | 76f287e | 2012-04-23 11:02:36 -0700 | [diff] [blame] | 2000 | } finally { |
| 2001 | children.recycle(); |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 2002 | } |
| 2003 | } |
| 2004 | |
| 2005 | /** |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 2006 | * Implement this method to intercept hover events before they are handled |
| 2007 | * by child views. |
| 2008 | * <p> |
| 2009 | * This method is called before dispatching a hover event to a child of |
| 2010 | * the view group or to the view group's own {@link #onHoverEvent} to allow |
| 2011 | * the view group a chance to intercept the hover event. |
| 2012 | * This method can also be used to watch all pointer motions that occur within |
| 2013 | * the bounds of the view group even when the pointer is hovering over |
| 2014 | * a child of the view group rather than over the view group itself. |
| 2015 | * </p><p> |
| 2016 | * The view group can prevent its children from receiving hover events by |
| 2017 | * implementing this method and returning <code>true</code> to indicate |
| 2018 | * that it would like to intercept hover events. The view group must |
| 2019 | * continuously return <code>true</code> from {@link #onInterceptHoverEvent} |
| 2020 | * for as long as it wishes to continue intercepting hover events from |
| 2021 | * its children. |
| 2022 | * </p><p> |
| 2023 | * Interception preserves the invariant that at most one view can be |
| 2024 | * hovered at a time by transferring hover focus from the currently hovered |
| 2025 | * child to the view group or vice-versa as needed. |
| 2026 | * </p><p> |
| 2027 | * If this method returns <code>true</code> and a child is already hovered, then the |
| 2028 | * child view will first receive a hover exit event and then the view group |
| 2029 | * itself will receive a hover enter event in {@link #onHoverEvent}. |
| 2030 | * Likewise, if this method had previously returned <code>true</code> to intercept hover |
| 2031 | * events and instead returns <code>false</code> while the pointer is hovering |
| 2032 | * within the bounds of one of a child, then the view group will first receive a |
| 2033 | * hover exit event in {@link #onHoverEvent} and then the hovered child will |
| 2034 | * receive a hover enter event. |
| 2035 | * </p><p> |
| 2036 | * The default implementation always returns false. |
| 2037 | * </p> |
| 2038 | * |
| 2039 | * @param event The motion event that describes the hover. |
| 2040 | * @return True if the view group would like to intercept the hover event |
| 2041 | * and prevent its children from receiving it. |
| 2042 | */ |
| 2043 | public boolean onInterceptHoverEvent(MotionEvent event) { |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 2044 | return false; |
| 2045 | } |
| 2046 | |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2047 | private static MotionEvent obtainMotionEventNoHistoryOrSelf(MotionEvent event) { |
| 2048 | if (event.getHistorySize() == 0) { |
| 2049 | return event; |
| 2050 | } |
| 2051 | return MotionEvent.obtainNoHistory(event); |
| 2052 | } |
| 2053 | |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 2054 | /** |
| 2055 | * {@inheritDoc} |
| 2056 | */ |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2057 | @Override |
| 2058 | protected boolean dispatchGenericPointerEvent(MotionEvent event) { |
| 2059 | // Send the event to the child under the pointer. |
| 2060 | final int childrenCount = mChildrenCount; |
| 2061 | if (childrenCount != 0) { |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2062 | final float x = event.getX(); |
| 2063 | final float y = event.getY(); |
| 2064 | |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 2065 | final ArrayList<View> preorderedList = buildOrderedChildList(); |
| 2066 | final boolean customOrder = preorderedList == null |
| 2067 | && isChildrenDrawingOrderEnabled(); |
| 2068 | final View[] children = mChildren; |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2069 | for (int i = childrenCount - 1; i >= 0; i--) { |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 2070 | int childIndex = customOrder ? getChildDrawingOrder(childrenCount, i) : i; |
| 2071 | final View child = (preorderedList == null) |
| 2072 | ? children[childIndex] : preorderedList.get(childIndex); |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2073 | if (!canViewReceivePointerEvents(child) |
| 2074 | || !isTransformedTouchPointInView(x, y, child, null)) { |
| 2075 | continue; |
| 2076 | } |
| 2077 | |
| 2078 | if (dispatchTransformedGenericPointerEvent(event, child)) { |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 2079 | if (preorderedList != null) preorderedList.clear(); |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2080 | return true; |
| 2081 | } |
| 2082 | } |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 2083 | if (preorderedList != null) preorderedList.clear(); |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2084 | } |
| 2085 | |
| 2086 | // No child handled the event. Send it to this view group. |
| 2087 | return super.dispatchGenericPointerEvent(event); |
| 2088 | } |
| 2089 | |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 2090 | /** |
| 2091 | * {@inheritDoc} |
| 2092 | */ |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2093 | @Override |
| 2094 | protected boolean dispatchGenericFocusedEvent(MotionEvent event) { |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 2095 | // 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] | 2096 | if ((mPrivateFlags & (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) |
| 2097 | == (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) { |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2098 | return super.dispatchGenericFocusedEvent(event); |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 2099 | } else if (mFocused != null && (mFocused.mPrivateFlags & PFLAG_HAS_BOUNDS) |
| 2100 | == PFLAG_HAS_BOUNDS) { |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 2101 | return mFocused.dispatchGenericMotionEvent(event); |
| 2102 | } |
| 2103 | return false; |
| 2104 | } |
| 2105 | |
| 2106 | /** |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2107 | * Dispatches a generic pointer event to a child, taking into account |
| 2108 | * transformations that apply to the child. |
| 2109 | * |
| 2110 | * @param event The event to send. |
| 2111 | * @param child The view to send the event to. |
| 2112 | * @return {@code true} if the child handled the event. |
| 2113 | */ |
| 2114 | private boolean dispatchTransformedGenericPointerEvent(MotionEvent event, View child) { |
| 2115 | final float offsetX = mScrollX - child.mLeft; |
| 2116 | final float offsetY = mScrollY - child.mTop; |
| 2117 | |
| 2118 | boolean handled; |
| 2119 | if (!child.hasIdentityMatrix()) { |
| 2120 | MotionEvent transformedEvent = MotionEvent.obtain(event); |
| 2121 | transformedEvent.offsetLocation(offsetX, offsetY); |
| 2122 | transformedEvent.transform(child.getInverseMatrix()); |
| 2123 | handled = child.dispatchGenericMotionEvent(transformedEvent); |
| 2124 | transformedEvent.recycle(); |
| 2125 | } else { |
| 2126 | event.offsetLocation(offsetX, offsetY); |
| 2127 | handled = child.dispatchGenericMotionEvent(event); |
| 2128 | event.offsetLocation(-offsetX, -offsetY); |
| 2129 | } |
| 2130 | return handled; |
| 2131 | } |
| 2132 | |
| 2133 | /** |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 2134 | * {@inheritDoc} |
| 2135 | */ |
| 2136 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2137 | public boolean dispatchTouchEvent(MotionEvent ev) { |
Jeff Brown | 21bc5c9 | 2011-02-28 18:27:14 -0800 | [diff] [blame] | 2138 | if (mInputEventConsistencyVerifier != null) { |
| 2139 | mInputEventConsistencyVerifier.onTouchEvent(ev, 1); |
| 2140 | } |
| 2141 | |
Svet Ganov | 0a2ccee | 2015-02-06 10:12:32 -0800 | [diff] [blame] | 2142 | // If the event targets the accessibility focused view and this is it, start |
| 2143 | // normal event dispatch. Maybe a descendant is what will handle the click. |
Svet Ganov | f33fe1f | 2015-02-06 19:23:31 -0800 | [diff] [blame] | 2144 | if (ev.isTargetAccessibilityFocus() && isAccessibilityFocusedViewOrHost()) { |
Svet Ganov | 0a2ccee | 2015-02-06 10:12:32 -0800 | [diff] [blame] | 2145 | ev.setTargetAccessibilityFocus(false); |
| 2146 | } |
| 2147 | |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2148 | boolean handled = false; |
| 2149 | if (onFilterTouchEventForSecurity(ev)) { |
| 2150 | final int action = ev.getAction(); |
| 2151 | final int actionMasked = action & MotionEvent.ACTION_MASK; |
Jeff Brown | 85a3176 | 2010-09-01 17:01:00 -0700 | [diff] [blame] | 2152 | |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2153 | // Handle an initial down. |
| 2154 | if (actionMasked == MotionEvent.ACTION_DOWN) { |
| 2155 | // Throw away all previous state when starting a new touch gesture. |
| 2156 | // The framework may have dropped the up or cancel event for the previous gesture |
| 2157 | // due to an app switch, ANR, or some other state change. |
| 2158 | cancelAndClearTouchTargets(ev); |
| 2159 | resetTouchState(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2160 | } |
Adam Powell | b08013c | 2010-09-16 16:28:11 -0700 | [diff] [blame] | 2161 | |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2162 | // Check for interception. |
| 2163 | final boolean intercepted; |
Svet Ganov | f33fe1f | 2015-02-06 19:23:31 -0800 | [diff] [blame] | 2164 | if (actionMasked == MotionEvent.ACTION_DOWN |
| 2165 | || mFirstTouchTarget != null) { |
| 2166 | final boolean disallowIntercept = (mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0; |
| 2167 | if (!disallowIntercept) { |
| 2168 | intercepted = onInterceptTouchEvent(ev); |
| 2169 | ev.setAction(action); // restore action in case it was changed |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2170 | } else { |
Svet Ganov | f33fe1f | 2015-02-06 19:23:31 -0800 | [diff] [blame] | 2171 | intercepted = false; |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2172 | } |
| 2173 | } else { |
Svet Ganov | f33fe1f | 2015-02-06 19:23:31 -0800 | [diff] [blame] | 2174 | // There are no touch targets and this action is not an initial down |
| 2175 | // so this view group continues to intercept touches. |
| 2176 | intercepted = true; |
| 2177 | } |
| 2178 | |
| 2179 | // If intercepted, start normal event dispatch. Also if there is already |
| 2180 | // a view that is handling the gesture, do normal event dispatch. |
| 2181 | if (intercepted || mFirstTouchTarget != null) { |
| 2182 | ev.setTargetAccessibilityFocus(false); |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2183 | } |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2184 | |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2185 | // Check for cancelation. |
| 2186 | final boolean canceled = resetCancelNextUpFlag(this) |
| 2187 | || actionMasked == MotionEvent.ACTION_CANCEL; |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2188 | |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2189 | // Update list of touch targets for pointer down, if needed. |
| 2190 | final boolean split = (mGroupFlags & FLAG_SPLIT_MOTION_EVENTS) != 0; |
| 2191 | TouchTarget newTouchTarget = null; |
| 2192 | boolean alreadyDispatchedToNewTouchTarget = false; |
| 2193 | if (!canceled && !intercepted) { |
Svetoslav | c73cfa0 | 2015-02-09 17:14:28 -0800 | [diff] [blame] | 2194 | |
| 2195 | // If the event is targeting accessiiblity focus we give it to the |
| 2196 | // view that has accessibility focus and if it does not handle it |
| 2197 | // we clear the flag and dispatch the event to all children as usual. |
| 2198 | // We are looking up the accessibility focused host to avoid keeping |
| 2199 | // state since these events are very rare. |
| 2200 | View childWithAccessibilityFocus = ev.isTargetAccessibilityFocus() |
| 2201 | ? findChildWithAccessibilityFocus() : null; |
| 2202 | |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2203 | if (actionMasked == MotionEvent.ACTION_DOWN |
| 2204 | || (split && actionMasked == MotionEvent.ACTION_POINTER_DOWN) |
Svet Ganov | f33fe1f | 2015-02-06 19:23:31 -0800 | [diff] [blame] | 2205 | || actionMasked == MotionEvent.ACTION_HOVER_MOVE) { |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2206 | final int actionIndex = ev.getActionIndex(); // always 0 for down |
| 2207 | final int idBitsToAssign = split ? 1 << ev.getPointerId(actionIndex) |
| 2208 | : TouchTarget.ALL_POINTER_IDS; |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2209 | |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2210 | // Clean up earlier touch targets for this pointer id in case they |
| 2211 | // have become out of sync. |
| 2212 | removePointersFromTouchTargets(idBitsToAssign); |
| 2213 | |
| 2214 | final int childrenCount = mChildrenCount; |
Chet Haase | 9c17fe6 | 2013-03-22 17:05:55 -0700 | [diff] [blame] | 2215 | if (newTouchTarget == null && childrenCount != 0) { |
Chet Haase | edf6f4b | 2013-03-26 07:55:30 -0700 | [diff] [blame] | 2216 | final float x = ev.getX(actionIndex); |
| 2217 | final float y = ev.getY(actionIndex); |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2218 | // Find a child that can receive the event. |
| 2219 | // Scan children from front to back. |
Filip Gruszczynski | a33bdf3 | 2015-11-19 18:22:16 -0800 | [diff] [blame] | 2220 | final ArrayList<View> preorderedList = buildTouchDispatchChildList(); |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 2221 | final boolean customOrder = preorderedList == null |
| 2222 | && isChildrenDrawingOrderEnabled(); |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2223 | final View[] children = mChildren; |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2224 | for (int i = childrenCount - 1; i >= 0; i--) { |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 2225 | final int childIndex = customOrder |
| 2226 | ? getChildDrawingOrder(childrenCount, i) : i; |
| 2227 | final View child = (preorderedList == null) |
| 2228 | ? children[childIndex] : preorderedList.get(childIndex); |
Svetoslav | c73cfa0 | 2015-02-09 17:14:28 -0800 | [diff] [blame] | 2229 | |
| 2230 | // If there is a view that has accessibility focus we want it |
| 2231 | // to get the event first and if not handled we will perform a |
| 2232 | // normal dispatch. We may do a double iteration but this is |
| 2233 | // safer given the timeframe. |
| 2234 | if (childWithAccessibilityFocus != null) { |
| 2235 | if (childWithAccessibilityFocus != child) { |
| 2236 | continue; |
| 2237 | } |
| 2238 | childWithAccessibilityFocus = null; |
| 2239 | i = childrenCount - 1; |
| 2240 | } |
| 2241 | |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2242 | if (!canViewReceivePointerEvents(child) |
| 2243 | || !isTransformedTouchPointInView(x, y, child, null)) { |
Svetoslav | c73cfa0 | 2015-02-09 17:14:28 -0800 | [diff] [blame] | 2244 | ev.setTargetAccessibilityFocus(false); |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2245 | continue; |
| 2246 | } |
| 2247 | |
| 2248 | newTouchTarget = getTouchTarget(child); |
| 2249 | if (newTouchTarget != null) { |
| 2250 | // Child is already receiving touch within its bounds. |
| 2251 | // Give it the new pointer in addition to the ones it is handling. |
| 2252 | newTouchTarget.pointerIdBits |= idBitsToAssign; |
| 2253 | break; |
| 2254 | } |
| 2255 | |
| 2256 | resetCancelNextUpFlag(child); |
| 2257 | if (dispatchTransformedTouchEvent(ev, false, child, idBitsToAssign)) { |
| 2258 | // Child wants to receive touch within its bounds. |
| 2259 | mLastTouchDownTime = ev.getDownTime(); |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 2260 | if (preorderedList != null) { |
| 2261 | // childIndex points into presorted list, find original index |
| 2262 | for (int j = 0; j < childrenCount; j++) { |
| 2263 | if (children[childIndex] == mChildren[j]) { |
| 2264 | mLastTouchDownIndex = j; |
| 2265 | break; |
| 2266 | } |
| 2267 | } |
| 2268 | } else { |
| 2269 | mLastTouchDownIndex = childIndex; |
| 2270 | } |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2271 | mLastTouchDownX = ev.getX(); |
| 2272 | mLastTouchDownY = ev.getY(); |
| 2273 | newTouchTarget = addTouchTarget(child, idBitsToAssign); |
| 2274 | alreadyDispatchedToNewTouchTarget = true; |
| 2275 | break; |
| 2276 | } |
Svetoslav | c73cfa0 | 2015-02-09 17:14:28 -0800 | [diff] [blame] | 2277 | |
| 2278 | // The accessibility focus didn't handle the event, so clear |
| 2279 | // the flag and do a normal dispatch to all children. |
| 2280 | ev.setTargetAccessibilityFocus(false); |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2281 | } |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 2282 | if (preorderedList != null) preorderedList.clear(); |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2283 | } |
| 2284 | |
| 2285 | if (newTouchTarget == null && mFirstTouchTarget != null) { |
| 2286 | // Did not find a child to receive the event. |
| 2287 | // Assign the pointer to the least recently added target. |
| 2288 | newTouchTarget = mFirstTouchTarget; |
| 2289 | while (newTouchTarget.next != null) { |
| 2290 | newTouchTarget = newTouchTarget.next; |
| 2291 | } |
| 2292 | newTouchTarget.pointerIdBits |= idBitsToAssign; |
| 2293 | } |
| 2294 | } |
| 2295 | } |
| 2296 | |
| 2297 | // Dispatch to touch targets. |
| 2298 | if (mFirstTouchTarget == null) { |
| 2299 | // No touch targets so treat this as an ordinary view. |
| 2300 | handled = dispatchTransformedTouchEvent(ev, canceled, null, |
| 2301 | TouchTarget.ALL_POINTER_IDS); |
| 2302 | } else { |
| 2303 | // Dispatch to touch targets, excluding the new touch target if we already |
| 2304 | // dispatched to it. Cancel touch targets if necessary. |
| 2305 | TouchTarget predecessor = null; |
| 2306 | TouchTarget target = mFirstTouchTarget; |
| 2307 | while (target != null) { |
| 2308 | final TouchTarget next = target.next; |
| 2309 | if (alreadyDispatchedToNewTouchTarget && target == newTouchTarget) { |
| 2310 | handled = true; |
| 2311 | } else { |
| 2312 | final boolean cancelChild = resetCancelNextUpFlag(target.child) |
Chet Haase | 9c17fe6 | 2013-03-22 17:05:55 -0700 | [diff] [blame] | 2313 | || intercepted; |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2314 | if (dispatchTransformedTouchEvent(ev, cancelChild, |
| 2315 | target.child, target.pointerIdBits)) { |
| 2316 | handled = true; |
| 2317 | } |
| 2318 | if (cancelChild) { |
| 2319 | if (predecessor == null) { |
| 2320 | mFirstTouchTarget = next; |
| 2321 | } else { |
| 2322 | predecessor.next = next; |
| 2323 | } |
| 2324 | target.recycle(); |
| 2325 | target = next; |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2326 | continue; |
| 2327 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2328 | } |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2329 | predecessor = target; |
| 2330 | target = next; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2331 | } |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2332 | } |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2333 | |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2334 | // Update list of touch targets for pointer up or cancel, if needed. |
| 2335 | if (canceled |
| 2336 | || actionMasked == MotionEvent.ACTION_UP |
| 2337 | || actionMasked == MotionEvent.ACTION_HOVER_MOVE) { |
| 2338 | resetTouchState(); |
| 2339 | } else if (split && actionMasked == MotionEvent.ACTION_POINTER_UP) { |
| 2340 | final int actionIndex = ev.getActionIndex(); |
| 2341 | final int idBitsToRemove = 1 << ev.getPointerId(actionIndex); |
| 2342 | removePointersFromTouchTargets(idBitsToRemove); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2343 | } |
| 2344 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 2345 | |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2346 | if (!handled && mInputEventConsistencyVerifier != null) { |
| 2347 | mInputEventConsistencyVerifier.onUnhandledEvent(ev, 1); |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2348 | } |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2349 | return handled; |
| 2350 | } |
| 2351 | |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 2352 | /** |
Filip Gruszczynski | a33bdf3 | 2015-11-19 18:22:16 -0800 | [diff] [blame] | 2353 | * Provide custom ordering of views in which the touch will be dispatched. |
| 2354 | * |
| 2355 | * This is called within a tight loop, so you are not allowed to allocate objects, including |
| 2356 | * the return array. Instead, you should return a pre-allocated list that will be cleared |
| 2357 | * after the dispatch is finished. |
| 2358 | * @hide |
| 2359 | */ |
| 2360 | public ArrayList<View> buildTouchDispatchChildList() { |
| 2361 | return buildOrderedChildList(); |
| 2362 | } |
| 2363 | |
| 2364 | /** |
Svetoslav | c73cfa0 | 2015-02-09 17:14:28 -0800 | [diff] [blame] | 2365 | * Finds the child which has accessibility focus. |
| 2366 | * |
| 2367 | * @return The child that has focus. |
| 2368 | */ |
| 2369 | private View findChildWithAccessibilityFocus() { |
| 2370 | ViewRootImpl viewRoot = getViewRootImpl(); |
| 2371 | if (viewRoot == null) { |
| 2372 | return null; |
| 2373 | } |
| 2374 | |
| 2375 | View current = viewRoot.getAccessibilityFocusedHost(); |
| 2376 | if (current == null) { |
| 2377 | return null; |
| 2378 | } |
| 2379 | |
| 2380 | ViewParent parent = current.getParent(); |
| 2381 | while (parent instanceof View) { |
| 2382 | if (parent == this) { |
| 2383 | return current; |
| 2384 | } |
| 2385 | current = (View) parent; |
| 2386 | parent = current.getParent(); |
| 2387 | } |
| 2388 | |
| 2389 | return null; |
| 2390 | } |
| 2391 | |
| 2392 | /** |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 2393 | * Resets all touch state in preparation for a new cycle. |
| 2394 | */ |
| 2395 | private void resetTouchState() { |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2396 | clearTouchTargets(); |
| 2397 | resetCancelNextUpFlag(this); |
| 2398 | mGroupFlags &= ~FLAG_DISALLOW_INTERCEPT; |
Adam Powell | 10ba277 | 2014-04-15 09:46:51 -0700 | [diff] [blame] | 2399 | mNestedScrollAxes = SCROLL_AXIS_NONE; |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2400 | } |
| 2401 | |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 2402 | /** |
| 2403 | * Resets the cancel next up flag. |
| 2404 | * Returns true if the flag was previously set. |
| 2405 | */ |
Romain Guy | a998dff | 2012-03-23 18:58:36 -0700 | [diff] [blame] | 2406 | private static boolean resetCancelNextUpFlag(View view) { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 2407 | if ((view.mPrivateFlags & PFLAG_CANCEL_NEXT_UP_EVENT) != 0) { |
| 2408 | view.mPrivateFlags &= ~PFLAG_CANCEL_NEXT_UP_EVENT; |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2409 | return true; |
Adam Cohen | 9b07394 | 2010-08-19 16:49:52 -0700 | [diff] [blame] | 2410 | } |
| 2411 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2412 | } |
| 2413 | |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 2414 | /** |
| 2415 | * Clears all touch targets. |
| 2416 | */ |
| 2417 | private void clearTouchTargets() { |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2418 | TouchTarget target = mFirstTouchTarget; |
| 2419 | if (target != null) { |
| 2420 | do { |
| 2421 | TouchTarget next = target.next; |
| 2422 | target.recycle(); |
| 2423 | target = next; |
| 2424 | } while (target != null); |
| 2425 | mFirstTouchTarget = null; |
| 2426 | } |
| 2427 | } |
| 2428 | |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 2429 | /** |
| 2430 | * Cancels and clears all touch targets. |
| 2431 | */ |
| 2432 | private void cancelAndClearTouchTargets(MotionEvent event) { |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2433 | if (mFirstTouchTarget != null) { |
| 2434 | boolean syntheticEvent = false; |
| 2435 | if (event == null) { |
| 2436 | final long now = SystemClock.uptimeMillis(); |
| 2437 | event = MotionEvent.obtain(now, now, |
| 2438 | MotionEvent.ACTION_CANCEL, 0.0f, 0.0f, 0); |
Jeff Brown | 2fdbc5a | 2011-06-30 12:25:54 -0700 | [diff] [blame] | 2439 | event.setSource(InputDevice.SOURCE_TOUCHSCREEN); |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2440 | syntheticEvent = true; |
| 2441 | } |
| 2442 | |
| 2443 | for (TouchTarget target = mFirstTouchTarget; target != null; target = target.next) { |
| 2444 | resetCancelNextUpFlag(target.child); |
| 2445 | dispatchTransformedTouchEvent(event, true, target.child, target.pointerIdBits); |
| 2446 | } |
| 2447 | clearTouchTargets(); |
| 2448 | |
| 2449 | if (syntheticEvent) { |
| 2450 | event.recycle(); |
| 2451 | } |
| 2452 | } |
| 2453 | } |
| 2454 | |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 2455 | /** |
| 2456 | * Gets the touch target for specified child view. |
| 2457 | * Returns null if not found. |
| 2458 | */ |
| 2459 | private TouchTarget getTouchTarget(View child) { |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2460 | for (TouchTarget target = mFirstTouchTarget; target != null; target = target.next) { |
| 2461 | if (target.child == child) { |
| 2462 | return target; |
| 2463 | } |
| 2464 | } |
| 2465 | return null; |
| 2466 | } |
| 2467 | |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 2468 | /** |
| 2469 | * Adds a touch target for specified child to the beginning of the list. |
| 2470 | * Assumes the target child is not already present. |
| 2471 | */ |
| 2472 | private TouchTarget addTouchTarget(View child, int pointerIdBits) { |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2473 | TouchTarget target = TouchTarget.obtain(child, pointerIdBits); |
| 2474 | target.next = mFirstTouchTarget; |
| 2475 | mFirstTouchTarget = target; |
| 2476 | return target; |
| 2477 | } |
| 2478 | |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 2479 | /** |
| 2480 | * Removes the pointer ids from consideration. |
| 2481 | */ |
| 2482 | private void removePointersFromTouchTargets(int pointerIdBits) { |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2483 | TouchTarget predecessor = null; |
| 2484 | TouchTarget target = mFirstTouchTarget; |
| 2485 | while (target != null) { |
| 2486 | final TouchTarget next = target.next; |
| 2487 | if ((target.pointerIdBits & pointerIdBits) != 0) { |
| 2488 | target.pointerIdBits &= ~pointerIdBits; |
| 2489 | if (target.pointerIdBits == 0) { |
| 2490 | if (predecessor == null) { |
| 2491 | mFirstTouchTarget = next; |
| 2492 | } else { |
| 2493 | predecessor.next = next; |
| 2494 | } |
| 2495 | target.recycle(); |
| 2496 | target = next; |
| 2497 | continue; |
| 2498 | } |
| 2499 | } |
| 2500 | predecessor = target; |
| 2501 | target = next; |
| 2502 | } |
| 2503 | } |
| 2504 | |
Jeff Brown | 59a422e | 2012-04-19 15:19:19 -0700 | [diff] [blame] | 2505 | private void cancelTouchTarget(View view) { |
| 2506 | TouchTarget predecessor = null; |
| 2507 | TouchTarget target = mFirstTouchTarget; |
| 2508 | while (target != null) { |
| 2509 | final TouchTarget next = target.next; |
| 2510 | if (target.child == view) { |
| 2511 | if (predecessor == null) { |
| 2512 | mFirstTouchTarget = next; |
| 2513 | } else { |
| 2514 | predecessor.next = next; |
| 2515 | } |
| 2516 | target.recycle(); |
| 2517 | |
| 2518 | final long now = SystemClock.uptimeMillis(); |
| 2519 | MotionEvent event = MotionEvent.obtain(now, now, |
| 2520 | MotionEvent.ACTION_CANCEL, 0.0f, 0.0f, 0); |
| 2521 | event.setSource(InputDevice.SOURCE_TOUCHSCREEN); |
| 2522 | view.dispatchTouchEvent(event); |
| 2523 | event.recycle(); |
| 2524 | return; |
| 2525 | } |
| 2526 | predecessor = target; |
| 2527 | target = next; |
| 2528 | } |
| 2529 | } |
| 2530 | |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 2531 | /** |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2532 | * Returns true if a child view can receive pointer events. |
| 2533 | * @hide |
| 2534 | */ |
| 2535 | private static boolean canViewReceivePointerEvents(View child) { |
| 2536 | return (child.mViewFlags & VISIBILITY_MASK) == VISIBLE |
| 2537 | || child.getAnimation() != null; |
| 2538 | } |
| 2539 | |
Alan Viverette | b942b6f | 2014-12-08 10:37:39 -0800 | [diff] [blame] | 2540 | private float[] getTempPoint() { |
| 2541 | if (mTempPoint == null) { |
| 2542 | mTempPoint = new float[2]; |
| 2543 | } |
| 2544 | return mTempPoint; |
| 2545 | } |
| 2546 | |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2547 | /** |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 2548 | * Returns true if a child view contains the specified point when transformed |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2549 | * into its coordinate space. |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 2550 | * Child must not be null. |
Adam Cohen | a32edd4 | 2010-10-26 10:35:01 -0700 | [diff] [blame] | 2551 | * @hide |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 2552 | */ |
Adam Cohen | a32edd4 | 2010-10-26 10:35:01 -0700 | [diff] [blame] | 2553 | protected boolean isTransformedTouchPointInView(float x, float y, View child, |
Christopher Tate | 2c095f3 | 2010-10-04 14:13:40 -0700 | [diff] [blame] | 2554 | PointF outLocalPoint) { |
Alan Viverette | b942b6f | 2014-12-08 10:37:39 -0800 | [diff] [blame] | 2555 | final float[] point = getTempPoint(); |
| 2556 | point[0] = x; |
| 2557 | point[1] = y; |
| 2558 | transformPointToViewLocal(point, child); |
| 2559 | final boolean isInView = child.pointInView(point[0], point[1]); |
Christopher Tate | 2c095f3 | 2010-10-04 14:13:40 -0700 | [diff] [blame] | 2560 | if (isInView && outLocalPoint != null) { |
Alan Viverette | b942b6f | 2014-12-08 10:37:39 -0800 | [diff] [blame] | 2561 | outLocalPoint.set(point[0], point[1]); |
Christopher Tate | 2c095f3 | 2010-10-04 14:13:40 -0700 | [diff] [blame] | 2562 | } |
| 2563 | return isInView; |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 2564 | } |
| 2565 | |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 2566 | /** |
Alan Viverette | b942b6f | 2014-12-08 10:37:39 -0800 | [diff] [blame] | 2567 | * @hide |
| 2568 | */ |
| 2569 | public void transformPointToViewLocal(float[] point, View child) { |
| 2570 | point[0] += mScrollX - child.mLeft; |
| 2571 | point[1] += mScrollY - child.mTop; |
| 2572 | |
| 2573 | if (!child.hasIdentityMatrix()) { |
| 2574 | child.getInverseMatrix().mapPoints(point); |
| 2575 | } |
| 2576 | } |
| 2577 | |
| 2578 | /** |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 2579 | * 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] | 2580 | * filters out irrelevant pointer ids, and overrides its action if necessary. |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 2581 | * If child is null, assumes the MotionEvent will be sent to this ViewGroup instead. |
| 2582 | */ |
| 2583 | private boolean dispatchTransformedTouchEvent(MotionEvent event, boolean cancel, |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2584 | View child, int desiredPointerIdBits) { |
| 2585 | final boolean handled; |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 2586 | |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2587 | // Canceling motions is a special case. We don't need to perform any transformations |
| 2588 | // or filtering. The important part is the action, not the contents. |
| 2589 | final int oldAction = event.getAction(); |
| 2590 | if (cancel || oldAction == MotionEvent.ACTION_CANCEL) { |
| 2591 | event.setAction(MotionEvent.ACTION_CANCEL); |
| 2592 | if (child == null) { |
| 2593 | handled = super.dispatchTouchEvent(event); |
| 2594 | } else { |
| 2595 | handled = child.dispatchTouchEvent(event); |
| 2596 | } |
| 2597 | event.setAction(oldAction); |
| 2598 | return handled; |
| 2599 | } |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 2600 | |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2601 | // Calculate the number of pointers to deliver. |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 2602 | final int oldPointerIdBits = event.getPointerIdBits(); |
| 2603 | final int newPointerIdBits = oldPointerIdBits & desiredPointerIdBits; |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 2604 | |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2605 | // If for some reason we ended up in an inconsistent state where it looks like we |
| 2606 | // 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] | 2607 | if (newPointerIdBits == 0) { |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2608 | return false; |
| 2609 | } |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 2610 | |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2611 | // If the number of pointers is the same and we don't need to perform any fancy |
| 2612 | // irreversible transformations, then we can reuse the motion event for this |
| 2613 | // 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] | 2614 | // Otherwise we need to make a copy. |
| 2615 | final MotionEvent transformedEvent; |
| 2616 | if (newPointerIdBits == oldPointerIdBits) { |
| 2617 | if (child == null || child.hasIdentityMatrix()) { |
| 2618 | if (child == null) { |
| 2619 | handled = super.dispatchTouchEvent(event); |
| 2620 | } else { |
| 2621 | final float offsetX = mScrollX - child.mLeft; |
| 2622 | final float offsetY = mScrollY - child.mTop; |
| 2623 | event.offsetLocation(offsetX, offsetY); |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 2624 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 2625 | handled = child.dispatchTouchEvent(event); |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2626 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 2627 | event.offsetLocation(-offsetX, -offsetY); |
| 2628 | } |
| 2629 | return handled; |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2630 | } |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2631 | transformedEvent = MotionEvent.obtain(event); |
| 2632 | } else { |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 2633 | transformedEvent = event.split(newPointerIdBits); |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 2634 | } |
| 2635 | |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2636 | // Perform any necessary transformations and dispatch. |
| 2637 | if (child == null) { |
| 2638 | handled = super.dispatchTouchEvent(transformedEvent); |
| 2639 | } else { |
| 2640 | final float offsetX = mScrollX - child.mLeft; |
| 2641 | final float offsetY = mScrollY - child.mTop; |
| 2642 | transformedEvent.offsetLocation(offsetX, offsetY); |
| 2643 | if (! child.hasIdentityMatrix()) { |
| 2644 | transformedEvent.transform(child.getInverseMatrix()); |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 2645 | } |
| 2646 | |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2647 | handled = child.dispatchTouchEvent(transformedEvent); |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 2648 | } |
| 2649 | |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2650 | // Done. |
| 2651 | transformedEvent.recycle(); |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 2652 | return handled; |
| 2653 | } |
| 2654 | |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 2655 | /** |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 2656 | * 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] | 2657 | * dispatch. This behavior is enabled by default for applications that target an |
| 2658 | * SDK version of {@link Build.VERSION_CODES#HONEYCOMB} or newer. |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 2659 | * |
| 2660 | * <p>When this option is enabled MotionEvents may be split and dispatched to different child |
| 2661 | * views depending on where each pointer initially went down. This allows for user interactions |
| 2662 | * such as scrolling two panes of content independently, chording of buttons, and performing |
| 2663 | * independent gestures on different pieces of content. |
| 2664 | * |
| 2665 | * @param split <code>true</code> to allow MotionEvents to be split and dispatched to multiple |
| 2666 | * child views. <code>false</code> to only allow one child view to be the target of |
| 2667 | * any MotionEvent received by this ViewGroup. |
Scott Main | 27a8508 | 2013-06-10 10:39:48 -0700 | [diff] [blame] | 2668 | * @attr ref android.R.styleable#ViewGroup_splitMotionEvents |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 2669 | */ |
| 2670 | public void setMotionEventSplittingEnabled(boolean split) { |
| 2671 | // TODO Applications really shouldn't change this setting mid-touch event, |
| 2672 | // but perhaps this should handle that case and send ACTION_CANCELs to any child views |
| 2673 | // with gestures in progress when this is changed. |
| 2674 | if (split) { |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 2675 | mGroupFlags |= FLAG_SPLIT_MOTION_EVENTS; |
| 2676 | } else { |
| 2677 | mGroupFlags &= ~FLAG_SPLIT_MOTION_EVENTS; |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 2678 | } |
| 2679 | } |
| 2680 | |
| 2681 | /** |
Jeff Brown | 995e774 | 2010-12-22 16:59:36 -0800 | [diff] [blame] | 2682 | * 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] | 2683 | * @return true if MotionEvents dispatched to this ViewGroup can be split to multiple children. |
| 2684 | */ |
| 2685 | public boolean isMotionEventSplittingEnabled() { |
| 2686 | return (mGroupFlags & FLAG_SPLIT_MOTION_EVENTS) == FLAG_SPLIT_MOTION_EVENTS; |
| 2687 | } |
| 2688 | |
| 2689 | /** |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 2690 | * Returns true if this ViewGroup should be considered as a single entity for removal |
| 2691 | * when executing an Activity transition. If this is false, child elements will move |
| 2692 | * individually during the transition. |
George Mount | 427c641 | 2014-11-05 16:45:36 -0800 | [diff] [blame] | 2693 | * |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 2694 | * @return True if the ViewGroup should be acted on together during an Activity transition. |
George Mount | 427c641 | 2014-11-05 16:45:36 -0800 | [diff] [blame] | 2695 | * The default value is true when there is a non-null background or if |
| 2696 | * {@link #getTransitionName()} is not null or if a |
| 2697 | * non-null {@link android.view.ViewOutlineProvider} other than |
| 2698 | * {@link android.view.ViewOutlineProvider#BACKGROUND} was given to |
| 2699 | * {@link #setOutlineProvider(ViewOutlineProvider)} and false otherwise. |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 2700 | */ |
| 2701 | public boolean isTransitionGroup() { |
| 2702 | if ((mGroupFlags & FLAG_IS_TRANSITION_GROUP_SET) != 0) { |
| 2703 | return ((mGroupFlags & FLAG_IS_TRANSITION_GROUP) != 0); |
| 2704 | } else { |
George Mount | 427c641 | 2014-11-05 16:45:36 -0800 | [diff] [blame] | 2705 | final ViewOutlineProvider outlineProvider = getOutlineProvider(); |
| 2706 | return getBackground() != null || getTransitionName() != null || |
| 2707 | (outlineProvider != null && outlineProvider != ViewOutlineProvider.BACKGROUND); |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 2708 | } |
| 2709 | } |
| 2710 | |
| 2711 | /** |
| 2712 | * 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] | 2713 | * Activity Transitions. |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 2714 | * @param isTransitionGroup Whether or not the ViewGroup should be treated as a unit |
| 2715 | * in Activity transitions. If false, the ViewGroup won't transition, |
| 2716 | * only its children. If true, the entire ViewGroup will transition |
| 2717 | * together. |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 2718 | * @see android.app.ActivityOptions#makeSceneTransitionAnimation(android.app.Activity, |
| 2719 | * android.util.Pair[]) |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 2720 | */ |
| 2721 | public void setTransitionGroup(boolean isTransitionGroup) { |
| 2722 | mGroupFlags |= FLAG_IS_TRANSITION_GROUP_SET; |
| 2723 | if (isTransitionGroup) { |
| 2724 | mGroupFlags |= FLAG_IS_TRANSITION_GROUP; |
| 2725 | } else { |
| 2726 | mGroupFlags &= ~FLAG_IS_TRANSITION_GROUP; |
| 2727 | } |
| 2728 | } |
| 2729 | |
| 2730 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2731 | * {@inheritDoc} |
| 2732 | */ |
| 2733 | public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) { |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 2734 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2735 | if (disallowIntercept == ((mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0)) { |
| 2736 | // We're already in this state, assume our ancestors are too |
| 2737 | return; |
| 2738 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 2739 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2740 | if (disallowIntercept) { |
| 2741 | mGroupFlags |= FLAG_DISALLOW_INTERCEPT; |
| 2742 | } else { |
| 2743 | mGroupFlags &= ~FLAG_DISALLOW_INTERCEPT; |
| 2744 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 2745 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2746 | // Pass it up to our parent |
| 2747 | if (mParent != null) { |
| 2748 | mParent.requestDisallowInterceptTouchEvent(disallowIntercept); |
| 2749 | } |
| 2750 | } |
| 2751 | |
| 2752 | /** |
| 2753 | * Implement this method to intercept all touch screen motion events. This |
| 2754 | * allows you to watch events as they are dispatched to your children, and |
| 2755 | * take ownership of the current gesture at any point. |
| 2756 | * |
| 2757 | * <p>Using this function takes some care, as it has a fairly complicated |
| 2758 | * interaction with {@link View#onTouchEvent(MotionEvent) |
| 2759 | * View.onTouchEvent(MotionEvent)}, and using it requires implementing |
| 2760 | * that method as well as this one in the correct way. Events will be |
| 2761 | * received in the following order: |
| 2762 | * |
| 2763 | * <ol> |
| 2764 | * <li> You will receive the down event here. |
| 2765 | * <li> The down event will be handled either by a child of this view |
| 2766 | * group, or given to your own onTouchEvent() method to handle; this means |
| 2767 | * you should implement onTouchEvent() to return true, so you will |
| 2768 | * continue to see the rest of the gesture (instead of looking for |
| 2769 | * a parent view to handle it). Also, by returning true from |
| 2770 | * onTouchEvent(), you will not receive any following |
| 2771 | * events in onInterceptTouchEvent() and all touch processing must |
| 2772 | * happen in onTouchEvent() like normal. |
| 2773 | * <li> For as long as you return false from this function, each following |
| 2774 | * event (up to and including the final up) will be delivered first here |
| 2775 | * and then to the target's onTouchEvent(). |
| 2776 | * <li> If you return true from here, you will not receive any |
| 2777 | * following events: the target view will receive the same event but |
| 2778 | * with the action {@link MotionEvent#ACTION_CANCEL}, and all further |
| 2779 | * events will be delivered to your onTouchEvent() method and no longer |
| 2780 | * appear here. |
| 2781 | * </ol> |
| 2782 | * |
| 2783 | * @param ev The motion event being dispatched down the hierarchy. |
| 2784 | * @return Return true to steal motion events from the children and have |
| 2785 | * them dispatched to this ViewGroup through onTouchEvent(). |
| 2786 | * The current target will receive an ACTION_CANCEL event, and no further |
| 2787 | * messages will be delivered here. |
| 2788 | */ |
| 2789 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
| 2790 | return false; |
| 2791 | } |
| 2792 | |
| 2793 | /** |
| 2794 | * {@inheritDoc} |
| 2795 | * |
| 2796 | * Looks for a view to give focus to respecting the setting specified by |
| 2797 | * {@link #getDescendantFocusability()}. |
| 2798 | * |
| 2799 | * Uses {@link #onRequestFocusInDescendants(int, android.graphics.Rect)} to |
| 2800 | * find focus within the children of this group when appropriate. |
| 2801 | * |
| 2802 | * @see #FOCUS_BEFORE_DESCENDANTS |
| 2803 | * @see #FOCUS_AFTER_DESCENDANTS |
| 2804 | * @see #FOCUS_BLOCK_DESCENDANTS |
Filip Gruszczynski | a33bdf3 | 2015-11-19 18:22:16 -0800 | [diff] [blame] | 2805 | * @see #onRequestFocusInDescendants(int, android.graphics.Rect) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2806 | */ |
| 2807 | @Override |
| 2808 | public boolean requestFocus(int direction, Rect previouslyFocusedRect) { |
| 2809 | if (DBG) { |
| 2810 | System.out.println(this + " ViewGroup.requestFocus direction=" |
| 2811 | + direction); |
| 2812 | } |
| 2813 | int descendantFocusability = getDescendantFocusability(); |
| 2814 | |
| 2815 | switch (descendantFocusability) { |
| 2816 | case FOCUS_BLOCK_DESCENDANTS: |
| 2817 | return super.requestFocus(direction, previouslyFocusedRect); |
| 2818 | case FOCUS_BEFORE_DESCENDANTS: { |
| 2819 | final boolean took = super.requestFocus(direction, previouslyFocusedRect); |
| 2820 | return took ? took : onRequestFocusInDescendants(direction, previouslyFocusedRect); |
| 2821 | } |
| 2822 | case FOCUS_AFTER_DESCENDANTS: { |
| 2823 | final boolean took = onRequestFocusInDescendants(direction, previouslyFocusedRect); |
| 2824 | return took ? took : super.requestFocus(direction, previouslyFocusedRect); |
| 2825 | } |
| 2826 | default: |
| 2827 | throw new IllegalStateException("descendant focusability must be " |
| 2828 | + "one of FOCUS_BEFORE_DESCENDANTS, FOCUS_AFTER_DESCENDANTS, FOCUS_BLOCK_DESCENDANTS " |
| 2829 | + "but is " + descendantFocusability); |
| 2830 | } |
| 2831 | } |
| 2832 | |
| 2833 | /** |
| 2834 | * Look for a descendant to call {@link View#requestFocus} on. |
| 2835 | * Called by {@link ViewGroup#requestFocus(int, android.graphics.Rect)} |
| 2836 | * when it wants to request focus within its children. Override this to |
| 2837 | * customize how your {@link ViewGroup} requests focus within its children. |
| 2838 | * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT |
| 2839 | * @param previouslyFocusedRect The rectangle (in this View's coordinate system) |
| 2840 | * to give a finer grained hint about where focus is coming from. May be null |
| 2841 | * if there is no hint. |
| 2842 | * @return Whether focus was taken. |
| 2843 | */ |
| 2844 | @SuppressWarnings({"ConstantConditions"}) |
| 2845 | protected boolean onRequestFocusInDescendants(int direction, |
| 2846 | Rect previouslyFocusedRect) { |
| 2847 | int index; |
| 2848 | int increment; |
| 2849 | int end; |
| 2850 | int count = mChildrenCount; |
| 2851 | if ((direction & FOCUS_FORWARD) != 0) { |
| 2852 | index = 0; |
| 2853 | increment = 1; |
| 2854 | end = count; |
| 2855 | } else { |
| 2856 | index = count - 1; |
| 2857 | increment = -1; |
| 2858 | end = -1; |
| 2859 | } |
| 2860 | final View[] children = mChildren; |
| 2861 | for (int i = index; i != end; i += increment) { |
| 2862 | View child = children[i]; |
| 2863 | if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) { |
| 2864 | if (child.requestFocus(direction, previouslyFocusedRect)) { |
| 2865 | return true; |
| 2866 | } |
| 2867 | } |
| 2868 | } |
| 2869 | return false; |
| 2870 | } |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 2871 | |
Romain Guy | a440b00 | 2010-02-24 15:57:54 -0800 | [diff] [blame] | 2872 | /** |
| 2873 | * {@inheritDoc} |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 2874 | * |
Romain Guy | dcc490f | 2010-02-24 17:59:35 -0800 | [diff] [blame] | 2875 | * @hide |
Romain Guy | a440b00 | 2010-02-24 15:57:54 -0800 | [diff] [blame] | 2876 | */ |
| 2877 | @Override |
| 2878 | public void dispatchStartTemporaryDetach() { |
| 2879 | super.dispatchStartTemporaryDetach(); |
| 2880 | final int count = mChildrenCount; |
| 2881 | final View[] children = mChildren; |
| 2882 | for (int i = 0; i < count; i++) { |
| 2883 | children[i].dispatchStartTemporaryDetach(); |
| 2884 | } |
| 2885 | } |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 2886 | |
Romain Guy | a440b00 | 2010-02-24 15:57:54 -0800 | [diff] [blame] | 2887 | /** |
| 2888 | * {@inheritDoc} |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 2889 | * |
Romain Guy | dcc490f | 2010-02-24 17:59:35 -0800 | [diff] [blame] | 2890 | * @hide |
Romain Guy | a440b00 | 2010-02-24 15:57:54 -0800 | [diff] [blame] | 2891 | */ |
| 2892 | @Override |
| 2893 | public void dispatchFinishTemporaryDetach() { |
| 2894 | super.dispatchFinishTemporaryDetach(); |
| 2895 | final int count = mChildrenCount; |
| 2896 | final View[] children = mChildren; |
| 2897 | for (int i = 0; i < count; i++) { |
| 2898 | children[i].dispatchFinishTemporaryDetach(); |
| 2899 | } |
| 2900 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2901 | |
| 2902 | /** |
| 2903 | * {@inheritDoc} |
| 2904 | */ |
| 2905 | @Override |
| 2906 | void dispatchAttachedToWindow(AttachInfo info, int visibility) { |
Adam Powell | 4b86788 | 2011-09-16 12:59:46 -0700 | [diff] [blame] | 2907 | mGroupFlags |= FLAG_PREVENT_DISPATCH_ATTACHED_TO_WINDOW; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2908 | super.dispatchAttachedToWindow(info, visibility); |
Adam Powell | 4b86788 | 2011-09-16 12:59:46 -0700 | [diff] [blame] | 2909 | mGroupFlags &= ~FLAG_PREVENT_DISPATCH_ATTACHED_TO_WINDOW; |
| 2910 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2911 | final int count = mChildrenCount; |
| 2912 | final View[] children = mChildren; |
| 2913 | for (int i = 0; i < count; i++) { |
Dianne Hackborn | 3a3a6cf | 2012-03-26 10:24:04 -0700 | [diff] [blame] | 2914 | final View child = children[i]; |
| 2915 | child.dispatchAttachedToWindow(info, |
Adam Powell | eb2b0af | 2015-05-20 18:26:35 -0700 | [diff] [blame] | 2916 | combineVisibility(visibility, child.getVisibility())); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2917 | } |
Chet Haase | c633d2f | 2015-04-07 10:29:39 -0700 | [diff] [blame] | 2918 | final int transientCount = mTransientIndices == null ? 0 : mTransientIndices.size(); |
| 2919 | for (int i = 0; i < transientCount; ++i) { |
| 2920 | View view = mTransientViews.get(i); |
Adam Powell | eb2b0af | 2015-05-20 18:26:35 -0700 | [diff] [blame] | 2921 | view.dispatchAttachedToWindow(info, |
| 2922 | combineVisibility(visibility, view.getVisibility())); |
Chet Haase | c633d2f | 2015-04-07 10:29:39 -0700 | [diff] [blame] | 2923 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2924 | } |
| 2925 | |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 2926 | @Override |
Romain Guy | bb9908b | 2012-03-08 11:14:07 -0800 | [diff] [blame] | 2927 | void dispatchScreenStateChanged(int screenState) { |
| 2928 | super.dispatchScreenStateChanged(screenState); |
| 2929 | |
| 2930 | final int count = mChildrenCount; |
| 2931 | final View[] children = mChildren; |
| 2932 | for (int i = 0; i < count; i++) { |
| 2933 | children[i].dispatchScreenStateChanged(screenState); |
| 2934 | } |
| 2935 | } |
| 2936 | |
Alan Viverette | a54956a | 2015-01-07 16:05:02 -0800 | [diff] [blame] | 2937 | /** @hide */ |
Romain Guy | bb9908b | 2012-03-08 11:14:07 -0800 | [diff] [blame] | 2938 | @Override |
Alan Viverette | a54956a | 2015-01-07 16:05:02 -0800 | [diff] [blame] | 2939 | public boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) { |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 2940 | boolean handled = false; |
| 2941 | if (includeForAccessibility()) { |
| 2942 | handled = super.dispatchPopulateAccessibilityEventInternal(event); |
| 2943 | if (handled) { |
| 2944 | return handled; |
| 2945 | } |
Svetoslav Ganov | b84b94e | 2011-09-22 19:26:56 -0700 | [diff] [blame] | 2946 | } |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 2947 | // Let our children have a shot in populating the event. |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 2948 | ChildListForAccessibility children = ChildListForAccessibility.obtain(this, true); |
Svetoslav Ganov | 76f287e | 2012-04-23 11:02:36 -0700 | [diff] [blame] | 2949 | try { |
| 2950 | final int childCount = children.getChildCount(); |
| 2951 | for (int i = 0; i < childCount; i++) { |
| 2952 | View child = children.getChildAt(i); |
| 2953 | if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) { |
| 2954 | handled = child.dispatchPopulateAccessibilityEvent(event); |
| 2955 | if (handled) { |
Svetoslav Ganov | 76f287e | 2012-04-23 11:02:36 -0700 | [diff] [blame] | 2956 | return handled; |
| 2957 | } |
Svetoslav Ganov | 6179ea3 | 2011-06-28 01:12:41 -0700 | [diff] [blame] | 2958 | } |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 2959 | } |
Svetoslav Ganov | 76f287e | 2012-04-23 11:02:36 -0700 | [diff] [blame] | 2960 | } finally { |
| 2961 | children.recycle(); |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 2962 | } |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 2963 | return false; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 2964 | } |
| 2965 | |
Dianne Hackborn | 6251f0d | 2015-04-01 16:45:03 -0700 | [diff] [blame] | 2966 | /** |
Dianne Hackborn | 49b043f | 2015-05-07 14:21:38 -0700 | [diff] [blame] | 2967 | * Dispatch creation of {@link ViewStructure} down the hierarchy. This implementation |
Dianne Hackborn | 6251f0d | 2015-04-01 16:45:03 -0700 | [diff] [blame] | 2968 | * adds in all child views of the view group, in addition to calling the default View |
| 2969 | * implementation. |
| 2970 | */ |
Dianne Hackborn | 49b043f | 2015-05-07 14:21:38 -0700 | [diff] [blame] | 2971 | public void dispatchProvideStructure(ViewStructure structure) { |
| 2972 | super.dispatchProvideStructure(structure); |
Dianne Hackborn | 799d8d0 | 2015-04-22 16:36:39 -0700 | [diff] [blame] | 2973 | if (!isAssistBlocked()) { |
| 2974 | if (structure.getChildCount() == 0) { |
| 2975 | final int childrenCount = getChildCount(); |
| 2976 | if (childrenCount > 0) { |
| 2977 | structure.setChildCount(childrenCount); |
Dianne Hackborn | 6ff55fc | 2015-08-05 18:07:31 -0700 | [diff] [blame] | 2978 | ArrayList<View> preorderedList = buildOrderedChildList(); |
| 2979 | boolean customOrder = preorderedList == null |
Dianne Hackborn | 799d8d0 | 2015-04-22 16:36:39 -0700 | [diff] [blame] | 2980 | && isChildrenDrawingOrderEnabled(); |
| 2981 | final View[] children = mChildren; |
| 2982 | for (int i=0; i<childrenCount; i++) { |
Dianne Hackborn | 6ff55fc | 2015-08-05 18:07:31 -0700 | [diff] [blame] | 2983 | int childIndex; |
| 2984 | try { |
| 2985 | childIndex = customOrder ? getChildDrawingOrder(childrenCount, i) : i; |
| 2986 | } catch (IndexOutOfBoundsException e) { |
| 2987 | childIndex = i; |
| 2988 | if (mContext.getApplicationInfo().targetSdkVersion |
| 2989 | < Build.VERSION_CODES.M) { |
| 2990 | Log.w(TAG, "Bad getChildDrawingOrder while collecting assist @ " |
| 2991 | + i + " of " + childrenCount, e); |
| 2992 | // At least one app is failing when we call getChildDrawingOrder |
| 2993 | // at this point, so deal semi-gracefully with it by falling back |
| 2994 | // on the basic order. |
| 2995 | customOrder = false; |
| 2996 | if (i > 0) { |
| 2997 | // If we failed at the first index, there really isn't |
| 2998 | // anything to do -- we will just proceed with the simple |
| 2999 | // sequence order. |
| 3000 | // Otherwise, we failed in the middle, so need to come up |
| 3001 | // with an order for the remaining indices and use that. |
| 3002 | // Failed at the first one, easy peasy. |
| 3003 | int[] permutation = new int[childrenCount]; |
| 3004 | SparseBooleanArray usedIndices = new SparseBooleanArray(); |
| 3005 | // Go back and collected the indices we have done so far. |
| 3006 | for (int j=0; j<i; j++) { |
| 3007 | permutation[j] = getChildDrawingOrder(childrenCount, j); |
| 3008 | usedIndices.put(permutation[j], true); |
| 3009 | } |
| 3010 | // Fill in the remaining indices with indices that have not |
| 3011 | // yet been used. |
| 3012 | int nextIndex = 0; |
| 3013 | for (int j=i; j<childrenCount; j++) { |
| 3014 | while (usedIndices.get(nextIndex, false)) { |
| 3015 | nextIndex++; |
| 3016 | } |
| 3017 | permutation[j] = nextIndex; |
| 3018 | nextIndex++; |
| 3019 | } |
| 3020 | // Build the final view list. |
| 3021 | preorderedList = new ArrayList<>(childrenCount); |
| 3022 | for (int j=0; j<childrenCount; j++) { |
| 3023 | preorderedList.add(children[permutation[j]]); |
| 3024 | } |
| 3025 | } |
| 3026 | } else { |
| 3027 | throw e; |
| 3028 | } |
| 3029 | } |
Dianne Hackborn | 799d8d0 | 2015-04-22 16:36:39 -0700 | [diff] [blame] | 3030 | final View child = (preorderedList == null) |
| 3031 | ? children[childIndex] : preorderedList.get(childIndex); |
Dianne Hackborn | 49b043f | 2015-05-07 14:21:38 -0700 | [diff] [blame] | 3032 | ViewStructure cstructure = structure.newChild(i); |
| 3033 | child.dispatchProvideStructure(cstructure); |
Dianne Hackborn | 799d8d0 | 2015-04-22 16:36:39 -0700 | [diff] [blame] | 3034 | } |
Dianne Hackborn | 6251f0d | 2015-04-01 16:45:03 -0700 | [diff] [blame] | 3035 | } |
| 3036 | } |
| 3037 | } |
| 3038 | } |
| 3039 | |
Alan Viverette | a54956a | 2015-01-07 16:05:02 -0800 | [diff] [blame] | 3040 | /** @hide */ |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 3041 | @Override |
Alan Viverette | a54956a | 2015-01-07 16:05:02 -0800 | [diff] [blame] | 3042 | public void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) { |
Svetoslav Ganov | 031d9c1 | 2011-09-09 16:41:13 -0700 | [diff] [blame] | 3043 | super.onInitializeAccessibilityNodeInfoInternal(info); |
Svet Ganov | 55bdb10 | 2015-02-06 12:41:17 -0800 | [diff] [blame] | 3044 | if (getAccessibilityNodeProvider() != null) { |
| 3045 | return; |
| 3046 | } |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 3047 | if (mAttachInfo != null) { |
Alan Viverette | f0aed09 | 2013-11-06 15:33:03 -0800 | [diff] [blame] | 3048 | final ArrayList<View> childrenForAccessibility = mAttachInfo.mTempArrayList; |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 3049 | childrenForAccessibility.clear(); |
| 3050 | addChildrenForAccessibility(childrenForAccessibility); |
| 3051 | final int childrenForAccessibilityCount = childrenForAccessibility.size(); |
| 3052 | for (int i = 0; i < childrenForAccessibilityCount; i++) { |
Alan Viverette | f0aed09 | 2013-11-06 15:33:03 -0800 | [diff] [blame] | 3053 | final View child = childrenForAccessibility.get(i); |
| 3054 | info.addChildUnchecked(child); |
Svetoslav Ganov | ea1da3d | 2011-06-15 17:16:02 -0700 | [diff] [blame] | 3055 | } |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 3056 | childrenForAccessibility.clear(); |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 3057 | } |
| 3058 | } |
| 3059 | |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 3060 | @Override |
Maxim Bogatov | f399af3 | 2015-06-16 15:15:49 -0700 | [diff] [blame] | 3061 | public CharSequence getAccessibilityClassName() { |
| 3062 | return ViewGroup.class.getName(); |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 3063 | } |
| 3064 | |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 3065 | @Override |
Alan Viverette | 77e9a28 | 2013-09-12 17:16:09 -0700 | [diff] [blame] | 3066 | public void notifySubtreeAccessibilityStateChanged(View child, View source, int changeType) { |
| 3067 | // If this is a live region, we should send a subtree change event |
| 3068 | // from this view. Otherwise, we can let it propagate up. |
| 3069 | if (getAccessibilityLiveRegion() != ACCESSIBILITY_LIVE_REGION_NONE) { |
| 3070 | notifyViewAccessibilityStateChangedIfNeeded(changeType); |
| 3071 | } else if (mParent != null) { |
Adam Powell | 504a10f | 2013-07-11 15:25:59 -0700 | [diff] [blame] | 3072 | try { |
Alan Viverette | 77e9a28 | 2013-09-12 17:16:09 -0700 | [diff] [blame] | 3073 | mParent.notifySubtreeAccessibilityStateChanged(this, source, changeType); |
Adam Powell | 504a10f | 2013-07-11 15:25:59 -0700 | [diff] [blame] | 3074 | } catch (AbstractMethodError e) { |
| 3075 | Log.e(VIEW_LOG_TAG, mParent.getClass().getSimpleName() + |
| 3076 | " does not fully implement ViewParent", e); |
| 3077 | } |
Svetoslav | 6254f48 | 2013-06-04 17:22:14 -0700 | [diff] [blame] | 3078 | } |
| 3079 | } |
| 3080 | |
| 3081 | @Override |
| 3082 | void resetSubtreeAccessibilityStateChanged() { |
| 3083 | super.resetSubtreeAccessibilityStateChanged(); |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 3084 | View[] children = mChildren; |
| 3085 | final int childCount = mChildrenCount; |
| 3086 | for (int i = 0; i < childCount; i++) { |
Svetoslav | 6254f48 | 2013-06-04 17:22:14 -0700 | [diff] [blame] | 3087 | children[i].resetSubtreeAccessibilityStateChanged(); |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 3088 | } |
| 3089 | } |
| 3090 | |
| 3091 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3092 | * {@inheritDoc} |
Adam Powell | b6ab098 | 2015-01-07 17:00:12 -0800 | [diff] [blame] | 3093 | * |
| 3094 | * <p>Subclasses should always call <code>super.onNestedPrePerformAccessibilityAction</code></p> |
| 3095 | * |
| 3096 | * @param target The target view dispatching this action |
| 3097 | * @param action Action being performed; see |
| 3098 | * {@link android.view.accessibility.AccessibilityNodeInfo} |
| 3099 | * @param args Optional action arguments |
| 3100 | * @return false by default. Subclasses should return true if they handle the event. |
| 3101 | */ |
| 3102 | @Override |
| 3103 | public boolean onNestedPrePerformAccessibilityAction(View target, int action, Bundle args) { |
| 3104 | return false; |
| 3105 | } |
| 3106 | |
| 3107 | /** |
| 3108 | * {@inheritDoc} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3109 | */ |
| 3110 | @Override |
| 3111 | void dispatchDetachedFromWindow() { |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 3112 | // 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] | 3113 | // dispatching motion events to a child; we need to get rid of that |
| 3114 | // child to avoid dispatching events to it after the window is torn |
| 3115 | // down. To make sure we keep the child in a consistent state, we |
| 3116 | // first send it an ACTION_CANCEL motion event. |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 3117 | cancelAndClearTouchTargets(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3118 | |
Jeff Brown | 59a422e | 2012-04-19 15:19:19 -0700 | [diff] [blame] | 3119 | // Similarly, set ACTION_EXIT to all hover targets and clear them. |
| 3120 | exitHoverTargets(); |
| 3121 | |
Chet Haase | 9c08744 | 2011-01-12 16:20:16 -0800 | [diff] [blame] | 3122 | // In case view is detached while transition is running |
Chet Haase | b989502 | 2013-04-02 15:10:58 -0700 | [diff] [blame] | 3123 | mLayoutCalledWhileSuppressed = false; |
Chet Haase | 9c08744 | 2011-01-12 16:20:16 -0800 | [diff] [blame] | 3124 | |
Christopher Tate | 86cab1b | 2011-01-13 20:28:55 -0800 | [diff] [blame] | 3125 | // Tear down our drag tracking |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 3126 | mChildrenInterestedInDrag = null; |
| 3127 | mIsInterestedInDrag = false; |
| 3128 | if (mCurrentDragStartEvent != null) { |
| 3129 | mCurrentDragStartEvent.recycle(); |
| 3130 | mCurrentDragStartEvent = null; |
Christopher Tate | 86cab1b | 2011-01-13 20:28:55 -0800 | [diff] [blame] | 3131 | } |
| 3132 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3133 | final int count = mChildrenCount; |
| 3134 | final View[] children = mChildren; |
| 3135 | for (int i = 0; i < count; i++) { |
| 3136 | children[i].dispatchDetachedFromWindow(); |
| 3137 | } |
John Reck | ca7a9da | 2014-03-05 16:29:07 -0800 | [diff] [blame] | 3138 | clearDisappearingChildren(); |
Chet Haase | c633d2f | 2015-04-07 10:29:39 -0700 | [diff] [blame] | 3139 | final int transientCount = mTransientViews == null ? 0 : mTransientIndices.size(); |
| 3140 | for (int i = 0; i < transientCount; ++i) { |
| 3141 | View view = mTransientViews.get(i); |
| 3142 | view.dispatchDetachedFromWindow(); |
| 3143 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3144 | super.dispatchDetachedFromWindow(); |
| 3145 | } |
| 3146 | |
Fabrice Di Meglio | 23c89fd | 2012-08-13 12:17:42 -0700 | [diff] [blame] | 3147 | /** |
| 3148 | * @hide |
| 3149 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3150 | @Override |
Fabrice Di Meglio | 23c89fd | 2012-08-13 12:17:42 -0700 | [diff] [blame] | 3151 | protected void internalSetPadding(int left, int top, int right, int bottom) { |
Romain Guy | 2440e67 | 2012-08-07 14:43:43 -0700 | [diff] [blame] | 3152 | super.internalSetPadding(left, top, right, bottom); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3153 | |
Romain Guy | 13f35f3 | 2011-03-24 12:03:17 -0700 | [diff] [blame] | 3154 | if ((mPaddingLeft | mPaddingTop | mPaddingRight | mPaddingBottom) != 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3155 | mGroupFlags |= FLAG_PADDING_NOT_NULL; |
| 3156 | } else { |
| 3157 | mGroupFlags &= ~FLAG_PADDING_NOT_NULL; |
| 3158 | } |
| 3159 | } |
| 3160 | |
| 3161 | /** |
| 3162 | * {@inheritDoc} |
| 3163 | */ |
| 3164 | @Override |
| 3165 | protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) { |
| 3166 | super.dispatchSaveInstanceState(container); |
| 3167 | final int count = mChildrenCount; |
| 3168 | final View[] children = mChildren; |
| 3169 | for (int i = 0; i < count; i++) { |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 3170 | View c = children[i]; |
| 3171 | if ((c.mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED) { |
| 3172 | c.dispatchSaveInstanceState(container); |
| 3173 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3174 | } |
| 3175 | } |
| 3176 | |
| 3177 | /** |
Romain Guy | 9fc2781 | 2011-04-27 14:21:41 -0700 | [diff] [blame] | 3178 | * Perform dispatching of a {@link #saveHierarchyState(android.util.SparseArray)} freeze()} |
| 3179 | * to only this view, not to its children. For use when overriding |
| 3180 | * {@link #dispatchSaveInstanceState(android.util.SparseArray)} dispatchFreeze()} to allow |
| 3181 | * 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] | 3182 | * |
| 3183 | * @param container the container |
| 3184 | */ |
| 3185 | protected void dispatchFreezeSelfOnly(SparseArray<Parcelable> container) { |
| 3186 | super.dispatchSaveInstanceState(container); |
| 3187 | } |
| 3188 | |
| 3189 | /** |
| 3190 | * {@inheritDoc} |
| 3191 | */ |
| 3192 | @Override |
| 3193 | protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) { |
| 3194 | super.dispatchRestoreInstanceState(container); |
| 3195 | final int count = mChildrenCount; |
| 3196 | final View[] children = mChildren; |
| 3197 | for (int i = 0; i < count; i++) { |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 3198 | View c = children[i]; |
| 3199 | if ((c.mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED) { |
| 3200 | c.dispatchRestoreInstanceState(container); |
| 3201 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3202 | } |
| 3203 | } |
| 3204 | |
| 3205 | /** |
Romain Guy | 02739a8 | 2011-05-16 11:43:18 -0700 | [diff] [blame] | 3206 | * Perform dispatching of a {@link #restoreHierarchyState(android.util.SparseArray)} |
| 3207 | * to only this view, not to its children. For use when overriding |
| 3208 | * {@link #dispatchRestoreInstanceState(android.util.SparseArray)} to allow |
| 3209 | * 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] | 3210 | * |
| 3211 | * @param container the container |
| 3212 | */ |
| 3213 | protected void dispatchThawSelfOnly(SparseArray<Parcelable> container) { |
| 3214 | super.dispatchRestoreInstanceState(container); |
| 3215 | } |
| 3216 | |
| 3217 | /** |
| 3218 | * Enables or disables the drawing cache for each child of this view group. |
| 3219 | * |
| 3220 | * @param enabled true to enable the cache, false to dispose of it |
| 3221 | */ |
| 3222 | protected void setChildrenDrawingCacheEnabled(boolean enabled) { |
| 3223 | if (enabled || (mPersistentDrawingCache & PERSISTENT_ALL_CACHES) != PERSISTENT_ALL_CACHES) { |
| 3224 | final View[] children = mChildren; |
| 3225 | final int count = mChildrenCount; |
| 3226 | for (int i = 0; i < count; i++) { |
| 3227 | children[i].setDrawingCacheEnabled(enabled); |
| 3228 | } |
| 3229 | } |
| 3230 | } |
| 3231 | |
| 3232 | @Override |
Romain Guy | 223ff5c | 2010-03-02 17:07:47 -0800 | [diff] [blame] | 3233 | Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) { |
Romain Guy | 65554f2 | 2010-03-22 18:58:21 -0700 | [diff] [blame] | 3234 | int count = mChildrenCount; |
| 3235 | int[] visibilities = null; |
| 3236 | |
Romain Guy | 223ff5c | 2010-03-02 17:07:47 -0800 | [diff] [blame] | 3237 | if (skipChildren) { |
Romain Guy | 65554f2 | 2010-03-22 18:58:21 -0700 | [diff] [blame] | 3238 | visibilities = new int[count]; |
| 3239 | for (int i = 0; i < count; i++) { |
| 3240 | View child = getChildAt(i); |
| 3241 | visibilities[i] = child.getVisibility(); |
| 3242 | if (visibilities[i] == View.VISIBLE) { |
| 3243 | child.setVisibility(INVISIBLE); |
| 3244 | } |
| 3245 | } |
Romain Guy | 223ff5c | 2010-03-02 17:07:47 -0800 | [diff] [blame] | 3246 | } |
| 3247 | |
| 3248 | Bitmap b = super.createSnapshot(quality, backgroundColor, skipChildren); |
Romain Guy | 65554f2 | 2010-03-22 18:58:21 -0700 | [diff] [blame] | 3249 | |
| 3250 | if (skipChildren) { |
| 3251 | for (int i = 0; i < count; i++) { |
| 3252 | getChildAt(i).setVisibility(visibilities[i]); |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 3253 | } |
Romain Guy | 65554f2 | 2010-03-22 18:58:21 -0700 | [diff] [blame] | 3254 | } |
Romain Guy | 223ff5c | 2010-03-02 17:07:47 -0800 | [diff] [blame] | 3255 | |
| 3256 | return b; |
| 3257 | } |
| 3258 | |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 3259 | /** Return true if this ViewGroup is laying out using optical bounds. */ |
| 3260 | boolean isLayoutModeOptical() { |
| 3261 | return mLayoutMode == LAYOUT_MODE_OPTICAL_BOUNDS; |
| 3262 | } |
Romain Guy | cbc6774 | 2012-04-27 16:12:57 -0700 | [diff] [blame] | 3263 | |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 3264 | Insets computeOpticalInsets() { |
| 3265 | if (isLayoutModeOptical()) { |
| 3266 | int left = 0; |
| 3267 | int top = 0; |
| 3268 | int right = 0; |
| 3269 | int bottom = 0; |
| 3270 | for (int i = 0; i < mChildrenCount; i++) { |
| 3271 | View child = getChildAt(i); |
| 3272 | if (child.getVisibility() == VISIBLE) { |
| 3273 | Insets insets = child.getOpticalInsets(); |
| 3274 | left = Math.max(left, insets.left); |
| 3275 | top = Math.max(top, insets.top); |
| 3276 | right = Math.max(right, insets.right); |
| 3277 | bottom = Math.max(bottom, insets.bottom); |
| 3278 | } |
| 3279 | } |
| 3280 | return Insets.of(left, top, right, bottom); |
| 3281 | } else { |
| 3282 | return Insets.NONE; |
| 3283 | } |
| 3284 | } |
| 3285 | |
| 3286 | private static void fillRect(Canvas canvas, Paint paint, int x1, int y1, int x2, int y2) { |
| 3287 | if (x1 != x2 && y1 != y2) { |
| 3288 | if (x1 > x2) { |
| 3289 | int tmp = x1; x1 = x2; x2 = tmp; |
| 3290 | } |
| 3291 | if (y1 > y2) { |
| 3292 | int tmp = y1; y1 = y2; y2 = tmp; |
| 3293 | } |
| 3294 | canvas.drawRect(x1, y1, x2, y2, paint); |
| 3295 | } |
| 3296 | } |
| 3297 | |
| 3298 | private static int sign(int x) { |
| 3299 | return (x >= 0) ? 1 : -1; |
| 3300 | } |
| 3301 | |
| 3302 | private static void drawCorner(Canvas c, Paint paint, int x1, int y1, int dx, int dy, int lw) { |
| 3303 | fillRect(c, paint, x1, y1, x1 + dx, y1 + lw * sign(dy)); |
| 3304 | fillRect(c, paint, x1, y1, x1 + lw * sign(dx), y1 + dy); |
| 3305 | } |
| 3306 | |
| 3307 | private int dipsToPixels(int dips) { |
| 3308 | float scale = getContext().getResources().getDisplayMetrics().density; |
| 3309 | return (int) (dips * scale + 0.5f); |
| 3310 | } |
| 3311 | |
Romain Guy | 6410c0a | 2013-06-17 11:21:58 -0700 | [diff] [blame] | 3312 | private static void drawRectCorners(Canvas canvas, int x1, int y1, int x2, int y2, Paint paint, |
| 3313 | int lineLength, int lineWidth) { |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 3314 | drawCorner(canvas, paint, x1, y1, lineLength, lineLength, lineWidth); |
| 3315 | drawCorner(canvas, paint, x1, y2, lineLength, -lineLength, lineWidth); |
| 3316 | drawCorner(canvas, paint, x2, y1, -lineLength, lineLength, lineWidth); |
| 3317 | drawCorner(canvas, paint, x2, y2, -lineLength, -lineLength, lineWidth); |
| 3318 | } |
| 3319 | |
| 3320 | private static void fillDifference(Canvas canvas, |
| 3321 | int x2, int y2, int x3, int y3, |
| 3322 | int dx1, int dy1, int dx2, int dy2, Paint paint) { |
| 3323 | int x1 = x2 - dx1; |
| 3324 | int y1 = y2 - dy1; |
| 3325 | |
| 3326 | int x4 = x3 + dx2; |
| 3327 | int y4 = y3 + dy2; |
| 3328 | |
| 3329 | fillRect(canvas, paint, x1, y1, x4, y2); |
| 3330 | fillRect(canvas, paint, x1, y2, x2, y3); |
| 3331 | fillRect(canvas, paint, x3, y2, x4, y3); |
| 3332 | fillRect(canvas, paint, x1, y3, x4, y4); |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 3333 | } |
| 3334 | |
| 3335 | /** |
| 3336 | * @hide |
| 3337 | */ |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 3338 | protected void onDebugDrawMargins(Canvas canvas, Paint paint) { |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 3339 | for (int i = 0; i < getChildCount(); i++) { |
| 3340 | View c = getChildAt(i); |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 3341 | c.getLayoutParams().onDebugDraw(c, canvas, paint); |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 3342 | } |
| 3343 | } |
| 3344 | |
| 3345 | /** |
| 3346 | * @hide |
| 3347 | */ |
| 3348 | protected void onDebugDraw(Canvas canvas) { |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 3349 | Paint paint = getDebugPaint(); |
| 3350 | |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 3351 | // Draw optical bounds |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 3352 | { |
| 3353 | paint.setColor(Color.RED); |
| 3354 | paint.setStyle(Paint.Style.STROKE); |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 3355 | |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 3356 | for (int i = 0; i < getChildCount(); i++) { |
| 3357 | View c = getChildAt(i); |
Chris Craik | a1dab8b | 2015-06-30 13:51:25 -0700 | [diff] [blame] | 3358 | if (c.getVisibility() != View.GONE) { |
| 3359 | Insets insets = c.getOpticalInsets(); |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 3360 | |
Chris Craik | a1dab8b | 2015-06-30 13:51:25 -0700 | [diff] [blame] | 3361 | drawRect(canvas, paint, |
| 3362 | c.getLeft() + insets.left, |
| 3363 | c.getTop() + insets.top, |
| 3364 | c.getRight() - insets.right - 1, |
| 3365 | c.getBottom() - insets.bottom - 1); |
| 3366 | } |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 3367 | } |
| 3368 | } |
| 3369 | |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 3370 | // Draw margins |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 3371 | { |
| 3372 | paint.setColor(Color.argb(63, 255, 0, 255)); |
| 3373 | paint.setStyle(Paint.Style.FILL); |
Philip Milne | 604f440 | 2012-04-24 19:27:11 -0700 | [diff] [blame] | 3374 | |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 3375 | onDebugDrawMargins(canvas, paint); |
| 3376 | } |
| 3377 | |
| 3378 | // Draw clip bounds |
| 3379 | { |
| 3380 | paint.setColor(Color.rgb(63, 127, 255)); |
| 3381 | paint.setStyle(Paint.Style.FILL); |
| 3382 | |
| 3383 | int lineLength = dipsToPixels(8); |
| 3384 | int lineWidth = dipsToPixels(1); |
| 3385 | for (int i = 0; i < getChildCount(); i++) { |
| 3386 | View c = getChildAt(i); |
Chris Craik | a1dab8b | 2015-06-30 13:51:25 -0700 | [diff] [blame] | 3387 | if (c.getVisibility() != View.GONE) { |
| 3388 | drawRectCorners(canvas, c.getLeft(), c.getTop(), c.getRight(), c.getBottom(), |
| 3389 | paint, lineLength, lineWidth); |
| 3390 | } |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 3391 | } |
Philip Milne | 604f440 | 2012-04-24 19:27:11 -0700 | [diff] [blame] | 3392 | } |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 3393 | } |
| 3394 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3395 | /** |
| 3396 | * {@inheritDoc} |
| 3397 | */ |
| 3398 | @Override |
| 3399 | protected void dispatchDraw(Canvas canvas) { |
Chris Craik | a753f4c | 2014-07-24 12:39:17 -0700 | [diff] [blame] | 3400 | boolean usingRenderNodeProperties = canvas.isRecordingFor(mRenderNode); |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 3401 | final int childrenCount = mChildrenCount; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3402 | final View[] children = mChildren; |
| 3403 | int flags = mGroupFlags; |
| 3404 | |
| 3405 | if ((flags & FLAG_RUN_ANIMATION) != 0 && canAnimate()) { |
Romain Guy | 0d9275e | 2010-10-26 14:22:30 -0700 | [diff] [blame] | 3406 | final boolean buildCache = !isHardwareAccelerated(); |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 3407 | for (int i = 0; i < childrenCount; i++) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3408 | final View child = children[i]; |
| 3409 | if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) { |
| 3410 | final LayoutParams params = child.getLayoutParams(); |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 3411 | attachLayoutAnimationParameters(child, params, i, childrenCount); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3412 | bindLayoutAnimation(child); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3413 | } |
| 3414 | } |
| 3415 | |
| 3416 | final LayoutAnimationController controller = mLayoutAnimationController; |
| 3417 | if (controller.willOverlap()) { |
| 3418 | mGroupFlags |= FLAG_OPTIMIZE_INVALIDATE; |
| 3419 | } |
| 3420 | |
| 3421 | controller.start(); |
| 3422 | |
| 3423 | mGroupFlags &= ~FLAG_RUN_ANIMATION; |
| 3424 | mGroupFlags &= ~FLAG_ANIMATION_DONE; |
| 3425 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3426 | if (mAnimationListener != null) { |
| 3427 | mAnimationListener.onAnimationStart(controller.getAnimation()); |
| 3428 | } |
| 3429 | } |
| 3430 | |
Selim Cinek | 19cadc2 | 2014-04-16 17:27:19 +0200 | [diff] [blame] | 3431 | int clipSaveCount = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3432 | final boolean clipToPadding = (flags & CLIP_TO_PADDING_MASK) == CLIP_TO_PADDING_MASK; |
| 3433 | if (clipToPadding) { |
Chris Craik | e4cf152 | 2014-08-04 17:55:22 -0700 | [diff] [blame] | 3434 | clipSaveCount = canvas.save(); |
Romain Guy | 8f2d94f | 2009-03-25 18:04:42 -0700 | [diff] [blame] | 3435 | canvas.clipRect(mScrollX + mPaddingLeft, mScrollY + mPaddingTop, |
| 3436 | mScrollX + mRight - mLeft - mPaddingRight, |
| 3437 | mScrollY + mBottom - mTop - mPaddingBottom); |
Selim Cinek | 19cadc2 | 2014-04-16 17:27:19 +0200 | [diff] [blame] | 3438 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3439 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3440 | // We will draw our child's animation, let's reset the flag |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 3441 | mPrivateFlags &= ~PFLAG_DRAW_ANIMATION; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3442 | mGroupFlags &= ~FLAG_INVALIDATE_REQUIRED; |
| 3443 | |
| 3444 | boolean more = false; |
| 3445 | final long drawingTime = getDrawingTime(); |
| 3446 | |
Chris Craik | 8afd0f2 | 2014-08-21 17:41:57 -0700 | [diff] [blame] | 3447 | if (usingRenderNodeProperties) canvas.insertReorderBarrier(); |
Chet Haase | c633d2f | 2015-04-07 10:29:39 -0700 | [diff] [blame] | 3448 | final int transientCount = mTransientIndices == null ? 0 : mTransientIndices.size(); |
| 3449 | int transientIndex = transientCount != 0 ? 0 : -1; |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 3450 | // Only use the preordered list if not HW accelerated, since the HW pipeline will do the |
| 3451 | // draw reordering internally |
Chris Craik | a753f4c | 2014-07-24 12:39:17 -0700 | [diff] [blame] | 3452 | final ArrayList<View> preorderedList = usingRenderNodeProperties |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 3453 | ? null : buildOrderedChildList(); |
| 3454 | final boolean customOrder = preorderedList == null |
| 3455 | && isChildrenDrawingOrderEnabled(); |
| 3456 | for (int i = 0; i < childrenCount; i++) { |
Chet Haase | c633d2f | 2015-04-07 10:29:39 -0700 | [diff] [blame] | 3457 | while (transientIndex >= 0 && mTransientIndices.get(transientIndex) == i) { |
| 3458 | final View transientChild = mTransientViews.get(transientIndex); |
| 3459 | if ((transientChild.mViewFlags & VISIBILITY_MASK) == VISIBLE || |
| 3460 | transientChild.getAnimation() != null) { |
| 3461 | more |= drawChild(canvas, transientChild, drawingTime); |
| 3462 | } |
| 3463 | transientIndex++; |
| 3464 | if (transientIndex >= transientCount) { |
| 3465 | transientIndex = -1; |
| 3466 | } |
| 3467 | } |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 3468 | int childIndex = customOrder ? getChildDrawingOrder(childrenCount, i) : i; |
| 3469 | final View child = (preorderedList == null) |
| 3470 | ? children[childIndex] : preorderedList.get(childIndex); |
| 3471 | if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null) { |
| 3472 | more |= drawChild(canvas, child, drawingTime); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3473 | } |
| 3474 | } |
Chet Haase | c633d2f | 2015-04-07 10:29:39 -0700 | [diff] [blame] | 3475 | while (transientIndex >= 0) { |
| 3476 | // there may be additional transient views after the normal views |
| 3477 | final View transientChild = mTransientViews.get(transientIndex); |
| 3478 | if ((transientChild.mViewFlags & VISIBILITY_MASK) == VISIBLE || |
| 3479 | transientChild.getAnimation() != null) { |
| 3480 | more |= drawChild(canvas, transientChild, drawingTime); |
| 3481 | } |
| 3482 | transientIndex++; |
| 3483 | if (transientIndex >= transientCount) { |
| 3484 | break; |
| 3485 | } |
| 3486 | } |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 3487 | if (preorderedList != null) preorderedList.clear(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3488 | |
| 3489 | // Draw any disappearing views that have animations |
| 3490 | if (mDisappearingChildren != null) { |
| 3491 | final ArrayList<View> disappearingChildren = mDisappearingChildren; |
| 3492 | final int disappearingCount = disappearingChildren.size() - 1; |
| 3493 | // Go backwards -- we may delete as animations finish |
| 3494 | for (int i = disappearingCount; i >= 0; i--) { |
| 3495 | final View child = disappearingChildren.get(i); |
| 3496 | more |= drawChild(canvas, child, drawingTime); |
| 3497 | } |
| 3498 | } |
Chris Craik | 8afd0f2 | 2014-08-21 17:41:57 -0700 | [diff] [blame] | 3499 | if (usingRenderNodeProperties) canvas.insertInorderBarrier(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3500 | |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 3501 | if (debugDraw()) { |
| 3502 | onDebugDraw(canvas); |
| 3503 | } |
| 3504 | |
Chris Craik | e4cf152 | 2014-08-04 17:55:22 -0700 | [diff] [blame] | 3505 | if (clipToPadding) { |
Selim Cinek | 19cadc2 | 2014-04-16 17:27:19 +0200 | [diff] [blame] | 3506 | canvas.restoreToCount(clipSaveCount); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3507 | } |
| 3508 | |
| 3509 | // mGroupFlags might have been updated by drawChild() |
| 3510 | flags = mGroupFlags; |
| 3511 | |
| 3512 | if ((flags & FLAG_INVALIDATE_REQUIRED) == FLAG_INVALIDATE_REQUIRED) { |
Romain Guy | 849d0a3 | 2011-02-01 17:20:48 -0800 | [diff] [blame] | 3513 | invalidate(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3514 | } |
| 3515 | |
| 3516 | if ((flags & FLAG_ANIMATION_DONE) == 0 && (flags & FLAG_NOTIFY_ANIMATION_LISTENER) == 0 && |
| 3517 | mLayoutAnimationController.isDone() && !more) { |
| 3518 | // We want to erase the drawing cache and notify the listener after the |
| 3519 | // next frame is drawn because one extra invalidate() is caused by |
| 3520 | // drawChild() after the animation is over |
| 3521 | mGroupFlags |= FLAG_NOTIFY_ANIMATION_LISTENER; |
| 3522 | final Runnable end = new Runnable() { |
| 3523 | public void run() { |
| 3524 | notifyAnimationListener(); |
| 3525 | } |
| 3526 | }; |
| 3527 | post(end); |
| 3528 | } |
| 3529 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 3530 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3531 | /** |
Chet Haase | edf6f4b | 2013-03-26 07:55:30 -0700 | [diff] [blame] | 3532 | * Returns the ViewGroupOverlay for this view group, creating it if it does |
| 3533 | * not yet exist. In addition to {@link ViewOverlay}'s support for drawables, |
| 3534 | * {@link ViewGroupOverlay} allows views to be added to the overlay. These |
| 3535 | * views, like overlay drawables, are visual-only; they do not receive input |
| 3536 | * events and should not be used as anything other than a temporary |
| 3537 | * representation of a view in a parent container, such as might be used |
| 3538 | * by an animation effect. |
| 3539 | * |
Chet Haase | 9539949 | 2013-04-08 14:30:31 -0700 | [diff] [blame] | 3540 | * <p>Note: Overlays do not currently work correctly with {@link |
| 3541 | * SurfaceView} or {@link TextureView}; contents in overlays for these |
| 3542 | * types of views may not display correctly.</p> |
| 3543 | * |
Chet Haase | edf6f4b | 2013-03-26 07:55:30 -0700 | [diff] [blame] | 3544 | * @return The ViewGroupOverlay object for this view. |
| 3545 | * @see ViewGroupOverlay |
| 3546 | */ |
| 3547 | @Override |
| 3548 | public ViewGroupOverlay getOverlay() { |
| 3549 | if (mOverlay == null) { |
| 3550 | mOverlay = new ViewGroupOverlay(mContext, this); |
| 3551 | } |
| 3552 | return (ViewGroupOverlay) mOverlay; |
| 3553 | } |
| 3554 | |
| 3555 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3556 | * Returns the index of the child to draw for this iteration. Override this |
| 3557 | * if you want to change the drawing order of children. By default, it |
| 3558 | * returns i. |
| 3559 | * <p> |
Romain Guy | 293451e | 2009-11-04 13:59:48 -0800 | [diff] [blame] | 3560 | * NOTE: In order for this method to be called, you must enable child ordering |
| 3561 | * first by calling {@link #setChildrenDrawingOrderEnabled(boolean)}. |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 3562 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3563 | * @param i The current iteration. |
| 3564 | * @return The index of the child to draw this iteration. |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 3565 | * |
Romain Guy | 293451e | 2009-11-04 13:59:48 -0800 | [diff] [blame] | 3566 | * @see #setChildrenDrawingOrderEnabled(boolean) |
| 3567 | * @see #isChildrenDrawingOrderEnabled() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3568 | */ |
| 3569 | protected int getChildDrawingOrder(int childCount, int i) { |
| 3570 | return i; |
| 3571 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 3572 | |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 3573 | private boolean hasChildWithZ() { |
| 3574 | for (int i = 0; i < mChildrenCount; i++) { |
| 3575 | if (mChildren[i].getZ() != 0) return true; |
| 3576 | } |
| 3577 | return false; |
| 3578 | } |
| 3579 | |
| 3580 | /** |
| 3581 | * Populates (and returns) mPreSortedChildren with a pre-ordered list of the View's children, |
Chris Craik | 57c79c8 | 2014-09-30 12:54:31 -0700 | [diff] [blame] | 3582 | * sorted first by Z, then by child drawing order (if applicable). This list must be cleared |
| 3583 | * after use to avoid leaking child Views. |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 3584 | * |
| 3585 | * Uses a stable, insertion sort which is commonly O(n) for ViewGroups with very few elevated |
| 3586 | * children. |
| 3587 | */ |
George Mount | 8120652 | 2014-09-26 21:53:39 -0700 | [diff] [blame] | 3588 | ArrayList<View> buildOrderedChildList() { |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 3589 | final int count = mChildrenCount; |
| 3590 | if (count <= 1 || !hasChildWithZ()) return null; |
| 3591 | |
| 3592 | if (mPreSortedChildren == null) { |
| 3593 | mPreSortedChildren = new ArrayList<View>(count); |
| 3594 | } else { |
| 3595 | mPreSortedChildren.ensureCapacity(count); |
| 3596 | } |
| 3597 | |
| 3598 | final boolean useCustomOrder = isChildrenDrawingOrderEnabled(); |
| 3599 | for (int i = 0; i < mChildrenCount; i++) { |
| 3600 | // add next child (in child order) to end of list |
| 3601 | int childIndex = useCustomOrder ? getChildDrawingOrder(mChildrenCount, i) : i; |
| 3602 | View nextChild = mChildren[childIndex]; |
| 3603 | float currentZ = nextChild.getZ(); |
| 3604 | |
| 3605 | // insert ahead of any Views with greater Z |
| 3606 | int insertIndex = i; |
| 3607 | while (insertIndex > 0 && mPreSortedChildren.get(insertIndex - 1).getZ() > currentZ) { |
| 3608 | insertIndex--; |
| 3609 | } |
| 3610 | mPreSortedChildren.add(insertIndex, nextChild); |
| 3611 | } |
| 3612 | return mPreSortedChildren; |
| 3613 | } |
| 3614 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3615 | private void notifyAnimationListener() { |
| 3616 | mGroupFlags &= ~FLAG_NOTIFY_ANIMATION_LISTENER; |
| 3617 | mGroupFlags |= FLAG_ANIMATION_DONE; |
| 3618 | |
| 3619 | if (mAnimationListener != null) { |
| 3620 | final Runnable end = new Runnable() { |
| 3621 | public void run() { |
| 3622 | mAnimationListener.onAnimationEnd(mLayoutAnimationController.getAnimation()); |
| 3623 | } |
| 3624 | }; |
| 3625 | post(end); |
| 3626 | } |
| 3627 | |
Romain Guy | 849d0a3 | 2011-02-01 17:20:48 -0800 | [diff] [blame] | 3628 | invalidate(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3629 | } |
| 3630 | |
| 3631 | /** |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 3632 | * This method is used to cause children of this ViewGroup to restore or recreate their |
| 3633 | * display lists. It is called by getDisplayList() when the parent ViewGroup does not need |
| 3634 | * to recreate its own display list, which would happen if it went through the normal |
| 3635 | * draw/dispatchDraw mechanisms. |
| 3636 | * |
| 3637 | * @hide |
| 3638 | */ |
| 3639 | @Override |
| 3640 | protected void dispatchGetDisplayList() { |
| 3641 | final int count = mChildrenCount; |
| 3642 | final View[] children = mChildren; |
| 3643 | for (int i = 0; i < count; i++) { |
| 3644 | final View child = children[i]; |
John Reck | c2330f5 | 2015-04-28 13:18:52 -0700 | [diff] [blame] | 3645 | if (((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null)) { |
Chet Haase | 6c0665f | 2014-08-01 13:32:27 -0700 | [diff] [blame] | 3646 | recreateChildDisplayList(child); |
Romain Guy | 2f57ba5 | 2011-02-03 18:03:29 -0800 | [diff] [blame] | 3647 | } |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 3648 | } |
Chet Haase | 91cedf1 | 2013-03-11 07:56:30 -0700 | [diff] [blame] | 3649 | if (mOverlay != null) { |
Chet Haase | edf6f4b | 2013-03-26 07:55:30 -0700 | [diff] [blame] | 3650 | View overlayView = mOverlay.getOverlayView(); |
Chet Haase | 6c0665f | 2014-08-01 13:32:27 -0700 | [diff] [blame] | 3651 | recreateChildDisplayList(overlayView); |
Chet Haase | 91cedf1 | 2013-03-11 07:56:30 -0700 | [diff] [blame] | 3652 | } |
Chet Haase | 6c0665f | 2014-08-01 13:32:27 -0700 | [diff] [blame] | 3653 | if (mDisappearingChildren != null) { |
| 3654 | final ArrayList<View> disappearingChildren = mDisappearingChildren; |
| 3655 | final int disappearingCount = disappearingChildren.size(); |
| 3656 | for (int i = 0; i < disappearingCount; ++i) { |
| 3657 | final View child = disappearingChildren.get(i); |
| 3658 | recreateChildDisplayList(child); |
| 3659 | } |
| 3660 | } |
| 3661 | } |
| 3662 | |
| 3663 | private void recreateChildDisplayList(View child) { |
Chris Craik | 31a2d06 | 2015-05-01 14:22:47 -0700 | [diff] [blame] | 3664 | child.mRecreateDisplayList = (child.mPrivateFlags & PFLAG_INVALIDATED) != 0; |
Chet Haase | 6c0665f | 2014-08-01 13:32:27 -0700 | [diff] [blame] | 3665 | child.mPrivateFlags &= ~PFLAG_INVALIDATED; |
Chris Craik | 31a2d06 | 2015-05-01 14:22:47 -0700 | [diff] [blame] | 3666 | child.updateDisplayListIfDirty(); |
Chet Haase | 6c0665f | 2014-08-01 13:32:27 -0700 | [diff] [blame] | 3667 | child.mRecreateDisplayList = false; |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 3668 | } |
| 3669 | |
| 3670 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3671 | * Draw one child of this View Group. This method is responsible for getting |
| 3672 | * the canvas in the right state. This includes clipping, translating so |
| 3673 | * that the child's scrolled origin is at 0, 0, and applying any animation |
| 3674 | * transformations. |
| 3675 | * |
| 3676 | * @param canvas The canvas on which to draw the child |
| 3677 | * @param child Who to draw |
Chet Haase | bcca79a | 2012-02-14 08:45:14 -0800 | [diff] [blame] | 3678 | * @param drawingTime The time at which draw is occurring |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3679 | * @return True if an invalidate() was issued |
| 3680 | */ |
| 3681 | protected boolean drawChild(Canvas canvas, View child, long drawingTime) { |
Chet Haase | 64a48c1 | 2012-02-13 16:33:29 -0800 | [diff] [blame] | 3682 | return child.draw(canvas, this, drawingTime); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3683 | } |
| 3684 | |
Alan Viverette | 922e1c6 | 2015-05-05 17:18:27 -0700 | [diff] [blame] | 3685 | @Override |
| 3686 | void getScrollIndicatorBounds(@NonNull Rect out) { |
| 3687 | super.getScrollIndicatorBounds(out); |
| 3688 | |
| 3689 | // If we have padding and we're supposed to clip children to that |
| 3690 | // padding, offset the scroll indicators to match our clip bounds. |
| 3691 | final boolean clipToPadding = (mGroupFlags & CLIP_TO_PADDING_MASK) == CLIP_TO_PADDING_MASK; |
| 3692 | if (clipToPadding) { |
| 3693 | out.left += mPaddingLeft; |
| 3694 | out.right -= mPaddingRight; |
| 3695 | out.top += mPaddingTop; |
| 3696 | out.bottom -= mPaddingBottom; |
| 3697 | } |
| 3698 | } |
| 3699 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3700 | /** |
Chris Craik | d863a10 | 2013-12-19 13:31:15 -0800 | [diff] [blame] | 3701 | * 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] | 3702 | * The default value is true. |
| 3703 | * @see #setClipChildren(boolean) |
| 3704 | * |
| 3705 | * @return True if the group's children will be clipped to their bounds, |
| 3706 | * false otherwise. |
| 3707 | */ |
Chris Craik | 5c75c52 | 2014-09-05 14:08:08 -0700 | [diff] [blame] | 3708 | @ViewDebug.ExportedProperty(category = "drawing") |
Chet Haase | 430742f | 2013-04-12 11:18:36 -0700 | [diff] [blame] | 3709 | public boolean getClipChildren() { |
| 3710 | return ((mGroupFlags & FLAG_CLIP_CHILDREN) != 0); |
| 3711 | } |
| 3712 | |
| 3713 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3714 | * By default, children are clipped to their bounds before drawing. This |
| 3715 | * allows view groups to override this behavior for animations, etc. |
| 3716 | * |
| 3717 | * @param clipChildren true to clip children to their bounds, |
| 3718 | * false otherwise |
| 3719 | * @attr ref android.R.styleable#ViewGroup_clipChildren |
| 3720 | */ |
| 3721 | public void setClipChildren(boolean clipChildren) { |
Chet Haase | a1cff50 | 2012-02-21 13:43:44 -0800 | [diff] [blame] | 3722 | boolean previousValue = (mGroupFlags & FLAG_CLIP_CHILDREN) == FLAG_CLIP_CHILDREN; |
| 3723 | if (clipChildren != previousValue) { |
| 3724 | setBooleanFlag(FLAG_CLIP_CHILDREN, clipChildren); |
Chet Haase | 1271e2c | 2012-04-20 09:54:27 -0700 | [diff] [blame] | 3725 | for (int i = 0; i < mChildrenCount; ++i) { |
| 3726 | View child = getChildAt(i); |
Chris Craik | 64a12e1 | 2014-03-28 18:12:12 -0700 | [diff] [blame] | 3727 | if (child.mRenderNode != null) { |
| 3728 | child.mRenderNode.setClipToBounds(clipChildren); |
Chet Haase | a1cff50 | 2012-02-21 13:43:44 -0800 | [diff] [blame] | 3729 | } |
| 3730 | } |
John Reck | aae9f3b | 2014-07-28 09:30:36 -0700 | [diff] [blame] | 3731 | invalidate(true); |
Chet Haase | a1cff50 | 2012-02-21 13:43:44 -0800 | [diff] [blame] | 3732 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3733 | } |
| 3734 | |
| 3735 | /** |
Doris Liu | b134b5e | 2015-05-28 17:26:46 -0700 | [diff] [blame] | 3736 | * Sets whether this ViewGroup will clip its children to its padding and resize (but not |
| 3737 | * clip) any EdgeEffect to the padded region, if padding is present. |
Chris Craik | b165296 | 2014-11-14 17:05:06 -0800 | [diff] [blame] | 3738 | * <p> |
| 3739 | * By default, children are clipped to the padding of their parent |
Doris Liu | b134b5e | 2015-05-28 17:26:46 -0700 | [diff] [blame] | 3740 | * ViewGroup. This clipping behavior is only enabled if padding is non-zero. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3741 | * |
Doris Liu | b134b5e | 2015-05-28 17:26:46 -0700 | [diff] [blame] | 3742 | * @param clipToPadding true to clip children to the padding of the group, and resize (but |
| 3743 | * not clip) any EdgeEffect to the padded region. False otherwise. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3744 | * @attr ref android.R.styleable#ViewGroup_clipToPadding |
| 3745 | */ |
| 3746 | public void setClipToPadding(boolean clipToPadding) { |
John Reck | 9fa3a24 | 2014-06-27 15:57:19 -0700 | [diff] [blame] | 3747 | if (hasBooleanFlag(FLAG_CLIP_TO_PADDING) != clipToPadding) { |
| 3748 | setBooleanFlag(FLAG_CLIP_TO_PADDING, clipToPadding); |
John Reck | aae9f3b | 2014-07-28 09:30:36 -0700 | [diff] [blame] | 3749 | invalidate(true); |
John Reck | 9fa3a24 | 2014-06-27 15:57:19 -0700 | [diff] [blame] | 3750 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3751 | } |
| 3752 | |
| 3753 | /** |
Doris Liu | b134b5e | 2015-05-28 17:26:46 -0700 | [diff] [blame] | 3754 | * Returns whether this ViewGroup will clip its children to its padding, and resize (but |
| 3755 | * not clip) any EdgeEffect to the padded region, if padding is present. |
Chris Craik | b165296 | 2014-11-14 17:05:06 -0800 | [diff] [blame] | 3756 | * <p> |
| 3757 | * By default, children are clipped to the padding of their parent |
| 3758 | * Viewgroup. This clipping behavior is only enabled if padding is non-zero. |
Adam Powell | 1c35b08 | 2014-07-11 15:37:15 -0700 | [diff] [blame] | 3759 | * |
Doris Liu | b134b5e | 2015-05-28 17:26:46 -0700 | [diff] [blame] | 3760 | * @return true if this ViewGroup clips children to its padding and resizes (but doesn't |
| 3761 | * clip) any EdgeEffect to the padded region, false otherwise. |
Adam Powell | 1c35b08 | 2014-07-11 15:37:15 -0700 | [diff] [blame] | 3762 | * |
| 3763 | * @attr ref android.R.styleable#ViewGroup_clipToPadding |
| 3764 | */ |
Chris Craik | 5c75c52 | 2014-09-05 14:08:08 -0700 | [diff] [blame] | 3765 | @ViewDebug.ExportedProperty(category = "drawing") |
Adam Powell | 1c35b08 | 2014-07-11 15:37:15 -0700 | [diff] [blame] | 3766 | public boolean getClipToPadding() { |
| 3767 | return hasBooleanFlag(FLAG_CLIP_TO_PADDING); |
| 3768 | } |
| 3769 | |
| 3770 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3771 | * {@inheritDoc} |
| 3772 | */ |
| 3773 | @Override |
| 3774 | public void dispatchSetSelected(boolean selected) { |
| 3775 | final View[] children = mChildren; |
| 3776 | final int count = mChildrenCount; |
| 3777 | for (int i = 0; i < count; i++) { |
| 3778 | children[i].setSelected(selected); |
| 3779 | } |
| 3780 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 3781 | |
Dianne Hackborn | d0fa371 | 2010-09-14 18:57:14 -0700 | [diff] [blame] | 3782 | /** |
| 3783 | * {@inheritDoc} |
| 3784 | */ |
| 3785 | @Override |
| 3786 | public void dispatchSetActivated(boolean activated) { |
| 3787 | final View[] children = mChildren; |
| 3788 | final int count = mChildrenCount; |
| 3789 | for (int i = 0; i < count; i++) { |
Dianne Hackborn | d0fa371 | 2010-09-14 18:57:14 -0700 | [diff] [blame] | 3790 | children[i].setActivated(activated); |
| 3791 | } |
| 3792 | } |
| 3793 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3794 | @Override |
| 3795 | protected void dispatchSetPressed(boolean pressed) { |
| 3796 | final View[] children = mChildren; |
| 3797 | final int count = mChildrenCount; |
| 3798 | for (int i = 0; i < count; i++) { |
Adam Powell | 035a1fc | 2012-02-27 15:23:50 -0800 | [diff] [blame] | 3799 | final View child = children[i]; |
| 3800 | // Children that are clickable on their own should not |
| 3801 | // show a pressed state when their parent view does. |
| 3802 | // Clearing a pressed state always propagates. |
| 3803 | if (!pressed || (!child.isClickable() && !child.isLongClickable())) { |
| 3804 | child.setPressed(pressed); |
| 3805 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3806 | } |
| 3807 | } |
| 3808 | |
Alan Viverette | b942b6f | 2014-12-08 10:37:39 -0800 | [diff] [blame] | 3809 | /** |
| 3810 | * Dispatches drawable hotspot changes to child views that meet at least |
| 3811 | * one of the following criteria: |
| 3812 | * <ul> |
| 3813 | * <li>Returns {@code false} from both {@link View#isClickable()} and |
| 3814 | * {@link View#isLongClickable()}</li> |
| 3815 | * <li>Requests duplication of parent state via |
| 3816 | * {@link View#setDuplicateParentStateEnabled(boolean)}</li> |
| 3817 | * </ul> |
| 3818 | * |
| 3819 | * @param x hotspot x coordinate |
| 3820 | * @param y hotspot y coordinate |
| 3821 | * @see #drawableHotspotChanged(float, float) |
| 3822 | */ |
| 3823 | @Override |
| 3824 | public void dispatchDrawableHotspotChanged(float x, float y) { |
| 3825 | final int count = mChildrenCount; |
| 3826 | if (count == 0) { |
| 3827 | return; |
| 3828 | } |
| 3829 | |
| 3830 | final View[] children = mChildren; |
| 3831 | for (int i = 0; i < count; i++) { |
| 3832 | final View child = children[i]; |
| 3833 | // Children that are clickable on their own should not |
| 3834 | // receive hotspots when their parent view does. |
| 3835 | final boolean nonActionable = !child.isClickable() && !child.isLongClickable(); |
| 3836 | final boolean duplicatesState = (child.mViewFlags & DUPLICATE_PARENT_STATE) != 0; |
| 3837 | if (nonActionable || duplicatesState) { |
| 3838 | final float[] point = getTempPoint(); |
| 3839 | point[0] = x; |
| 3840 | point[1] = y; |
| 3841 | transformPointToViewLocal(point, child); |
| 3842 | child.drawableHotspotChanged(point[0], point[1]); |
| 3843 | } |
| 3844 | } |
| 3845 | } |
| 3846 | |
Adam Powell | 1487466 | 2013-07-18 19:42:41 -0700 | [diff] [blame] | 3847 | @Override |
| 3848 | void dispatchCancelPendingInputEvents() { |
| 3849 | super.dispatchCancelPendingInputEvents(); |
| 3850 | |
| 3851 | final View[] children = mChildren; |
| 3852 | final int count = mChildrenCount; |
| 3853 | for (int i = 0; i < count; i++) { |
| 3854 | children[i].dispatchCancelPendingInputEvents(); |
| 3855 | } |
| 3856 | } |
| 3857 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3858 | /** |
| 3859 | * When this property is set to true, this ViewGroup supports static transformations on |
| 3860 | * children; this causes |
| 3861 | * {@link #getChildStaticTransformation(View, android.view.animation.Transformation)} to be |
| 3862 | * invoked when a child is drawn. |
| 3863 | * |
| 3864 | * Any subclass overriding |
| 3865 | * {@link #getChildStaticTransformation(View, android.view.animation.Transformation)} should |
| 3866 | * set this property to true. |
| 3867 | * |
| 3868 | * @param enabled True to enable static transformations on children, false otherwise. |
| 3869 | * |
Chet Haase | 599913d | 2012-07-23 16:22:05 -0700 | [diff] [blame] | 3870 | * @see #getChildStaticTransformation(View, android.view.animation.Transformation) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3871 | */ |
| 3872 | protected void setStaticTransformationsEnabled(boolean enabled) { |
| 3873 | setBooleanFlag(FLAG_SUPPORT_STATIC_TRANSFORMATIONS, enabled); |
| 3874 | } |
| 3875 | |
| 3876 | /** |
Chet Haase | 2d46fcc | 2011-12-19 18:01:05 -0800 | [diff] [blame] | 3877 | * Sets <code>t</code> to be the static transformation of the child, if set, returning a |
| 3878 | * boolean to indicate whether a static transform was set. The default implementation |
| 3879 | * simply returns <code>false</code>; subclasses may override this method for different |
Chet Haase | 599913d | 2012-07-23 16:22:05 -0700 | [diff] [blame] | 3880 | * behavior. {@link #setStaticTransformationsEnabled(boolean)} must be set to true |
| 3881 | * for this method to be called. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3882 | * |
Chet Haase | 2d46fcc | 2011-12-19 18:01:05 -0800 | [diff] [blame] | 3883 | * @param child The child view whose static transform is being requested |
| 3884 | * @param t The Transformation which will hold the result |
| 3885 | * @return true if the transformation was set, false otherwise |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 3886 | * @see #setStaticTransformationsEnabled(boolean) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3887 | */ |
| 3888 | protected boolean getChildStaticTransformation(View child, Transformation t) { |
| 3889 | return false; |
| 3890 | } |
| 3891 | |
Romain Guy | f699130 | 2013-06-05 17:19:01 -0700 | [diff] [blame] | 3892 | Transformation getChildTransformation() { |
| 3893 | if (mChildTransformation == null) { |
| 3894 | mChildTransformation = new Transformation(); |
| 3895 | } |
| 3896 | return mChildTransformation; |
| 3897 | } |
| 3898 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3899 | /** |
| 3900 | * {@hide} |
| 3901 | */ |
| 3902 | @Override |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 3903 | protected View findViewTraversal(@IdRes int id) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3904 | if (id == mID) { |
| 3905 | return this; |
| 3906 | } |
| 3907 | |
| 3908 | final View[] where = mChildren; |
| 3909 | final int len = mChildrenCount; |
| 3910 | |
| 3911 | for (int i = 0; i < len; i++) { |
| 3912 | View v = where[i]; |
| 3913 | |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 3914 | if ((v.mPrivateFlags & PFLAG_IS_ROOT_NAMESPACE) == 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3915 | v = v.findViewById(id); |
| 3916 | |
| 3917 | if (v != null) { |
| 3918 | return v; |
| 3919 | } |
| 3920 | } |
| 3921 | } |
| 3922 | |
| 3923 | return null; |
| 3924 | } |
| 3925 | |
| 3926 | /** |
| 3927 | * {@hide} |
| 3928 | */ |
| 3929 | @Override |
| 3930 | protected View findViewWithTagTraversal(Object tag) { |
| 3931 | if (tag != null && tag.equals(mTag)) { |
| 3932 | return this; |
| 3933 | } |
| 3934 | |
| 3935 | final View[] where = mChildren; |
| 3936 | final int len = mChildrenCount; |
| 3937 | |
| 3938 | for (int i = 0; i < len; i++) { |
| 3939 | View v = where[i]; |
| 3940 | |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 3941 | if ((v.mPrivateFlags & PFLAG_IS_ROOT_NAMESPACE) == 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3942 | v = v.findViewWithTag(tag); |
| 3943 | |
| 3944 | if (v != null) { |
| 3945 | return v; |
| 3946 | } |
| 3947 | } |
| 3948 | } |
| 3949 | |
| 3950 | return null; |
| 3951 | } |
| 3952 | |
| 3953 | /** |
Jeff Brown | 4e6319b | 2010-12-13 10:36:51 -0800 | [diff] [blame] | 3954 | * {@hide} |
| 3955 | */ |
| 3956 | @Override |
Jeff Brown | 4dfbec2 | 2011-08-15 14:55:37 -0700 | [diff] [blame] | 3957 | protected View findViewByPredicateTraversal(Predicate<View> predicate, View childToSkip) { |
Jeff Brown | 4e6319b | 2010-12-13 10:36:51 -0800 | [diff] [blame] | 3958 | if (predicate.apply(this)) { |
| 3959 | return this; |
| 3960 | } |
| 3961 | |
| 3962 | final View[] where = mChildren; |
| 3963 | final int len = mChildrenCount; |
| 3964 | |
| 3965 | for (int i = 0; i < len; i++) { |
| 3966 | View v = where[i]; |
| 3967 | |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 3968 | if (v != childToSkip && (v.mPrivateFlags & PFLAG_IS_ROOT_NAMESPACE) == 0) { |
Jeff Brown | 4e6319b | 2010-12-13 10:36:51 -0800 | [diff] [blame] | 3969 | v = v.findViewByPredicate(predicate); |
| 3970 | |
| 3971 | if (v != null) { |
| 3972 | return v; |
| 3973 | } |
| 3974 | } |
| 3975 | } |
| 3976 | |
| 3977 | return null; |
| 3978 | } |
| 3979 | |
| 3980 | /** |
Chet Haase | c633d2f | 2015-04-07 10:29:39 -0700 | [diff] [blame] | 3981 | * This method adds a view to this container at the specified index purely for the |
| 3982 | * purposes of allowing that view to draw even though it is not a normal child of |
| 3983 | * the container. That is, the view does not participate in layout, focus, accessibility, |
| 3984 | * input, or other normal view operations; it is purely an item to be drawn during the normal |
| 3985 | * rendering operation of this container. The index that it is added at is the order |
| 3986 | * in which it will be drawn, with respect to the other views in the container. |
| 3987 | * For example, a transient view added at index 0 will be drawn before all other views |
| 3988 | * in the container because it will be drawn first (including before any real view |
| 3989 | * at index 0). There can be more than one transient view at any particular index; |
| 3990 | * these views will be drawn in the order in which they were added to the list of |
| 3991 | * transient views. The index of transient views can also be greater than the number |
| 3992 | * of normal views in the container; that just means that they will be drawn after all |
| 3993 | * other views are drawn. |
| 3994 | * |
| 3995 | * <p>Note that since transient views do not participate in layout, they must be sized |
| 3996 | * manually or, more typically, they should just use the size that they had before they |
| 3997 | * were removed from their container.</p> |
| 3998 | * |
| 3999 | * <p>Transient views are useful for handling animations of views that have been removed |
| 4000 | * from the container, but which should be animated out after the removal. Adding these |
| 4001 | * views as transient views allows them to participate in drawing without side-effecting |
| 4002 | * the layout of the container.</p> |
| 4003 | * |
| 4004 | * <p>Transient views must always be explicitly {@link #removeTransientView(View) removed} |
| 4005 | * from the container when they are no longer needed. For example, a transient view |
| 4006 | * which is added in order to fade it out in its old location should be removed |
| 4007 | * once the animation is complete.</p> |
| 4008 | * |
| 4009 | * @param view The view to be added |
| 4010 | * @param index The index at which this view should be drawn, must be >= 0. |
| 4011 | * This value is relative to the {@link #getChildAt(int) index} values in the normal |
| 4012 | * child list of this container, where any transient view at a particular index will |
| 4013 | * be drawn before any normal child at that same index. |
Chris Craik | 66b4139 | 2015-04-17 10:08:10 -0700 | [diff] [blame] | 4014 | * |
| 4015 | * @hide |
Chet Haase | c633d2f | 2015-04-07 10:29:39 -0700 | [diff] [blame] | 4016 | */ |
| 4017 | public void addTransientView(View view, int index) { |
| 4018 | if (index < 0) { |
| 4019 | return; |
| 4020 | } |
| 4021 | if (mTransientIndices == null) { |
| 4022 | mTransientIndices = new ArrayList<Integer>(); |
| 4023 | mTransientViews = new ArrayList<View>(); |
| 4024 | } |
| 4025 | final int oldSize = mTransientIndices.size(); |
| 4026 | if (oldSize > 0) { |
| 4027 | int insertionIndex; |
| 4028 | for (insertionIndex = 0; insertionIndex < oldSize; ++insertionIndex) { |
| 4029 | if (index < mTransientIndices.get(insertionIndex)) { |
| 4030 | break; |
| 4031 | } |
| 4032 | } |
| 4033 | mTransientIndices.add(insertionIndex, index); |
| 4034 | mTransientViews.add(insertionIndex, view); |
| 4035 | } else { |
| 4036 | mTransientIndices.add(index); |
| 4037 | mTransientViews.add(view); |
| 4038 | } |
| 4039 | view.mParent = this; |
| 4040 | view.dispatchAttachedToWindow(mAttachInfo, (mViewFlags&VISIBILITY_MASK)); |
| 4041 | invalidate(true); |
| 4042 | } |
| 4043 | |
| 4044 | /** |
| 4045 | * Removes a view from the list of transient views in this container. If there is no |
| 4046 | * such transient view, this method does nothing. |
| 4047 | * |
| 4048 | * @param view The transient view to be removed |
Chris Craik | 66b4139 | 2015-04-17 10:08:10 -0700 | [diff] [blame] | 4049 | * |
| 4050 | * @hide |
Chet Haase | c633d2f | 2015-04-07 10:29:39 -0700 | [diff] [blame] | 4051 | */ |
| 4052 | public void removeTransientView(View view) { |
| 4053 | if (mTransientViews == null) { |
| 4054 | return; |
| 4055 | } |
| 4056 | final int size = mTransientViews.size(); |
| 4057 | for (int i = 0; i < size; ++i) { |
| 4058 | if (view == mTransientViews.get(i)) { |
| 4059 | mTransientViews.remove(i); |
| 4060 | mTransientIndices.remove(i); |
| 4061 | view.mParent = null; |
| 4062 | view.dispatchDetachedFromWindow(); |
| 4063 | invalidate(true); |
| 4064 | return; |
| 4065 | } |
| 4066 | } |
| 4067 | } |
| 4068 | |
| 4069 | /** |
| 4070 | * Returns the number of transient views in this container. Specific transient |
| 4071 | * views and the index at which they were added can be retrieved via |
| 4072 | * {@link #getTransientView(int)} and {@link #getTransientViewIndex(int)}. |
| 4073 | * |
| 4074 | * @see #addTransientView(View, int) |
| 4075 | * @return The number of transient views in this container |
Chris Craik | 66b4139 | 2015-04-17 10:08:10 -0700 | [diff] [blame] | 4076 | * |
| 4077 | * @hide |
Chet Haase | c633d2f | 2015-04-07 10:29:39 -0700 | [diff] [blame] | 4078 | */ |
| 4079 | public int getTransientViewCount() { |
| 4080 | return mTransientIndices == null ? 0 : mTransientIndices.size(); |
| 4081 | } |
| 4082 | |
| 4083 | /** |
| 4084 | * Given a valid position within the list of transient views, returns the index of |
| 4085 | * the transient view at that position. |
| 4086 | * |
| 4087 | * @param position The position of the index being queried. Must be at least 0 |
| 4088 | * and less than the value returned by {@link #getTransientViewCount()}. |
| 4089 | * @return The index of the transient view stored in the given position if the |
| 4090 | * position is valid, otherwise -1 |
Chris Craik | 66b4139 | 2015-04-17 10:08:10 -0700 | [diff] [blame] | 4091 | * |
| 4092 | * @hide |
Chet Haase | c633d2f | 2015-04-07 10:29:39 -0700 | [diff] [blame] | 4093 | */ |
| 4094 | public int getTransientViewIndex(int position) { |
| 4095 | if (position < 0 || mTransientIndices == null || position >= mTransientIndices.size()) { |
| 4096 | return -1; |
| 4097 | } |
| 4098 | return mTransientIndices.get(position); |
| 4099 | } |
| 4100 | |
| 4101 | /** |
| 4102 | * Given a valid position within the list of transient views, returns the |
| 4103 | * transient view at that position. |
| 4104 | * |
| 4105 | * @param position The position of the view being queried. Must be at least 0 |
| 4106 | * and less than the value returned by {@link #getTransientViewCount()}. |
| 4107 | * @return The transient view stored in the given position if the |
| 4108 | * position is valid, otherwise null |
Chris Craik | 66b4139 | 2015-04-17 10:08:10 -0700 | [diff] [blame] | 4109 | * |
| 4110 | * @hide |
Chet Haase | c633d2f | 2015-04-07 10:29:39 -0700 | [diff] [blame] | 4111 | */ |
| 4112 | public View getTransientView(int position) { |
| 4113 | if (mTransientViews == null || position >= mTransientViews.size()) { |
| 4114 | return null; |
| 4115 | } |
| 4116 | return mTransientViews.get(position); |
| 4117 | } |
| 4118 | |
| 4119 | /** |
Romain Guy | 393a52c | 2012-05-22 20:21:08 -0700 | [diff] [blame] | 4120 | * <p>Adds a child view. If no layout parameters are already set on the child, the |
| 4121 | * default parameters for this ViewGroup are set on the child.</p> |
Filip Gruszczynski | a33bdf3 | 2015-11-19 18:22:16 -0800 | [diff] [blame] | 4122 | * |
Romain Guy | 393a52c | 2012-05-22 20:21:08 -0700 | [diff] [blame] | 4123 | * <p><strong>Note:</strong> do not invoke this method from |
| 4124 | * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)}, |
| 4125 | * {@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] | 4126 | * |
| 4127 | * @param child the child view to add |
| 4128 | * |
| 4129 | * @see #generateDefaultLayoutParams() |
| 4130 | */ |
| 4131 | public void addView(View child) { |
| 4132 | addView(child, -1); |
| 4133 | } |
| 4134 | |
| 4135 | /** |
| 4136 | * Adds a child view. If no layout parameters are already set on the child, the |
| 4137 | * default parameters for this ViewGroup are set on the child. |
Filip Gruszczynski | a33bdf3 | 2015-11-19 18:22:16 -0800 | [diff] [blame] | 4138 | * |
Romain Guy | 393a52c | 2012-05-22 20:21:08 -0700 | [diff] [blame] | 4139 | * <p><strong>Note:</strong> do not invoke this method from |
| 4140 | * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)}, |
| 4141 | * {@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] | 4142 | * |
| 4143 | * @param child the child view to add |
| 4144 | * @param index the position at which to add the child |
| 4145 | * |
| 4146 | * @see #generateDefaultLayoutParams() |
| 4147 | */ |
| 4148 | public void addView(View child, int index) { |
Adam Powell | 45a9da5 | 2014-10-09 09:44:18 -0700 | [diff] [blame] | 4149 | if (child == null) { |
| 4150 | throw new IllegalArgumentException("Cannot add a null child view to a ViewGroup"); |
| 4151 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4152 | LayoutParams params = child.getLayoutParams(); |
| 4153 | if (params == null) { |
| 4154 | params = generateDefaultLayoutParams(); |
| 4155 | if (params == null) { |
| 4156 | throw new IllegalArgumentException("generateDefaultLayoutParams() cannot return null"); |
| 4157 | } |
| 4158 | } |
| 4159 | addView(child, index, params); |
| 4160 | } |
| 4161 | |
| 4162 | /** |
| 4163 | * Adds a child view with this ViewGroup's default layout parameters and the |
| 4164 | * specified width and height. |
| 4165 | * |
Romain Guy | 393a52c | 2012-05-22 20:21:08 -0700 | [diff] [blame] | 4166 | * <p><strong>Note:</strong> do not invoke this method from |
| 4167 | * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)}, |
| 4168 | * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p> |
| 4169 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4170 | * @param child the child view to add |
| 4171 | */ |
| 4172 | public void addView(View child, int width, int height) { |
| 4173 | final LayoutParams params = generateDefaultLayoutParams(); |
| 4174 | params.width = width; |
| 4175 | params.height = height; |
| 4176 | addView(child, -1, params); |
| 4177 | } |
| 4178 | |
| 4179 | /** |
| 4180 | * Adds a child view with the specified layout parameters. |
| 4181 | * |
Romain Guy | 393a52c | 2012-05-22 20:21:08 -0700 | [diff] [blame] | 4182 | * <p><strong>Note:</strong> do not invoke this method from |
| 4183 | * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)}, |
| 4184 | * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p> |
| 4185 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4186 | * @param child the child view to add |
| 4187 | * @param params the layout parameters to set on the child |
| 4188 | */ |
| 4189 | public void addView(View child, LayoutParams params) { |
| 4190 | addView(child, -1, params); |
| 4191 | } |
| 4192 | |
| 4193 | /** |
| 4194 | * Adds a child view with the specified layout parameters. |
| 4195 | * |
Romain Guy | 393a52c | 2012-05-22 20:21:08 -0700 | [diff] [blame] | 4196 | * <p><strong>Note:</strong> do not invoke this method from |
| 4197 | * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)}, |
| 4198 | * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p> |
| 4199 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4200 | * @param child the child view to add |
Alan Viverette | 77bb6f1 | 2015-02-11 17:24:33 -0800 | [diff] [blame] | 4201 | * @param index the position at which to add the child or -1 to add last |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4202 | * @param params the layout parameters to set on the child |
| 4203 | */ |
| 4204 | public void addView(View child, int index, LayoutParams params) { |
| 4205 | if (DBG) { |
| 4206 | System.out.println(this + " addView"); |
| 4207 | } |
| 4208 | |
Adam Powell | 45a9da5 | 2014-10-09 09:44:18 -0700 | [diff] [blame] | 4209 | if (child == null) { |
| 4210 | throw new IllegalArgumentException("Cannot add a null child view to a ViewGroup"); |
| 4211 | } |
| 4212 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4213 | // addViewInner() will call child.requestLayout() when setting the new LayoutParams |
| 4214 | // therefore, we call requestLayout() on ourselves before, so that the child's request |
| 4215 | // will be blocked at our level |
| 4216 | requestLayout(); |
Romain Guy | 849d0a3 | 2011-02-01 17:20:48 -0800 | [diff] [blame] | 4217 | invalidate(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4218 | addViewInner(child, index, params, false); |
| 4219 | } |
| 4220 | |
| 4221 | /** |
| 4222 | * {@inheritDoc} |
| 4223 | */ |
| 4224 | public void updateViewLayout(View view, ViewGroup.LayoutParams params) { |
| 4225 | if (!checkLayoutParams(params)) { |
| 4226 | throw new IllegalArgumentException("Invalid LayoutParams supplied to " + this); |
| 4227 | } |
| 4228 | if (view.mParent != this) { |
| 4229 | throw new IllegalArgumentException("Given view not a child of " + this); |
| 4230 | } |
| 4231 | view.setLayoutParams(params); |
| 4232 | } |
| 4233 | |
| 4234 | /** |
| 4235 | * {@inheritDoc} |
| 4236 | */ |
| 4237 | protected boolean checkLayoutParams(ViewGroup.LayoutParams p) { |
| 4238 | return p != null; |
| 4239 | } |
| 4240 | |
| 4241 | /** |
| 4242 | * Interface definition for a callback to be invoked when the hierarchy |
| 4243 | * within this view changed. The hierarchy changes whenever a child is added |
| 4244 | * to or removed from this view. |
| 4245 | */ |
| 4246 | public interface OnHierarchyChangeListener { |
| 4247 | /** |
| 4248 | * Called when a new child is added to a parent view. |
| 4249 | * |
| 4250 | * @param parent the view in which a child was added |
| 4251 | * @param child the new child view added in the hierarchy |
| 4252 | */ |
| 4253 | void onChildViewAdded(View parent, View child); |
| 4254 | |
| 4255 | /** |
| 4256 | * Called when a child is removed from a parent view. |
| 4257 | * |
| 4258 | * @param parent the view from which the child was removed |
| 4259 | * @param child the child removed from the hierarchy |
| 4260 | */ |
| 4261 | void onChildViewRemoved(View parent, View child); |
| 4262 | } |
| 4263 | |
| 4264 | /** |
| 4265 | * Register a callback to be invoked when a child is added to or removed |
| 4266 | * from this view. |
| 4267 | * |
| 4268 | * @param listener the callback to invoke on hierarchy change |
| 4269 | */ |
| 4270 | public void setOnHierarchyChangeListener(OnHierarchyChangeListener listener) { |
| 4271 | mOnHierarchyChangeListener = listener; |
| 4272 | } |
| 4273 | |
Adam Powell | 6690d01 | 2015-06-17 16:41:56 -0700 | [diff] [blame] | 4274 | void dispatchViewAdded(View child) { |
| 4275 | onViewAdded(child); |
Philip Milne | f51d91c | 2011-07-18 16:12:19 -0700 | [diff] [blame] | 4276 | if (mOnHierarchyChangeListener != null) { |
| 4277 | mOnHierarchyChangeListener.onChildViewAdded(this, child); |
| 4278 | } |
| 4279 | } |
| 4280 | |
| 4281 | /** |
Adam Powell | 6690d01 | 2015-06-17 16:41:56 -0700 | [diff] [blame] | 4282 | * Called when a new child is added to this ViewGroup. Overrides should always |
| 4283 | * call super.onViewAdded. |
| 4284 | * |
| 4285 | * @param child the added child view |
Philip Milne | f51d91c | 2011-07-18 16:12:19 -0700 | [diff] [blame] | 4286 | */ |
Adam Powell | 6690d01 | 2015-06-17 16:41:56 -0700 | [diff] [blame] | 4287 | public void onViewAdded(View child) { |
| 4288 | } |
| 4289 | |
| 4290 | void dispatchViewRemoved(View child) { |
| 4291 | onViewRemoved(child); |
Philip Milne | f51d91c | 2011-07-18 16:12:19 -0700 | [diff] [blame] | 4292 | if (mOnHierarchyChangeListener != null) { |
| 4293 | mOnHierarchyChangeListener.onChildViewRemoved(this, child); |
| 4294 | } |
| 4295 | } |
| 4296 | |
Adam Powell | 6690d01 | 2015-06-17 16:41:56 -0700 | [diff] [blame] | 4297 | /** |
| 4298 | * Called when a child view is removed from this ViewGroup. Overrides should always |
| 4299 | * call super.onViewRemoved. |
| 4300 | * |
| 4301 | * @param child the removed child view |
| 4302 | */ |
| 4303 | public void onViewRemoved(View child) { |
| 4304 | } |
| 4305 | |
Philip Milne | cfb631b | 2012-10-26 10:51:46 -0700 | [diff] [blame] | 4306 | private void clearCachedLayoutMode() { |
Svetoslav | 6254f48 | 2013-06-04 17:22:14 -0700 | [diff] [blame] | 4307 | if (!hasBooleanFlag(FLAG_LAYOUT_MODE_WAS_EXPLICITLY_SET)) { |
Philip Milne | cfb631b | 2012-10-26 10:51:46 -0700 | [diff] [blame] | 4308 | mLayoutMode = LAYOUT_MODE_UNDEFINED; |
| 4309 | } |
| 4310 | } |
| 4311 | |
| 4312 | @Override |
| 4313 | protected void onAttachedToWindow() { |
| 4314 | super.onAttachedToWindow(); |
| 4315 | clearCachedLayoutMode(); |
| 4316 | } |
| 4317 | |
| 4318 | @Override |
| 4319 | protected void onDetachedFromWindow() { |
| 4320 | super.onDetachedFromWindow(); |
| 4321 | clearCachedLayoutMode(); |
| 4322 | } |
| 4323 | |
Philip Milne | f51d91c | 2011-07-18 16:12:19 -0700 | [diff] [blame] | 4324 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4325 | * Adds a view during layout. This is useful if in your onLayout() method, |
| 4326 | * you need to add more views (as does the list view for example). |
| 4327 | * |
| 4328 | * If index is negative, it means put it at the end of the list. |
| 4329 | * |
| 4330 | * @param child the view to add to the group |
Alan Viverette | 77bb6f1 | 2015-02-11 17:24:33 -0800 | [diff] [blame] | 4331 | * @param index the index at which the child must be added or -1 to add last |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4332 | * @param params the layout parameters to associate with the child |
| 4333 | * @return true if the child was added, false otherwise |
| 4334 | */ |
| 4335 | protected boolean addViewInLayout(View child, int index, LayoutParams params) { |
| 4336 | return addViewInLayout(child, index, params, false); |
| 4337 | } |
| 4338 | |
| 4339 | /** |
| 4340 | * Adds a view during layout. This is useful if in your onLayout() method, |
| 4341 | * you need to add more views (as does the list view for example). |
| 4342 | * |
| 4343 | * If index is negative, it means put it at the end of the list. |
| 4344 | * |
| 4345 | * @param child the view to add to the group |
Alan Viverette | 77bb6f1 | 2015-02-11 17:24:33 -0800 | [diff] [blame] | 4346 | * @param index the index at which the child must be added or -1 to add last |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4347 | * @param params the layout parameters to associate with the child |
| 4348 | * @param preventRequestLayout if true, calling this method will not trigger a |
| 4349 | * layout request on child |
| 4350 | * @return true if the child was added, false otherwise |
| 4351 | */ |
| 4352 | protected boolean addViewInLayout(View child, int index, LayoutParams params, |
| 4353 | boolean preventRequestLayout) { |
Adam Powell | 45a9da5 | 2014-10-09 09:44:18 -0700 | [diff] [blame] | 4354 | if (child == null) { |
| 4355 | throw new IllegalArgumentException("Cannot add a null child view to a ViewGroup"); |
| 4356 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4357 | child.mParent = null; |
| 4358 | addViewInner(child, index, params, preventRequestLayout); |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 4359 | child.mPrivateFlags = (child.mPrivateFlags & ~PFLAG_DIRTY_MASK) | PFLAG_DRAWN; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4360 | return true; |
| 4361 | } |
| 4362 | |
| 4363 | /** |
| 4364 | * Prevents the specified child to be laid out during the next layout pass. |
| 4365 | * |
| 4366 | * @param child the child on which to perform the cleanup |
| 4367 | */ |
| 4368 | protected void cleanupLayoutState(View child) { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 4369 | child.mPrivateFlags &= ~View.PFLAG_FORCE_LAYOUT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4370 | } |
| 4371 | |
| 4372 | private void addViewInner(View child, int index, LayoutParams params, |
| 4373 | boolean preventRequestLayout) { |
| 4374 | |
Chet Haase | e8e45d3 | 2011-03-02 17:07:35 -0800 | [diff] [blame] | 4375 | if (mTransition != null) { |
| 4376 | // Don't prevent other add transitions from completing, but cancel remove |
| 4377 | // transitions to let them complete the process before we add to the container |
| 4378 | mTransition.cancel(LayoutTransition.DISAPPEARING); |
Chet Haase | add6577 | 2011-02-09 16:47:29 -0800 | [diff] [blame] | 4379 | } |
| 4380 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4381 | if (child.getParent() != null) { |
| 4382 | throw new IllegalStateException("The specified child already has a parent. " + |
| 4383 | "You must call removeView() on the child's parent first."); |
| 4384 | } |
| 4385 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4386 | if (mTransition != null) { |
Chet Haase | 5e25c2c | 2010-09-16 11:15:56 -0700 | [diff] [blame] | 4387 | mTransition.addChild(this, child); |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4388 | } |
| 4389 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4390 | if (!checkLayoutParams(params)) { |
| 4391 | params = generateLayoutParams(params); |
| 4392 | } |
| 4393 | |
| 4394 | if (preventRequestLayout) { |
| 4395 | child.mLayoutParams = params; |
| 4396 | } else { |
| 4397 | child.setLayoutParams(params); |
| 4398 | } |
| 4399 | |
| 4400 | if (index < 0) { |
| 4401 | index = mChildrenCount; |
| 4402 | } |
| 4403 | |
| 4404 | addInArray(child, index); |
| 4405 | |
| 4406 | // tell our children |
| 4407 | if (preventRequestLayout) { |
| 4408 | child.assignParent(this); |
| 4409 | } else { |
| 4410 | child.mParent = this; |
| 4411 | } |
| 4412 | |
| 4413 | if (child.hasFocus()) { |
| 4414 | requestChildFocus(child, child.findFocus()); |
| 4415 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 4416 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4417 | AttachInfo ai = mAttachInfo; |
Adam Powell | 4b86788 | 2011-09-16 12:59:46 -0700 | [diff] [blame] | 4418 | if (ai != null && (mGroupFlags & FLAG_PREVENT_DISPATCH_ATTACHED_TO_WINDOW) == 0) { |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 4419 | boolean lastKeepOn = ai.mKeepScreenOn; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4420 | ai.mKeepScreenOn = false; |
| 4421 | child.dispatchAttachedToWindow(mAttachInfo, (mViewFlags&VISIBILITY_MASK)); |
| 4422 | if (ai.mKeepScreenOn) { |
| 4423 | needGlobalAttributesUpdate(true); |
| 4424 | } |
| 4425 | ai.mKeepScreenOn = lastKeepOn; |
| 4426 | } |
| 4427 | |
Fabrice Di Meglio | 9a04856 | 2012-09-26 14:55:56 -0700 | [diff] [blame] | 4428 | if (child.isLayoutDirectionInherited()) { |
Fabrice Di Meglio | a7e0bcd | 2012-10-16 19:55:01 -0700 | [diff] [blame] | 4429 | child.resetRtlProperties(); |
Fabrice Di Meglio | 9a04856 | 2012-09-26 14:55:56 -0700 | [diff] [blame] | 4430 | } |
| 4431 | |
Adam Powell | 6690d01 | 2015-06-17 16:41:56 -0700 | [diff] [blame] | 4432 | dispatchViewAdded(child); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4433 | |
| 4434 | if ((child.mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE) { |
| 4435 | mGroupFlags |= FLAG_NOTIFY_CHILDREN_ON_DRAWABLE_STATE_CHANGE; |
| 4436 | } |
Adam Powell | 539ee87 | 2012-02-03 19:00:49 -0800 | [diff] [blame] | 4437 | |
| 4438 | if (child.hasTransientState()) { |
| 4439 | childHasTransientStateChanged(child, true); |
| 4440 | } |
Svetoslav | 6254f48 | 2013-06-04 17:22:14 -0700 | [diff] [blame] | 4441 | |
Svetoslav | 8e3feb1 | 2014-02-24 13:46:47 -0800 | [diff] [blame] | 4442 | if (child.getVisibility() != View.GONE) { |
Svetoslav | 00dbe81 | 2013-06-10 12:51:09 -0700 | [diff] [blame] | 4443 | notifySubtreeAccessibilityStateChangedIfNeeded(); |
Svetoslav | 6254f48 | 2013-06-04 17:22:14 -0700 | [diff] [blame] | 4444 | } |
Chet Haase | c633d2f | 2015-04-07 10:29:39 -0700 | [diff] [blame] | 4445 | |
| 4446 | if (mTransientIndices != null) { |
| 4447 | final int transientCount = mTransientIndices.size(); |
| 4448 | for (int i = 0; i < transientCount; ++i) { |
| 4449 | final int oldIndex = mTransientIndices.get(i); |
| 4450 | if (index <= oldIndex) { |
| 4451 | mTransientIndices.set(i, oldIndex + 1); |
| 4452 | } |
| 4453 | } |
| 4454 | } |
Vadim Tryshev | 02ed4a0 | 2015-10-23 17:39:33 -0700 | [diff] [blame] | 4455 | |
| 4456 | if (mCurrentDragStartEvent != null && child.getVisibility() == VISIBLE) { |
| 4457 | notifyChildOfDragStart(child); |
| 4458 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4459 | } |
| 4460 | |
| 4461 | private void addInArray(View child, int index) { |
| 4462 | View[] children = mChildren; |
| 4463 | final int count = mChildrenCount; |
| 4464 | final int size = children.length; |
| 4465 | if (index == count) { |
| 4466 | if (size == count) { |
| 4467 | mChildren = new View[size + ARRAY_CAPACITY_INCREMENT]; |
| 4468 | System.arraycopy(children, 0, mChildren, 0, size); |
| 4469 | children = mChildren; |
| 4470 | } |
| 4471 | children[mChildrenCount++] = child; |
| 4472 | } else if (index < count) { |
| 4473 | if (size == count) { |
| 4474 | mChildren = new View[size + ARRAY_CAPACITY_INCREMENT]; |
| 4475 | System.arraycopy(children, 0, mChildren, 0, index); |
| 4476 | System.arraycopy(children, index, mChildren, index + 1, count - index); |
| 4477 | children = mChildren; |
| 4478 | } else { |
| 4479 | System.arraycopy(children, index, children, index + 1, count - index); |
| 4480 | } |
| 4481 | children[index] = child; |
| 4482 | mChildrenCount++; |
Joe Onorato | 03ab0c7 | 2011-01-06 15:46:27 -0800 | [diff] [blame] | 4483 | if (mLastTouchDownIndex >= index) { |
| 4484 | mLastTouchDownIndex++; |
| 4485 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4486 | } else { |
| 4487 | throw new IndexOutOfBoundsException("index=" + index + " count=" + count); |
| 4488 | } |
| 4489 | } |
| 4490 | |
| 4491 | // This method also sets the child's mParent to null |
| 4492 | private void removeFromArray(int index) { |
| 4493 | final View[] children = mChildren; |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4494 | if (!(mTransitioningViews != null && mTransitioningViews.contains(children[index]))) { |
| 4495 | children[index].mParent = null; |
| 4496 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4497 | final int count = mChildrenCount; |
| 4498 | if (index == count - 1) { |
| 4499 | children[--mChildrenCount] = null; |
| 4500 | } else if (index >= 0 && index < count) { |
| 4501 | System.arraycopy(children, index + 1, children, index, count - index - 1); |
| 4502 | children[--mChildrenCount] = null; |
| 4503 | } else { |
| 4504 | throw new IndexOutOfBoundsException(); |
| 4505 | } |
Joe Onorato | 03ab0c7 | 2011-01-06 15:46:27 -0800 | [diff] [blame] | 4506 | if (mLastTouchDownIndex == index) { |
| 4507 | mLastTouchDownTime = 0; |
| 4508 | mLastTouchDownIndex = -1; |
| 4509 | } else if (mLastTouchDownIndex > index) { |
| 4510 | mLastTouchDownIndex--; |
| 4511 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4512 | } |
| 4513 | |
| 4514 | // This method also sets the children's mParent to null |
| 4515 | private void removeFromArray(int start, int count) { |
| 4516 | final View[] children = mChildren; |
| 4517 | final int childrenCount = mChildrenCount; |
| 4518 | |
| 4519 | start = Math.max(0, start); |
| 4520 | final int end = Math.min(childrenCount, start + count); |
| 4521 | |
| 4522 | if (start == end) { |
| 4523 | return; |
| 4524 | } |
| 4525 | |
| 4526 | if (end == childrenCount) { |
| 4527 | for (int i = start; i < end; i++) { |
| 4528 | children[i].mParent = null; |
| 4529 | children[i] = null; |
| 4530 | } |
| 4531 | } else { |
| 4532 | for (int i = start; i < end; i++) { |
| 4533 | children[i].mParent = null; |
| 4534 | } |
| 4535 | |
| 4536 | // Since we're looping above, we might as well do the copy, but is arraycopy() |
| 4537 | // faster than the extra 2 bounds checks we would do in the loop? |
| 4538 | System.arraycopy(children, end, children, start, childrenCount - end); |
| 4539 | |
| 4540 | for (int i = childrenCount - (end - start); i < childrenCount; i++) { |
| 4541 | children[i] = null; |
| 4542 | } |
| 4543 | } |
| 4544 | |
| 4545 | mChildrenCount -= (end - start); |
| 4546 | } |
| 4547 | |
| 4548 | private void bindLayoutAnimation(View child) { |
| 4549 | Animation a = mLayoutAnimationController.getAnimationForView(child); |
| 4550 | child.setAnimation(a); |
| 4551 | } |
| 4552 | |
| 4553 | /** |
| 4554 | * Subclasses should override this method to set layout animation |
| 4555 | * parameters on the supplied child. |
| 4556 | * |
| 4557 | * @param child the child to associate with animation parameters |
| 4558 | * @param params the child's layout parameters which hold the animation |
| 4559 | * parameters |
| 4560 | * @param index the index of the child in the view group |
| 4561 | * @param count the number of children in the view group |
| 4562 | */ |
| 4563 | protected void attachLayoutAnimationParameters(View child, |
| 4564 | LayoutParams params, int index, int count) { |
| 4565 | LayoutAnimationController.AnimationParameters animationParams = |
| 4566 | params.layoutAnimationParameters; |
| 4567 | if (animationParams == null) { |
| 4568 | animationParams = new LayoutAnimationController.AnimationParameters(); |
| 4569 | params.layoutAnimationParameters = animationParams; |
| 4570 | } |
| 4571 | |
| 4572 | animationParams.count = count; |
| 4573 | animationParams.index = index; |
| 4574 | } |
| 4575 | |
| 4576 | /** |
| 4577 | * {@inheritDoc} |
Filip Gruszczynski | a33bdf3 | 2015-11-19 18:22:16 -0800 | [diff] [blame] | 4578 | * |
Romain Guy | 393a52c | 2012-05-22 20:21:08 -0700 | [diff] [blame] | 4579 | * <p><strong>Note:</strong> do not invoke this method from |
| 4580 | * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)}, |
| 4581 | * {@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] | 4582 | */ |
| 4583 | public void removeView(View view) { |
Alan Viverette | 177ec460 | 2014-10-17 13:34:50 -0700 | [diff] [blame] | 4584 | if (removeViewInternal(view)) { |
| 4585 | requestLayout(); |
| 4586 | invalidate(true); |
| 4587 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4588 | } |
| 4589 | |
| 4590 | /** |
| 4591 | * Removes a view during layout. This is useful if in your onLayout() method, |
| 4592 | * you need to remove more views. |
| 4593 | * |
Romain Guy | 393a52c | 2012-05-22 20:21:08 -0700 | [diff] [blame] | 4594 | * <p><strong>Note:</strong> do not invoke this method from |
| 4595 | * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)}, |
| 4596 | * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p> |
Filip Gruszczynski | a33bdf3 | 2015-11-19 18:22:16 -0800 | [diff] [blame] | 4597 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4598 | * @param view the view to remove from the group |
| 4599 | */ |
| 4600 | public void removeViewInLayout(View view) { |
| 4601 | removeViewInternal(view); |
| 4602 | } |
| 4603 | |
| 4604 | /** |
| 4605 | * Removes a range of views during layout. This is useful if in your onLayout() method, |
| 4606 | * you need to remove more views. |
| 4607 | * |
Romain Guy | 393a52c | 2012-05-22 20:21:08 -0700 | [diff] [blame] | 4608 | * <p><strong>Note:</strong> do not invoke this method from |
| 4609 | * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)}, |
| 4610 | * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p> |
| 4611 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4612 | * @param start the index of the first view to remove from the group |
| 4613 | * @param count the number of views to remove from the group |
| 4614 | */ |
| 4615 | public void removeViewsInLayout(int start, int count) { |
| 4616 | removeViewsInternal(start, count); |
| 4617 | } |
| 4618 | |
| 4619 | /** |
| 4620 | * Removes the view at the specified position in the group. |
| 4621 | * |
Romain Guy | 393a52c | 2012-05-22 20:21:08 -0700 | [diff] [blame] | 4622 | * <p><strong>Note:</strong> do not invoke this method from |
| 4623 | * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)}, |
| 4624 | * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p> |
Filip Gruszczynski | a33bdf3 | 2015-11-19 18:22:16 -0800 | [diff] [blame] | 4625 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4626 | * @param index the position in the group of the view to remove |
| 4627 | */ |
| 4628 | public void removeViewAt(int index) { |
| 4629 | removeViewInternal(index, getChildAt(index)); |
| 4630 | requestLayout(); |
Romain Guy | 849d0a3 | 2011-02-01 17:20:48 -0800 | [diff] [blame] | 4631 | invalidate(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4632 | } |
| 4633 | |
| 4634 | /** |
| 4635 | * Removes the specified range of views from the group. |
| 4636 | * |
Romain Guy | 393a52c | 2012-05-22 20:21:08 -0700 | [diff] [blame] | 4637 | * <p><strong>Note:</strong> do not invoke this method from |
| 4638 | * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)}, |
| 4639 | * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p> |
| 4640 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4641 | * @param start the first position in the group of the range of views to remove |
| 4642 | * @param count the number of views to remove |
| 4643 | */ |
| 4644 | public void removeViews(int start, int count) { |
| 4645 | removeViewsInternal(start, count); |
| 4646 | requestLayout(); |
Romain Guy | 849d0a3 | 2011-02-01 17:20:48 -0800 | [diff] [blame] | 4647 | invalidate(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4648 | } |
| 4649 | |
Alan Viverette | 177ec460 | 2014-10-17 13:34:50 -0700 | [diff] [blame] | 4650 | private boolean removeViewInternal(View view) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4651 | final int index = indexOfChild(view); |
| 4652 | if (index >= 0) { |
| 4653 | removeViewInternal(index, view); |
Alan Viverette | 177ec460 | 2014-10-17 13:34:50 -0700 | [diff] [blame] | 4654 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4655 | } |
Alan Viverette | 177ec460 | 2014-10-17 13:34:50 -0700 | [diff] [blame] | 4656 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4657 | } |
| 4658 | |
| 4659 | private void removeViewInternal(int index, View view) { |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4660 | if (mTransition != null) { |
Chet Haase | 5e25c2c | 2010-09-16 11:15:56 -0700 | [diff] [blame] | 4661 | mTransition.removeChild(this, view); |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4662 | } |
| 4663 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4664 | boolean clearChildFocus = false; |
| 4665 | if (view == mFocused) { |
Alan Viverette | 223622a | 2013-12-17 13:29:02 -0800 | [diff] [blame] | 4666 | view.unFocus(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4667 | clearChildFocus = true; |
| 4668 | } |
| 4669 | |
Alan Viverette | 632af84 | 2014-10-28 13:45:11 -0700 | [diff] [blame] | 4670 | view.clearAccessibilityFocus(); |
Svetoslav Ganov | 961bf0e | 2012-05-08 09:40:03 -0700 | [diff] [blame] | 4671 | |
Jeff Brown | 59a422e | 2012-04-19 15:19:19 -0700 | [diff] [blame] | 4672 | cancelTouchTarget(view); |
| 4673 | cancelHoverTarget(view); |
| 4674 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4675 | if (view.getAnimation() != null || |
| 4676 | (mTransitioningViews != null && mTransitioningViews.contains(view))) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4677 | addDisappearingView(view); |
| 4678 | } else if (view.mAttachInfo != null) { |
| 4679 | view.dispatchDetachedFromWindow(); |
| 4680 | } |
| 4681 | |
Adam Powell | 539ee87 | 2012-02-03 19:00:49 -0800 | [diff] [blame] | 4682 | if (view.hasTransientState()) { |
| 4683 | childHasTransientStateChanged(view, false); |
| 4684 | } |
| 4685 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4686 | needGlobalAttributesUpdate(false); |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 4687 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4688 | removeFromArray(index); |
| 4689 | |
| 4690 | if (clearChildFocus) { |
| 4691 | clearChildFocus(view); |
Svetoslav Ganov | 149567f | 2013-01-08 15:23:34 -0800 | [diff] [blame] | 4692 | if (!rootViewRequestFocus()) { |
| 4693 | notifyGlobalFocusCleared(this); |
| 4694 | } |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 4695 | } |
Romain Guy | 6fb0563 | 2012-11-29 10:50:33 -0800 | [diff] [blame] | 4696 | |
Adam Powell | 6690d01 | 2015-06-17 16:41:56 -0700 | [diff] [blame] | 4697 | dispatchViewRemoved(view); |
Svetoslav | 6254f48 | 2013-06-04 17:22:14 -0700 | [diff] [blame] | 4698 | |
Svetoslav | 8e3feb1 | 2014-02-24 13:46:47 -0800 | [diff] [blame] | 4699 | if (view.getVisibility() != View.GONE) { |
Svetoslav | 00dbe81 | 2013-06-10 12:51:09 -0700 | [diff] [blame] | 4700 | notifySubtreeAccessibilityStateChangedIfNeeded(); |
Svetoslav | 6254f48 | 2013-06-04 17:22:14 -0700 | [diff] [blame] | 4701 | } |
Chet Haase | c633d2f | 2015-04-07 10:29:39 -0700 | [diff] [blame] | 4702 | |
| 4703 | int transientCount = mTransientIndices == null ? 0 : mTransientIndices.size(); |
| 4704 | for (int i = 0; i < transientCount; ++i) { |
| 4705 | final int oldIndex = mTransientIndices.get(i); |
| 4706 | if (index < oldIndex) { |
| 4707 | mTransientIndices.set(i, oldIndex - 1); |
| 4708 | } |
| 4709 | } |
Vadim Tryshev | 02ed4a0 | 2015-10-23 17:39:33 -0700 | [diff] [blame] | 4710 | |
| 4711 | if (mCurrentDragStartEvent != null) { |
| 4712 | mChildrenInterestedInDrag.remove(view); |
| 4713 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4714 | } |
| 4715 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4716 | /** |
| 4717 | * Sets the LayoutTransition object for this ViewGroup. If the LayoutTransition object is |
| 4718 | * not null, changes in layout which occur because of children being added to or removed from |
| 4719 | * the ViewGroup will be animated according to the animations defined in that LayoutTransition |
| 4720 | * object. By default, the transition object is null (so layout changes are not animated). |
| 4721 | * |
Chet Haase | ef3cbfd | 2013-08-21 14:01:02 -0700 | [diff] [blame] | 4722 | * <p>Replacing a non-null transition will cause that previous transition to be |
| 4723 | * canceled, if it is currently running, to restore this container to |
| 4724 | * its correct post-transition state.</p> |
| 4725 | * |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4726 | * @param transition The LayoutTransition object that will animated changes in layout. A value |
| 4727 | * of <code>null</code> means no transition will run on layout changes. |
Chet Haase | 13cc120 | 2010-09-03 15:39:20 -0700 | [diff] [blame] | 4728 | * @attr ref android.R.styleable#ViewGroup_animateLayoutChanges |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4729 | */ |
| 4730 | public void setLayoutTransition(LayoutTransition transition) { |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 4731 | if (mTransition != null) { |
Chet Haase | fee6f2b | 2013-08-27 12:22:29 -0700 | [diff] [blame] | 4732 | LayoutTransition previousTransition = mTransition; |
| 4733 | previousTransition.cancel(); |
| 4734 | previousTransition.removeTransitionListener(mLayoutTransitionListener); |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 4735 | } |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4736 | mTransition = transition; |
Chet Haase | 13cc120 | 2010-09-03 15:39:20 -0700 | [diff] [blame] | 4737 | if (mTransition != null) { |
| 4738 | mTransition.addTransitionListener(mLayoutTransitionListener); |
| 4739 | } |
| 4740 | } |
| 4741 | |
| 4742 | /** |
| 4743 | * Gets the LayoutTransition object for this ViewGroup. If the LayoutTransition object is |
| 4744 | * not null, changes in layout which occur because of children being added to or removed from |
| 4745 | * the ViewGroup will be animated according to the animations defined in that LayoutTransition |
| 4746 | * object. By default, the transition object is null (so layout changes are not animated). |
| 4747 | * |
| 4748 | * @return LayoutTranstion The LayoutTransition object that will animated changes in layout. |
| 4749 | * A value of <code>null</code> means no transition will run on layout changes. |
| 4750 | */ |
| 4751 | public LayoutTransition getLayoutTransition() { |
| 4752 | return mTransition; |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4753 | } |
| 4754 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4755 | private void removeViewsInternal(int start, int count) { |
Chris Craik | 18a759d | 2015-10-14 14:16:33 -0700 | [diff] [blame] | 4756 | final int end = start + count; |
| 4757 | |
| 4758 | if (start < 0 || count < 0 || end > mChildrenCount) { |
| 4759 | throw new IndexOutOfBoundsException(); |
| 4760 | } |
| 4761 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4762 | final View focused = mFocused; |
| 4763 | final boolean detach = mAttachInfo != null; |
Svetoslav Ganov | 149567f | 2013-01-08 15:23:34 -0800 | [diff] [blame] | 4764 | boolean clearChildFocus = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4765 | |
| 4766 | final View[] children = mChildren; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4767 | |
| 4768 | for (int i = start; i < end; i++) { |
| 4769 | final View view = children[i]; |
| 4770 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4771 | if (mTransition != null) { |
Chet Haase | 5e25c2c | 2010-09-16 11:15:56 -0700 | [diff] [blame] | 4772 | mTransition.removeChild(this, view); |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4773 | } |
| 4774 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4775 | if (view == focused) { |
Alan Viverette | 223622a | 2013-12-17 13:29:02 -0800 | [diff] [blame] | 4776 | view.unFocus(null); |
Svetoslav Ganov | 149567f | 2013-01-08 15:23:34 -0800 | [diff] [blame] | 4777 | clearChildFocus = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4778 | } |
| 4779 | |
Alan Viverette | 632af84 | 2014-10-28 13:45:11 -0700 | [diff] [blame] | 4780 | view.clearAccessibilityFocus(); |
Svetoslav Ganov | 961bf0e | 2012-05-08 09:40:03 -0700 | [diff] [blame] | 4781 | |
Jeff Brown | 59a422e | 2012-04-19 15:19:19 -0700 | [diff] [blame] | 4782 | cancelTouchTarget(view); |
| 4783 | cancelHoverTarget(view); |
| 4784 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4785 | if (view.getAnimation() != null || |
| 4786 | (mTransitioningViews != null && mTransitioningViews.contains(view))) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4787 | addDisappearingView(view); |
| 4788 | } else if (detach) { |
| 4789 | view.dispatchDetachedFromWindow(); |
| 4790 | } |
| 4791 | |
Adam Powell | 539ee87 | 2012-02-03 19:00:49 -0800 | [diff] [blame] | 4792 | if (view.hasTransientState()) { |
| 4793 | childHasTransientStateChanged(view, false); |
| 4794 | } |
| 4795 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4796 | needGlobalAttributesUpdate(false); |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 4797 | |
Adam Powell | 6690d01 | 2015-06-17 16:41:56 -0700 | [diff] [blame] | 4798 | dispatchViewRemoved(view); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4799 | } |
| 4800 | |
| 4801 | removeFromArray(start, count); |
| 4802 | |
Svetoslav Ganov | 149567f | 2013-01-08 15:23:34 -0800 | [diff] [blame] | 4803 | if (clearChildFocus) { |
| 4804 | clearChildFocus(focused); |
| 4805 | if (!rootViewRequestFocus()) { |
| 4806 | notifyGlobalFocusCleared(focused); |
| 4807 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4808 | } |
| 4809 | } |
| 4810 | |
| 4811 | /** |
| 4812 | * Call this method to remove all child views from the |
| 4813 | * ViewGroup. |
Filip Gruszczynski | a33bdf3 | 2015-11-19 18:22:16 -0800 | [diff] [blame] | 4814 | * |
Romain Guy | 393a52c | 2012-05-22 20:21:08 -0700 | [diff] [blame] | 4815 | * <p><strong>Note:</strong> do not invoke this method from |
| 4816 | * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)}, |
| 4817 | * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4818 | */ |
| 4819 | public void removeAllViews() { |
| 4820 | removeAllViewsInLayout(); |
| 4821 | requestLayout(); |
Romain Guy | 849d0a3 | 2011-02-01 17:20:48 -0800 | [diff] [blame] | 4822 | invalidate(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4823 | } |
| 4824 | |
| 4825 | /** |
| 4826 | * Called by a ViewGroup subclass to remove child views from itself, |
| 4827 | * when it must first know its size on screen before it can calculate how many |
| 4828 | * child views it will render. An example is a Gallery or a ListView, which |
| 4829 | * may "have" 50 children, but actually only render the number of children |
| 4830 | * that can currently fit inside the object on screen. Do not call |
| 4831 | * this method unless you are extending ViewGroup and understand the |
| 4832 | * view measuring and layout pipeline. |
Romain Guy | 393a52c | 2012-05-22 20:21:08 -0700 | [diff] [blame] | 4833 | * |
| 4834 | * <p><strong>Note:</strong> do not invoke this method from |
| 4835 | * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)}, |
| 4836 | * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4837 | */ |
| 4838 | public void removeAllViewsInLayout() { |
| 4839 | final int count = mChildrenCount; |
| 4840 | if (count <= 0) { |
| 4841 | return; |
| 4842 | } |
| 4843 | |
| 4844 | final View[] children = mChildren; |
| 4845 | mChildrenCount = 0; |
| 4846 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4847 | final View focused = mFocused; |
| 4848 | final boolean detach = mAttachInfo != null; |
Svetoslav Ganov | 149567f | 2013-01-08 15:23:34 -0800 | [diff] [blame] | 4849 | boolean clearChildFocus = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4850 | |
| 4851 | needGlobalAttributesUpdate(false); |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 4852 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4853 | for (int i = count - 1; i >= 0; i--) { |
| 4854 | final View view = children[i]; |
| 4855 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4856 | if (mTransition != null) { |
Chet Haase | 5e25c2c | 2010-09-16 11:15:56 -0700 | [diff] [blame] | 4857 | mTransition.removeChild(this, view); |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4858 | } |
| 4859 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4860 | if (view == focused) { |
Alan Viverette | 223622a | 2013-12-17 13:29:02 -0800 | [diff] [blame] | 4861 | view.unFocus(null); |
Svetoslav Ganov | 149567f | 2013-01-08 15:23:34 -0800 | [diff] [blame] | 4862 | clearChildFocus = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4863 | } |
| 4864 | |
Alan Viverette | 632af84 | 2014-10-28 13:45:11 -0700 | [diff] [blame] | 4865 | view.clearAccessibilityFocus(); |
Svetoslav Ganov | 961bf0e | 2012-05-08 09:40:03 -0700 | [diff] [blame] | 4866 | |
Jeff Brown | 59a422e | 2012-04-19 15:19:19 -0700 | [diff] [blame] | 4867 | cancelTouchTarget(view); |
| 4868 | cancelHoverTarget(view); |
| 4869 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4870 | if (view.getAnimation() != null || |
| 4871 | (mTransitioningViews != null && mTransitioningViews.contains(view))) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4872 | addDisappearingView(view); |
| 4873 | } else if (detach) { |
| 4874 | view.dispatchDetachedFromWindow(); |
| 4875 | } |
| 4876 | |
Adam Powell | 539ee87 | 2012-02-03 19:00:49 -0800 | [diff] [blame] | 4877 | if (view.hasTransientState()) { |
| 4878 | childHasTransientStateChanged(view, false); |
| 4879 | } |
| 4880 | |
Adam Powell | 6690d01 | 2015-06-17 16:41:56 -0700 | [diff] [blame] | 4881 | dispatchViewRemoved(view); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4882 | |
| 4883 | view.mParent = null; |
| 4884 | children[i] = null; |
| 4885 | } |
| 4886 | |
Svetoslav Ganov | 149567f | 2013-01-08 15:23:34 -0800 | [diff] [blame] | 4887 | if (clearChildFocus) { |
| 4888 | clearChildFocus(focused); |
| 4889 | if (!rootViewRequestFocus()) { |
| 4890 | notifyGlobalFocusCleared(focused); |
| 4891 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4892 | } |
| 4893 | } |
| 4894 | |
| 4895 | /** |
| 4896 | * Finishes the removal of a detached view. This method will dispatch the detached from |
| 4897 | * window event and notify the hierarchy change listener. |
Chet Haase | ca479d4 | 2012-08-30 17:20:08 -0700 | [diff] [blame] | 4898 | * <p> |
| 4899 | * This method is intended to be lightweight and makes no assumptions about whether the |
| 4900 | * parent or child should be redrawn. Proper use of this method will include also making |
| 4901 | * any appropriate {@link #requestLayout()} or {@link #invalidate()} calls. |
| 4902 | * For example, callers can {@link #post(Runnable) post} a {@link Runnable} |
| 4903 | * which performs a {@link #requestLayout()} on the next frame, after all detach/remove |
| 4904 | * 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] | 4905 | * |
| 4906 | * @param child the child to be definitely removed from the view hierarchy |
| 4907 | * @param animate if true and the view has an animation, the view is placed in the |
| 4908 | * disappearing views list, otherwise, it is detached from the window |
| 4909 | * |
| 4910 | * @see #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams) |
| 4911 | * @see #detachAllViewsFromParent() |
| 4912 | * @see #detachViewFromParent(View) |
| 4913 | * @see #detachViewFromParent(int) |
| 4914 | */ |
| 4915 | protected void removeDetachedView(View child, boolean animate) { |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4916 | if (mTransition != null) { |
Chet Haase | 5e25c2c | 2010-09-16 11:15:56 -0700 | [diff] [blame] | 4917 | mTransition.removeChild(this, child); |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4918 | } |
| 4919 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4920 | if (child == mFocused) { |
| 4921 | child.clearFocus(); |
| 4922 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 4923 | |
Svetoslav Ganov | 961bf0e | 2012-05-08 09:40:03 -0700 | [diff] [blame] | 4924 | child.clearAccessibilityFocus(); |
| 4925 | |
Jeff Brown | 59a422e | 2012-04-19 15:19:19 -0700 | [diff] [blame] | 4926 | cancelTouchTarget(child); |
| 4927 | cancelHoverTarget(child); |
| 4928 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4929 | if ((animate && child.getAnimation() != null) || |
| 4930 | (mTransitioningViews != null && mTransitioningViews.contains(child))) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4931 | addDisappearingView(child); |
| 4932 | } else if (child.mAttachInfo != null) { |
| 4933 | child.dispatchDetachedFromWindow(); |
| 4934 | } |
| 4935 | |
Adam Powell | 539ee87 | 2012-02-03 19:00:49 -0800 | [diff] [blame] | 4936 | if (child.hasTransientState()) { |
| 4937 | childHasTransientStateChanged(child, false); |
| 4938 | } |
| 4939 | |
Adam Powell | 6690d01 | 2015-06-17 16:41:56 -0700 | [diff] [blame] | 4940 | dispatchViewRemoved(child); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4941 | } |
| 4942 | |
| 4943 | /** |
| 4944 | * 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] | 4945 | * sets the layout parameters and puts the view in the list of children so that |
| 4946 | * it can be retrieved by calling {@link #getChildAt(int)}. |
| 4947 | * <p> |
| 4948 | * This method is intended to be lightweight and makes no assumptions about whether the |
| 4949 | * parent or child should be redrawn. Proper use of this method will include also making |
| 4950 | * any appropriate {@link #requestLayout()} or {@link #invalidate()} calls. |
| 4951 | * For example, callers can {@link #post(Runnable) post} a {@link Runnable} |
| 4952 | * which performs a {@link #requestLayout()} on the next frame, after all detach/attach |
| 4953 | * calls are finished, causing layout to be run prior to redrawing the view hierarchy. |
| 4954 | * <p> |
| 4955 | * 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] | 4956 | * |
| 4957 | * @param child the child to attach |
| 4958 | * @param index the index at which the child should be attached |
| 4959 | * @param params the layout parameters of the child |
| 4960 | * |
| 4961 | * @see #removeDetachedView(View, boolean) |
| 4962 | * @see #detachAllViewsFromParent() |
| 4963 | * @see #detachViewFromParent(View) |
| 4964 | * @see #detachViewFromParent(int) |
| 4965 | */ |
| 4966 | protected void attachViewToParent(View child, int index, LayoutParams params) { |
| 4967 | child.mLayoutParams = params; |
| 4968 | |
| 4969 | if (index < 0) { |
| 4970 | index = mChildrenCount; |
| 4971 | } |
| 4972 | |
| 4973 | addInArray(child, index); |
| 4974 | |
| 4975 | child.mParent = this; |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 4976 | child.mPrivateFlags = (child.mPrivateFlags & ~PFLAG_DIRTY_MASK |
| 4977 | & ~PFLAG_DRAWING_CACHE_VALID) |
| 4978 | | PFLAG_DRAWN | PFLAG_INVALIDATED; |
| 4979 | this.mPrivateFlags |= PFLAG_INVALIDATED; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4980 | |
| 4981 | if (child.hasFocus()) { |
| 4982 | requestChildFocus(child, child.findFocus()); |
| 4983 | } |
| 4984 | } |
| 4985 | |
| 4986 | /** |
Chet Haase | ca479d4 | 2012-08-30 17:20:08 -0700 | [diff] [blame] | 4987 | * Detaches a view from its parent. Detaching a view should be followed |
| 4988 | * either by a call to |
| 4989 | * {@link #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)} |
| 4990 | * or a call to {@link #removeDetachedView(View, boolean)}. Detachment should only be |
| 4991 | * temporary; reattachment or removal should happen within the same drawing cycle as |
| 4992 | * detachment. When a view is detached, its parent is null and cannot be retrieved by a |
| 4993 | * call to {@link #getChildAt(int)}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4994 | * |
| 4995 | * @param child the child to detach |
| 4996 | * |
| 4997 | * @see #detachViewFromParent(int) |
| 4998 | * @see #detachViewsFromParent(int, int) |
| 4999 | * @see #detachAllViewsFromParent() |
| 5000 | * @see #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams) |
| 5001 | * @see #removeDetachedView(View, boolean) |
| 5002 | */ |
| 5003 | protected void detachViewFromParent(View child) { |
| 5004 | removeFromArray(indexOfChild(child)); |
| 5005 | } |
| 5006 | |
| 5007 | /** |
Chet Haase | ca479d4 | 2012-08-30 17:20:08 -0700 | [diff] [blame] | 5008 | * Detaches a view from its parent. Detaching a view should be followed |
| 5009 | * either by a call to |
| 5010 | * {@link #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)} |
| 5011 | * or a call to {@link #removeDetachedView(View, boolean)}. Detachment should only be |
| 5012 | * temporary; reattachment or removal should happen within the same drawing cycle as |
| 5013 | * detachment. When a view is detached, its parent is null and cannot be retrieved by a |
| 5014 | * call to {@link #getChildAt(int)}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5015 | * |
| 5016 | * @param index the index of the child to detach |
| 5017 | * |
| 5018 | * @see #detachViewFromParent(View) |
| 5019 | * @see #detachAllViewsFromParent() |
| 5020 | * @see #detachViewsFromParent(int, int) |
| 5021 | * @see #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams) |
| 5022 | * @see #removeDetachedView(View, boolean) |
| 5023 | */ |
| 5024 | protected void detachViewFromParent(int index) { |
| 5025 | removeFromArray(index); |
| 5026 | } |
| 5027 | |
| 5028 | /** |
Chet Haase | ca479d4 | 2012-08-30 17:20:08 -0700 | [diff] [blame] | 5029 | * Detaches a range of views from their parents. Detaching a view should be followed |
| 5030 | * either by a call to |
| 5031 | * {@link #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)} |
| 5032 | * or a call to {@link #removeDetachedView(View, boolean)}. Detachment should only be |
| 5033 | * temporary; reattachment or removal should happen within the same drawing cycle as |
| 5034 | * detachment. When a view is detached, its parent is null and cannot be retrieved by a |
| 5035 | * call to {@link #getChildAt(int)}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5036 | * |
| 5037 | * @param start the first index of the childrend range to detach |
| 5038 | * @param count the number of children to detach |
| 5039 | * |
| 5040 | * @see #detachViewFromParent(View) |
| 5041 | * @see #detachViewFromParent(int) |
| 5042 | * @see #detachAllViewsFromParent() |
| 5043 | * @see #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams) |
| 5044 | * @see #removeDetachedView(View, boolean) |
| 5045 | */ |
| 5046 | protected void detachViewsFromParent(int start, int count) { |
| 5047 | removeFromArray(start, count); |
| 5048 | } |
| 5049 | |
| 5050 | /** |
Chet Haase | ca479d4 | 2012-08-30 17:20:08 -0700 | [diff] [blame] | 5051 | * Detaches all views from the parent. Detaching a view should be followed |
| 5052 | * either by a call to |
| 5053 | * {@link #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)} |
| 5054 | * or a call to {@link #removeDetachedView(View, boolean)}. Detachment should only be |
| 5055 | * temporary; reattachment or removal should happen within the same drawing cycle as |
| 5056 | * detachment. When a view is detached, its parent is null and cannot be retrieved by a |
| 5057 | * call to {@link #getChildAt(int)}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5058 | * |
| 5059 | * @see #detachViewFromParent(View) |
| 5060 | * @see #detachViewFromParent(int) |
| 5061 | * @see #detachViewsFromParent(int, int) |
| 5062 | * @see #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams) |
| 5063 | * @see #removeDetachedView(View, boolean) |
| 5064 | */ |
| 5065 | protected void detachAllViewsFromParent() { |
| 5066 | final int count = mChildrenCount; |
| 5067 | if (count <= 0) { |
| 5068 | return; |
| 5069 | } |
| 5070 | |
| 5071 | final View[] children = mChildren; |
| 5072 | mChildrenCount = 0; |
| 5073 | |
| 5074 | for (int i = count - 1; i >= 0; i--) { |
| 5075 | children[i].mParent = null; |
| 5076 | children[i] = null; |
| 5077 | } |
| 5078 | } |
| 5079 | |
| 5080 | /** |
| 5081 | * Don't call or override this method. It is used for the implementation of |
| 5082 | * the view hierarchy. |
| 5083 | */ |
| 5084 | public final void invalidateChild(View child, final Rect dirty) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5085 | ViewParent parent = this; |
| 5086 | |
| 5087 | final AttachInfo attachInfo = mAttachInfo; |
| 5088 | if (attachInfo != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5089 | // If the child is drawing an animation, we want to copy this flag onto |
| 5090 | // ourselves and the parent to make sure the invalidate request goes |
| 5091 | // through |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 5092 | final boolean drawAnimation = (child.mPrivateFlags & PFLAG_DRAW_ANIMATION) |
| 5093 | == PFLAG_DRAW_ANIMATION; |
Romain Guy | 24443ea | 2009-05-11 11:56:30 -0700 | [diff] [blame] | 5094 | |
Romain Guy | fe455af | 2012-02-15 16:40:20 -0800 | [diff] [blame] | 5095 | // Check whether the child that requests the invalidate is fully opaque |
| 5096 | // Views being animated or transformed are not considered opaque because we may |
| 5097 | // be invalidating their old position and need the parent to paint behind them. |
| 5098 | Matrix childMatrix = child.getMatrix(); |
| 5099 | final boolean isOpaque = child.isOpaque() && !drawAnimation && |
| 5100 | child.getAnimation() == null && childMatrix.isIdentity(); |
| 5101 | // Mark the child as dirty, using the appropriate flag |
| 5102 | // Make sure we do not set both flags at the same time |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 5103 | int opaqueFlag = isOpaque ? PFLAG_DIRTY_OPAQUE : PFLAG_DIRTY; |
Romain Guy | fe455af | 2012-02-15 16:40:20 -0800 | [diff] [blame] | 5104 | |
John Reck | 96bb8ad | 2014-06-19 10:53:03 -0700 | [diff] [blame] | 5105 | if (child.mLayerType != LAYER_TYPE_NONE) { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 5106 | mPrivateFlags |= PFLAG_INVALIDATED; |
| 5107 | mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID; |
Romain Guy | fe455af | 2012-02-15 16:40:20 -0800 | [diff] [blame] | 5108 | } |
| 5109 | |
| 5110 | final int[] location = attachInfo.mInvalidateChildLocation; |
| 5111 | location[CHILD_LEFT_INDEX] = child.mLeft; |
| 5112 | location[CHILD_TOP_INDEX] = child.mTop; |
Chet Haase | 599913d | 2012-07-23 16:22:05 -0700 | [diff] [blame] | 5113 | if (!childMatrix.isIdentity() || |
| 5114 | (mGroupFlags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) { |
Romain Guy | fe455af | 2012-02-15 16:40:20 -0800 | [diff] [blame] | 5115 | RectF boundingRect = attachInfo.mTmpTransformRect; |
| 5116 | boundingRect.set(dirty); |
Chet Haase | 599913d | 2012-07-23 16:22:05 -0700 | [diff] [blame] | 5117 | Matrix transformMatrix; |
| 5118 | if ((mGroupFlags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) { |
| 5119 | Transformation t = attachInfo.mTmpTransformation; |
| 5120 | boolean transformed = getChildStaticTransformation(child, t); |
| 5121 | if (transformed) { |
| 5122 | transformMatrix = attachInfo.mTmpMatrix; |
| 5123 | transformMatrix.set(t.getMatrix()); |
| 5124 | if (!childMatrix.isIdentity()) { |
| 5125 | transformMatrix.preConcat(childMatrix); |
| 5126 | } |
| 5127 | } else { |
| 5128 | transformMatrix = childMatrix; |
| 5129 | } |
| 5130 | } else { |
| 5131 | transformMatrix = childMatrix; |
| 5132 | } |
| 5133 | transformMatrix.mapRect(boundingRect); |
Alan Viverette | c45b1d4 | 2015-11-16 15:38:59 -0500 | [diff] [blame] | 5134 | dirty.set((int) Math.floor(boundingRect.left), |
| 5135 | (int) Math.floor(boundingRect.top), |
| 5136 | (int) Math.ceil(boundingRect.right), |
| 5137 | (int) Math.ceil(boundingRect.bottom)); |
Romain Guy | fe455af | 2012-02-15 16:40:20 -0800 | [diff] [blame] | 5138 | } |
| 5139 | |
| 5140 | do { |
| 5141 | View view = null; |
| 5142 | if (parent instanceof View) { |
| 5143 | view = (View) parent; |
Romain Guy | fe455af | 2012-02-15 16:40:20 -0800 | [diff] [blame] | 5144 | } |
| 5145 | |
| 5146 | if (drawAnimation) { |
| 5147 | if (view != null) { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 5148 | view.mPrivateFlags |= PFLAG_DRAW_ANIMATION; |
Romain Guy | fe455af | 2012-02-15 16:40:20 -0800 | [diff] [blame] | 5149 | } else if (parent instanceof ViewRootImpl) { |
| 5150 | ((ViewRootImpl) parent).mIsAnimating = true; |
| 5151 | } |
| 5152 | } |
| 5153 | |
| 5154 | // If the parent is dirty opaque or not dirty, mark it dirty with the opaque |
| 5155 | // flag coming from the child that initiated the invalidate |
| 5156 | if (view != null) { |
| 5157 | if ((view.mViewFlags & FADING_EDGE_MASK) != 0 && |
| 5158 | view.getSolidColor() == 0) { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 5159 | opaqueFlag = PFLAG_DIRTY; |
Romain Guy | fe455af | 2012-02-15 16:40:20 -0800 | [diff] [blame] | 5160 | } |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 5161 | if ((view.mPrivateFlags & PFLAG_DIRTY_MASK) != PFLAG_DIRTY) { |
| 5162 | view.mPrivateFlags = (view.mPrivateFlags & ~PFLAG_DIRTY_MASK) | opaqueFlag; |
Romain Guy | fe455af | 2012-02-15 16:40:20 -0800 | [diff] [blame] | 5163 | } |
| 5164 | } |
| 5165 | |
| 5166 | parent = parent.invalidateChildInParent(location, dirty); |
| 5167 | if (view != null) { |
| 5168 | // Account for transform on current parent |
| 5169 | Matrix m = view.getMatrix(); |
| 5170 | if (!m.isIdentity()) { |
| 5171 | RectF boundingRect = attachInfo.mTmpTransformRect; |
| 5172 | boundingRect.set(dirty); |
| 5173 | m.mapRect(boundingRect); |
Alan Viverette | c45b1d4 | 2015-11-16 15:38:59 -0500 | [diff] [blame] | 5174 | dirty.set((int) Math.floor(boundingRect.left), |
| 5175 | (int) Math.floor(boundingRect.top), |
| 5176 | (int) Math.ceil(boundingRect.right), |
| 5177 | (int) Math.ceil(boundingRect.bottom)); |
Romain Guy | fe455af | 2012-02-15 16:40:20 -0800 | [diff] [blame] | 5178 | } |
| 5179 | } |
| 5180 | } while (parent != null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5181 | } |
| 5182 | } |
| 5183 | |
| 5184 | /** |
| 5185 | * Don't call or override this method. It is used for the implementation of |
| 5186 | * the view hierarchy. |
| 5187 | * |
| 5188 | * This implementation returns null if this ViewGroup does not have a parent, |
| 5189 | * if this ViewGroup is already fully invalidated or if the dirty rectangle |
| 5190 | * does not intersect with this ViewGroup's bounds. |
| 5191 | */ |
| 5192 | public ViewParent invalidateChildInParent(final int[] location, final Rect dirty) { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 5193 | if ((mPrivateFlags & PFLAG_DRAWN) == PFLAG_DRAWN || |
| 5194 | (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == PFLAG_DRAWING_CACHE_VALID) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5195 | if ((mGroupFlags & (FLAG_OPTIMIZE_INVALIDATE | FLAG_ANIMATION_DONE)) != |
| 5196 | FLAG_OPTIMIZE_INVALIDATE) { |
| 5197 | dirty.offset(location[CHILD_LEFT_INDEX] - mScrollX, |
| 5198 | location[CHILD_TOP_INDEX] - mScrollY); |
Chet Haase | a4f14eb | 2013-04-22 11:11:39 -0700 | [diff] [blame] | 5199 | if ((mGroupFlags & FLAG_CLIP_CHILDREN) == 0) { |
| 5200 | dirty.union(0, 0, mRight - mLeft, mBottom - mTop); |
| 5201 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5202 | |
| 5203 | final int left = mLeft; |
| 5204 | final int top = mTop; |
| 5205 | |
Chet Haase | 05e91ed | 2012-07-03 14:17:57 -0700 | [diff] [blame] | 5206 | if ((mGroupFlags & FLAG_CLIP_CHILDREN) == FLAG_CLIP_CHILDREN) { |
| 5207 | if (!dirty.intersect(0, 0, mRight - left, mBottom - top)) { |
| 5208 | dirty.setEmpty(); |
Romain Guy | 3a3133d | 2011-02-01 22:59:58 -0800 | [diff] [blame] | 5209 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5210 | } |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 5211 | mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID; |
Chet Haase | 05e91ed | 2012-07-03 14:17:57 -0700 | [diff] [blame] | 5212 | |
| 5213 | location[CHILD_LEFT_INDEX] = left; |
| 5214 | location[CHILD_TOP_INDEX] = top; |
| 5215 | |
John Reck | 96bb8ad | 2014-06-19 10:53:03 -0700 | [diff] [blame] | 5216 | if (mLayerType != LAYER_TYPE_NONE) { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 5217 | mPrivateFlags |= PFLAG_INVALIDATED; |
Chet Haase | 05e91ed | 2012-07-03 14:17:57 -0700 | [diff] [blame] | 5218 | } |
| 5219 | |
| 5220 | return mParent; |
| 5221 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5222 | } else { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 5223 | mPrivateFlags &= ~PFLAG_DRAWN & ~PFLAG_DRAWING_CACHE_VALID; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5224 | |
| 5225 | location[CHILD_LEFT_INDEX] = mLeft; |
| 5226 | location[CHILD_TOP_INDEX] = mTop; |
Chet Haase | a3db866 | 2011-07-19 10:36:05 -0700 | [diff] [blame] | 5227 | if ((mGroupFlags & FLAG_CLIP_CHILDREN) == FLAG_CLIP_CHILDREN) { |
| 5228 | dirty.set(0, 0, mRight - mLeft, mBottom - mTop); |
| 5229 | } else { |
| 5230 | // in case the dirty rect extends outside the bounds of this container |
| 5231 | dirty.union(0, 0, mRight - mLeft, mBottom - mTop); |
| 5232 | } |
Romain Guy | 3a3133d | 2011-02-01 22:59:58 -0800 | [diff] [blame] | 5233 | |
John Reck | 96bb8ad | 2014-06-19 10:53:03 -0700 | [diff] [blame] | 5234 | if (mLayerType != LAYER_TYPE_NONE) { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 5235 | mPrivateFlags |= PFLAG_INVALIDATED; |
Romain Guy | 3a3133d | 2011-02-01 22:59:58 -0800 | [diff] [blame] | 5236 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5237 | |
| 5238 | return mParent; |
| 5239 | } |
| 5240 | } |
| 5241 | |
| 5242 | return null; |
| 5243 | } |
| 5244 | |
| 5245 | /** |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 5246 | * Native-calculated damage path |
| 5247 | * Returns false if this path was unable to complete successfully. This means |
| 5248 | * it hit a ViewParent it doesn't recognize and needs to fall back to calculating |
| 5249 | * damage area |
| 5250 | * @hide |
| 5251 | */ |
| 5252 | public boolean damageChildDeferred(View child) { |
| 5253 | ViewParent parent = getParent(); |
| 5254 | while (parent != null) { |
| 5255 | if (parent instanceof ViewGroup) { |
| 5256 | parent = parent.getParent(); |
| 5257 | } else if (parent instanceof ViewRootImpl) { |
| 5258 | ((ViewRootImpl) parent).invalidate(); |
| 5259 | return true; |
| 5260 | } else { |
| 5261 | parent = null; |
| 5262 | } |
| 5263 | } |
| 5264 | return false; |
| 5265 | } |
| 5266 | |
| 5267 | /** |
Chet Haase | 9d1992d | 2012-03-13 11:03:25 -0700 | [diff] [blame] | 5268 | * Quick invalidation method called by View.invalidateViewProperty. This doesn't set the |
| 5269 | * DRAWN flags and doesn't handle the Animation logic that the default invalidation methods |
| 5270 | * do; all we want to do here is schedule a traversal with the appropriate dirty rect. |
| 5271 | * |
| 5272 | * @hide |
| 5273 | */ |
Chris Craik | 49e6c73 | 2014-03-31 12:34:11 -0700 | [diff] [blame] | 5274 | public void damageChild(View child, final Rect dirty) { |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 5275 | if (damageChildDeferred(child)) { |
| 5276 | return; |
| 5277 | } |
| 5278 | |
Chet Haase | 9d1992d | 2012-03-13 11:03:25 -0700 | [diff] [blame] | 5279 | ViewParent parent = this; |
| 5280 | |
| 5281 | final AttachInfo attachInfo = mAttachInfo; |
| 5282 | if (attachInfo != null) { |
Chet Haase | 9d1992d | 2012-03-13 11:03:25 -0700 | [diff] [blame] | 5283 | int left = child.mLeft; |
| 5284 | int top = child.mTop; |
| 5285 | if (!child.getMatrix().isIdentity()) { |
| 5286 | child.transformRect(dirty); |
| 5287 | } |
| 5288 | |
| 5289 | do { |
| 5290 | if (parent instanceof ViewGroup) { |
| 5291 | ViewGroup parentVG = (ViewGroup) parent; |
Chet Haase | b85967b | 2012-03-26 14:37:51 -0700 | [diff] [blame] | 5292 | if (parentVG.mLayerType != LAYER_TYPE_NONE) { |
| 5293 | // Layered parents should be recreated, not just re-issued |
| 5294 | parentVG.invalidate(); |
| 5295 | parent = null; |
| 5296 | } else { |
Chris Craik | 49e6c73 | 2014-03-31 12:34:11 -0700 | [diff] [blame] | 5297 | parent = parentVG.damageChildInParent(left, top, dirty); |
Chet Haase | b85967b | 2012-03-26 14:37:51 -0700 | [diff] [blame] | 5298 | left = parentVG.mLeft; |
| 5299 | top = parentVG.mTop; |
| 5300 | } |
Chet Haase | 9d1992d | 2012-03-13 11:03:25 -0700 | [diff] [blame] | 5301 | } else { |
| 5302 | // Reached the top; this calls into the usual invalidate method in |
| 5303 | // ViewRootImpl, which schedules a traversal |
| 5304 | final int[] location = attachInfo.mInvalidateChildLocation; |
| 5305 | location[0] = left; |
| 5306 | location[1] = top; |
| 5307 | parent = parent.invalidateChildInParent(location, dirty); |
| 5308 | } |
| 5309 | } while (parent != null); |
| 5310 | } |
| 5311 | } |
| 5312 | |
| 5313 | /** |
| 5314 | * Quick invalidation method that simply transforms the dirty rect into the parent's |
| 5315 | * coordinate system, pruning the invalidation if the parent has already been invalidated. |
Chet Haase | e4a2d7c | 2013-06-21 17:49:36 -0700 | [diff] [blame] | 5316 | * |
| 5317 | * @hide |
Chet Haase | 9d1992d | 2012-03-13 11:03:25 -0700 | [diff] [blame] | 5318 | */ |
Chris Craik | 49e6c73 | 2014-03-31 12:34:11 -0700 | [diff] [blame] | 5319 | protected ViewParent damageChildInParent(int left, int top, final Rect dirty) { |
| 5320 | if ((mPrivateFlags & PFLAG_DRAWN) != 0 |
| 5321 | || (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) != 0) { |
Chet Haase | 9d1992d | 2012-03-13 11:03:25 -0700 | [diff] [blame] | 5322 | dirty.offset(left - mScrollX, top - mScrollY); |
Chet Haase | a4f14eb | 2013-04-22 11:11:39 -0700 | [diff] [blame] | 5323 | if ((mGroupFlags & FLAG_CLIP_CHILDREN) == 0) { |
| 5324 | dirty.union(0, 0, mRight - mLeft, mBottom - mTop); |
| 5325 | } |
Chet Haase | 9d1992d | 2012-03-13 11:03:25 -0700 | [diff] [blame] | 5326 | |
| 5327 | if ((mGroupFlags & FLAG_CLIP_CHILDREN) == 0 || |
| 5328 | dirty.intersect(0, 0, mRight - mLeft, mBottom - mTop)) { |
| 5329 | |
Chet Haase | 9d1992d | 2012-03-13 11:03:25 -0700 | [diff] [blame] | 5330 | if (!getMatrix().isIdentity()) { |
| 5331 | transformRect(dirty); |
| 5332 | } |
| 5333 | |
| 5334 | return mParent; |
| 5335 | } |
| 5336 | } |
| 5337 | |
| 5338 | return null; |
| 5339 | } |
| 5340 | |
| 5341 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5342 | * Offset a rectangle that is in a descendant's coordinate |
| 5343 | * space into our coordinate space. |
| 5344 | * @param descendant A descendant of this view |
| 5345 | * @param rect A rectangle defined in descendant's coordinate space. |
| 5346 | */ |
| 5347 | public final void offsetDescendantRectToMyCoords(View descendant, Rect rect) { |
| 5348 | offsetRectBetweenParentAndChild(descendant, rect, true, false); |
| 5349 | } |
| 5350 | |
| 5351 | /** |
| 5352 | * Offset a rectangle that is in our coordinate space into an ancestor's |
| 5353 | * coordinate space. |
| 5354 | * @param descendant A descendant of this view |
| 5355 | * @param rect A rectangle defined in descendant's coordinate space. |
| 5356 | */ |
| 5357 | public final void offsetRectIntoDescendantCoords(View descendant, Rect rect) { |
| 5358 | offsetRectBetweenParentAndChild(descendant, rect, false, false); |
| 5359 | } |
| 5360 | |
| 5361 | /** |
| 5362 | * Helper method that offsets a rect either from parent to descendant or |
| 5363 | * descendant to parent. |
| 5364 | */ |
| 5365 | void offsetRectBetweenParentAndChild(View descendant, Rect rect, |
| 5366 | boolean offsetFromChildToParent, boolean clipToBounds) { |
| 5367 | |
Adrian Roos | 83e725d | 2016-01-08 16:49:17 +0100 | [diff] [blame] | 5368 | final RectF rectF = mAttachInfo != null ? mAttachInfo.mTmpTransformRect1 : new RectF(); |
| 5369 | final Matrix inverse = mAttachInfo != null ? mAttachInfo.mTmpMatrix : new Matrix(); |
| 5370 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5371 | // already in the same coord system :) |
| 5372 | if (descendant == this) { |
| 5373 | return; |
| 5374 | } |
| 5375 | |
| 5376 | ViewParent theParent = descendant.mParent; |
| 5377 | |
| 5378 | // search and offset up to the parent |
| 5379 | while ((theParent != null) |
| 5380 | && (theParent instanceof View) |
| 5381 | && (theParent != this)) { |
| 5382 | |
| 5383 | if (offsetFromChildToParent) { |
Adrian Roos | 83e725d | 2016-01-08 16:49:17 +0100 | [diff] [blame] | 5384 | rect.offset(-descendant.mScrollX, -descendant.mScrollY); |
| 5385 | |
| 5386 | if (!descendant.hasIdentityMatrix()) { |
| 5387 | rectF.set(rect); |
| 5388 | descendant.getMatrix().mapRect(rectF); |
| 5389 | rectF.roundOut(rect); |
| 5390 | } |
| 5391 | |
| 5392 | rect.offset(descendant.mLeft, descendant.mTop); |
| 5393 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5394 | if (clipToBounds) { |
| 5395 | View p = (View) theParent; |
Doris Liu | 9607fbe | 2015-05-28 17:17:28 -0700 | [diff] [blame] | 5396 | boolean intersected = rect.intersect(0, 0, p.mRight - p.mLeft, |
| 5397 | p.mBottom - p.mTop); |
| 5398 | if (!intersected) { |
| 5399 | rect.setEmpty(); |
| 5400 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5401 | } |
| 5402 | } else { |
| 5403 | if (clipToBounds) { |
| 5404 | View p = (View) theParent; |
Doris Liu | 9607fbe | 2015-05-28 17:17:28 -0700 | [diff] [blame] | 5405 | boolean intersected = rect.intersect(0, 0, p.mRight - p.mLeft, |
| 5406 | p.mBottom - p.mTop); |
| 5407 | if (!intersected) { |
| 5408 | rect.setEmpty(); |
| 5409 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5410 | } |
Adrian Roos | 83e725d | 2016-01-08 16:49:17 +0100 | [diff] [blame] | 5411 | rect.offset(-descendant.mLeft, -descendant.mTop); |
| 5412 | |
| 5413 | if (!descendant.hasIdentityMatrix()) { |
| 5414 | descendant.getMatrix().invert(inverse); |
| 5415 | rectF.set(rect); |
| 5416 | inverse.mapRect(rectF); |
| 5417 | rectF.roundOut(rect); |
| 5418 | } |
| 5419 | |
| 5420 | rect.offset(descendant.mScrollX, descendant.mScrollY); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5421 | } |
| 5422 | |
| 5423 | descendant = (View) theParent; |
| 5424 | theParent = descendant.mParent; |
| 5425 | } |
| 5426 | |
| 5427 | // now that we are up to this view, need to offset one more time |
| 5428 | // to get into our coordinate space |
| 5429 | if (theParent == this) { |
| 5430 | if (offsetFromChildToParent) { |
Adrian Roos | 83e725d | 2016-01-08 16:49:17 +0100 | [diff] [blame] | 5431 | rect.offset(-descendant.mScrollX, -descendant.mScrollY); |
| 5432 | |
| 5433 | if (!descendant.hasIdentityMatrix()) { |
| 5434 | rectF.set(rect); |
| 5435 | descendant.getMatrix().mapRect(rectF); |
| 5436 | rectF.roundOut(rect); |
| 5437 | } |
| 5438 | |
| 5439 | rect.offset(descendant.mLeft, descendant.mTop); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5440 | } else { |
Adrian Roos | 83e725d | 2016-01-08 16:49:17 +0100 | [diff] [blame] | 5441 | rect.offset(-descendant.mLeft, -descendant.mTop); |
| 5442 | |
| 5443 | if (!descendant.hasIdentityMatrix()) { |
| 5444 | descendant.getMatrix().invert(inverse); |
| 5445 | rectF.set(rect); |
| 5446 | inverse.mapRect(rectF); |
| 5447 | rectF.roundOut(rect); |
| 5448 | } |
| 5449 | |
| 5450 | rect.offset(descendant.mScrollX, descendant.mScrollY); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5451 | } |
| 5452 | } else { |
| 5453 | throw new IllegalArgumentException("parameter must be a descendant of this view"); |
| 5454 | } |
| 5455 | } |
| 5456 | |
| 5457 | /** |
| 5458 | * Offset the vertical location of all children of this view by the specified number of pixels. |
| 5459 | * |
| 5460 | * @param offset the number of pixels to offset |
| 5461 | * |
| 5462 | * @hide |
| 5463 | */ |
| 5464 | public void offsetChildrenTopAndBottom(int offset) { |
| 5465 | final int count = mChildrenCount; |
| 5466 | final View[] children = mChildren; |
Romain Guy | 5549cb5 | 2013-05-06 18:42:08 -0700 | [diff] [blame] | 5467 | boolean invalidate = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5468 | |
| 5469 | for (int i = 0; i < count; i++) { |
| 5470 | final View v = children[i]; |
| 5471 | v.mTop += offset; |
| 5472 | v.mBottom += offset; |
Chris Craik | 64a12e1 | 2014-03-28 18:12:12 -0700 | [diff] [blame] | 5473 | if (v.mRenderNode != null) { |
Romain Guy | 5549cb5 | 2013-05-06 18:42:08 -0700 | [diff] [blame] | 5474 | invalidate = true; |
Chris Craik | 64a12e1 | 2014-03-28 18:12:12 -0700 | [diff] [blame] | 5475 | v.mRenderNode.offsetTopAndBottom(offset); |
Chet Haase | a1cff50 | 2012-02-21 13:43:44 -0800 | [diff] [blame] | 5476 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5477 | } |
Romain Guy | 5549cb5 | 2013-05-06 18:42:08 -0700 | [diff] [blame] | 5478 | |
| 5479 | if (invalidate) { |
| 5480 | invalidateViewProperty(false, false); |
| 5481 | } |
Guang Zhu | 84e2509 | 2014-05-01 21:12:55 -0700 | [diff] [blame] | 5482 | notifySubtreeAccessibilityStateChangedIfNeeded(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5483 | } |
| 5484 | |
| 5485 | /** |
| 5486 | * {@inheritDoc} |
| 5487 | */ |
| 5488 | public boolean getChildVisibleRect(View child, Rect r, android.graphics.Point offset) { |
Adam Powell | f93bb6d | 2011-12-12 15:21:57 -0800 | [diff] [blame] | 5489 | // It doesn't make a whole lot of sense to call this on a view that isn't attached, |
| 5490 | // but for some simple tests it can be useful. If we don't have attach info this |
| 5491 | // will allocate memory. |
| 5492 | final RectF rect = mAttachInfo != null ? mAttachInfo.mTmpTransformRect : new RectF(); |
Gilles Debunne | cea4513 | 2011-11-24 02:19:27 +0100 | [diff] [blame] | 5493 | rect.set(r); |
| 5494 | |
| 5495 | if (!child.hasIdentityMatrix()) { |
George Mount | 002d43d | 2014-11-11 12:54:43 -0800 | [diff] [blame] | 5496 | child.getMatrix().mapRect(rect); |
Gilles Debunne | cea4513 | 2011-11-24 02:19:27 +0100 | [diff] [blame] | 5497 | } |
| 5498 | |
Alan Viverette | ab2cf6d | 2014-11-26 14:34:36 -0800 | [diff] [blame] | 5499 | final int dx = child.mLeft - mScrollX; |
| 5500 | final int dy = child.mTop - mScrollY; |
Gilles Debunne | cea4513 | 2011-11-24 02:19:27 +0100 | [diff] [blame] | 5501 | |
| 5502 | rect.offset(dx, dy); |
| 5503 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5504 | if (offset != null) { |
Gilles Debunne | cea4513 | 2011-11-24 02:19:27 +0100 | [diff] [blame] | 5505 | if (!child.hasIdentityMatrix()) { |
Adam Powell | f93bb6d | 2011-12-12 15:21:57 -0800 | [diff] [blame] | 5506 | float[] position = mAttachInfo != null ? mAttachInfo.mTmpTransformLocation |
| 5507 | : new float[2]; |
Gilles Debunne | cea4513 | 2011-11-24 02:19:27 +0100 | [diff] [blame] | 5508 | position[0] = offset.x; |
| 5509 | position[1] = offset.y; |
| 5510 | child.getMatrix().mapPoints(position); |
Alan Viverette | c45b1d4 | 2015-11-16 15:38:59 -0500 | [diff] [blame] | 5511 | offset.x = Math.round(position[0]); |
| 5512 | offset.y = Math.round(position[1]); |
Gilles Debunne | cea4513 | 2011-11-24 02:19:27 +0100 | [diff] [blame] | 5513 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5514 | offset.x += dx; |
| 5515 | offset.y += dy; |
| 5516 | } |
Gilles Debunne | cea4513 | 2011-11-24 02:19:27 +0100 | [diff] [blame] | 5517 | |
Alan Viverette | ab2cf6d | 2014-11-26 14:34:36 -0800 | [diff] [blame] | 5518 | final int width = mRight - mLeft; |
| 5519 | final int height = mBottom - mTop; |
| 5520 | |
George Mount | 002d43d | 2014-11-11 12:54:43 -0800 | [diff] [blame] | 5521 | boolean rectIsVisible = true; |
Adam Powell | 35da41e | 2014-12-10 18:59:34 -0800 | [diff] [blame] | 5522 | if (mParent == null || |
| 5523 | (mParent instanceof ViewGroup && ((ViewGroup) mParent).getClipChildren())) { |
Alan Viverette | ab2cf6d | 2014-11-26 14:34:36 -0800 | [diff] [blame] | 5524 | // Clip to bounds. |
| 5525 | rectIsVisible = rect.intersect(0, 0, width, height); |
Gilles Debunne | cea4513 | 2011-11-24 02:19:27 +0100 | [diff] [blame] | 5526 | } |
| 5527 | |
George Mount | 002d43d | 2014-11-11 12:54:43 -0800 | [diff] [blame] | 5528 | if (rectIsVisible && (mGroupFlags & CLIP_TO_PADDING_MASK) == CLIP_TO_PADDING_MASK) { |
Alan Viverette | ab2cf6d | 2014-11-26 14:34:36 -0800 | [diff] [blame] | 5529 | // Clip to padding. |
George Mount | 002d43d | 2014-11-11 12:54:43 -0800 | [diff] [blame] | 5530 | rectIsVisible = rect.intersect(mPaddingLeft, mPaddingTop, |
Alan Viverette | ab2cf6d | 2014-11-26 14:34:36 -0800 | [diff] [blame] | 5531 | width - mPaddingRight, height - mPaddingBottom); |
George Mount | 002d43d | 2014-11-11 12:54:43 -0800 | [diff] [blame] | 5532 | } |
| 5533 | |
| 5534 | if (rectIsVisible && mClipBounds != null) { |
Alan Viverette | ab2cf6d | 2014-11-26 14:34:36 -0800 | [diff] [blame] | 5535 | // Clip to clipBounds. |
George Mount | 002d43d | 2014-11-11 12:54:43 -0800 | [diff] [blame] | 5536 | rectIsVisible = rect.intersect(mClipBounds.left, mClipBounds.top, mClipBounds.right, |
| 5537 | mClipBounds.bottom); |
| 5538 | } |
Alan Viverette | c45b1d4 | 2015-11-16 15:38:59 -0500 | [diff] [blame] | 5539 | r.set((int) Math.floor(rect.left), (int) Math.floor(rect.top), |
| 5540 | (int) Math.ceil(rect.right), (int) Math.ceil(rect.bottom)); |
George Mount | 002d43d | 2014-11-11 12:54:43 -0800 | [diff] [blame] | 5541 | if (rectIsVisible && mParent != null) { |
| 5542 | rectIsVisible = mParent.getChildVisibleRect(this, r, offset); |
| 5543 | } |
| 5544 | return rectIsVisible; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5545 | } |
| 5546 | |
| 5547 | /** |
| 5548 | * {@inheritDoc} |
| 5549 | */ |
| 5550 | @Override |
Chet Haase | 9c08744 | 2011-01-12 16:20:16 -0800 | [diff] [blame] | 5551 | public final void layout(int l, int t, int r, int b) { |
Chet Haase | 430742f | 2013-04-12 11:18:36 -0700 | [diff] [blame] | 5552 | if (!mSuppressLayout && (mTransition == null || !mTransition.isChangingLayout())) { |
Chet Haase | 7dd4a53 | 2012-04-16 13:35:09 -0700 | [diff] [blame] | 5553 | if (mTransition != null) { |
| 5554 | mTransition.layoutChange(this); |
| 5555 | } |
Chet Haase | 9c08744 | 2011-01-12 16:20:16 -0800 | [diff] [blame] | 5556 | super.layout(l, t, r, b); |
| 5557 | } else { |
| 5558 | // 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] | 5559 | mLayoutCalledWhileSuppressed = true; |
Chet Haase | 9c08744 | 2011-01-12 16:20:16 -0800 | [diff] [blame] | 5560 | } |
| 5561 | } |
| 5562 | |
| 5563 | /** |
| 5564 | * {@inheritDoc} |
| 5565 | */ |
| 5566 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5567 | protected abstract void onLayout(boolean changed, |
| 5568 | int l, int t, int r, int b); |
| 5569 | |
| 5570 | /** |
| 5571 | * Indicates whether the view group has the ability to animate its children |
| 5572 | * after the first layout. |
| 5573 | * |
| 5574 | * @return true if the children can be animated, false otherwise |
| 5575 | */ |
| 5576 | protected boolean canAnimate() { |
| 5577 | return mLayoutAnimationController != null; |
| 5578 | } |
| 5579 | |
| 5580 | /** |
| 5581 | * Runs the layout animation. Calling this method triggers a relayout of |
| 5582 | * this view group. |
| 5583 | */ |
| 5584 | public void startLayoutAnimation() { |
| 5585 | if (mLayoutAnimationController != null) { |
| 5586 | mGroupFlags |= FLAG_RUN_ANIMATION; |
| 5587 | requestLayout(); |
| 5588 | } |
| 5589 | } |
| 5590 | |
| 5591 | /** |
| 5592 | * Schedules the layout animation to be played after the next layout pass |
| 5593 | * of this view group. This can be used to restart the layout animation |
| 5594 | * when the content of the view group changes or when the activity is |
| 5595 | * paused and resumed. |
| 5596 | */ |
| 5597 | public void scheduleLayoutAnimation() { |
| 5598 | mGroupFlags |= FLAG_RUN_ANIMATION; |
| 5599 | } |
| 5600 | |
| 5601 | /** |
| 5602 | * Sets the layout animation controller used to animate the group's |
| 5603 | * children after the first layout. |
| 5604 | * |
| 5605 | * @param controller the animation controller |
| 5606 | */ |
| 5607 | public void setLayoutAnimation(LayoutAnimationController controller) { |
| 5608 | mLayoutAnimationController = controller; |
| 5609 | if (mLayoutAnimationController != null) { |
| 5610 | mGroupFlags |= FLAG_RUN_ANIMATION; |
| 5611 | } |
| 5612 | } |
| 5613 | |
| 5614 | /** |
| 5615 | * Returns the layout animation controller used to animate the group's |
| 5616 | * children. |
| 5617 | * |
| 5618 | * @return the current animation controller |
| 5619 | */ |
| 5620 | public LayoutAnimationController getLayoutAnimation() { |
| 5621 | return mLayoutAnimationController; |
| 5622 | } |
| 5623 | |
| 5624 | /** |
| 5625 | * Indicates whether the children's drawing cache is used during a layout |
| 5626 | * animation. By default, the drawing cache is enabled but this will prevent |
| 5627 | * nested layout animations from working. To nest animations, you must disable |
| 5628 | * the cache. |
| 5629 | * |
| 5630 | * @return true if the animation cache is enabled, false otherwise |
| 5631 | * |
| 5632 | * @see #setAnimationCacheEnabled(boolean) |
| 5633 | * @see View#setDrawingCacheEnabled(boolean) |
Chris Craik | 5a6bbae | 2015-04-10 17:41:34 -0700 | [diff] [blame] | 5634 | * |
Dianne Hackborn | 0e3de6c | 2015-07-29 15:20:21 -0700 | [diff] [blame] | 5635 | * @deprecated As of {@link android.os.Build.VERSION_CODES#M}, this property is ignored. |
Chris Craik | 5a6bbae | 2015-04-10 17:41:34 -0700 | [diff] [blame] | 5636 | * Caching behavior of children may be controlled through {@link View#setLayerType(int, Paint)}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5637 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5638 | public boolean isAnimationCacheEnabled() { |
| 5639 | return (mGroupFlags & FLAG_ANIMATION_CACHE) == FLAG_ANIMATION_CACHE; |
| 5640 | } |
| 5641 | |
| 5642 | /** |
| 5643 | * Enables or disables the children's drawing cache during a layout animation. |
| 5644 | * By default, the drawing cache is enabled but this will prevent nested |
| 5645 | * layout animations from working. To nest animations, you must disable the |
| 5646 | * cache. |
| 5647 | * |
| 5648 | * @param enabled true to enable the animation cache, false otherwise |
| 5649 | * |
| 5650 | * @see #isAnimationCacheEnabled() |
| 5651 | * @see View#setDrawingCacheEnabled(boolean) |
Chris Craik | 5a6bbae | 2015-04-10 17:41:34 -0700 | [diff] [blame] | 5652 | * |
Dianne Hackborn | 0e3de6c | 2015-07-29 15:20:21 -0700 | [diff] [blame] | 5653 | * @deprecated As of {@link android.os.Build.VERSION_CODES#M}, this property is ignored. |
Chris Craik | 5a6bbae | 2015-04-10 17:41:34 -0700 | [diff] [blame] | 5654 | * Caching behavior of children may be controlled through {@link View#setLayerType(int, Paint)}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5655 | */ |
| 5656 | public void setAnimationCacheEnabled(boolean enabled) { |
| 5657 | setBooleanFlag(FLAG_ANIMATION_CACHE, enabled); |
| 5658 | } |
| 5659 | |
| 5660 | /** |
| 5661 | * Indicates whether this ViewGroup will always try to draw its children using their |
| 5662 | * drawing cache. By default this property is enabled. |
| 5663 | * |
| 5664 | * @return true if the animation cache is enabled, false otherwise |
| 5665 | * |
| 5666 | * @see #setAlwaysDrawnWithCacheEnabled(boolean) |
| 5667 | * @see #setChildrenDrawnWithCacheEnabled(boolean) |
| 5668 | * @see View#setDrawingCacheEnabled(boolean) |
Chris Craik | 5a6bbae | 2015-04-10 17:41:34 -0700 | [diff] [blame] | 5669 | * |
Dianne Hackborn | 0e3de6c | 2015-07-29 15:20:21 -0700 | [diff] [blame] | 5670 | * @deprecated As of {@link android.os.Build.VERSION_CODES#M}, this property is ignored. |
Chris Craik | 5a6bbae | 2015-04-10 17:41:34 -0700 | [diff] [blame] | 5671 | * Child views may no longer have their caching behavior disabled by parents. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5672 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5673 | public boolean isAlwaysDrawnWithCacheEnabled() { |
| 5674 | return (mGroupFlags & FLAG_ALWAYS_DRAWN_WITH_CACHE) == FLAG_ALWAYS_DRAWN_WITH_CACHE; |
| 5675 | } |
| 5676 | |
| 5677 | /** |
| 5678 | * Indicates whether this ViewGroup will always try to draw its children using their |
| 5679 | * drawing cache. This property can be set to true when the cache rendering is |
| 5680 | * slightly different from the children's normal rendering. Renderings can be different, |
| 5681 | * for instance, when the cache's quality is set to low. |
| 5682 | * |
| 5683 | * When this property is disabled, the ViewGroup will use the drawing cache of its |
| 5684 | * children only when asked to. It's usually the task of subclasses to tell ViewGroup |
| 5685 | * when to start using the drawing cache and when to stop using it. |
| 5686 | * |
| 5687 | * @param always true to always draw with the drawing cache, false otherwise |
| 5688 | * |
| 5689 | * @see #isAlwaysDrawnWithCacheEnabled() |
| 5690 | * @see #setChildrenDrawnWithCacheEnabled(boolean) |
| 5691 | * @see View#setDrawingCacheEnabled(boolean) |
| 5692 | * @see View#setDrawingCacheQuality(int) |
Chris Craik | 5a6bbae | 2015-04-10 17:41:34 -0700 | [diff] [blame] | 5693 | * |
Dianne Hackborn | 0e3de6c | 2015-07-29 15:20:21 -0700 | [diff] [blame] | 5694 | * @deprecated As of {@link android.os.Build.VERSION_CODES#M}, this property is ignored. |
Chris Craik | 5a6bbae | 2015-04-10 17:41:34 -0700 | [diff] [blame] | 5695 | * Child views may no longer have their caching behavior disabled by parents. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5696 | */ |
| 5697 | public void setAlwaysDrawnWithCacheEnabled(boolean always) { |
| 5698 | setBooleanFlag(FLAG_ALWAYS_DRAWN_WITH_CACHE, always); |
| 5699 | } |
| 5700 | |
| 5701 | /** |
| 5702 | * Indicates whether the ViewGroup is currently drawing its children using |
| 5703 | * their drawing cache. |
| 5704 | * |
| 5705 | * @return true if children should be drawn with their cache, false otherwise |
| 5706 | * |
| 5707 | * @see #setAlwaysDrawnWithCacheEnabled(boolean) |
| 5708 | * @see #setChildrenDrawnWithCacheEnabled(boolean) |
Chris Craik | 5a6bbae | 2015-04-10 17:41:34 -0700 | [diff] [blame] | 5709 | * |
Dianne Hackborn | 0e3de6c | 2015-07-29 15:20:21 -0700 | [diff] [blame] | 5710 | * @deprecated As of {@link android.os.Build.VERSION_CODES#M}, this property is ignored. |
Chris Craik | 5a6bbae | 2015-04-10 17:41:34 -0700 | [diff] [blame] | 5711 | * Child views may no longer be forced to cache their rendering state by their parents. |
| 5712 | * Use {@link View#setLayerType(int, Paint)} on individual Views instead. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5713 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5714 | protected boolean isChildrenDrawnWithCacheEnabled() { |
| 5715 | return (mGroupFlags & FLAG_CHILDREN_DRAWN_WITH_CACHE) == FLAG_CHILDREN_DRAWN_WITH_CACHE; |
| 5716 | } |
| 5717 | |
| 5718 | /** |
| 5719 | * Tells the ViewGroup to draw its children using their drawing cache. This property |
| 5720 | * is ignored when {@link #isAlwaysDrawnWithCacheEnabled()} is true. A child's drawing cache |
| 5721 | * will be used only if it has been enabled. |
| 5722 | * |
| 5723 | * Subclasses should call this method to start and stop using the drawing cache when |
| 5724 | * they perform performance sensitive operations, like scrolling or animating. |
| 5725 | * |
| 5726 | * @param enabled true if children should be drawn with their cache, false otherwise |
| 5727 | * |
| 5728 | * @see #setAlwaysDrawnWithCacheEnabled(boolean) |
| 5729 | * @see #isChildrenDrawnWithCacheEnabled() |
Chris Craik | 5a6bbae | 2015-04-10 17:41:34 -0700 | [diff] [blame] | 5730 | * |
Dianne Hackborn | 0e3de6c | 2015-07-29 15:20:21 -0700 | [diff] [blame] | 5731 | * @deprecated As of {@link android.os.Build.VERSION_CODES#M}, this property is ignored. |
Chris Craik | 5a6bbae | 2015-04-10 17:41:34 -0700 | [diff] [blame] | 5732 | * Child views may no longer be forced to cache their rendering state by their parents. |
| 5733 | * Use {@link View#setLayerType(int, Paint)} on individual Views instead. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5734 | */ |
| 5735 | protected void setChildrenDrawnWithCacheEnabled(boolean enabled) { |
| 5736 | setBooleanFlag(FLAG_CHILDREN_DRAWN_WITH_CACHE, enabled); |
| 5737 | } |
| 5738 | |
Romain Guy | 293451e | 2009-11-04 13:59:48 -0800 | [diff] [blame] | 5739 | /** |
| 5740 | * Indicates whether the ViewGroup is drawing its children in the order defined by |
| 5741 | * {@link #getChildDrawingOrder(int, int)}. |
| 5742 | * |
| 5743 | * @return true if children drawing order is defined by {@link #getChildDrawingOrder(int, int)}, |
| 5744 | * false otherwise |
| 5745 | * |
| 5746 | * @see #setChildrenDrawingOrderEnabled(boolean) |
| 5747 | * @see #getChildDrawingOrder(int, int) |
| 5748 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 5749 | @ViewDebug.ExportedProperty(category = "drawing") |
Romain Guy | 293451e | 2009-11-04 13:59:48 -0800 | [diff] [blame] | 5750 | protected boolean isChildrenDrawingOrderEnabled() { |
| 5751 | return (mGroupFlags & FLAG_USE_CHILD_DRAWING_ORDER) == FLAG_USE_CHILD_DRAWING_ORDER; |
| 5752 | } |
| 5753 | |
| 5754 | /** |
| 5755 | * Tells the ViewGroup whether to draw its children in the order defined by the method |
| 5756 | * {@link #getChildDrawingOrder(int, int)}. |
Chris Craik | e83cbd4 | 2014-09-03 17:52:24 -0700 | [diff] [blame] | 5757 | * <p> |
| 5758 | * Note that {@link View#getZ() Z} reordering, done by {@link #dispatchDraw(Canvas)}, |
| 5759 | * will override custom child ordering done via this method. |
Romain Guy | 293451e | 2009-11-04 13:59:48 -0800 | [diff] [blame] | 5760 | * |
| 5761 | * @param enabled true if the order of the children when drawing is determined by |
| 5762 | * {@link #getChildDrawingOrder(int, int)}, false otherwise |
| 5763 | * |
| 5764 | * @see #isChildrenDrawingOrderEnabled() |
| 5765 | * @see #getChildDrawingOrder(int, int) |
| 5766 | */ |
| 5767 | protected void setChildrenDrawingOrderEnabled(boolean enabled) { |
| 5768 | setBooleanFlag(FLAG_USE_CHILD_DRAWING_ORDER, enabled); |
| 5769 | } |
| 5770 | |
Svetoslav | 6254f48 | 2013-06-04 17:22:14 -0700 | [diff] [blame] | 5771 | private boolean hasBooleanFlag(int flag) { |
Philip Milne | f091b66 | 2013-02-27 11:15:21 -0800 | [diff] [blame] | 5772 | return (mGroupFlags & flag) == flag; |
| 5773 | } |
| 5774 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5775 | private void setBooleanFlag(int flag, boolean value) { |
| 5776 | if (value) { |
| 5777 | mGroupFlags |= flag; |
| 5778 | } else { |
| 5779 | mGroupFlags &= ~flag; |
| 5780 | } |
| 5781 | } |
| 5782 | |
| 5783 | /** |
| 5784 | * Returns an integer indicating what types of drawing caches are kept in memory. |
| 5785 | * |
| 5786 | * @see #setPersistentDrawingCache(int) |
| 5787 | * @see #setAnimationCacheEnabled(boolean) |
| 5788 | * |
| 5789 | * @return one or a combination of {@link #PERSISTENT_NO_CACHE}, |
| 5790 | * {@link #PERSISTENT_ANIMATION_CACHE}, {@link #PERSISTENT_SCROLLING_CACHE} |
| 5791 | * and {@link #PERSISTENT_ALL_CACHES} |
| 5792 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 5793 | @ViewDebug.ExportedProperty(category = "drawing", mapping = { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5794 | @ViewDebug.IntToString(from = PERSISTENT_NO_CACHE, to = "NONE"), |
Romain Guy | 203688c | 2010-05-12 15:41:32 -0700 | [diff] [blame] | 5795 | @ViewDebug.IntToString(from = PERSISTENT_ANIMATION_CACHE, to = "ANIMATION"), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5796 | @ViewDebug.IntToString(from = PERSISTENT_SCROLLING_CACHE, to = "SCROLLING"), |
| 5797 | @ViewDebug.IntToString(from = PERSISTENT_ALL_CACHES, to = "ALL") |
| 5798 | }) |
| 5799 | public int getPersistentDrawingCache() { |
| 5800 | return mPersistentDrawingCache; |
| 5801 | } |
| 5802 | |
| 5803 | /** |
| 5804 | * Indicates what types of drawing caches should be kept in memory after |
| 5805 | * they have been created. |
| 5806 | * |
| 5807 | * @see #getPersistentDrawingCache() |
| 5808 | * @see #setAnimationCacheEnabled(boolean) |
| 5809 | * |
| 5810 | * @param drawingCacheToKeep one or a combination of {@link #PERSISTENT_NO_CACHE}, |
| 5811 | * {@link #PERSISTENT_ANIMATION_CACHE}, {@link #PERSISTENT_SCROLLING_CACHE} |
| 5812 | * and {@link #PERSISTENT_ALL_CACHES} |
| 5813 | */ |
| 5814 | public void setPersistentDrawingCache(int drawingCacheToKeep) { |
| 5815 | mPersistentDrawingCache = drawingCacheToKeep & PERSISTENT_ALL_CACHES; |
| 5816 | } |
| 5817 | |
Philip Milne | f091b66 | 2013-02-27 11:15:21 -0800 | [diff] [blame] | 5818 | private void setLayoutMode(int layoutMode, boolean explicitly) { |
| 5819 | mLayoutMode = layoutMode; |
| 5820 | setBooleanFlag(FLAG_LAYOUT_MODE_WAS_EXPLICITLY_SET, explicitly); |
| 5821 | } |
| 5822 | |
| 5823 | /** |
| 5824 | * Recursively traverse the view hierarchy, resetting the layoutMode of any |
| 5825 | * descendants that had inherited a different layoutMode from a previous parent. |
| 5826 | * Recursion terminates when a descendant's mode is: |
| 5827 | * <ul> |
| 5828 | * <li>Undefined</li> |
| 5829 | * <li>The same as the root node's</li> |
| 5830 | * <li>A mode that had been explicitly set</li> |
| 5831 | * <ul/> |
| 5832 | * The first two clauses are optimizations. |
| 5833 | * @param layoutModeOfRoot |
| 5834 | */ |
| 5835 | @Override |
| 5836 | void invalidateInheritedLayoutMode(int layoutModeOfRoot) { |
| 5837 | if (mLayoutMode == LAYOUT_MODE_UNDEFINED || |
| 5838 | mLayoutMode == layoutModeOfRoot || |
Svetoslav | 6254f48 | 2013-06-04 17:22:14 -0700 | [diff] [blame] | 5839 | hasBooleanFlag(FLAG_LAYOUT_MODE_WAS_EXPLICITLY_SET)) { |
Philip Milne | f091b66 | 2013-02-27 11:15:21 -0800 | [diff] [blame] | 5840 | return; |
| 5841 | } |
| 5842 | setLayoutMode(LAYOUT_MODE_UNDEFINED, false); |
| 5843 | |
| 5844 | // apply recursively |
| 5845 | for (int i = 0, N = getChildCount(); i < N; i++) { |
| 5846 | getChildAt(i).invalidateInheritedLayoutMode(layoutModeOfRoot); |
| 5847 | } |
| 5848 | } |
| 5849 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5850 | /** |
Philip Milne | cfb631b | 2012-10-26 10:51:46 -0700 | [diff] [blame] | 5851 | * Returns the basis of alignment during layout operations on this ViewGroup: |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 5852 | * either {@link #LAYOUT_MODE_CLIP_BOUNDS} or {@link #LAYOUT_MODE_OPTICAL_BOUNDS}. |
Philip Milne | cfb631b | 2012-10-26 10:51:46 -0700 | [diff] [blame] | 5853 | * <p> |
| 5854 | * If no layoutMode was explicitly set, either programmatically or in an XML resource, |
| 5855 | * the method returns the layoutMode of the view's parent ViewGroup if such a parent exists, |
| 5856 | * 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] | 5857 | * |
Philip Milne | fcc6a0f | 2012-04-16 16:12:19 -0700 | [diff] [blame] | 5858 | * @return the layout mode to use during layout operations |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 5859 | * |
| 5860 | * @see #setLayoutMode(int) |
| 5861 | */ |
| 5862 | public int getLayoutMode() { |
Philip Milne | cfb631b | 2012-10-26 10:51:46 -0700 | [diff] [blame] | 5863 | if (mLayoutMode == LAYOUT_MODE_UNDEFINED) { |
Philip Milne | f091b66 | 2013-02-27 11:15:21 -0800 | [diff] [blame] | 5864 | int inheritedLayoutMode = (mParent instanceof ViewGroup) ? |
| 5865 | ((ViewGroup) mParent).getLayoutMode() : LAYOUT_MODE_DEFAULT; |
| 5866 | setLayoutMode(inheritedLayoutMode, false); |
Philip Milne | cfb631b | 2012-10-26 10:51:46 -0700 | [diff] [blame] | 5867 | } |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 5868 | return mLayoutMode; |
| 5869 | } |
| 5870 | |
| 5871 | /** |
Philip Milne | cfb631b | 2012-10-26 10:51:46 -0700 | [diff] [blame] | 5872 | * Sets the basis of alignment during the layout of this ViewGroup. |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 5873 | * Valid values are either {@link #LAYOUT_MODE_CLIP_BOUNDS} or |
| 5874 | * {@link #LAYOUT_MODE_OPTICAL_BOUNDS}. |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 5875 | * |
Philip Milne | fcc6a0f | 2012-04-16 16:12:19 -0700 | [diff] [blame] | 5876 | * @param layoutMode the layout mode to use during layout operations |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 5877 | * |
| 5878 | * @see #getLayoutMode() |
Scott Main | 27a8508 | 2013-06-10 10:39:48 -0700 | [diff] [blame] | 5879 | * @attr ref android.R.styleable#ViewGroup_layoutMode |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 5880 | */ |
| 5881 | public void setLayoutMode(int layoutMode) { |
| 5882 | if (mLayoutMode != layoutMode) { |
Philip Milne | f091b66 | 2013-02-27 11:15:21 -0800 | [diff] [blame] | 5883 | invalidateInheritedLayoutMode(layoutMode); |
| 5884 | setLayoutMode(layoutMode, layoutMode != LAYOUT_MODE_UNDEFINED); |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 5885 | requestLayout(); |
| 5886 | } |
| 5887 | } |
| 5888 | |
| 5889 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5890 | * Returns a new set of layout parameters based on the supplied attributes set. |
| 5891 | * |
| 5892 | * @param attrs the attributes to build the layout parameters from |
| 5893 | * |
| 5894 | * @return an instance of {@link android.view.ViewGroup.LayoutParams} or one |
| 5895 | * of its descendants |
| 5896 | */ |
| 5897 | public LayoutParams generateLayoutParams(AttributeSet attrs) { |
| 5898 | return new LayoutParams(getContext(), attrs); |
| 5899 | } |
| 5900 | |
| 5901 | /** |
| 5902 | * Returns a safe set of layout parameters based on the supplied layout params. |
| 5903 | * When a ViewGroup is passed a View whose layout params do not pass the test of |
| 5904 | * {@link #checkLayoutParams(android.view.ViewGroup.LayoutParams)}, this method |
| 5905 | * is invoked. This method should return a new set of layout params suitable for |
| 5906 | * this ViewGroup, possibly by copying the appropriate attributes from the |
| 5907 | * specified set of layout params. |
| 5908 | * |
| 5909 | * @param p The layout parameters to convert into a suitable set of layout parameters |
| 5910 | * for this ViewGroup. |
| 5911 | * |
| 5912 | * @return an instance of {@link android.view.ViewGroup.LayoutParams} or one |
| 5913 | * of its descendants |
| 5914 | */ |
| 5915 | protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) { |
Chet Haase | 4610eef | 2015-12-03 07:38:11 -0800 | [diff] [blame] | 5916 | return p; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5917 | } |
| 5918 | |
| 5919 | /** |
| 5920 | * Returns a set of default layout parameters. These parameters are requested |
| 5921 | * when the View passed to {@link #addView(View)} has no layout parameters |
| 5922 | * already set. If null is returned, an exception is thrown from addView. |
| 5923 | * |
| 5924 | * @return a set of default layout parameters or null |
| 5925 | */ |
| 5926 | protected LayoutParams generateDefaultLayoutParams() { |
| 5927 | return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); |
| 5928 | } |
| 5929 | |
| 5930 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5931 | * {@inheritDoc} |
| 5932 | */ |
| 5933 | @Override |
| 5934 | protected void debug(int depth) { |
| 5935 | super.debug(depth); |
| 5936 | String output; |
| 5937 | |
| 5938 | if (mFocused != null) { |
| 5939 | output = debugIndent(depth); |
| 5940 | output += "mFocused"; |
| 5941 | Log.d(VIEW_LOG_TAG, output); |
| 5942 | } |
| 5943 | if (mChildrenCount != 0) { |
| 5944 | output = debugIndent(depth); |
| 5945 | output += "{"; |
| 5946 | Log.d(VIEW_LOG_TAG, output); |
| 5947 | } |
| 5948 | int count = mChildrenCount; |
| 5949 | for (int i = 0; i < count; i++) { |
| 5950 | View child = mChildren[i]; |
| 5951 | child.debug(depth + 1); |
| 5952 | } |
| 5953 | |
| 5954 | if (mChildrenCount != 0) { |
| 5955 | output = debugIndent(depth); |
| 5956 | output += "}"; |
| 5957 | Log.d(VIEW_LOG_TAG, output); |
| 5958 | } |
| 5959 | } |
| 5960 | |
| 5961 | /** |
| 5962 | * Returns the position in the group of the specified child view. |
| 5963 | * |
| 5964 | * @param child the view for which to get the position |
| 5965 | * @return a positive integer representing the position of the view in the |
| 5966 | * group, or -1 if the view does not exist in the group |
| 5967 | */ |
| 5968 | public int indexOfChild(View child) { |
| 5969 | final int count = mChildrenCount; |
| 5970 | final View[] children = mChildren; |
| 5971 | for (int i = 0; i < count; i++) { |
| 5972 | if (children[i] == child) { |
| 5973 | return i; |
| 5974 | } |
| 5975 | } |
| 5976 | return -1; |
| 5977 | } |
| 5978 | |
| 5979 | /** |
| 5980 | * Returns the number of children in the group. |
| 5981 | * |
| 5982 | * @return a positive integer representing the number of children in |
| 5983 | * the group |
| 5984 | */ |
| 5985 | public int getChildCount() { |
| 5986 | return mChildrenCount; |
| 5987 | } |
| 5988 | |
| 5989 | /** |
| 5990 | * Returns the view at the specified position in the group. |
| 5991 | * |
| 5992 | * @param index the position at which to get the view from |
| 5993 | * @return the view at the specified position or null if the position |
| 5994 | * does not exist within the group |
| 5995 | */ |
| 5996 | public View getChildAt(int index) { |
Adam Powell | 3ba8f5d6 | 2011-03-07 15:36:33 -0800 | [diff] [blame] | 5997 | if (index < 0 || index >= mChildrenCount) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5998 | return null; |
| 5999 | } |
Adam Powell | 3ba8f5d6 | 2011-03-07 15:36:33 -0800 | [diff] [blame] | 6000 | return mChildren[index]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6001 | } |
| 6002 | |
| 6003 | /** |
| 6004 | * Ask all of the children of this view to measure themselves, taking into |
| 6005 | * account both the MeasureSpec requirements for this view and its padding. |
| 6006 | * We skip children that are in the GONE state The heavy lifting is done in |
| 6007 | * getChildMeasureSpec. |
| 6008 | * |
| 6009 | * @param widthMeasureSpec The width requirements for this view |
| 6010 | * @param heightMeasureSpec The height requirements for this view |
| 6011 | */ |
| 6012 | protected void measureChildren(int widthMeasureSpec, int heightMeasureSpec) { |
| 6013 | final int size = mChildrenCount; |
| 6014 | final View[] children = mChildren; |
| 6015 | for (int i = 0; i < size; ++i) { |
| 6016 | final View child = children[i]; |
| 6017 | if ((child.mViewFlags & VISIBILITY_MASK) != GONE) { |
| 6018 | measureChild(child, widthMeasureSpec, heightMeasureSpec); |
| 6019 | } |
| 6020 | } |
| 6021 | } |
| 6022 | |
| 6023 | /** |
| 6024 | * Ask one of the children of this view to measure itself, taking into |
| 6025 | * account both the MeasureSpec requirements for this view and its padding. |
| 6026 | * The heavy lifting is done in getChildMeasureSpec. |
| 6027 | * |
| 6028 | * @param child The child to measure |
| 6029 | * @param parentWidthMeasureSpec The width requirements for this view |
| 6030 | * @param parentHeightMeasureSpec The height requirements for this view |
| 6031 | */ |
| 6032 | protected void measureChild(View child, int parentWidthMeasureSpec, |
| 6033 | int parentHeightMeasureSpec) { |
| 6034 | final LayoutParams lp = child.getLayoutParams(); |
| 6035 | |
| 6036 | final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec, |
| 6037 | mPaddingLeft + mPaddingRight, lp.width); |
| 6038 | final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec, |
| 6039 | mPaddingTop + mPaddingBottom, lp.height); |
| 6040 | |
| 6041 | child.measure(childWidthMeasureSpec, childHeightMeasureSpec); |
| 6042 | } |
| 6043 | |
| 6044 | /** |
| 6045 | * Ask one of the children of this view to measure itself, taking into |
| 6046 | * account both the MeasureSpec requirements for this view and its padding |
| 6047 | * and margins. The child must have MarginLayoutParams The heavy lifting is |
| 6048 | * done in getChildMeasureSpec. |
| 6049 | * |
| 6050 | * @param child The child to measure |
| 6051 | * @param parentWidthMeasureSpec The width requirements for this view |
| 6052 | * @param widthUsed Extra space that has been used up by the parent |
| 6053 | * horizontally (possibly by other children of the parent) |
| 6054 | * @param parentHeightMeasureSpec The height requirements for this view |
| 6055 | * @param heightUsed Extra space that has been used up by the parent |
| 6056 | * vertically (possibly by other children of the parent) |
| 6057 | */ |
| 6058 | protected void measureChildWithMargins(View child, |
| 6059 | int parentWidthMeasureSpec, int widthUsed, |
| 6060 | int parentHeightMeasureSpec, int heightUsed) { |
| 6061 | final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams(); |
| 6062 | |
| 6063 | final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec, |
| 6064 | mPaddingLeft + mPaddingRight + lp.leftMargin + lp.rightMargin |
| 6065 | + widthUsed, lp.width); |
| 6066 | final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec, |
| 6067 | mPaddingTop + mPaddingBottom + lp.topMargin + lp.bottomMargin |
| 6068 | + heightUsed, lp.height); |
| 6069 | |
| 6070 | child.measure(childWidthMeasureSpec, childHeightMeasureSpec); |
| 6071 | } |
| 6072 | |
| 6073 | /** |
| 6074 | * Does the hard part of measureChildren: figuring out the MeasureSpec to |
| 6075 | * pass to a particular child. This method figures out the right MeasureSpec |
| 6076 | * for one dimension (height or width) of one child view. |
| 6077 | * |
| 6078 | * The goal is to combine information from our MeasureSpec with the |
| 6079 | * LayoutParams of the child to get the best possible results. For example, |
| 6080 | * if the this view knows its size (because its MeasureSpec has a mode of |
| 6081 | * EXACTLY), and the child has indicated in its LayoutParams that it wants |
| 6082 | * to be the same size as the parent, the parent should ask the child to |
| 6083 | * layout given an exact size. |
| 6084 | * |
| 6085 | * @param spec The requirements for this view |
| 6086 | * @param padding The padding of this view for the current dimension and |
| 6087 | * margins, if applicable |
| 6088 | * @param childDimension How big the child wants to be in the current |
| 6089 | * dimension |
| 6090 | * @return a MeasureSpec integer for the child |
| 6091 | */ |
| 6092 | public static int getChildMeasureSpec(int spec, int padding, int childDimension) { |
| 6093 | int specMode = MeasureSpec.getMode(spec); |
| 6094 | int specSize = MeasureSpec.getSize(spec); |
| 6095 | |
| 6096 | int size = Math.max(0, specSize - padding); |
| 6097 | |
| 6098 | int resultSize = 0; |
| 6099 | int resultMode = 0; |
| 6100 | |
| 6101 | switch (specMode) { |
| 6102 | // Parent has imposed an exact size on us |
| 6103 | case MeasureSpec.EXACTLY: |
| 6104 | if (childDimension >= 0) { |
| 6105 | resultSize = childDimension; |
| 6106 | resultMode = MeasureSpec.EXACTLY; |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 6107 | } else if (childDimension == LayoutParams.MATCH_PARENT) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6108 | // Child wants to be our size. So be it. |
| 6109 | resultSize = size; |
| 6110 | resultMode = MeasureSpec.EXACTLY; |
| 6111 | } else if (childDimension == LayoutParams.WRAP_CONTENT) { |
| 6112 | // Child wants to determine its own size. It can't be |
| 6113 | // bigger than us. |
| 6114 | resultSize = size; |
| 6115 | resultMode = MeasureSpec.AT_MOST; |
| 6116 | } |
| 6117 | break; |
| 6118 | |
| 6119 | // Parent has imposed a maximum size on us |
| 6120 | case MeasureSpec.AT_MOST: |
| 6121 | if (childDimension >= 0) { |
| 6122 | // Child wants a specific size... so be it |
| 6123 | resultSize = childDimension; |
| 6124 | resultMode = MeasureSpec.EXACTLY; |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 6125 | } else if (childDimension == LayoutParams.MATCH_PARENT) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6126 | // Child wants to be our size, but our size is not fixed. |
| 6127 | // Constrain child to not be bigger than us. |
| 6128 | resultSize = size; |
| 6129 | resultMode = MeasureSpec.AT_MOST; |
| 6130 | } else if (childDimension == LayoutParams.WRAP_CONTENT) { |
| 6131 | // Child wants to determine its own size. It can't be |
| 6132 | // bigger than us. |
| 6133 | resultSize = size; |
| 6134 | resultMode = MeasureSpec.AT_MOST; |
| 6135 | } |
| 6136 | break; |
| 6137 | |
| 6138 | // Parent asked to see how big we want to be |
| 6139 | case MeasureSpec.UNSPECIFIED: |
| 6140 | if (childDimension >= 0) { |
| 6141 | // Child wants a specific size... let him have it |
| 6142 | resultSize = childDimension; |
| 6143 | resultMode = MeasureSpec.EXACTLY; |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 6144 | } else if (childDimension == LayoutParams.MATCH_PARENT) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6145 | // Child wants to be our size... find out how big it should |
| 6146 | // be |
Adam Powell | d5dbf4b | 2015-06-11 13:19:24 -0700 | [diff] [blame] | 6147 | resultSize = View.sUseZeroUnspecifiedMeasureSpec ? 0 : size; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6148 | resultMode = MeasureSpec.UNSPECIFIED; |
| 6149 | } else if (childDimension == LayoutParams.WRAP_CONTENT) { |
| 6150 | // Child wants to determine its own size.... find out how |
| 6151 | // big it should be |
Adam Powell | d5dbf4b | 2015-06-11 13:19:24 -0700 | [diff] [blame] | 6152 | resultSize = View.sUseZeroUnspecifiedMeasureSpec ? 0 : size; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6153 | resultMode = MeasureSpec.UNSPECIFIED; |
| 6154 | } |
| 6155 | break; |
| 6156 | } |
| 6157 | return MeasureSpec.makeMeasureSpec(resultSize, resultMode); |
| 6158 | } |
| 6159 | |
| 6160 | |
| 6161 | /** |
| 6162 | * Removes any pending animations for views that have been removed. Call |
| 6163 | * this if you don't want animations for exiting views to stack up. |
| 6164 | */ |
| 6165 | public void clearDisappearingChildren() { |
John Reck | ca7a9da | 2014-03-05 16:29:07 -0800 | [diff] [blame] | 6166 | final ArrayList<View> disappearingChildren = mDisappearingChildren; |
| 6167 | if (disappearingChildren != null) { |
| 6168 | final int count = disappearingChildren.size(); |
| 6169 | for (int i = 0; i < count; i++) { |
| 6170 | final View view = disappearingChildren.get(i); |
| 6171 | if (view.mAttachInfo != null) { |
| 6172 | view.dispatchDetachedFromWindow(); |
| 6173 | } |
| 6174 | view.clearAnimation(); |
| 6175 | } |
| 6176 | disappearingChildren.clear(); |
Chet Haase | b85967b | 2012-03-26 14:37:51 -0700 | [diff] [blame] | 6177 | invalidate(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6178 | } |
| 6179 | } |
| 6180 | |
| 6181 | /** |
| 6182 | * Add a view which is removed from mChildren but still needs animation |
| 6183 | * |
| 6184 | * @param v View to add |
| 6185 | */ |
| 6186 | private void addDisappearingView(View v) { |
| 6187 | ArrayList<View> disappearingChildren = mDisappearingChildren; |
| 6188 | |
| 6189 | if (disappearingChildren == null) { |
| 6190 | disappearingChildren = mDisappearingChildren = new ArrayList<View>(); |
| 6191 | } |
| 6192 | |
| 6193 | disappearingChildren.add(v); |
| 6194 | } |
| 6195 | |
| 6196 | /** |
| 6197 | * Cleanup a view when its animation is done. This may mean removing it from |
| 6198 | * the list of disappearing views. |
| 6199 | * |
| 6200 | * @param view The view whose animation has finished |
| 6201 | * @param animation The animation, cannot be null |
| 6202 | */ |
Chet Haase | 64a48c1 | 2012-02-13 16:33:29 -0800 | [diff] [blame] | 6203 | void finishAnimatingView(final View view, Animation animation) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6204 | final ArrayList<View> disappearingChildren = mDisappearingChildren; |
| 6205 | if (disappearingChildren != null) { |
| 6206 | if (disappearingChildren.contains(view)) { |
| 6207 | disappearingChildren.remove(view); |
| 6208 | |
| 6209 | if (view.mAttachInfo != null) { |
| 6210 | view.dispatchDetachedFromWindow(); |
| 6211 | } |
| 6212 | |
| 6213 | view.clearAnimation(); |
| 6214 | mGroupFlags |= FLAG_INVALIDATE_REQUIRED; |
| 6215 | } |
| 6216 | } |
| 6217 | |
| 6218 | if (animation != null && !animation.getFillAfter()) { |
| 6219 | view.clearAnimation(); |
| 6220 | } |
| 6221 | |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 6222 | if ((view.mPrivateFlags & PFLAG_ANIMATION_STARTED) == PFLAG_ANIMATION_STARTED) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6223 | view.onAnimationEnd(); |
| 6224 | // Should be performed by onAnimationEnd() but this avoid an infinite loop, |
| 6225 | // so we'd rather be safe than sorry |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 6226 | view.mPrivateFlags &= ~PFLAG_ANIMATION_STARTED; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6227 | // Draw one more frame after the animation is done |
| 6228 | mGroupFlags |= FLAG_INVALIDATE_REQUIRED; |
| 6229 | } |
| 6230 | } |
| 6231 | |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 6232 | /** |
Chet Haase | aceafe6 | 2011-08-26 15:44:33 -0700 | [diff] [blame] | 6233 | * Utility function called by View during invalidation to determine whether a view that |
| 6234 | * is invisible or gone should still be invalidated because it is being transitioned (and |
| 6235 | * therefore still needs to be drawn). |
| 6236 | */ |
| 6237 | boolean isViewTransitioning(View view) { |
| 6238 | return (mTransitioningViews != null && mTransitioningViews.contains(view)); |
| 6239 | } |
| 6240 | |
| 6241 | /** |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 6242 | * This method tells the ViewGroup that the given View object, which should have this |
| 6243 | * ViewGroup as its parent, |
| 6244 | * should be kept around (re-displayed when the ViewGroup draws its children) even if it |
| 6245 | * is removed from its parent. This allows animations, such as those used by |
| 6246 | * {@link android.app.Fragment} and {@link android.animation.LayoutTransition} to animate |
| 6247 | * the removal of views. A call to this method should always be accompanied by a later call |
| 6248 | * to {@link #endViewTransition(View)}, such as after an animation on the View has finished, |
| 6249 | * so that the View finally gets removed. |
| 6250 | * |
| 6251 | * @param view The View object to be kept visible even if it gets removed from its parent. |
| 6252 | */ |
| 6253 | public void startViewTransition(View view) { |
| 6254 | if (view.mParent == this) { |
| 6255 | if (mTransitioningViews == null) { |
| 6256 | mTransitioningViews = new ArrayList<View>(); |
| 6257 | } |
| 6258 | mTransitioningViews.add(view); |
| 6259 | } |
| 6260 | } |
| 6261 | |
| 6262 | /** |
| 6263 | * This method should always be called following an earlier call to |
| 6264 | * {@link #startViewTransition(View)}. The given View is finally removed from its parent |
| 6265 | * and will no longer be displayed. Note that this method does not perform the functionality |
| 6266 | * of removing a view from its parent; it just discontinues the display of a View that |
| 6267 | * has previously been removed. |
| 6268 | * |
| 6269 | * @return view The View object that has been removed but is being kept around in the visible |
| 6270 | * hierarchy by an earlier call to {@link #startViewTransition(View)}. |
| 6271 | */ |
| 6272 | public void endViewTransition(View view) { |
| 6273 | if (mTransitioningViews != null) { |
| 6274 | mTransitioningViews.remove(view); |
| 6275 | final ArrayList<View> disappearingChildren = mDisappearingChildren; |
| 6276 | if (disappearingChildren != null && disappearingChildren.contains(view)) { |
| 6277 | disappearingChildren.remove(view); |
Chet Haase | 5e25c2c | 2010-09-16 11:15:56 -0700 | [diff] [blame] | 6278 | if (mVisibilityChangingChildren != null && |
| 6279 | mVisibilityChangingChildren.contains(view)) { |
| 6280 | mVisibilityChangingChildren.remove(view); |
| 6281 | } else { |
| 6282 | if (view.mAttachInfo != null) { |
| 6283 | view.dispatchDetachedFromWindow(); |
| 6284 | } |
| 6285 | if (view.mParent != null) { |
| 6286 | view.mParent = null; |
| 6287 | } |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 6288 | } |
Chet Haase | b85967b | 2012-03-26 14:37:51 -0700 | [diff] [blame] | 6289 | invalidate(); |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 6290 | } |
| 6291 | } |
| 6292 | } |
| 6293 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 6294 | private LayoutTransition.TransitionListener mLayoutTransitionListener = |
| 6295 | new LayoutTransition.TransitionListener() { |
| 6296 | @Override |
| 6297 | public void startTransition(LayoutTransition transition, ViewGroup container, |
| 6298 | View view, int transitionType) { |
| 6299 | // We only care about disappearing items, since we need special logic to keep |
| 6300 | // those items visible after they've been 'removed' |
| 6301 | if (transitionType == LayoutTransition.DISAPPEARING) { |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 6302 | startViewTransition(view); |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 6303 | } |
| 6304 | } |
| 6305 | |
| 6306 | @Override |
| 6307 | public void endTransition(LayoutTransition transition, ViewGroup container, |
| 6308 | View view, int transitionType) { |
Chet Haase | b989502 | 2013-04-02 15:10:58 -0700 | [diff] [blame] | 6309 | if (mLayoutCalledWhileSuppressed && !transition.isChangingLayout()) { |
Chet Haase | 9c08744 | 2011-01-12 16:20:16 -0800 | [diff] [blame] | 6310 | requestLayout(); |
Chet Haase | b989502 | 2013-04-02 15:10:58 -0700 | [diff] [blame] | 6311 | mLayoutCalledWhileSuppressed = false; |
Chet Haase | 9c08744 | 2011-01-12 16:20:16 -0800 | [diff] [blame] | 6312 | } |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 6313 | if (transitionType == LayoutTransition.DISAPPEARING && mTransitioningViews != null) { |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 6314 | endViewTransition(view); |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 6315 | } |
| 6316 | } |
| 6317 | }; |
| 6318 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6319 | /** |
Chet Haase | b989502 | 2013-04-02 15:10:58 -0700 | [diff] [blame] | 6320 | * Tells this ViewGroup to suppress all layout() calls until layout |
| 6321 | * suppression is disabled with a later call to suppressLayout(false). |
| 6322 | * When layout suppression is disabled, a requestLayout() call is sent |
| 6323 | * if layout() was attempted while layout was being suppressed. |
| 6324 | * |
| 6325 | * @hide |
| 6326 | */ |
| 6327 | public void suppressLayout(boolean suppress) { |
| 6328 | mSuppressLayout = suppress; |
| 6329 | if (!suppress) { |
| 6330 | if (mLayoutCalledWhileSuppressed) { |
| 6331 | requestLayout(); |
| 6332 | mLayoutCalledWhileSuppressed = false; |
| 6333 | } |
| 6334 | } |
| 6335 | } |
| 6336 | |
| 6337 | /** |
Chet Haase | 199acdf | 2013-07-24 18:40:55 -0700 | [diff] [blame] | 6338 | * Returns whether layout calls on this container are currently being |
| 6339 | * suppressed, due to an earlier call to {@link #suppressLayout(boolean)}. |
| 6340 | * |
| 6341 | * @return true if layout calls are currently suppressed, false otherwise. |
| 6342 | * |
| 6343 | * @hide |
| 6344 | */ |
| 6345 | public boolean isLayoutSuppressed() { |
| 6346 | return mSuppressLayout; |
| 6347 | } |
| 6348 | |
| 6349 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6350 | * {@inheritDoc} |
| 6351 | */ |
| 6352 | @Override |
| 6353 | public boolean gatherTransparentRegion(Region region) { |
| 6354 | // If no transparent regions requested, we are always opaque. |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 6355 | 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] | 6356 | if (meOpaque && region == null) { |
| 6357 | // The caller doesn't care about the region, so stop now. |
| 6358 | return true; |
| 6359 | } |
| 6360 | super.gatherTransparentRegion(region); |
| 6361 | final View[] children = mChildren; |
| 6362 | final int count = mChildrenCount; |
| 6363 | boolean noneOfTheChildrenAreTransparent = true; |
| 6364 | for (int i = 0; i < count; i++) { |
| 6365 | final View child = children[i]; |
Mathias Agopian | e338115 | 2010-12-02 15:19:36 -0800 | [diff] [blame] | 6366 | if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6367 | if (!child.gatherTransparentRegion(region)) { |
| 6368 | noneOfTheChildrenAreTransparent = false; |
| 6369 | } |
| 6370 | } |
| 6371 | } |
| 6372 | return meOpaque || noneOfTheChildrenAreTransparent; |
| 6373 | } |
| 6374 | |
| 6375 | /** |
| 6376 | * {@inheritDoc} |
| 6377 | */ |
| 6378 | public void requestTransparentRegion(View child) { |
| 6379 | if (child != null) { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 6380 | child.mPrivateFlags |= View.PFLAG_REQUEST_TRANSPARENT_REGIONS; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6381 | if (mParent != null) { |
| 6382 | mParent.requestTransparentRegion(this); |
| 6383 | } |
| 6384 | } |
| 6385 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 6386 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6387 | @Override |
Adam Powell | 46e38fd | 2014-02-03 10:16:49 -0800 | [diff] [blame] | 6388 | public WindowInsets dispatchApplyWindowInsets(WindowInsets insets) { |
| 6389 | insets = super.dispatchApplyWindowInsets(insets); |
Adam Powell | 0d9fdba | 2014-06-11 15:33:08 -0700 | [diff] [blame] | 6390 | if (!insets.isConsumed()) { |
Adam Powell | 46e38fd | 2014-02-03 10:16:49 -0800 | [diff] [blame] | 6391 | final int count = getChildCount(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6392 | for (int i = 0; i < count; i++) { |
Adam Powell | 46e38fd | 2014-02-03 10:16:49 -0800 | [diff] [blame] | 6393 | insets = getChildAt(i).dispatchApplyWindowInsets(insets); |
Adam Powell | 0d9fdba | 2014-06-11 15:33:08 -0700 | [diff] [blame] | 6394 | if (insets.isConsumed()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6395 | break; |
| 6396 | } |
| 6397 | } |
| 6398 | } |
Adam Powell | 46e38fd | 2014-02-03 10:16:49 -0800 | [diff] [blame] | 6399 | return insets; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6400 | } |
| 6401 | |
| 6402 | /** |
| 6403 | * Returns the animation listener to which layout animation events are |
| 6404 | * sent. |
| 6405 | * |
| 6406 | * @return an {@link android.view.animation.Animation.AnimationListener} |
| 6407 | */ |
| 6408 | public Animation.AnimationListener getLayoutAnimationListener() { |
| 6409 | return mAnimationListener; |
| 6410 | } |
| 6411 | |
| 6412 | @Override |
| 6413 | protected void drawableStateChanged() { |
| 6414 | super.drawableStateChanged(); |
| 6415 | |
| 6416 | if ((mGroupFlags & FLAG_NOTIFY_CHILDREN_ON_DRAWABLE_STATE_CHANGE) != 0) { |
| 6417 | if ((mGroupFlags & FLAG_ADD_STATES_FROM_CHILDREN) != 0) { |
| 6418 | throw new IllegalStateException("addStateFromChildren cannot be enabled if a" |
| 6419 | + " child has duplicateParentState set to true"); |
| 6420 | } |
| 6421 | |
| 6422 | final View[] children = mChildren; |
| 6423 | final int count = mChildrenCount; |
| 6424 | |
| 6425 | for (int i = 0; i < count; i++) { |
| 6426 | final View child = children[i]; |
| 6427 | if ((child.mViewFlags & DUPLICATE_PARENT_STATE) != 0) { |
| 6428 | child.refreshDrawableState(); |
| 6429 | } |
| 6430 | } |
| 6431 | } |
| 6432 | } |
| 6433 | |
| 6434 | @Override |
Dianne Hackborn | e213677 | 2010-11-04 15:08:59 -0700 | [diff] [blame] | 6435 | public void jumpDrawablesToCurrentState() { |
| 6436 | super.jumpDrawablesToCurrentState(); |
| 6437 | final View[] children = mChildren; |
| 6438 | final int count = mChildrenCount; |
| 6439 | for (int i = 0; i < count; i++) { |
| 6440 | children[i].jumpDrawablesToCurrentState(); |
| 6441 | } |
| 6442 | } |
| 6443 | |
| 6444 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6445 | protected int[] onCreateDrawableState(int extraSpace) { |
| 6446 | if ((mGroupFlags & FLAG_ADD_STATES_FROM_CHILDREN) == 0) { |
| 6447 | return super.onCreateDrawableState(extraSpace); |
| 6448 | } |
| 6449 | |
| 6450 | int need = 0; |
| 6451 | int n = getChildCount(); |
| 6452 | for (int i = 0; i < n; i++) { |
| 6453 | int[] childState = getChildAt(i).getDrawableState(); |
| 6454 | |
| 6455 | if (childState != null) { |
| 6456 | need += childState.length; |
| 6457 | } |
| 6458 | } |
| 6459 | |
| 6460 | int[] state = super.onCreateDrawableState(extraSpace + need); |
| 6461 | |
| 6462 | for (int i = 0; i < n; i++) { |
| 6463 | int[] childState = getChildAt(i).getDrawableState(); |
| 6464 | |
| 6465 | if (childState != null) { |
| 6466 | state = mergeDrawableStates(state, childState); |
| 6467 | } |
| 6468 | } |
| 6469 | |
| 6470 | return state; |
| 6471 | } |
| 6472 | |
| 6473 | /** |
| 6474 | * Sets whether this ViewGroup's drawable states also include |
| 6475 | * its children's drawable states. This is used, for example, to |
| 6476 | * make a group appear to be focused when its child EditText or button |
| 6477 | * is focused. |
| 6478 | */ |
| 6479 | public void setAddStatesFromChildren(boolean addsStates) { |
| 6480 | if (addsStates) { |
| 6481 | mGroupFlags |= FLAG_ADD_STATES_FROM_CHILDREN; |
| 6482 | } else { |
| 6483 | mGroupFlags &= ~FLAG_ADD_STATES_FROM_CHILDREN; |
| 6484 | } |
| 6485 | |
| 6486 | refreshDrawableState(); |
| 6487 | } |
| 6488 | |
| 6489 | /** |
| 6490 | * Returns whether this ViewGroup's drawable states also include |
| 6491 | * its children's drawable states. This is used, for example, to |
| 6492 | * make a group appear to be focused when its child EditText or button |
| 6493 | * is focused. |
| 6494 | */ |
| 6495 | public boolean addStatesFromChildren() { |
| 6496 | return (mGroupFlags & FLAG_ADD_STATES_FROM_CHILDREN) != 0; |
| 6497 | } |
| 6498 | |
| 6499 | /** |
Jeff Smith | a45746e | 2012-07-19 14:19:24 -0500 | [diff] [blame] | 6500 | * If {@link #addStatesFromChildren} is true, refreshes this group's |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6501 | * drawable state (to include the states from its children). |
| 6502 | */ |
| 6503 | public void childDrawableStateChanged(View child) { |
| 6504 | if ((mGroupFlags & FLAG_ADD_STATES_FROM_CHILDREN) != 0) { |
| 6505 | refreshDrawableState(); |
| 6506 | } |
| 6507 | } |
| 6508 | |
| 6509 | /** |
| 6510 | * Specifies the animation listener to which layout animation events must |
| 6511 | * be sent. Only |
| 6512 | * {@link android.view.animation.Animation.AnimationListener#onAnimationStart(Animation)} |
| 6513 | * and |
| 6514 | * {@link android.view.animation.Animation.AnimationListener#onAnimationEnd(Animation)} |
| 6515 | * are invoked. |
| 6516 | * |
| 6517 | * @param animationListener the layout animation listener |
| 6518 | */ |
| 6519 | public void setLayoutAnimationListener(Animation.AnimationListener animationListener) { |
| 6520 | mAnimationListener = animationListener; |
| 6521 | } |
| 6522 | |
| 6523 | /** |
Chet Haase | cca2c98 | 2011-05-20 14:34:18 -0700 | [diff] [blame] | 6524 | * This method is called by LayoutTransition when there are 'changing' animations that need |
| 6525 | * to start after the layout/setup phase. The request is forwarded to the ViewAncestor, who |
| 6526 | * starts all pending transitions prior to the drawing phase in the current traversal. |
| 6527 | * |
| 6528 | * @param transition The LayoutTransition to be started on the next traversal. |
| 6529 | * |
| 6530 | * @hide |
| 6531 | */ |
| 6532 | public void requestTransitionStart(LayoutTransition transition) { |
Dianne Hackborn | 6dd005b | 2011-07-18 13:22:50 -0700 | [diff] [blame] | 6533 | ViewRootImpl viewAncestor = getViewRootImpl(); |
Chet Haase | 1abf7fa | 2011-08-17 18:31:56 -0700 | [diff] [blame] | 6534 | if (viewAncestor != null) { |
| 6535 | viewAncestor.requestTransitionStart(transition); |
| 6536 | } |
Chet Haase | cca2c98 | 2011-05-20 14:34:18 -0700 | [diff] [blame] | 6537 | } |
| 6538 | |
Fabrice Di Meglio | 4457e85 | 2012-09-18 19:23:12 -0700 | [diff] [blame] | 6539 | /** |
| 6540 | * @hide |
| 6541 | */ |
Fabrice Di Meglio | 80dc53d | 2011-06-21 18:36:33 -0700 | [diff] [blame] | 6542 | @Override |
Fabrice Di Meglio | 09ecb25 | 2013-05-03 16:51:55 -0700 | [diff] [blame] | 6543 | public boolean resolveRtlPropertiesIfNeeded() { |
| 6544 | final boolean result = super.resolveRtlPropertiesIfNeeded(); |
| 6545 | // We dont need to resolve the children RTL properties if nothing has changed for the parent |
| 6546 | if (result) { |
| 6547 | int count = getChildCount(); |
| 6548 | for (int i = 0; i < count; i++) { |
| 6549 | final View child = getChildAt(i); |
| 6550 | if (child.isLayoutDirectionInherited()) { |
| 6551 | child.resolveRtlPropertiesIfNeeded(); |
| 6552 | } |
Fabrice Di Meglio | 84ebb35 | 2012-10-11 16:27:37 -0700 | [diff] [blame] | 6553 | } |
| 6554 | } |
Fabrice Di Meglio | 09ecb25 | 2013-05-03 16:51:55 -0700 | [diff] [blame] | 6555 | return result; |
Fabrice Di Meglio | 84ebb35 | 2012-10-11 16:27:37 -0700 | [diff] [blame] | 6556 | } |
| 6557 | |
| 6558 | /** |
| 6559 | * @hide |
| 6560 | */ |
| 6561 | @Override |
Fabrice Di Meglio | 9a04856 | 2012-09-26 14:55:56 -0700 | [diff] [blame] | 6562 | public boolean resolveLayoutDirection() { |
| 6563 | final boolean result = super.resolveLayoutDirection(); |
| 6564 | if (result) { |
| 6565 | int count = getChildCount(); |
| 6566 | for (int i = 0; i < count; i++) { |
| 6567 | final View child = getChildAt(i); |
| 6568 | if (child.isLayoutDirectionInherited()) { |
| 6569 | child.resolveLayoutDirection(); |
| 6570 | } |
Fabrice Di Meglio | 1f88ba8 | 2012-09-24 14:56:49 -0700 | [diff] [blame] | 6571 | } |
| 6572 | } |
Fabrice Di Meglio | 9a04856 | 2012-09-26 14:55:56 -0700 | [diff] [blame] | 6573 | return result; |
Fabrice Di Meglio | 1f88ba8 | 2012-09-24 14:56:49 -0700 | [diff] [blame] | 6574 | } |
| 6575 | |
| 6576 | /** |
| 6577 | * @hide |
| 6578 | */ |
| 6579 | @Override |
Fabrice Di Meglio | 9a04856 | 2012-09-26 14:55:56 -0700 | [diff] [blame] | 6580 | public boolean resolveTextDirection() { |
| 6581 | final boolean result = super.resolveTextDirection(); |
| 6582 | if (result) { |
| 6583 | int count = getChildCount(); |
| 6584 | for (int i = 0; i < count; i++) { |
| 6585 | final View child = getChildAt(i); |
| 6586 | if (child.isTextDirectionInherited()) { |
| 6587 | child.resolveTextDirection(); |
| 6588 | } |
Fabrice Di Meglio | 1f88ba8 | 2012-09-24 14:56:49 -0700 | [diff] [blame] | 6589 | } |
| 6590 | } |
Fabrice Di Meglio | 9a04856 | 2012-09-26 14:55:56 -0700 | [diff] [blame] | 6591 | return result; |
Fabrice Di Meglio | 1f88ba8 | 2012-09-24 14:56:49 -0700 | [diff] [blame] | 6592 | } |
| 6593 | |
| 6594 | /** |
| 6595 | * @hide |
| 6596 | */ |
| 6597 | @Override |
Fabrice Di Meglio | 9a04856 | 2012-09-26 14:55:56 -0700 | [diff] [blame] | 6598 | public boolean resolveTextAlignment() { |
| 6599 | final boolean result = super.resolveTextAlignment(); |
| 6600 | if (result) { |
| 6601 | int count = getChildCount(); |
| 6602 | for (int i = 0; i < count; i++) { |
| 6603 | final View child = getChildAt(i); |
| 6604 | if (child.isTextAlignmentInherited()) { |
| 6605 | child.resolveTextAlignment(); |
| 6606 | } |
Fabrice Di Meglio | 1f88ba8 | 2012-09-24 14:56:49 -0700 | [diff] [blame] | 6607 | } |
| 6608 | } |
Fabrice Di Meglio | 9a04856 | 2012-09-26 14:55:56 -0700 | [diff] [blame] | 6609 | return result; |
Fabrice Di Meglio | 1f88ba8 | 2012-09-24 14:56:49 -0700 | [diff] [blame] | 6610 | } |
| 6611 | |
| 6612 | /** |
| 6613 | * @hide |
| 6614 | */ |
| 6615 | @Override |
Fabrice Di Meglio | 84ebb35 | 2012-10-11 16:27:37 -0700 | [diff] [blame] | 6616 | public void resolvePadding() { |
| 6617 | super.resolvePadding(); |
| 6618 | int count = getChildCount(); |
| 6619 | for (int i = 0; i < count; i++) { |
| 6620 | final View child = getChildAt(i); |
Adam Powell | 05f3512 | 2014-11-10 17:47:37 -0800 | [diff] [blame] | 6621 | if (child.isLayoutDirectionInherited() && !child.isPaddingResolved()) { |
Fabrice Di Meglio | 84ebb35 | 2012-10-11 16:27:37 -0700 | [diff] [blame] | 6622 | child.resolvePadding(); |
| 6623 | } |
| 6624 | } |
| 6625 | } |
| 6626 | |
| 6627 | /** |
| 6628 | * @hide |
| 6629 | */ |
| 6630 | @Override |
| 6631 | protected void resolveDrawables() { |
| 6632 | super.resolveDrawables(); |
| 6633 | int count = getChildCount(); |
| 6634 | for (int i = 0; i < count; i++) { |
| 6635 | final View child = getChildAt(i); |
Adam Powell | 05f3512 | 2014-11-10 17:47:37 -0800 | [diff] [blame] | 6636 | if (child.isLayoutDirectionInherited() && !child.areDrawablesResolved()) { |
Fabrice Di Meglio | 84ebb35 | 2012-10-11 16:27:37 -0700 | [diff] [blame] | 6637 | child.resolveDrawables(); |
| 6638 | } |
| 6639 | } |
| 6640 | } |
| 6641 | |
Fabrice Di Meglio | 1e0ed6b | 2012-10-18 16:06:52 -0700 | [diff] [blame] | 6642 | /** |
| 6643 | * @hide |
| 6644 | */ |
Fabrice Di Meglio | fcc3348 | 2012-10-18 11:11:51 -0700 | [diff] [blame] | 6645 | @Override |
| 6646 | public void resolveLayoutParams() { |
| 6647 | super.resolveLayoutParams(); |
| 6648 | int count = getChildCount(); |
| 6649 | for (int i = 0; i < count; i++) { |
| 6650 | final View child = getChildAt(i); |
| 6651 | child.resolveLayoutParams(); |
| 6652 | } |
| 6653 | } |
| 6654 | |
Fabrice Di Meglio | 84ebb35 | 2012-10-11 16:27:37 -0700 | [diff] [blame] | 6655 | /** |
| 6656 | * @hide |
| 6657 | */ |
| 6658 | @Override |
Fabrice Di Meglio | 4457e85 | 2012-09-18 19:23:12 -0700 | [diff] [blame] | 6659 | public void resetResolvedLayoutDirection() { |
| 6660 | super.resetResolvedLayoutDirection(); |
| 6661 | |
Fabrice Di Meglio | 4457e85 | 2012-09-18 19:23:12 -0700 | [diff] [blame] | 6662 | int count = getChildCount(); |
Fabrice Di Meglio | 80dc53d | 2011-06-21 18:36:33 -0700 | [diff] [blame] | 6663 | for (int i = 0; i < count; i++) { |
| 6664 | final View child = getChildAt(i); |
Fabrice Di Meglio | e56ffdc | 2012-09-23 14:51:16 -0700 | [diff] [blame] | 6665 | if (child.isLayoutDirectionInherited()) { |
Fabrice Di Meglio | 7f86c80 | 2011-07-01 15:09:24 -0700 | [diff] [blame] | 6666 | child.resetResolvedLayoutDirection(); |
Fabrice Di Meglio | 80dc53d | 2011-06-21 18:36:33 -0700 | [diff] [blame] | 6667 | } |
Fabrice Di Meglio | 1f88ba8 | 2012-09-24 14:56:49 -0700 | [diff] [blame] | 6668 | } |
| 6669 | } |
| 6670 | |
| 6671 | /** |
| 6672 | * @hide |
| 6673 | */ |
| 6674 | @Override |
| 6675 | public void resetResolvedTextDirection() { |
| 6676 | super.resetResolvedTextDirection(); |
| 6677 | |
| 6678 | int count = getChildCount(); |
| 6679 | for (int i = 0; i < count; i++) { |
| 6680 | final View child = getChildAt(i); |
Fabrice Di Meglio | 97e146c | 2012-09-23 15:45:16 -0700 | [diff] [blame] | 6681 | if (child.isTextDirectionInherited()) { |
Fabrice Di Meglio | 2226886 | 2011-06-27 18:13:18 -0700 | [diff] [blame] | 6682 | child.resetResolvedTextDirection(); |
| 6683 | } |
Fabrice Di Meglio | 1f88ba8 | 2012-09-24 14:56:49 -0700 | [diff] [blame] | 6684 | } |
| 6685 | } |
| 6686 | |
| 6687 | /** |
| 6688 | * @hide |
| 6689 | */ |
| 6690 | @Override |
| 6691 | public void resetResolvedTextAlignment() { |
| 6692 | super.resetResolvedTextAlignment(); |
| 6693 | |
| 6694 | int count = getChildCount(); |
| 6695 | for (int i = 0; i < count; i++) { |
| 6696 | final View child = getChildAt(i); |
Fabrice Di Meglio | 1a7d487 | 2012-09-23 16:19:58 -0700 | [diff] [blame] | 6697 | if (child.isTextAlignmentInherited()) { |
Fabrice Di Meglio | 9da0f8a | 2012-03-13 19:37:57 -0700 | [diff] [blame] | 6698 | child.resetResolvedTextAlignment(); |
| 6699 | } |
| 6700 | } |
| 6701 | } |
| 6702 | |
Fabrice Di Meglio | 2226886 | 2011-06-27 18:13:18 -0700 | [diff] [blame] | 6703 | /** |
Fabrice Di Meglio | 84ebb35 | 2012-10-11 16:27:37 -0700 | [diff] [blame] | 6704 | * @hide |
| 6705 | */ |
| 6706 | @Override |
| 6707 | public void resetResolvedPadding() { |
| 6708 | super.resetResolvedPadding(); |
| 6709 | |
| 6710 | int count = getChildCount(); |
| 6711 | for (int i = 0; i < count; i++) { |
| 6712 | final View child = getChildAt(i); |
| 6713 | if (child.isLayoutDirectionInherited()) { |
| 6714 | child.resetResolvedPadding(); |
| 6715 | } |
| 6716 | } |
| 6717 | } |
| 6718 | |
| 6719 | /** |
| 6720 | * @hide |
| 6721 | */ |
| 6722 | @Override |
| 6723 | protected void resetResolvedDrawables() { |
| 6724 | super.resetResolvedDrawables(); |
| 6725 | |
| 6726 | int count = getChildCount(); |
| 6727 | for (int i = 0; i < count; i++) { |
| 6728 | final View child = getChildAt(i); |
| 6729 | if (child.isLayoutDirectionInherited()) { |
| 6730 | child.resetResolvedDrawables(); |
| 6731 | } |
| 6732 | } |
| 6733 | } |
| 6734 | |
| 6735 | /** |
Patrick Dubroy | e0a799a | 2011-05-04 16:19:22 -0700 | [diff] [blame] | 6736 | * Return true if the pressed state should be delayed for children or descendants of this |
| 6737 | * ViewGroup. Generally, this should be done for containers that can scroll, such as a List. |
| 6738 | * This prevents the pressed state from appearing when the user is actually trying to scroll |
| 6739 | * the content. |
| 6740 | * |
| 6741 | * The default implementation returns true for compatibility reasons. Subclasses that do |
| 6742 | * not scroll should generally override this method and return false. |
| 6743 | */ |
| 6744 | public boolean shouldDelayChildPressedState() { |
| 6745 | return true; |
| 6746 | } |
| 6747 | |
Adam Powell | 10ba277 | 2014-04-15 09:46:51 -0700 | [diff] [blame] | 6748 | /** |
| 6749 | * @inheritDoc |
| 6750 | */ |
| 6751 | @Override |
| 6752 | public boolean onStartNestedScroll(View child, View target, int nestedScrollAxes) { |
| 6753 | return false; |
| 6754 | } |
| 6755 | |
| 6756 | /** |
| 6757 | * @inheritDoc |
| 6758 | */ |
| 6759 | @Override |
| 6760 | public void onNestedScrollAccepted(View child, View target, int axes) { |
| 6761 | mNestedScrollAxes = axes; |
| 6762 | } |
| 6763 | |
| 6764 | /** |
| 6765 | * @inheritDoc |
| 6766 | * |
| 6767 | * <p>The default implementation of onStopNestedScroll calls |
| 6768 | * {@link #stopNestedScroll()} to halt any recursive nested scrolling in progress.</p> |
| 6769 | */ |
| 6770 | @Override |
| 6771 | public void onStopNestedScroll(View child) { |
| 6772 | // Stop any recursive nested scrolling. |
| 6773 | stopNestedScroll(); |
Adam Powell | d4a22d4 | 2015-04-16 15:44:10 -0700 | [diff] [blame] | 6774 | mNestedScrollAxes = 0; |
Adam Powell | 10ba277 | 2014-04-15 09:46:51 -0700 | [diff] [blame] | 6775 | } |
| 6776 | |
| 6777 | /** |
| 6778 | * @inheritDoc |
| 6779 | */ |
| 6780 | @Override |
| 6781 | public void onNestedScroll(View target, int dxConsumed, int dyConsumed, |
| 6782 | int dxUnconsumed, int dyUnconsumed) { |
| 6783 | // Do nothing |
| 6784 | } |
| 6785 | |
| 6786 | /** |
| 6787 | * @inheritDoc |
| 6788 | */ |
| 6789 | @Override |
| 6790 | public void onNestedPreScroll(View target, int dx, int dy, int[] consumed) { |
| 6791 | // Do nothing |
| 6792 | } |
| 6793 | |
| 6794 | /** |
| 6795 | * @inheritDoc |
| 6796 | */ |
| 6797 | @Override |
Adam Powell | b36e4f9 | 2014-05-01 10:23:33 -0700 | [diff] [blame] | 6798 | public boolean onNestedFling(View target, float velocityX, float velocityY, boolean consumed) { |
Adam Powell | 10ba277 | 2014-04-15 09:46:51 -0700 | [diff] [blame] | 6799 | return false; |
| 6800 | } |
| 6801 | |
| 6802 | /** |
Adam Powell | b72be59 | 2014-07-16 21:41:31 -0700 | [diff] [blame] | 6803 | * @inheritDoc |
| 6804 | */ |
| 6805 | @Override |
| 6806 | public boolean onNestedPreFling(View target, float velocityX, float velocityY) { |
| 6807 | return false; |
| 6808 | } |
| 6809 | |
| 6810 | /** |
Adam Powell | 10ba277 | 2014-04-15 09:46:51 -0700 | [diff] [blame] | 6811 | * Return the current axes of nested scrolling for this ViewGroup. |
| 6812 | * |
| 6813 | * <p>A ViewGroup returning something other than {@link #SCROLL_AXIS_NONE} is currently |
| 6814 | * acting as a nested scrolling parent for one or more descendant views in the hierarchy.</p> |
| 6815 | * |
| 6816 | * @return Flags indicating the current axes of nested scrolling |
| 6817 | * @see #SCROLL_AXIS_HORIZONTAL |
| 6818 | * @see #SCROLL_AXIS_VERTICAL |
| 6819 | * @see #SCROLL_AXIS_NONE |
| 6820 | */ |
| 6821 | public int getNestedScrollAxes() { |
| 6822 | return mNestedScrollAxes; |
| 6823 | } |
| 6824 | |
Philip Milne | d7dd890 | 2012-01-26 16:55:30 -0800 | [diff] [blame] | 6825 | /** @hide */ |
| 6826 | protected void onSetLayoutParams(View child, LayoutParams layoutParams) { |
| 6827 | } |
| 6828 | |
George Mount | e180337 | 2014-02-26 19:00:52 +0000 | [diff] [blame] | 6829 | /** @hide */ |
| 6830 | @Override |
| 6831 | public void captureTransitioningViews(List<View> transitioningViews) { |
| 6832 | if (getVisibility() != View.VISIBLE) { |
| 6833 | return; |
| 6834 | } |
| 6835 | if (isTransitionGroup()) { |
| 6836 | transitioningViews.add(this); |
| 6837 | } else { |
| 6838 | int count = getChildCount(); |
| 6839 | for (int i = 0; i < count; i++) { |
| 6840 | View child = getChildAt(i); |
| 6841 | child.captureTransitioningViews(transitioningViews); |
| 6842 | } |
| 6843 | } |
| 6844 | } |
| 6845 | |
| 6846 | /** @hide */ |
| 6847 | @Override |
George Mount | abb352a | 2014-05-09 10:27:20 -0700 | [diff] [blame] | 6848 | public void findNamedViews(Map<String, View> namedElements) { |
George Mount | fe361d2 | 2014-07-08 17:25:25 -0700 | [diff] [blame] | 6849 | if (getVisibility() != VISIBLE && mGhostView == null) { |
George Mount | e180337 | 2014-02-26 19:00:52 +0000 | [diff] [blame] | 6850 | return; |
| 6851 | } |
George Mount | abb352a | 2014-05-09 10:27:20 -0700 | [diff] [blame] | 6852 | super.findNamedViews(namedElements); |
George Mount | e180337 | 2014-02-26 19:00:52 +0000 | [diff] [blame] | 6853 | int count = getChildCount(); |
| 6854 | for (int i = 0; i < count; i++) { |
| 6855 | View child = getChildAt(i); |
George Mount | abb352a | 2014-05-09 10:27:20 -0700 | [diff] [blame] | 6856 | child.findNamedViews(namedElements); |
George Mount | e180337 | 2014-02-26 19:00:52 +0000 | [diff] [blame] | 6857 | } |
| 6858 | } |
| 6859 | |
Patrick Dubroy | e0a799a | 2011-05-04 16:19:22 -0700 | [diff] [blame] | 6860 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6861 | * LayoutParams are used by views to tell their parents how they want to be |
| 6862 | * laid out. See |
| 6863 | * {@link android.R.styleable#ViewGroup_Layout ViewGroup Layout Attributes} |
| 6864 | * for a list of all child view attributes that this class supports. |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 6865 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6866 | * <p> |
| 6867 | * The base LayoutParams class just describes how big the view wants to be |
| 6868 | * for both width and height. For each dimension, it can specify one of: |
| 6869 | * <ul> |
Dirk Dougherty | 75c66da | 2010-03-25 16:33:33 -0700 | [diff] [blame] | 6870 | * <li>FILL_PARENT (renamed MATCH_PARENT in API Level 8 and higher), which |
| 6871 | * 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] | 6872 | * <li> WRAP_CONTENT, which means that the view wants to be just big enough |
| 6873 | * to enclose its content (plus padding) |
Dirk Dougherty | 75c66da | 2010-03-25 16:33:33 -0700 | [diff] [blame] | 6874 | * <li> an exact number |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6875 | * </ul> |
| 6876 | * There are subclasses of LayoutParams for different subclasses of |
| 6877 | * ViewGroup. For example, AbsoluteLayout has its own subclass of |
Joe Fernandez | 558459f | 2011-10-13 16:47:36 -0700 | [diff] [blame] | 6878 | * LayoutParams which adds an X and Y value.</p> |
| 6879 | * |
| 6880 | * <div class="special reference"> |
| 6881 | * <h3>Developer Guides</h3> |
| 6882 | * <p>For more information about creating user interface layouts, read the |
| 6883 | * <a href="{@docRoot}guide/topics/ui/declaring-layout.html">XML Layouts</a> developer |
| 6884 | * guide.</p></div> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6885 | * |
| 6886 | * @attr ref android.R.styleable#ViewGroup_Layout_layout_height |
| 6887 | * @attr ref android.R.styleable#ViewGroup_Layout_layout_width |
| 6888 | */ |
| 6889 | public static class LayoutParams { |
| 6890 | /** |
Dirk Dougherty | 75c66da | 2010-03-25 16:33:33 -0700 | [diff] [blame] | 6891 | * Special value for the height or width requested by a View. |
| 6892 | * FILL_PARENT means that the view wants to be as big as its parent, |
| 6893 | * minus the parent's padding, if any. This value is deprecated |
| 6894 | * 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] | 6895 | */ |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 6896 | @SuppressWarnings({"UnusedDeclaration"}) |
| 6897 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6898 | public static final int FILL_PARENT = -1; |
| 6899 | |
| 6900 | /** |
| 6901 | * Special value for the height or width requested by a View. |
Gilles Debunne | f5c6eff | 2010-02-09 19:08:36 -0800 | [diff] [blame] | 6902 | * 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] | 6903 | * minus the parent's padding, if any. Introduced in API Level 8. |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 6904 | */ |
| 6905 | public static final int MATCH_PARENT = -1; |
| 6906 | |
| 6907 | /** |
| 6908 | * 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] | 6909 | * WRAP_CONTENT means that the view wants to be just large enough to fit |
| 6910 | * its own internal content, taking its own padding into account. |
| 6911 | */ |
| 6912 | public static final int WRAP_CONTENT = -2; |
| 6913 | |
| 6914 | /** |
Dirk Dougherty | 75c66da | 2010-03-25 16:33:33 -0700 | [diff] [blame] | 6915 | * Information about how wide the view wants to be. Can be one of the |
Mark Doliner | d0646dc | 2014-08-27 16:04:02 -0700 | [diff] [blame] | 6916 | * constants FILL_PARENT (replaced by MATCH_PARENT |
| 6917 | * 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] | 6918 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 6919 | @ViewDebug.ExportedProperty(category = "layout", mapping = { |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 6920 | @ViewDebug.IntToString(from = MATCH_PARENT, to = "MATCH_PARENT"), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6921 | @ViewDebug.IntToString(from = WRAP_CONTENT, to = "WRAP_CONTENT") |
| 6922 | }) |
| 6923 | public int width; |
| 6924 | |
| 6925 | /** |
Dirk Dougherty | 75c66da | 2010-03-25 16:33:33 -0700 | [diff] [blame] | 6926 | * Information about how tall the view wants to be. Can be one of the |
Mark Doliner | d0646dc | 2014-08-27 16:04:02 -0700 | [diff] [blame] | 6927 | * constants FILL_PARENT (replaced by MATCH_PARENT |
| 6928 | * 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] | 6929 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 6930 | @ViewDebug.ExportedProperty(category = "layout", mapping = { |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 6931 | @ViewDebug.IntToString(from = MATCH_PARENT, to = "MATCH_PARENT"), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6932 | @ViewDebug.IntToString(from = WRAP_CONTENT, to = "WRAP_CONTENT") |
| 6933 | }) |
| 6934 | public int height; |
| 6935 | |
| 6936 | /** |
| 6937 | * Used to animate layouts. |
| 6938 | */ |
| 6939 | public LayoutAnimationController.AnimationParameters layoutAnimationParameters; |
| 6940 | |
| 6941 | /** |
| 6942 | * Creates a new set of layout parameters. The values are extracted from |
| 6943 | * the supplied attributes set and context. The XML attributes mapped |
| 6944 | * to this set of layout parameters are: |
| 6945 | * |
| 6946 | * <ul> |
| 6947 | * <li><code>layout_width</code>: the width, either an exact value, |
Dirk Dougherty | 75c66da | 2010-03-25 16:33:33 -0700 | [diff] [blame] | 6948 | * {@link #WRAP_CONTENT}, or {@link #FILL_PARENT} (replaced by |
| 6949 | * {@link #MATCH_PARENT} in API Level 8)</li> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6950 | * <li><code>layout_height</code>: the height, either an exact value, |
Dirk Dougherty | 75c66da | 2010-03-25 16:33:33 -0700 | [diff] [blame] | 6951 | * {@link #WRAP_CONTENT}, or {@link #FILL_PARENT} (replaced by |
| 6952 | * {@link #MATCH_PARENT} in API Level 8)</li> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6953 | * </ul> |
| 6954 | * |
| 6955 | * @param c the application environment |
| 6956 | * @param attrs the set of attributes from which to extract the layout |
| 6957 | * parameters' values |
| 6958 | */ |
| 6959 | public LayoutParams(Context c, AttributeSet attrs) { |
| 6960 | TypedArray a = c.obtainStyledAttributes(attrs, R.styleable.ViewGroup_Layout); |
| 6961 | setBaseAttributes(a, |
| 6962 | R.styleable.ViewGroup_Layout_layout_width, |
| 6963 | R.styleable.ViewGroup_Layout_layout_height); |
| 6964 | a.recycle(); |
| 6965 | } |
| 6966 | |
| 6967 | /** |
| 6968 | * Creates a new set of layout parameters with the specified width |
| 6969 | * and height. |
| 6970 | * |
Dirk Dougherty | 75c66da | 2010-03-25 16:33:33 -0700 | [diff] [blame] | 6971 | * @param width the width, either {@link #WRAP_CONTENT}, |
| 6972 | * {@link #FILL_PARENT} (replaced by {@link #MATCH_PARENT} in |
| 6973 | * API Level 8), or a fixed size in pixels |
| 6974 | * @param height the height, either {@link #WRAP_CONTENT}, |
| 6975 | * {@link #FILL_PARENT} (replaced by {@link #MATCH_PARENT} in |
| 6976 | * API Level 8), or a fixed size in pixels |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6977 | */ |
| 6978 | public LayoutParams(int width, int height) { |
| 6979 | this.width = width; |
| 6980 | this.height = height; |
| 6981 | } |
| 6982 | |
| 6983 | /** |
| 6984 | * Copy constructor. Clones the width and height values of the source. |
| 6985 | * |
| 6986 | * @param source The layout params to copy from. |
| 6987 | */ |
| 6988 | public LayoutParams(LayoutParams source) { |
| 6989 | this.width = source.width; |
| 6990 | this.height = source.height; |
| 6991 | } |
| 6992 | |
| 6993 | /** |
| 6994 | * Used internally by MarginLayoutParams. |
| 6995 | * @hide |
| 6996 | */ |
| 6997 | LayoutParams() { |
| 6998 | } |
| 6999 | |
| 7000 | /** |
Dave Burke | 579e140 | 2012-10-18 20:41:55 -0700 | [diff] [blame] | 7001 | * Extracts the layout parameters from the supplied attributes. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7002 | * |
| 7003 | * @param a the style attributes to extract the parameters from |
| 7004 | * @param widthAttr the identifier of the width attribute |
| 7005 | * @param heightAttr the identifier of the height attribute |
| 7006 | */ |
| 7007 | protected void setBaseAttributes(TypedArray a, int widthAttr, int heightAttr) { |
Dave Burke | 579e140 | 2012-10-18 20:41:55 -0700 | [diff] [blame] | 7008 | width = a.getLayoutDimension(widthAttr, "layout_width"); |
| 7009 | height = a.getLayoutDimension(heightAttr, "layout_height"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7010 | } |
| 7011 | |
| 7012 | /** |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7013 | * Resolve layout parameters depending on the layout direction. Subclasses that care about |
| 7014 | * layoutDirection changes should override this method. The default implementation does |
| 7015 | * nothing. |
| 7016 | * |
| 7017 | * @param layoutDirection the direction of the layout |
| 7018 | * |
| 7019 | * {@link View#LAYOUT_DIRECTION_LTR} |
| 7020 | * {@link View#LAYOUT_DIRECTION_RTL} |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7021 | */ |
Fabrice Di Meglio | 2918ab6 | 2012-10-10 16:39:25 -0700 | [diff] [blame] | 7022 | public void resolveLayoutDirection(int layoutDirection) { |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7023 | } |
| 7024 | |
| 7025 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7026 | * Returns a String representation of this set of layout parameters. |
| 7027 | * |
| 7028 | * @param output the String to prepend to the internal representation |
| 7029 | * @return a String with the following format: output + |
| 7030 | * "ViewGroup.LayoutParams={ width=WIDTH, height=HEIGHT }" |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 7031 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7032 | * @hide |
| 7033 | */ |
| 7034 | public String debug(String output) { |
| 7035 | return output + "ViewGroup.LayoutParams={ width=" |
| 7036 | + sizeToString(width) + ", height=" + sizeToString(height) + " }"; |
| 7037 | } |
| 7038 | |
| 7039 | /** |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 7040 | * Use {@code canvas} to draw suitable debugging annotations for these LayoutParameters. |
| 7041 | * |
| 7042 | * @param view the view that contains these layout parameters |
| 7043 | * @param canvas the canvas on which to draw |
| 7044 | * |
| 7045 | * @hide |
| 7046 | */ |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 7047 | public void onDebugDraw(View view, Canvas canvas, Paint paint) { |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 7048 | } |
| 7049 | |
| 7050 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7051 | * Converts the specified size to a readable String. |
| 7052 | * |
| 7053 | * @param size the size to convert |
| 7054 | * @return a String instance representing the supplied size |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 7055 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7056 | * @hide |
| 7057 | */ |
| 7058 | protected static String sizeToString(int size) { |
| 7059 | if (size == WRAP_CONTENT) { |
| 7060 | return "wrap-content"; |
| 7061 | } |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 7062 | if (size == MATCH_PARENT) { |
| 7063 | return "match-parent"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7064 | } |
| 7065 | return String.valueOf(size); |
| 7066 | } |
Siva Velusamy | 0d857b9 | 2015-04-22 10:23:56 -0700 | [diff] [blame] | 7067 | |
| 7068 | /** @hide */ |
| 7069 | void encode(@NonNull ViewHierarchyEncoder encoder) { |
| 7070 | encoder.beginObject(this); |
| 7071 | encodeProperties(encoder); |
| 7072 | encoder.endObject(); |
| 7073 | } |
| 7074 | |
| 7075 | /** @hide */ |
| 7076 | protected void encodeProperties(@NonNull ViewHierarchyEncoder encoder) { |
| 7077 | encoder.addProperty("width", width); |
| 7078 | encoder.addProperty("height", height); |
| 7079 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7080 | } |
| 7081 | |
| 7082 | /** |
| 7083 | * Per-child layout information for layouts that support margins. |
| 7084 | * See |
| 7085 | * {@link android.R.styleable#ViewGroup_MarginLayout ViewGroup Margin Layout Attributes} |
| 7086 | * for a list of all child view attributes that this class supports. |
| 7087 | */ |
| 7088 | public static class MarginLayoutParams extends ViewGroup.LayoutParams { |
| 7089 | /** |
Adam Powell | a7a735f | 2014-10-09 12:54:52 -0700 | [diff] [blame] | 7090 | * The left margin in pixels of the child. Margin values should be positive. |
Philip Milne | d7dd890 | 2012-01-26 16:55:30 -0800 | [diff] [blame] | 7091 | * Call {@link ViewGroup#setLayoutParams(LayoutParams)} after reassigning a new value |
| 7092 | * to this field. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7093 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 7094 | @ViewDebug.ExportedProperty(category = "layout") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7095 | public int leftMargin; |
| 7096 | |
| 7097 | /** |
Adam Powell | a7a735f | 2014-10-09 12:54:52 -0700 | [diff] [blame] | 7098 | * The top margin in pixels of the child. Margin values should be positive. |
Philip Milne | d7dd890 | 2012-01-26 16:55:30 -0800 | [diff] [blame] | 7099 | * Call {@link ViewGroup#setLayoutParams(LayoutParams)} after reassigning a new value |
| 7100 | * to this field. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7101 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 7102 | @ViewDebug.ExportedProperty(category = "layout") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7103 | public int topMargin; |
| 7104 | |
| 7105 | /** |
Adam Powell | a7a735f | 2014-10-09 12:54:52 -0700 | [diff] [blame] | 7106 | * The right margin in pixels of the child. Margin values should be positive. |
Philip Milne | d7dd890 | 2012-01-26 16:55:30 -0800 | [diff] [blame] | 7107 | * Call {@link ViewGroup#setLayoutParams(LayoutParams)} after reassigning a new value |
| 7108 | * to this field. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7109 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 7110 | @ViewDebug.ExportedProperty(category = "layout") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7111 | public int rightMargin; |
| 7112 | |
| 7113 | /** |
Adam Powell | a7a735f | 2014-10-09 12:54:52 -0700 | [diff] [blame] | 7114 | * The bottom margin in pixels of the child. Margin values should be positive. |
Philip Milne | d7dd890 | 2012-01-26 16:55:30 -0800 | [diff] [blame] | 7115 | * Call {@link ViewGroup#setLayoutParams(LayoutParams)} after reassigning a new value |
| 7116 | * to this field. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7117 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 7118 | @ViewDebug.ExportedProperty(category = "layout") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7119 | public int bottomMargin; |
| 7120 | |
| 7121 | /** |
Adam Powell | a7a735f | 2014-10-09 12:54:52 -0700 | [diff] [blame] | 7122 | * The start margin in pixels of the child. Margin values should be positive. |
Fabrice Di Meglio | 54546f2 | 2012-02-14 16:26:16 -0800 | [diff] [blame] | 7123 | * Call {@link ViewGroup#setLayoutParams(LayoutParams)} after reassigning a new value |
| 7124 | * to this field. |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7125 | */ |
| 7126 | @ViewDebug.ExportedProperty(category = "layout") |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 7127 | private int startMargin = DEFAULT_MARGIN_RELATIVE; |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7128 | |
| 7129 | /** |
Adam Powell | a7a735f | 2014-10-09 12:54:52 -0700 | [diff] [blame] | 7130 | * The end margin in pixels of the child. Margin values should be positive. |
Fabrice Di Meglio | 54546f2 | 2012-02-14 16:26:16 -0800 | [diff] [blame] | 7131 | * Call {@link ViewGroup#setLayoutParams(LayoutParams)} after reassigning a new value |
| 7132 | * to this field. |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7133 | */ |
| 7134 | @ViewDebug.ExportedProperty(category = "layout") |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 7135 | private int endMargin = DEFAULT_MARGIN_RELATIVE; |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7136 | |
| 7137 | /** |
| 7138 | * The default start and end margin. |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 7139 | * @hide |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7140 | */ |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 7141 | public static final int DEFAULT_MARGIN_RELATIVE = Integer.MIN_VALUE; |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7142 | |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7143 | /** |
| 7144 | * Bit 0: layout direction |
| 7145 | * Bit 1: layout direction |
| 7146 | * Bit 2: left margin undefined |
| 7147 | * Bit 3: right margin undefined |
| 7148 | * Bit 4: is RTL compatibility mode |
| 7149 | * Bit 5: need resolution |
| 7150 | * |
| 7151 | * Bit 6 to 7 not used |
| 7152 | * |
| 7153 | * @hide |
| 7154 | */ |
| 7155 | @ViewDebug.ExportedProperty(category = "layout", flagMapping = { |
| 7156 | @ViewDebug.FlagToString(mask = LAYOUT_DIRECTION_MASK, |
| 7157 | equals = LAYOUT_DIRECTION_MASK, name = "LAYOUT_DIRECTION"), |
| 7158 | @ViewDebug.FlagToString(mask = LEFT_MARGIN_UNDEFINED_MASK, |
| 7159 | equals = LEFT_MARGIN_UNDEFINED_MASK, name = "LEFT_MARGIN_UNDEFINED_MASK"), |
| 7160 | @ViewDebug.FlagToString(mask = RIGHT_MARGIN_UNDEFINED_MASK, |
| 7161 | equals = RIGHT_MARGIN_UNDEFINED_MASK, name = "RIGHT_MARGIN_UNDEFINED_MASK"), |
| 7162 | @ViewDebug.FlagToString(mask = RTL_COMPATIBILITY_MODE_MASK, |
| 7163 | equals = RTL_COMPATIBILITY_MODE_MASK, name = "RTL_COMPATIBILITY_MODE_MASK"), |
| 7164 | @ViewDebug.FlagToString(mask = NEED_RESOLUTION_MASK, |
| 7165 | equals = NEED_RESOLUTION_MASK, name = "NEED_RESOLUTION_MASK") |
Jon Miranda | 4597e98 | 2014-07-29 07:25:49 -0700 | [diff] [blame] | 7166 | }, formatToHexString = true) |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7167 | byte mMarginFlags; |
Fabrice Di Meglio | 69bd558 | 2012-07-02 13:17:24 -0700 | [diff] [blame] | 7168 | |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7169 | private static final int LAYOUT_DIRECTION_MASK = 0x00000003; |
| 7170 | private static final int LEFT_MARGIN_UNDEFINED_MASK = 0x00000004; |
| 7171 | private static final int RIGHT_MARGIN_UNDEFINED_MASK = 0x00000008; |
| 7172 | private static final int RTL_COMPATIBILITY_MODE_MASK = 0x00000010; |
| 7173 | private static final int NEED_RESOLUTION_MASK = 0x00000020; |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 7174 | |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7175 | private static final int DEFAULT_MARGIN_RESOLVED = 0; |
| 7176 | private static final int UNDEFINED_MARGIN = DEFAULT_MARGIN_RELATIVE; |
Fabrice Di Meglio | 02a7d56 | 2013-03-27 12:41:22 -0700 | [diff] [blame] | 7177 | |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7178 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7179 | * Creates a new set of layout parameters. The values are extracted from |
| 7180 | * the supplied attributes set and context. |
| 7181 | * |
| 7182 | * @param c the application environment |
| 7183 | * @param attrs the set of attributes from which to extract the layout |
| 7184 | * parameters' values |
| 7185 | */ |
| 7186 | public MarginLayoutParams(Context c, AttributeSet attrs) { |
| 7187 | super(); |
| 7188 | |
| 7189 | TypedArray a = c.obtainStyledAttributes(attrs, R.styleable.ViewGroup_MarginLayout); |
| 7190 | setBaseAttributes(a, |
| 7191 | R.styleable.ViewGroup_MarginLayout_layout_width, |
| 7192 | R.styleable.ViewGroup_MarginLayout_layout_height); |
| 7193 | |
| 7194 | int margin = a.getDimensionPixelSize( |
| 7195 | com.android.internal.R.styleable.ViewGroup_MarginLayout_layout_margin, -1); |
| 7196 | if (margin >= 0) { |
| 7197 | leftMargin = margin; |
| 7198 | topMargin = margin; |
| 7199 | rightMargin= margin; |
| 7200 | bottomMargin = margin; |
| 7201 | } else { |
| 7202 | leftMargin = a.getDimensionPixelSize( |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 7203 | R.styleable.ViewGroup_MarginLayout_layout_marginLeft, |
Fabrice Di Meglio | 02a7d56 | 2013-03-27 12:41:22 -0700 | [diff] [blame] | 7204 | UNDEFINED_MARGIN); |
| 7205 | if (leftMargin == UNDEFINED_MARGIN) { |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7206 | mMarginFlags |= LEFT_MARGIN_UNDEFINED_MASK; |
Fabrice Di Meglio | 02a7d56 | 2013-03-27 12:41:22 -0700 | [diff] [blame] | 7207 | leftMargin = DEFAULT_MARGIN_RESOLVED; |
| 7208 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7209 | rightMargin = a.getDimensionPixelSize( |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 7210 | R.styleable.ViewGroup_MarginLayout_layout_marginRight, |
Fabrice Di Meglio | 02a7d56 | 2013-03-27 12:41:22 -0700 | [diff] [blame] | 7211 | UNDEFINED_MARGIN); |
| 7212 | if (rightMargin == UNDEFINED_MARGIN) { |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7213 | mMarginFlags |= RIGHT_MARGIN_UNDEFINED_MASK; |
Fabrice Di Meglio | 02a7d56 | 2013-03-27 12:41:22 -0700 | [diff] [blame] | 7214 | rightMargin = DEFAULT_MARGIN_RESOLVED; |
| 7215 | } |
| 7216 | |
| 7217 | topMargin = a.getDimensionPixelSize( |
| 7218 | R.styleable.ViewGroup_MarginLayout_layout_marginTop, |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 7219 | DEFAULT_MARGIN_RESOLVED); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7220 | bottomMargin = a.getDimensionPixelSize( |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 7221 | R.styleable.ViewGroup_MarginLayout_layout_marginBottom, |
| 7222 | DEFAULT_MARGIN_RESOLVED); |
Fabrice Di Meglio | 02a7d56 | 2013-03-27 12:41:22 -0700 | [diff] [blame] | 7223 | |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7224 | startMargin = a.getDimensionPixelSize( |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 7225 | R.styleable.ViewGroup_MarginLayout_layout_marginStart, |
| 7226 | DEFAULT_MARGIN_RELATIVE); |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7227 | endMargin = a.getDimensionPixelSize( |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 7228 | R.styleable.ViewGroup_MarginLayout_layout_marginEnd, |
| 7229 | DEFAULT_MARGIN_RELATIVE); |
| 7230 | |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7231 | if (isMarginRelative()) { |
| 7232 | mMarginFlags |= NEED_RESOLUTION_MASK; |
| 7233 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7234 | } |
| 7235 | |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 7236 | final boolean hasRtlSupport = c.getApplicationInfo().hasRtlSupport(); |
| 7237 | final int targetSdkVersion = c.getApplicationInfo().targetSdkVersion; |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7238 | if (targetSdkVersion < JELLY_BEAN_MR1 || !hasRtlSupport) { |
| 7239 | mMarginFlags |= RTL_COMPATIBILITY_MODE_MASK; |
| 7240 | } |
| 7241 | |
| 7242 | // Layout direction is LTR by default |
| 7243 | mMarginFlags |= LAYOUT_DIRECTION_LTR; |
Fabrice Di Meglio | 69bd558 | 2012-07-02 13:17:24 -0700 | [diff] [blame] | 7244 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7245 | a.recycle(); |
| 7246 | } |
| 7247 | |
| 7248 | /** |
| 7249 | * {@inheritDoc} |
| 7250 | */ |
| 7251 | public MarginLayoutParams(int width, int height) { |
| 7252 | super(width, height); |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 7253 | |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7254 | mMarginFlags |= LEFT_MARGIN_UNDEFINED_MASK; |
| 7255 | mMarginFlags |= RIGHT_MARGIN_UNDEFINED_MASK; |
Fabrice Di Meglio | 02a7d56 | 2013-03-27 12:41:22 -0700 | [diff] [blame] | 7256 | |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7257 | mMarginFlags &= ~NEED_RESOLUTION_MASK; |
| 7258 | mMarginFlags &= ~RTL_COMPATIBILITY_MODE_MASK; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7259 | } |
| 7260 | |
| 7261 | /** |
| 7262 | * Copy constructor. Clones the width, height and margin values of the source. |
| 7263 | * |
| 7264 | * @param source The layout params to copy from. |
| 7265 | */ |
| 7266 | public MarginLayoutParams(MarginLayoutParams source) { |
| 7267 | this.width = source.width; |
| 7268 | this.height = source.height; |
| 7269 | |
| 7270 | this.leftMargin = source.leftMargin; |
| 7271 | this.topMargin = source.topMargin; |
| 7272 | this.rightMargin = source.rightMargin; |
| 7273 | this.bottomMargin = source.bottomMargin; |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7274 | this.startMargin = source.startMargin; |
| 7275 | this.endMargin = source.endMargin; |
Fabrice Di Meglio | 69bd558 | 2012-07-02 13:17:24 -0700 | [diff] [blame] | 7276 | |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7277 | this.mMarginFlags = source.mMarginFlags; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7278 | } |
| 7279 | |
| 7280 | /** |
| 7281 | * {@inheritDoc} |
| 7282 | */ |
| 7283 | public MarginLayoutParams(LayoutParams source) { |
| 7284 | super(source); |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 7285 | |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7286 | mMarginFlags |= LEFT_MARGIN_UNDEFINED_MASK; |
| 7287 | mMarginFlags |= RIGHT_MARGIN_UNDEFINED_MASK; |
Fabrice Di Meglio | 02a7d56 | 2013-03-27 12:41:22 -0700 | [diff] [blame] | 7288 | |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7289 | mMarginFlags &= ~NEED_RESOLUTION_MASK; |
| 7290 | mMarginFlags &= ~RTL_COMPATIBILITY_MODE_MASK; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7291 | } |
| 7292 | |
| 7293 | /** |
Adam Powell | d760083 | 2014-07-01 15:22:50 -0700 | [diff] [blame] | 7294 | * @hide Used internally. |
| 7295 | */ |
| 7296 | public final void copyMarginsFrom(MarginLayoutParams source) { |
| 7297 | this.leftMargin = source.leftMargin; |
| 7298 | this.topMargin = source.topMargin; |
| 7299 | this.rightMargin = source.rightMargin; |
| 7300 | this.bottomMargin = source.bottomMargin; |
| 7301 | this.startMargin = source.startMargin; |
| 7302 | this.endMargin = source.endMargin; |
| 7303 | |
| 7304 | this.mMarginFlags = source.mMarginFlags; |
| 7305 | } |
| 7306 | |
| 7307 | /** |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7308 | * Sets the margins, in pixels. A call to {@link android.view.View#requestLayout()} needs |
| 7309 | * to be done so that the new margins are taken into account. Left and right margins may be |
| 7310 | * overriden by {@link android.view.View#requestLayout()} depending on layout direction. |
Adam Powell | a7a735f | 2014-10-09 12:54:52 -0700 | [diff] [blame] | 7311 | * Margin values should be positive. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7312 | * |
| 7313 | * @param left the left margin size |
| 7314 | * @param top the top margin size |
| 7315 | * @param right the right margin size |
| 7316 | * @param bottom the bottom margin size |
| 7317 | * |
| 7318 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginLeft |
| 7319 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginTop |
| 7320 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginRight |
| 7321 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginBottom |
| 7322 | */ |
| 7323 | public void setMargins(int left, int top, int right, int bottom) { |
| 7324 | leftMargin = left; |
| 7325 | topMargin = top; |
| 7326 | rightMargin = right; |
| 7327 | bottomMargin = bottom; |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7328 | mMarginFlags &= ~LEFT_MARGIN_UNDEFINED_MASK; |
| 7329 | mMarginFlags &= ~RIGHT_MARGIN_UNDEFINED_MASK; |
| 7330 | if (isMarginRelative()) { |
| 7331 | mMarginFlags |= NEED_RESOLUTION_MASK; |
| 7332 | } else { |
| 7333 | mMarginFlags &= ~NEED_RESOLUTION_MASK; |
| 7334 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7335 | } |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7336 | |
| 7337 | /** |
| 7338 | * Sets the relative margins, in pixels. A call to {@link android.view.View#requestLayout()} |
| 7339 | * needs to be done so that the new relative margins are taken into account. Left and right |
| 7340 | * margins may be overriden by {@link android.view.View#requestLayout()} depending on layout |
Adam Powell | a7a735f | 2014-10-09 12:54:52 -0700 | [diff] [blame] | 7341 | * direction. Margin values should be positive. |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7342 | * |
| 7343 | * @param start the start margin size |
| 7344 | * @param top the top margin size |
| 7345 | * @param end the right margin size |
| 7346 | * @param bottom the bottom margin size |
| 7347 | * |
| 7348 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginStart |
| 7349 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginTop |
| 7350 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginEnd |
| 7351 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginBottom |
| 7352 | * |
| 7353 | * @hide |
| 7354 | */ |
| 7355 | public void setMarginsRelative(int start, int top, int end, int bottom) { |
| 7356 | startMargin = start; |
| 7357 | topMargin = top; |
| 7358 | endMargin = end; |
| 7359 | bottomMargin = bottom; |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7360 | mMarginFlags |= NEED_RESOLUTION_MASK; |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7361 | } |
| 7362 | |
| 7363 | /** |
Adam Powell | a7a735f | 2014-10-09 12:54:52 -0700 | [diff] [blame] | 7364 | * Sets the relative start margin. Margin values should be positive. |
Fabrice Di Meglio | a40627d | 2012-09-11 16:47:21 -0700 | [diff] [blame] | 7365 | * |
Fabrice Di Meglio | 61a2177 | 2012-09-12 16:33:13 -0700 | [diff] [blame] | 7366 | * @param start the start margin size |
Fabrice Di Meglio | a40627d | 2012-09-11 16:47:21 -0700 | [diff] [blame] | 7367 | * |
| 7368 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginStart |
| 7369 | */ |
| 7370 | public void setMarginStart(int start) { |
| 7371 | startMargin = start; |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7372 | mMarginFlags |= NEED_RESOLUTION_MASK; |
Fabrice Di Meglio | a40627d | 2012-09-11 16:47:21 -0700 | [diff] [blame] | 7373 | } |
| 7374 | |
| 7375 | /** |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7376 | * Returns the start margin in pixels. |
| 7377 | * |
| 7378 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginStart |
| 7379 | * |
| 7380 | * @return the start margin in pixels. |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7381 | */ |
| 7382 | public int getMarginStart() { |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 7383 | if (startMargin != DEFAULT_MARGIN_RELATIVE) return startMargin; |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7384 | if ((mMarginFlags & NEED_RESOLUTION_MASK) == NEED_RESOLUTION_MASK) { |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 7385 | doResolveMargins(); |
| 7386 | } |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7387 | switch(mMarginFlags & LAYOUT_DIRECTION_MASK) { |
Fabrice Di Meglio | 69bd558 | 2012-07-02 13:17:24 -0700 | [diff] [blame] | 7388 | case View.LAYOUT_DIRECTION_RTL: |
| 7389 | return rightMargin; |
| 7390 | case View.LAYOUT_DIRECTION_LTR: |
| 7391 | default: |
| 7392 | return leftMargin; |
| 7393 | } |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7394 | } |
| 7395 | |
| 7396 | /** |
Adam Powell | a7a735f | 2014-10-09 12:54:52 -0700 | [diff] [blame] | 7397 | * Sets the relative end margin. Margin values should be positive. |
Fabrice Di Meglio | a40627d | 2012-09-11 16:47:21 -0700 | [diff] [blame] | 7398 | * |
Fabrice Di Meglio | 61a2177 | 2012-09-12 16:33:13 -0700 | [diff] [blame] | 7399 | * @param end the end margin size |
Fabrice Di Meglio | a40627d | 2012-09-11 16:47:21 -0700 | [diff] [blame] | 7400 | * |
| 7401 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginEnd |
| 7402 | */ |
| 7403 | public void setMarginEnd(int end) { |
| 7404 | endMargin = end; |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7405 | mMarginFlags |= NEED_RESOLUTION_MASK; |
Fabrice Di Meglio | a40627d | 2012-09-11 16:47:21 -0700 | [diff] [blame] | 7406 | } |
| 7407 | |
| 7408 | /** |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7409 | * Returns the end margin in pixels. |
| 7410 | * |
| 7411 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginEnd |
| 7412 | * |
| 7413 | * @return the end margin in pixels. |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7414 | */ |
| 7415 | public int getMarginEnd() { |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 7416 | if (endMargin != DEFAULT_MARGIN_RELATIVE) return endMargin; |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7417 | if ((mMarginFlags & NEED_RESOLUTION_MASK) == NEED_RESOLUTION_MASK) { |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 7418 | doResolveMargins(); |
| 7419 | } |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7420 | switch(mMarginFlags & LAYOUT_DIRECTION_MASK) { |
Fabrice Di Meglio | 69bd558 | 2012-07-02 13:17:24 -0700 | [diff] [blame] | 7421 | case View.LAYOUT_DIRECTION_RTL: |
| 7422 | return leftMargin; |
| 7423 | case View.LAYOUT_DIRECTION_LTR: |
| 7424 | default: |
| 7425 | return rightMargin; |
| 7426 | } |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7427 | } |
| 7428 | |
| 7429 | /** |
| 7430 | * Check if margins are relative. |
| 7431 | * |
| 7432 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginStart |
| 7433 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginEnd |
| 7434 | * |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 7435 | * @return true if either marginStart or marginEnd has been set. |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7436 | */ |
| 7437 | public boolean isMarginRelative() { |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 7438 | return (startMargin != DEFAULT_MARGIN_RELATIVE || endMargin != DEFAULT_MARGIN_RELATIVE); |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7439 | } |
| 7440 | |
| 7441 | /** |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 7442 | * Set the layout direction |
| 7443 | * @param layoutDirection the layout direction. |
| 7444 | * Should be either {@link View#LAYOUT_DIRECTION_LTR} |
| 7445 | * or {@link View#LAYOUT_DIRECTION_RTL}. |
| 7446 | */ |
| 7447 | public void setLayoutDirection(int layoutDirection) { |
| 7448 | if (layoutDirection != View.LAYOUT_DIRECTION_LTR && |
| 7449 | layoutDirection != View.LAYOUT_DIRECTION_RTL) return; |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7450 | if (layoutDirection != (mMarginFlags & LAYOUT_DIRECTION_MASK)) { |
| 7451 | mMarginFlags &= ~LAYOUT_DIRECTION_MASK; |
| 7452 | mMarginFlags |= (layoutDirection & LAYOUT_DIRECTION_MASK); |
| 7453 | if (isMarginRelative()) { |
| 7454 | mMarginFlags |= NEED_RESOLUTION_MASK; |
| 7455 | } else { |
| 7456 | mMarginFlags &= ~NEED_RESOLUTION_MASK; |
| 7457 | } |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 7458 | } |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 7459 | } |
| 7460 | |
| 7461 | /** |
| 7462 | * Retuns the layout direction. Can be either {@link View#LAYOUT_DIRECTION_LTR} or |
| 7463 | * {@link View#LAYOUT_DIRECTION_RTL}. |
| 7464 | * |
| 7465 | * @return the layout direction. |
| 7466 | */ |
| 7467 | public int getLayoutDirection() { |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7468 | return (mMarginFlags & LAYOUT_DIRECTION_MASK); |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 7469 | } |
| 7470 | |
| 7471 | /** |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7472 | * 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] | 7473 | * may be overridden depending on layout direction. |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7474 | */ |
| 7475 | @Override |
Fabrice Di Meglio | 2918ab6 | 2012-10-10 16:39:25 -0700 | [diff] [blame] | 7476 | public void resolveLayoutDirection(int layoutDirection) { |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 7477 | setLayoutDirection(layoutDirection); |
Fabrice Di Meglio | 69bd558 | 2012-07-02 13:17:24 -0700 | [diff] [blame] | 7478 | |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 7479 | // No relative margin or pre JB-MR1 case or no need to resolve, just dont do anything |
| 7480 | // 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] | 7481 | if (!isMarginRelative() || |
| 7482 | (mMarginFlags & NEED_RESOLUTION_MASK) != NEED_RESOLUTION_MASK) return; |
Fabrice Di Meglio | 69bd558 | 2012-07-02 13:17:24 -0700 | [diff] [blame] | 7483 | |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 7484 | // Proceed with resolution |
| 7485 | doResolveMargins(); |
| 7486 | } |
| 7487 | |
| 7488 | private void doResolveMargins() { |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7489 | if ((mMarginFlags & RTL_COMPATIBILITY_MODE_MASK) == RTL_COMPATIBILITY_MODE_MASK) { |
Fabrice Di Meglio | 02a7d56 | 2013-03-27 12:41:22 -0700 | [diff] [blame] | 7490 | // if left or right margins are not defined and if we have some start or end margin |
| 7491 | // defined then use those start and end margins. |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7492 | if ((mMarginFlags & LEFT_MARGIN_UNDEFINED_MASK) == LEFT_MARGIN_UNDEFINED_MASK |
| 7493 | && startMargin > DEFAULT_MARGIN_RELATIVE) { |
Fabrice Di Meglio | 02a7d56 | 2013-03-27 12:41:22 -0700 | [diff] [blame] | 7494 | leftMargin = startMargin; |
| 7495 | } |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7496 | if ((mMarginFlags & RIGHT_MARGIN_UNDEFINED_MASK) == RIGHT_MARGIN_UNDEFINED_MASK |
| 7497 | && endMargin > DEFAULT_MARGIN_RELATIVE) { |
Fabrice Di Meglio | 02a7d56 | 2013-03-27 12:41:22 -0700 | [diff] [blame] | 7498 | rightMargin = endMargin; |
| 7499 | } |
| 7500 | } else { |
| 7501 | // We have some relative margins (either the start one or the end one or both). So use |
| 7502 | // them and override what has been defined for left and right margins. If either start |
| 7503 | // 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] | 7504 | switch(mMarginFlags & LAYOUT_DIRECTION_MASK) { |
Fabrice Di Meglio | 02a7d56 | 2013-03-27 12:41:22 -0700 | [diff] [blame] | 7505 | case View.LAYOUT_DIRECTION_RTL: |
| 7506 | leftMargin = (endMargin > DEFAULT_MARGIN_RELATIVE) ? |
| 7507 | endMargin : DEFAULT_MARGIN_RESOLVED; |
| 7508 | rightMargin = (startMargin > DEFAULT_MARGIN_RELATIVE) ? |
| 7509 | startMargin : DEFAULT_MARGIN_RESOLVED; |
| 7510 | break; |
| 7511 | case View.LAYOUT_DIRECTION_LTR: |
| 7512 | default: |
| 7513 | leftMargin = (startMargin > DEFAULT_MARGIN_RELATIVE) ? |
| 7514 | startMargin : DEFAULT_MARGIN_RESOLVED; |
| 7515 | rightMargin = (endMargin > DEFAULT_MARGIN_RELATIVE) ? |
| 7516 | endMargin : DEFAULT_MARGIN_RESOLVED; |
| 7517 | break; |
| 7518 | } |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7519 | } |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7520 | mMarginFlags &= ~NEED_RESOLUTION_MASK; |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7521 | } |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 7522 | |
Fabrice Di Meglio | 03b8d3a | 2012-09-27 17:05:27 -0700 | [diff] [blame] | 7523 | /** |
| 7524 | * @hide |
| 7525 | */ |
| 7526 | public boolean isLayoutRtl() { |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7527 | return ((mMarginFlags & LAYOUT_DIRECTION_MASK) == View.LAYOUT_DIRECTION_RTL); |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 7528 | } |
| 7529 | |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 7530 | /** |
| 7531 | * @hide |
| 7532 | */ |
| 7533 | @Override |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 7534 | public void onDebugDraw(View view, Canvas canvas, Paint paint) { |
| 7535 | Insets oi = isLayoutModeOptical(view.mParent) ? view.getOpticalInsets() : Insets.NONE; |
| 7536 | |
| 7537 | fillDifference(canvas, |
| 7538 | view.getLeft() + oi.left, |
| 7539 | view.getTop() + oi.top, |
| 7540 | view.getRight() - oi.right, |
| 7541 | view.getBottom() - oi.bottom, |
| 7542 | leftMargin, |
| 7543 | topMargin, |
| 7544 | rightMargin, |
| 7545 | bottomMargin, |
| 7546 | paint); |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 7547 | } |
Siva Velusamy | 0d857b9 | 2015-04-22 10:23:56 -0700 | [diff] [blame] | 7548 | |
| 7549 | /** @hide */ |
| 7550 | @Override |
| 7551 | protected void encodeProperties(@NonNull ViewHierarchyEncoder encoder) { |
| 7552 | super.encodeProperties(encoder); |
| 7553 | encoder.addProperty("leftMargin", leftMargin); |
| 7554 | encoder.addProperty("topMargin", topMargin); |
| 7555 | encoder.addProperty("rightMargin", rightMargin); |
| 7556 | encoder.addProperty("bottomMargin", bottomMargin); |
| 7557 | encoder.addProperty("startMargin", startMargin); |
| 7558 | encoder.addProperty("endMargin", endMargin); |
| 7559 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7560 | } |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 7561 | |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 7562 | /* Describes a touched view and the ids of the pointers that it has captured. |
| 7563 | * |
| 7564 | * This code assumes that pointer ids are always in the range 0..31 such that |
| 7565 | * it can use a bitfield to track which pointer ids are present. |
| 7566 | * As it happens, the lower layers of the input dispatch pipeline also use the |
| 7567 | * same trick so the assumption should be safe here... |
| 7568 | */ |
| 7569 | private static final class TouchTarget { |
| 7570 | private static final int MAX_RECYCLED = 32; |
Romain Guy | 6410c0a | 2013-06-17 11:21:58 -0700 | [diff] [blame] | 7571 | private static final Object sRecycleLock = new Object[0]; |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 7572 | private static TouchTarget sRecycleBin; |
| 7573 | private static int sRecycledCount; |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 7574 | |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 7575 | public static final int ALL_POINTER_IDS = -1; // all ones |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 7576 | |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 7577 | // The touched child view. |
| 7578 | public View child; |
| 7579 | |
| 7580 | // The combined bit mask of pointer ids for all pointers captured by the target. |
| 7581 | public int pointerIdBits; |
| 7582 | |
| 7583 | // The next target in the target list. |
| 7584 | public TouchTarget next; |
| 7585 | |
| 7586 | private TouchTarget() { |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 7587 | } |
| 7588 | |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 7589 | public static TouchTarget obtain(View child, int pointerIdBits) { |
| 7590 | final TouchTarget target; |
| 7591 | synchronized (sRecycleLock) { |
Adam Powell | 816c3be | 2010-08-23 18:00:05 -0700 | [diff] [blame] | 7592 | if (sRecycleBin == null) { |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 7593 | target = new TouchTarget(); |
Adam Powell | 816c3be | 2010-08-23 18:00:05 -0700 | [diff] [blame] | 7594 | } else { |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 7595 | target = sRecycleBin; |
| 7596 | sRecycleBin = target.next; |
| 7597 | sRecycledCount--; |
| 7598 | target.next = null; |
Adam Powell | 816c3be | 2010-08-23 18:00:05 -0700 | [diff] [blame] | 7599 | } |
Adam Powell | 816c3be | 2010-08-23 18:00:05 -0700 | [diff] [blame] | 7600 | } |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 7601 | target.child = child; |
| 7602 | target.pointerIdBits = pointerIdBits; |
| 7603 | return target; |
| 7604 | } |
Adam Powell | 816c3be | 2010-08-23 18:00:05 -0700 | [diff] [blame] | 7605 | |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 7606 | public void recycle() { |
| 7607 | synchronized (sRecycleLock) { |
| 7608 | if (sRecycledCount < MAX_RECYCLED) { |
| 7609 | next = sRecycleBin; |
| 7610 | sRecycleBin = this; |
| 7611 | sRecycledCount += 1; |
Patrick Dubroy | fb0547d2 | 2010-10-19 17:36:18 -0700 | [diff] [blame] | 7612 | } else { |
| 7613 | next = null; |
Adam Powell | 816c3be | 2010-08-23 18:00:05 -0700 | [diff] [blame] | 7614 | } |
Patrick Dubroy | fb0547d2 | 2010-10-19 17:36:18 -0700 | [diff] [blame] | 7615 | child = null; |
Adam Powell | 816c3be | 2010-08-23 18:00:05 -0700 | [diff] [blame] | 7616 | } |
| 7617 | } |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 7618 | } |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 7619 | |
| 7620 | /* Describes a hovered view. */ |
| 7621 | private static final class HoverTarget { |
| 7622 | private static final int MAX_RECYCLED = 32; |
Romain Guy | 6410c0a | 2013-06-17 11:21:58 -0700 | [diff] [blame] | 7623 | private static final Object sRecycleLock = new Object[0]; |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 7624 | private static HoverTarget sRecycleBin; |
| 7625 | private static int sRecycledCount; |
| 7626 | |
| 7627 | // The hovered child view. |
| 7628 | public View child; |
| 7629 | |
| 7630 | // The next target in the target list. |
| 7631 | public HoverTarget next; |
| 7632 | |
| 7633 | private HoverTarget() { |
| 7634 | } |
| 7635 | |
| 7636 | public static HoverTarget obtain(View child) { |
| 7637 | final HoverTarget target; |
| 7638 | synchronized (sRecycleLock) { |
| 7639 | if (sRecycleBin == null) { |
| 7640 | target = new HoverTarget(); |
| 7641 | } else { |
| 7642 | target = sRecycleBin; |
| 7643 | sRecycleBin = target.next; |
| 7644 | sRecycledCount--; |
| 7645 | target.next = null; |
| 7646 | } |
| 7647 | } |
| 7648 | target.child = child; |
| 7649 | return target; |
| 7650 | } |
| 7651 | |
| 7652 | public void recycle() { |
| 7653 | synchronized (sRecycleLock) { |
| 7654 | if (sRecycledCount < MAX_RECYCLED) { |
| 7655 | next = sRecycleBin; |
| 7656 | sRecycleBin = this; |
| 7657 | sRecycledCount += 1; |
| 7658 | } else { |
| 7659 | next = null; |
| 7660 | } |
| 7661 | child = null; |
| 7662 | } |
| 7663 | } |
| 7664 | } |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 7665 | |
| 7666 | /** |
| 7667 | * Pooled class that orderes the children of a ViewGroup from start |
| 7668 | * to end based on how they are laid out and the layout direction. |
| 7669 | */ |
| 7670 | static class ChildListForAccessibility { |
| 7671 | |
| 7672 | private static final int MAX_POOL_SIZE = 32; |
| 7673 | |
Svetoslav Ganov | be922dc | 2012-11-30 16:46:26 -0800 | [diff] [blame] | 7674 | private static final SynchronizedPool<ChildListForAccessibility> sPool = |
| 7675 | new SynchronizedPool<ChildListForAccessibility>(MAX_POOL_SIZE); |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 7676 | |
| 7677 | private final ArrayList<View> mChildren = new ArrayList<View>(); |
| 7678 | |
| 7679 | private final ArrayList<ViewLocationHolder> mHolders = new ArrayList<ViewLocationHolder>(); |
| 7680 | |
| 7681 | public static ChildListForAccessibility obtain(ViewGroup parent, boolean sort) { |
Svetoslav Ganov | be922dc | 2012-11-30 16:46:26 -0800 | [diff] [blame] | 7682 | ChildListForAccessibility list = sPool.acquire(); |
| 7683 | if (list == null) { |
| 7684 | list = new ChildListForAccessibility(); |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 7685 | } |
Svetoslav Ganov | be922dc | 2012-11-30 16:46:26 -0800 | [diff] [blame] | 7686 | list.init(parent, sort); |
| 7687 | return list; |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 7688 | } |
| 7689 | |
| 7690 | public void recycle() { |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 7691 | clear(); |
Svetoslav Ganov | be922dc | 2012-11-30 16:46:26 -0800 | [diff] [blame] | 7692 | sPool.release(this); |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 7693 | } |
| 7694 | |
| 7695 | public int getChildCount() { |
| 7696 | return mChildren.size(); |
| 7697 | } |
| 7698 | |
| 7699 | public View getChildAt(int index) { |
| 7700 | return mChildren.get(index); |
| 7701 | } |
| 7702 | |
| 7703 | public int getChildIndex(View child) { |
| 7704 | return mChildren.indexOf(child); |
| 7705 | } |
| 7706 | |
| 7707 | private void init(ViewGroup parent, boolean sort) { |
| 7708 | ArrayList<View> children = mChildren; |
| 7709 | final int childCount = parent.getChildCount(); |
| 7710 | for (int i = 0; i < childCount; i++) { |
| 7711 | View child = parent.getChildAt(i); |
| 7712 | children.add(child); |
| 7713 | } |
| 7714 | if (sort) { |
| 7715 | ArrayList<ViewLocationHolder> holders = mHolders; |
| 7716 | for (int i = 0; i < childCount; i++) { |
| 7717 | View child = children.get(i); |
| 7718 | ViewLocationHolder holder = ViewLocationHolder.obtain(parent, child); |
| 7719 | holders.add(holder); |
| 7720 | } |
Svetoslav | 88e447b | 2014-10-09 15:49:02 -0700 | [diff] [blame] | 7721 | sort(holders); |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 7722 | for (int i = 0; i < childCount; i++) { |
| 7723 | ViewLocationHolder holder = holders.get(i); |
| 7724 | children.set(i, holder.mView); |
| 7725 | holder.recycle(); |
| 7726 | } |
| 7727 | holders.clear(); |
| 7728 | } |
| 7729 | } |
| 7730 | |
Svetoslav | 88e447b | 2014-10-09 15:49:02 -0700 | [diff] [blame] | 7731 | private void sort(ArrayList<ViewLocationHolder> holders) { |
| 7732 | // This is gross but the least risky solution. The current comparison |
| 7733 | // strategy breaks transitivity but produces very good results. Coming |
| 7734 | // up with a new strategy requires time which we do not have, so ... |
| 7735 | try { |
| 7736 | ViewLocationHolder.setComparisonStrategy( |
| 7737 | ViewLocationHolder.COMPARISON_STRATEGY_STRIPE); |
| 7738 | Collections.sort(holders); |
| 7739 | } catch (IllegalArgumentException iae) { |
| 7740 | // Note that in practice this occurs extremely rarely in a couple |
| 7741 | // of pathological cases. |
| 7742 | ViewLocationHolder.setComparisonStrategy( |
| 7743 | ViewLocationHolder.COMPARISON_STRATEGY_LOCATION); |
| 7744 | Collections.sort(holders); |
| 7745 | } |
| 7746 | } |
| 7747 | |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 7748 | private void clear() { |
| 7749 | mChildren.clear(); |
| 7750 | } |
| 7751 | } |
| 7752 | |
| 7753 | /** |
| 7754 | * Pooled class that holds a View and its location with respect to |
| 7755 | * a specified root. This enables sorting of views based on their |
| 7756 | * coordinates without recomputing the position relative to the root |
| 7757 | * on every comparison. |
| 7758 | */ |
| 7759 | static class ViewLocationHolder implements Comparable<ViewLocationHolder> { |
| 7760 | |
| 7761 | private static final int MAX_POOL_SIZE = 32; |
| 7762 | |
Svetoslav Ganov | be922dc | 2012-11-30 16:46:26 -0800 | [diff] [blame] | 7763 | private static final SynchronizedPool<ViewLocationHolder> sPool = |
| 7764 | new SynchronizedPool<ViewLocationHolder>(MAX_POOL_SIZE); |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 7765 | |
Svetoslav | 88e447b | 2014-10-09 15:49:02 -0700 | [diff] [blame] | 7766 | public static final int COMPARISON_STRATEGY_STRIPE = 1; |
| 7767 | |
| 7768 | public static final int COMPARISON_STRATEGY_LOCATION = 2; |
| 7769 | |
| 7770 | private static int sComparisonStrategy = COMPARISON_STRATEGY_STRIPE; |
| 7771 | |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 7772 | private final Rect mLocation = new Rect(); |
| 7773 | |
| 7774 | public View mView; |
| 7775 | |
| 7776 | private int mLayoutDirection; |
| 7777 | |
| 7778 | public static ViewLocationHolder obtain(ViewGroup root, View view) { |
Svetoslav Ganov | be922dc | 2012-11-30 16:46:26 -0800 | [diff] [blame] | 7779 | ViewLocationHolder holder = sPool.acquire(); |
| 7780 | if (holder == null) { |
| 7781 | holder = new ViewLocationHolder(); |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 7782 | } |
Svetoslav Ganov | be922dc | 2012-11-30 16:46:26 -0800 | [diff] [blame] | 7783 | holder.init(root, view); |
| 7784 | return holder; |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 7785 | } |
| 7786 | |
Svetoslav | 88e447b | 2014-10-09 15:49:02 -0700 | [diff] [blame] | 7787 | public static void setComparisonStrategy(int strategy) { |
| 7788 | sComparisonStrategy = strategy; |
| 7789 | } |
| 7790 | |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 7791 | public void recycle() { |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 7792 | clear(); |
Svetoslav Ganov | be922dc | 2012-11-30 16:46:26 -0800 | [diff] [blame] | 7793 | sPool.release(this); |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 7794 | } |
| 7795 | |
| 7796 | @Override |
| 7797 | public int compareTo(ViewLocationHolder another) { |
| 7798 | // This instance is greater than an invalid argument. |
| 7799 | if (another == null) { |
| 7800 | return 1; |
| 7801 | } |
Svetoslav | 88e447b | 2014-10-09 15:49:02 -0700 | [diff] [blame] | 7802 | |
| 7803 | if (sComparisonStrategy == COMPARISON_STRATEGY_STRIPE) { |
| 7804 | // First is above second. |
| 7805 | if (mLocation.bottom - another.mLocation.top <= 0) { |
| 7806 | return -1; |
| 7807 | } |
| 7808 | // First is below second. |
| 7809 | if (mLocation.top - another.mLocation.bottom >= 0) { |
| 7810 | return 1; |
| 7811 | } |
| 7812 | } |
| 7813 | |
Svetoslav | 04cab1b | 2014-08-25 18:35:57 -0700 | [diff] [blame] | 7814 | // We are ordering left-to-right, top-to-bottom. |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 7815 | if (mLayoutDirection == LAYOUT_DIRECTION_LTR) { |
| 7816 | final int leftDifference = mLocation.left - another.mLocation.left; |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 7817 | if (leftDifference != 0) { |
| 7818 | return leftDifference; |
| 7819 | } |
| 7820 | } else { // RTL |
| 7821 | final int rightDifference = mLocation.right - another.mLocation.right; |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 7822 | if (rightDifference != 0) { |
| 7823 | return -rightDifference; |
| 7824 | } |
| 7825 | } |
Svetoslav | 04cab1b | 2014-08-25 18:35:57 -0700 | [diff] [blame] | 7826 | // We are ordering left-to-right, top-to-bottom. |
| 7827 | final int topDifference = mLocation.top - another.mLocation.top; |
| 7828 | if (topDifference != 0) { |
| 7829 | return topDifference; |
| 7830 | } |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 7831 | // Break tie by height. |
| 7832 | final int heightDiference = mLocation.height() - another.mLocation.height(); |
| 7833 | if (heightDiference != 0) { |
| 7834 | return -heightDiference; |
| 7835 | } |
| 7836 | // Break tie by width. |
| 7837 | final int widthDiference = mLocation.width() - another.mLocation.width(); |
| 7838 | if (widthDiference != 0) { |
| 7839 | return -widthDiference; |
| 7840 | } |
Svetoslav Ganov | 005b83b | 2012-04-16 18:17:17 -0700 | [diff] [blame] | 7841 | // Just break the tie somehow. The accessibliity ids are unique |
| 7842 | // and stable, hence this is deterministic tie breaking. |
| 7843 | return mView.getAccessibilityViewId() - another.mView.getAccessibilityViewId(); |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 7844 | } |
| 7845 | |
| 7846 | private void init(ViewGroup root, View view) { |
| 7847 | Rect viewLocation = mLocation; |
| 7848 | view.getDrawingRect(viewLocation); |
| 7849 | root.offsetDescendantRectToMyCoords(view, viewLocation); |
| 7850 | mView = view; |
Fabrice Di Meglio | e56ffdc | 2012-09-23 14:51:16 -0700 | [diff] [blame] | 7851 | mLayoutDirection = root.getLayoutDirection(); |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 7852 | } |
| 7853 | |
| 7854 | private void clear() { |
| 7855 | mView = null; |
| 7856 | mLocation.set(0, 0, 0, 0); |
| 7857 | } |
| 7858 | } |
Romain Guy | cbc6774 | 2012-04-27 16:12:57 -0700 | [diff] [blame] | 7859 | |
| 7860 | private static Paint getDebugPaint() { |
| 7861 | if (sDebugPaint == null) { |
| 7862 | sDebugPaint = new Paint(); |
| 7863 | sDebugPaint.setAntiAlias(false); |
| 7864 | } |
| 7865 | return sDebugPaint; |
| 7866 | } |
| 7867 | |
Romain Guy | 6410c0a | 2013-06-17 11:21:58 -0700 | [diff] [blame] | 7868 | 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] | 7869 | if (sDebugLines== null) { |
Romain Guy | 6410c0a | 2013-06-17 11:21:58 -0700 | [diff] [blame] | 7870 | // 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] | 7871 | sDebugLines = new float[16]; |
| 7872 | } |
| 7873 | |
Romain Guy | cbc6774 | 2012-04-27 16:12:57 -0700 | [diff] [blame] | 7874 | sDebugLines[0] = x1; |
| 7875 | sDebugLines[1] = y1; |
| 7876 | sDebugLines[2] = x2; |
| 7877 | sDebugLines[3] = y1; |
| 7878 | |
| 7879 | sDebugLines[4] = x2; |
| 7880 | sDebugLines[5] = y1; |
| 7881 | sDebugLines[6] = x2; |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 7882 | sDebugLines[7] = y2; |
Romain Guy | cbc6774 | 2012-04-27 16:12:57 -0700 | [diff] [blame] | 7883 | |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 7884 | sDebugLines[8] = x2; |
Romain Guy | cbc6774 | 2012-04-27 16:12:57 -0700 | [diff] [blame] | 7885 | sDebugLines[9] = y2; |
| 7886 | sDebugLines[10] = x1; |
| 7887 | sDebugLines[11] = y2; |
| 7888 | |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 7889 | sDebugLines[12] = x1; |
| 7890 | sDebugLines[13] = y2; |
Romain Guy | cbc6774 | 2012-04-27 16:12:57 -0700 | [diff] [blame] | 7891 | sDebugLines[14] = x1; |
| 7892 | sDebugLines[15] = y1; |
| 7893 | |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 7894 | canvas.drawLines(sDebugLines, paint); |
Romain Guy | cbc6774 | 2012-04-27 16:12:57 -0700 | [diff] [blame] | 7895 | } |
Siva Velusamy | 0d857b9 | 2015-04-22 10:23:56 -0700 | [diff] [blame] | 7896 | |
| 7897 | /** @hide */ |
| 7898 | @Override |
| 7899 | protected void encodeProperties(@NonNull ViewHierarchyEncoder encoder) { |
| 7900 | super.encodeProperties(encoder); |
| 7901 | |
| 7902 | encoder.addProperty("focus:descendantFocusability", getDescendantFocusability()); |
| 7903 | encoder.addProperty("drawing:clipChildren", getClipChildren()); |
| 7904 | encoder.addProperty("drawing:clipToPadding", getClipToPadding()); |
| 7905 | encoder.addProperty("drawing:childrenDrawingOrderEnabled", isChildrenDrawingOrderEnabled()); |
| 7906 | encoder.addProperty("drawing:persistentDrawingCache", getPersistentDrawingCache()); |
| 7907 | |
| 7908 | int n = getChildCount(); |
| 7909 | encoder.addProperty("meta:__childCount__", (short)n); |
| 7910 | for (int i = 0; i < n; i++) { |
| 7911 | encoder.addPropertyKey("meta:__child__" + i); |
| 7912 | getChildAt(i).encode(encoder); |
| 7913 | } |
| 7914 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7915 | } |