The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.view; |
| 18 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 19 | import android.animation.LayoutTransition; |
Tor Norbye | 7b9c912 | 2013-05-30 16:48:33 -0700 | [diff] [blame] | 20 | import android.annotation.IdRes; |
Alan Viverette | 922e1c6 | 2015-05-05 17:18:27 -0700 | [diff] [blame] | 21 | import android.annotation.NonNull; |
Tor Norbye | 83c6896 | 2015-03-10 20:55:31 -0700 | [diff] [blame] | 22 | import android.annotation.UiThread; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | import android.content.Context; |
Clara Bayarri | d5bf3ed | 2015-03-27 17:32:45 +0000 | [diff] [blame] | 24 | import android.content.Intent; |
Adam Powell | ff0d298 | 2014-07-10 20:34:14 -0700 | [diff] [blame] | 25 | import android.content.pm.PackageManager; |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 26 | import android.content.res.Configuration; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 27 | import android.content.res.TypedArray; |
| 28 | import android.graphics.Bitmap; |
| 29 | import android.graphics.Canvas; |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 30 | import android.graphics.Color; |
| 31 | import android.graphics.Insets; |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 32 | import android.graphics.Matrix; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | import android.graphics.Paint; |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 34 | import android.graphics.PointF; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | import android.graphics.Rect; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | import android.graphics.RectF; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 37 | import android.graphics.Region; |
Jeff Brown | 995e774 | 2010-12-22 16:59:36 -0800 | [diff] [blame] | 38 | import android.os.Build; |
Adam Powell | b6ab098 | 2015-01-07 17:00:12 -0800 | [diff] [blame] | 39 | import android.os.Bundle; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 40 | import android.os.Parcelable; |
| 41 | import android.os.SystemClock; |
| 42 | import android.util.AttributeSet; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 43 | import android.util.Log; |
Svetoslav Ganov | be922dc | 2012-11-30 16:46:26 -0800 | [diff] [blame] | 44 | import android.util.Pools.SynchronizedPool; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | import android.util.SparseArray; |
Dianne Hackborn | 6ff55fc | 2015-08-05 18:07:31 -0700 | [diff] [blame] | 46 | import android.util.SparseBooleanArray; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 47 | import android.view.accessibility.AccessibilityEvent; |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 48 | import android.view.accessibility.AccessibilityNodeInfo; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 49 | import android.view.animation.Animation; |
| 50 | import android.view.animation.AnimationUtils; |
| 51 | import android.view.animation.LayoutAnimationController; |
| 52 | import android.view.animation.Transformation; |
Doug Felt | cb379120 | 2011-07-07 11:57:48 -0700 | [diff] [blame] | 53 | |
Romain Guy | 0211a0a | 2011-02-14 16:34:59 -0800 | [diff] [blame] | 54 | import com.android.internal.R; |
| 55 | import com.android.internal.util.Predicate; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | |
| 57 | import java.util.ArrayList; |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 58 | import java.util.Collections; |
Christopher Tate | 86cab1b | 2011-01-13 20:28:55 -0800 | [diff] [blame] | 59 | import java.util.HashSet; |
George Mount | e180337 | 2014-02-26 19:00:52 +0000 | [diff] [blame] | 60 | import java.util.List; |
| 61 | import java.util.Map; |
Keisuke Kuroyanagi | d85bc50 | 2016-01-21 14:50:38 +0900 | [diff] [blame] | 62 | |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 63 | import static android.os.Build.VERSION_CODES.JELLY_BEAN_MR1; |
| 64 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | /** |
| 66 | * <p> |
| 67 | * A <code>ViewGroup</code> is a special view that can contain other views |
| 68 | * (called children.) The view group is the base class for layouts and views |
| 69 | * containers. This class also defines the |
| 70 | * {@link android.view.ViewGroup.LayoutParams} class which serves as the base |
| 71 | * class for layouts parameters. |
| 72 | * </p> |
| 73 | * |
| 74 | * <p> |
| 75 | * Also see {@link LayoutParams} for layout attributes. |
| 76 | * </p> |
Romain Guy | d6a463a | 2009-05-21 23:10:10 -0700 | [diff] [blame] | 77 | * |
Joe Fernandez | 558459f | 2011-10-13 16:47:36 -0700 | [diff] [blame] | 78 | * <div class="special reference"> |
| 79 | * <h3>Developer Guides</h3> |
| 80 | * <p>For more information about creating user interface layouts, read the |
| 81 | * <a href="{@docRoot}guide/topics/ui/declaring-layout.html">XML Layouts</a> developer |
| 82 | * guide.</p></div> |
| 83 | * |
Dianne Hackborn | 7caab0f | 2013-03-06 13:47:04 -0800 | [diff] [blame] | 84 | * <p>Here is a complete implementation of a custom ViewGroup that implements |
| 85 | * a simple {@link android.widget.FrameLayout} along with the ability to stack |
| 86 | * children in left and right gutters.</p> |
| 87 | * |
| 88 | * {@sample development/samples/ApiDemos/src/com/example/android/apis/view/CustomLayout.java |
| 89 | * Complete} |
| 90 | * |
| 91 | * <p>If you are implementing XML layout attributes as shown in the example, this is the |
| 92 | * corresponding definition for them that would go in <code>res/values/attrs.xml</code>:</p> |
| 93 | * |
| 94 | * {@sample development/samples/ApiDemos/res/values/attrs.xml CustomLayout} |
| 95 | * |
| 96 | * <p>Finally the layout manager can be used in an XML layout like so:</p> |
| 97 | * |
| 98 | * {@sample development/samples/ApiDemos/res/layout/custom_layout.xml Complete} |
| 99 | * |
Romain Guy | d6a463a | 2009-05-21 23:10:10 -0700 | [diff] [blame] | 100 | * @attr ref android.R.styleable#ViewGroup_clipChildren |
| 101 | * @attr ref android.R.styleable#ViewGroup_clipToPadding |
| 102 | * @attr ref android.R.styleable#ViewGroup_layoutAnimation |
| 103 | * @attr ref android.R.styleable#ViewGroup_animationCache |
| 104 | * @attr ref android.R.styleable#ViewGroup_persistentDrawingCache |
| 105 | * @attr ref android.R.styleable#ViewGroup_alwaysDrawnWithCache |
| 106 | * @attr ref android.R.styleable#ViewGroup_addStatesFromChildren |
| 107 | * @attr ref android.R.styleable#ViewGroup_descendantFocusability |
Chet Haase | 13cc120 | 2010-09-03 15:39:20 -0700 | [diff] [blame] | 108 | * @attr ref android.R.styleable#ViewGroup_animateLayoutChanges |
Scott Main | 27a8508 | 2013-06-10 10:39:48 -0700 | [diff] [blame] | 109 | * @attr ref android.R.styleable#ViewGroup_splitMotionEvents |
| 110 | * @attr ref android.R.styleable#ViewGroup_layoutMode |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 111 | */ |
Tor Norbye | 83c6896 | 2015-03-10 20:55:31 -0700 | [diff] [blame] | 112 | @UiThread |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 113 | public abstract class ViewGroup extends View implements ViewParent, ViewManager { |
Adam Powell | 539ee87 | 2012-02-03 19:00:49 -0800 | [diff] [blame] | 114 | private static final String TAG = "ViewGroup"; |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 115 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 116 | private static final boolean DBG = false; |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 117 | /** @hide */ |
| 118 | public static boolean DEBUG_DRAW = false; |
Gilles Debunne | cea4513 | 2011-11-24 02:19:27 +0100 | [diff] [blame] | 119 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 120 | /** |
| 121 | * Views which have been hidden or removed which need to be animated on |
| 122 | * their way out. |
| 123 | * This field should be made private, so it is hidden from the SDK. |
| 124 | * {@hide} |
| 125 | */ |
| 126 | protected ArrayList<View> mDisappearingChildren; |
| 127 | |
| 128 | /** |
| 129 | * Listener used to propagate events indicating when children are added |
| 130 | * and/or removed from a view group. |
| 131 | * This field should be made private, so it is hidden from the SDK. |
| 132 | * {@hide} |
| 133 | */ |
| 134 | protected OnHierarchyChangeListener mOnHierarchyChangeListener; |
| 135 | |
| 136 | // The view contained within this ViewGroup that has or contains focus. |
| 137 | private View mFocused; |
| 138 | |
Chet Haase | 4846032 | 2010-06-11 14:22:25 -0700 | [diff] [blame] | 139 | /** |
| 140 | * A Transformation used when drawing children, to |
| 141 | * apply on the child being drawn. |
| 142 | */ |
Romain Guy | f699130 | 2013-06-05 17:19:01 -0700 | [diff] [blame] | 143 | private Transformation mChildTransformation; |
Chet Haase | 4846032 | 2010-06-11 14:22:25 -0700 | [diff] [blame] | 144 | |
| 145 | /** |
| 146 | * Used to track the current invalidation region. |
| 147 | */ |
Chet Haase | 64a48c1 | 2012-02-13 16:33:29 -0800 | [diff] [blame] | 148 | RectF mInvalidateRegion; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 149 | |
Chet Haase | 4846032 | 2010-06-11 14:22:25 -0700 | [diff] [blame] | 150 | /** |
| 151 | * A Transformation used to calculate a correct |
| 152 | * invalidation area when the application is autoscaled. |
| 153 | */ |
Chet Haase | 64a48c1 | 2012-02-13 16:33:29 -0800 | [diff] [blame] | 154 | Transformation mInvalidationTransformation; |
Chet Haase | 4846032 | 2010-06-11 14:22:25 -0700 | [diff] [blame] | 155 | |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 156 | // View currently under an ongoing drag. Can be null, a child or this window. |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 157 | private View mCurrentDragView; |
| 158 | |
Christopher Tate | 86cab1b | 2011-01-13 20:28:55 -0800 | [diff] [blame] | 159 | // Metadata about the ongoing drag |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 160 | private DragEvent mCurrentDragStartEvent; |
| 161 | private boolean mIsInterestedInDrag; |
| 162 | private HashSet<View> mChildrenInterestedInDrag; |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 163 | |
| 164 | // Used during drag dispatch |
Romain Guy | 6410c0a | 2013-06-17 11:21:58 -0700 | [diff] [blame] | 165 | private PointF mLocalPoint; |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 166 | |
Alan Viverette | b942b6f | 2014-12-08 10:37:39 -0800 | [diff] [blame] | 167 | // Lazily-created holder for point computations. |
| 168 | private float[] mTempPoint; |
| 169 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 170 | // Layout animation |
| 171 | private LayoutAnimationController mLayoutAnimationController; |
| 172 | private Animation.AnimationListener mAnimationListener; |
| 173 | |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 174 | // First touch target in the linked list of touch targets. |
| 175 | private TouchTarget mFirstTouchTarget; |
| 176 | |
Joe Onorato | 03ab0c7 | 2011-01-06 15:46:27 -0800 | [diff] [blame] | 177 | // For debugging only. You can see these in hierarchyviewer. |
Romain Guy | e95003e | 2011-01-09 13:53:06 -0800 | [diff] [blame] | 178 | @SuppressWarnings({"FieldCanBeLocal", "UnusedDeclaration"}) |
Joe Onorato | 03ab0c7 | 2011-01-06 15:46:27 -0800 | [diff] [blame] | 179 | @ViewDebug.ExportedProperty(category = "events") |
| 180 | private long mLastTouchDownTime; |
| 181 | @ViewDebug.ExportedProperty(category = "events") |
| 182 | private int mLastTouchDownIndex = -1; |
Romain Guy | e95003e | 2011-01-09 13:53:06 -0800 | [diff] [blame] | 183 | @SuppressWarnings({"FieldCanBeLocal", "UnusedDeclaration"}) |
Joe Onorato | 03ab0c7 | 2011-01-06 15:46:27 -0800 | [diff] [blame] | 184 | @ViewDebug.ExportedProperty(category = "events") |
| 185 | private float mLastTouchDownX; |
Romain Guy | e95003e | 2011-01-09 13:53:06 -0800 | [diff] [blame] | 186 | @SuppressWarnings({"FieldCanBeLocal", "UnusedDeclaration"}) |
Joe Onorato | 03ab0c7 | 2011-01-06 15:46:27 -0800 | [diff] [blame] | 187 | @ViewDebug.ExportedProperty(category = "events") |
| 188 | private float mLastTouchDownY; |
| 189 | |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 190 | // First hover target in the linked list of hover targets. |
| 191 | // The hover targets are children which have received ACTION_HOVER_ENTER. |
| 192 | // They might not have actually handled the hover event, but we will |
| 193 | // continue sending hover events to them as long as the pointer remains over |
| 194 | // their bounds and the view group does not intercept hover. |
| 195 | private HoverTarget mFirstHoverTarget; |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 196 | |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 197 | // True if the view group itself received a hover event. |
| 198 | // It might not have actually handled the hover event. |
| 199 | private boolean mHoveredSelf; |
| 200 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 201 | /** |
| 202 | * Internal flags. |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 203 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 204 | * This field should be made private, so it is hidden from the SDK. |
| 205 | * {@hide} |
| 206 | */ |
Romain Guy | 2440e67 | 2012-08-07 14:43:43 -0700 | [diff] [blame] | 207 | @ViewDebug.ExportedProperty(flagMapping = { |
| 208 | @ViewDebug.FlagToString(mask = FLAG_CLIP_CHILDREN, equals = FLAG_CLIP_CHILDREN, |
| 209 | name = "CLIP_CHILDREN"), |
| 210 | @ViewDebug.FlagToString(mask = FLAG_CLIP_TO_PADDING, equals = FLAG_CLIP_TO_PADDING, |
| 211 | name = "CLIP_TO_PADDING"), |
| 212 | @ViewDebug.FlagToString(mask = FLAG_PADDING_NOT_NULL, equals = FLAG_PADDING_NOT_NULL, |
| 213 | name = "PADDING_NOT_NULL") |
Jon Miranda | 4597e98 | 2014-07-29 07:25:49 -0700 | [diff] [blame] | 214 | }, formatToHexString = true) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 215 | protected int mGroupFlags; |
| 216 | |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 217 | /** |
| 218 | * Either {@link #LAYOUT_MODE_CLIP_BOUNDS} or {@link #LAYOUT_MODE_OPTICAL_BOUNDS}. |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 219 | */ |
Philip Milne | cfb631b | 2012-10-26 10:51:46 -0700 | [diff] [blame] | 220 | private int mLayoutMode = LAYOUT_MODE_UNDEFINED; |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 221 | |
Romain Guy | 33f6beb | 2012-02-16 19:24:51 -0800 | [diff] [blame] | 222 | /** |
| 223 | * NOTE: If you change the flags below make sure to reflect the changes |
| 224 | * the DisplayList class |
| 225 | */ |
Filip Gruszczynski | a33bdf3 | 2015-11-19 18:22:16 -0800 | [diff] [blame] | 226 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 227 | // When set, ViewGroup invalidates only the child's rectangle |
| 228 | // Set by default |
Chet Haase | 64a48c1 | 2012-02-13 16:33:29 -0800 | [diff] [blame] | 229 | static final int FLAG_CLIP_CHILDREN = 0x1; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 230 | |
| 231 | // When set, ViewGroup excludes the padding area from the invalidate rectangle |
| 232 | // Set by default |
| 233 | private static final int FLAG_CLIP_TO_PADDING = 0x2; |
| 234 | |
| 235 | // When set, dispatchDraw() will invoke invalidate(); this is set by drawChild() when |
| 236 | // a child needs to be invalidated and FLAG_OPTIMIZE_INVALIDATE is set |
Chet Haase | 64a48c1 | 2012-02-13 16:33:29 -0800 | [diff] [blame] | 237 | static final int FLAG_INVALIDATE_REQUIRED = 0x4; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 238 | |
| 239 | // When set, dispatchDraw() will run the layout animation and unset the flag |
| 240 | private static final int FLAG_RUN_ANIMATION = 0x8; |
| 241 | |
| 242 | // When set, there is either no layout animation on the ViewGroup or the layout |
| 243 | // animation is over |
| 244 | // Set by default |
Chet Haase | 64a48c1 | 2012-02-13 16:33:29 -0800 | [diff] [blame] | 245 | static final int FLAG_ANIMATION_DONE = 0x10; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 246 | |
| 247 | // If set, this ViewGroup has padding; if unset there is no padding and we don't need |
| 248 | // to clip it, even if FLAG_CLIP_TO_PADDING is set |
| 249 | private static final int FLAG_PADDING_NOT_NULL = 0x20; |
| 250 | |
Chris Craik | 5a6bbae | 2015-04-10 17:41:34 -0700 | [diff] [blame] | 251 | /** @deprecated - functionality removed */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 252 | private static final int FLAG_ANIMATION_CACHE = 0x40; |
| 253 | |
| 254 | // When set, this ViewGroup converts calls to invalidate(Rect) to invalidate() during a |
| 255 | // layout animation; this avoid clobbering the hierarchy |
| 256 | // Automatically set when the layout animation starts, depending on the animation's |
| 257 | // characteristics |
Chet Haase | 64a48c1 | 2012-02-13 16:33:29 -0800 | [diff] [blame] | 258 | static final int FLAG_OPTIMIZE_INVALIDATE = 0x80; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 259 | |
| 260 | // When set, the next call to drawChild() will clear mChildTransformation's matrix |
Chet Haase | 64a48c1 | 2012-02-13 16:33:29 -0800 | [diff] [blame] | 261 | static final int FLAG_CLEAR_TRANSFORMATION = 0x100; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 262 | |
| 263 | // When set, this ViewGroup invokes mAnimationListener.onAnimationEnd() and removes |
| 264 | // the children's Bitmap caches if necessary |
| 265 | // This flag is set when the layout animation is over (after FLAG_ANIMATION_DONE is set) |
| 266 | private static final int FLAG_NOTIFY_ANIMATION_LISTENER = 0x200; |
| 267 | |
| 268 | /** |
| 269 | * When set, the drawing method will call {@link #getChildDrawingOrder(int, int)} |
| 270 | * to get the index of the child to draw for that iteration. |
Filip Gruszczynski | a33bdf3 | 2015-11-19 18:22:16 -0800 | [diff] [blame] | 271 | * |
Romain Guy | 293451e | 2009-11-04 13:59:48 -0800 | [diff] [blame] | 272 | * @hide |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 273 | */ |
| 274 | protected static final int FLAG_USE_CHILD_DRAWING_ORDER = 0x400; |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 275 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 276 | /** |
| 277 | * When set, this ViewGroup supports static transformations on children; this causes |
| 278 | * {@link #getChildStaticTransformation(View, android.view.animation.Transformation)} to be |
| 279 | * invoked when a child is drawn. |
| 280 | * |
| 281 | * Any subclass overriding |
| 282 | * {@link #getChildStaticTransformation(View, android.view.animation.Transformation)} should |
| 283 | * set this flags in {@link #mGroupFlags}. |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 284 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 285 | * {@hide} |
| 286 | */ |
| 287 | protected static final int FLAG_SUPPORT_STATIC_TRANSFORMATIONS = 0x800; |
| 288 | |
John Reck | fb5899d | 2014-08-15 18:51:27 -0700 | [diff] [blame] | 289 | // UNUSED FLAG VALUE: 0x1000; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 290 | |
| 291 | /** |
| 292 | * When set, this ViewGroup's drawable states also include those |
| 293 | * of its children. |
| 294 | */ |
| 295 | private static final int FLAG_ADD_STATES_FROM_CHILDREN = 0x2000; |
| 296 | |
Chris Craik | 5a6bbae | 2015-04-10 17:41:34 -0700 | [diff] [blame] | 297 | /** @deprecated functionality removed */ |
| 298 | private static final int FLAG_ALWAYS_DRAWN_WITH_CACHE = 0x4000; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 299 | |
Chris Craik | 5a6bbae | 2015-04-10 17:41:34 -0700 | [diff] [blame] | 300 | /** @deprecated functionality removed */ |
| 301 | private static final int FLAG_CHILDREN_DRAWN_WITH_CACHE = 0x8000; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 302 | |
| 303 | /** |
| 304 | * When set, this group will go through its list of children to notify them of |
| 305 | * any drawable state change. |
| 306 | */ |
| 307 | private static final int FLAG_NOTIFY_CHILDREN_ON_DRAWABLE_STATE_CHANGE = 0x10000; |
| 308 | |
| 309 | private static final int FLAG_MASK_FOCUSABILITY = 0x60000; |
| 310 | |
| 311 | /** |
| 312 | * This view will get focus before any of its descendants. |
| 313 | */ |
| 314 | public static final int FOCUS_BEFORE_DESCENDANTS = 0x20000; |
| 315 | |
| 316 | /** |
| 317 | * This view will get focus only if none of its descendants want it. |
| 318 | */ |
| 319 | public static final int FOCUS_AFTER_DESCENDANTS = 0x40000; |
| 320 | |
| 321 | /** |
| 322 | * This view will block any of its descendants from getting focus, even |
| 323 | * if they are focusable. |
| 324 | */ |
| 325 | public static final int FOCUS_BLOCK_DESCENDANTS = 0x60000; |
| 326 | |
| 327 | /** |
| 328 | * Used to map between enum in attrubutes and flag values. |
| 329 | */ |
| 330 | private static final int[] DESCENDANT_FOCUSABILITY_FLAGS = |
| 331 | {FOCUS_BEFORE_DESCENDANTS, FOCUS_AFTER_DESCENDANTS, |
| 332 | FOCUS_BLOCK_DESCENDANTS}; |
| 333 | |
| 334 | /** |
| 335 | * When set, this ViewGroup should not intercept touch events. |
Adam Powell | 110486f | 2010-06-22 17:14:44 -0700 | [diff] [blame] | 336 | * {@hide} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 337 | */ |
Adam Powell | 110486f | 2010-06-22 17:14:44 -0700 | [diff] [blame] | 338 | protected static final int FLAG_DISALLOW_INTERCEPT = 0x80000; |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 339 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 340 | /** |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 341 | * When set, this ViewGroup will split MotionEvents to multiple child Views when appropriate. |
| 342 | */ |
Adam Powell | f37df07 | 2010-09-17 16:22:49 -0700 | [diff] [blame] | 343 | private static final int FLAG_SPLIT_MOTION_EVENTS = 0x200000; |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 344 | |
| 345 | /** |
Adam Powell | 4b86788 | 2011-09-16 12:59:46 -0700 | [diff] [blame] | 346 | * When set, this ViewGroup will not dispatch onAttachedToWindow calls |
| 347 | * to children when adding new views. This is used to prevent multiple |
| 348 | * onAttached calls when a ViewGroup adds children in its own onAttached method. |
| 349 | */ |
| 350 | private static final int FLAG_PREVENT_DISPATCH_ATTACHED_TO_WINDOW = 0x400000; |
| 351 | |
| 352 | /** |
Philip Milne | cfb631b | 2012-10-26 10:51:46 -0700 | [diff] [blame] | 353 | * When true, indicates that a layoutMode has been explicitly set, either with |
| 354 | * an explicit call to {@link #setLayoutMode(int)} in code or from an XML resource. |
| 355 | * This distinguishes the situation in which a layout mode was inherited from |
| 356 | * one of the ViewGroup's ancestors and cached locally. |
| 357 | */ |
| 358 | private static final int FLAG_LAYOUT_MODE_WAS_EXPLICITLY_SET = 0x800000; |
| 359 | |
Chris Craik | b49f446 | 2014-03-20 12:44:20 -0700 | [diff] [blame] | 360 | static final int FLAG_IS_TRANSITION_GROUP = 0x1000000; |
Chris Craik | d863a10 | 2013-12-19 13:31:15 -0800 | [diff] [blame] | 361 | |
Chris Craik | b49f446 | 2014-03-20 12:44:20 -0700 | [diff] [blame] | 362 | static final int FLAG_IS_TRANSITION_GROUP_SET = 0x2000000; |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 363 | |
Chris Craik | d863a10 | 2013-12-19 13:31:15 -0800 | [diff] [blame] | 364 | /** |
Adam Powell | ff0d298 | 2014-07-10 20:34:14 -0700 | [diff] [blame] | 365 | * When set, focus will not be permitted to enter this group if a touchscreen is present. |
| 366 | */ |
| 367 | static final int FLAG_TOUCHSCREEN_BLOCKS_FOCUS = 0x4000000; |
| 368 | |
| 369 | /** |
Clara Bayarri | 4423d91 | 2015-03-02 19:42:48 +0000 | [diff] [blame] | 370 | * When true, indicates that a call to startActionModeForChild was made with the type parameter |
| 371 | * and should not be ignored. This helps in backwards compatibility with the existing method |
| 372 | * without a type. |
| 373 | * |
| 374 | * @see #startActionModeForChild(View, android.view.ActionMode.Callback) |
| 375 | * @see #startActionModeForChild(View, android.view.ActionMode.Callback, int) |
| 376 | */ |
| 377 | private static final int FLAG_START_ACTION_MODE_FOR_CHILD_IS_TYPED = 0x8000000; |
| 378 | |
| 379 | /** |
| 380 | * When true, indicates that a call to startActionModeForChild was made without the type |
| 381 | * parameter. This helps in backwards compatibility with the existing method |
| 382 | * without a type. |
| 383 | * |
| 384 | * @see #startActionModeForChild(View, android.view.ActionMode.Callback) |
| 385 | * @see #startActionModeForChild(View, android.view.ActionMode.Callback, int) |
| 386 | */ |
| 387 | private static final int FLAG_START_ACTION_MODE_FOR_CHILD_IS_NOT_TYPED = 0x10000000; |
| 388 | |
| 389 | /** |
Adam Powell | 2af189a | 2016-02-05 15:52:02 -0800 | [diff] [blame] | 390 | * When set, indicates that a call to showContextMenuForChild was made with explicit |
| 391 | * coordinates within the initiating child view. |
| 392 | */ |
| 393 | private static final int FLAG_SHOW_CONTEXT_MENU_WITH_COORDS = 0x20000000; |
| 394 | |
| 395 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 396 | * Indicates which types of drawing caches are to be kept in memory. |
| 397 | * This field should be made private, so it is hidden from the SDK. |
| 398 | * {@hide} |
| 399 | */ |
| 400 | protected int mPersistentDrawingCache; |
| 401 | |
| 402 | /** |
| 403 | * Used to indicate that no drawing cache should be kept in memory. |
| 404 | */ |
| 405 | public static final int PERSISTENT_NO_CACHE = 0x0; |
| 406 | |
| 407 | /** |
| 408 | * Used to indicate that the animation drawing cache should be kept in memory. |
| 409 | */ |
| 410 | public static final int PERSISTENT_ANIMATION_CACHE = 0x1; |
| 411 | |
| 412 | /** |
| 413 | * Used to indicate that the scrolling drawing cache should be kept in memory. |
| 414 | */ |
| 415 | public static final int PERSISTENT_SCROLLING_CACHE = 0x2; |
| 416 | |
| 417 | /** |
| 418 | * Used to indicate that all drawing caches should be kept in memory. |
| 419 | */ |
| 420 | public static final int PERSISTENT_ALL_CACHES = 0x3; |
| 421 | |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 422 | // Layout Modes |
| 423 | |
Philip Milne | cfb631b | 2012-10-26 10:51:46 -0700 | [diff] [blame] | 424 | private static final int LAYOUT_MODE_UNDEFINED = -1; |
| 425 | |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 426 | /** |
| 427 | * This constant is a {@link #setLayoutMode(int) layoutMode}. |
Philip Milne | 7a23b49 | 2012-04-24 22:12:36 -0700 | [diff] [blame] | 428 | * Clip bounds are the raw values of {@link #getLeft() left}, {@link #getTop() top}, |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 429 | * {@link #getRight() right} and {@link #getBottom() bottom}. |
| 430 | */ |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 431 | public static final int LAYOUT_MODE_CLIP_BOUNDS = 0; |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 432 | |
| 433 | /** |
| 434 | * This constant is a {@link #setLayoutMode(int) layoutMode}. |
Philip Milne | 7a23b49 | 2012-04-24 22:12:36 -0700 | [diff] [blame] | 435 | * Optical bounds describe where a widget appears to be. They sit inside the clip |
| 436 | * bounds which need to cover a larger area to allow other effects, |
| 437 | * such as shadows and glows, to be drawn. |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 438 | */ |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 439 | public static final int LAYOUT_MODE_OPTICAL_BOUNDS = 1; |
| 440 | |
| 441 | /** @hide */ |
Philip Milne | cfb631b | 2012-10-26 10:51:46 -0700 | [diff] [blame] | 442 | public static int LAYOUT_MODE_DEFAULT = LAYOUT_MODE_CLIP_BOUNDS; |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 443 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 444 | /** |
| 445 | * We clip to padding when FLAG_CLIP_TO_PADDING and FLAG_PADDING_NOT_NULL |
| 446 | * are set at the same time. |
| 447 | */ |
| 448 | protected static final int CLIP_TO_PADDING_MASK = FLAG_CLIP_TO_PADDING | FLAG_PADDING_NOT_NULL; |
| 449 | |
| 450 | // Index of the child's left position in the mLocation array |
| 451 | private static final int CHILD_LEFT_INDEX = 0; |
| 452 | // Index of the child's top position in the mLocation array |
| 453 | private static final int CHILD_TOP_INDEX = 1; |
| 454 | |
| 455 | // Child views of this ViewGroup |
| 456 | private View[] mChildren; |
| 457 | // Number of valid children in the mChildren array, the rest should be null or not |
| 458 | // considered as children |
| 459 | private int mChildrenCount; |
| 460 | |
Chet Haase | b989502 | 2013-04-02 15:10:58 -0700 | [diff] [blame] | 461 | // Whether layout calls are currently being suppressed, controlled by calls to |
| 462 | // suppressLayout() |
| 463 | boolean mSuppressLayout = false; |
| 464 | |
| 465 | // Whether any layout calls have actually been suppressed while mSuppressLayout |
| 466 | // has been true. This tracks whether we need to issue a requestLayout() when |
| 467 | // layout is later re-enabled. |
| 468 | private boolean mLayoutCalledWhileSuppressed = false; |
| 469 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 470 | private static final int ARRAY_INITIAL_CAPACITY = 12; |
| 471 | private static final int ARRAY_CAPACITY_INCREMENT = 12; |
| 472 | |
Romain Guy | cbc6774 | 2012-04-27 16:12:57 -0700 | [diff] [blame] | 473 | private static Paint sDebugPaint; |
| 474 | private static float[] sDebugLines; |
Philip Milne | 604f440 | 2012-04-24 19:27:11 -0700 | [diff] [blame] | 475 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 476 | // Used to draw cached views |
Chet Haase | 64a48c1 | 2012-02-13 16:33:29 -0800 | [diff] [blame] | 477 | Paint mCachePaint; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 478 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 479 | // Used to animate add/remove changes in layout |
| 480 | private LayoutTransition mTransition; |
| 481 | |
| 482 | // The set of views that are currently being transitioned. This list is used to track views |
| 483 | // being removed that should not actually be removed from the parent yet because they are |
| 484 | // being animated. |
| 485 | private ArrayList<View> mTransitioningViews; |
| 486 | |
Chet Haase | 5e25c2c | 2010-09-16 11:15:56 -0700 | [diff] [blame] | 487 | // List of children changing visibility. This is used to potentially keep rendering |
| 488 | // views during a transition when they otherwise would have become gone/invisible |
| 489 | private ArrayList<View> mVisibilityChangingChildren; |
| 490 | |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 491 | // Temporary holder of presorted children, only used for |
| 492 | // input/software draw dispatch for correctly Z ordering. |
| 493 | private ArrayList<View> mPreSortedChildren; |
| 494 | |
Adam Powell | 539ee87 | 2012-02-03 19:00:49 -0800 | [diff] [blame] | 495 | // Indicates how many of this container's child subtrees contain transient state |
| 496 | @ViewDebug.ExportedProperty(category = "layout") |
| 497 | private int mChildCountWithTransientState = 0; |
| 498 | |
Adam Powell | 10ba277 | 2014-04-15 09:46:51 -0700 | [diff] [blame] | 499 | /** |
| 500 | * Currently registered axes for nested scrolling. Flag set consisting of |
| 501 | * {@link #SCROLL_AXIS_HORIZONTAL} {@link #SCROLL_AXIS_VERTICAL} or {@link #SCROLL_AXIS_NONE} |
| 502 | * for null. |
| 503 | */ |
| 504 | private int mNestedScrollAxes; |
| 505 | |
Chet Haase | c633d2f | 2015-04-07 10:29:39 -0700 | [diff] [blame] | 506 | // Used to manage the list of transient views, added by addTransientView() |
| 507 | private List<Integer> mTransientIndices = null; |
| 508 | private List<View> mTransientViews = null; |
| 509 | |
| 510 | |
Clara Bayarri | 4423d91 | 2015-03-02 19:42:48 +0000 | [diff] [blame] | 511 | /** |
| 512 | * Empty ActionMode used as a sentinel in recursive entries to startActionModeForChild. |
| 513 | * |
| 514 | * @see #startActionModeForChild(View, android.view.ActionMode.Callback) |
| 515 | * @see #startActionModeForChild(View, android.view.ActionMode.Callback, int) |
| 516 | */ |
| 517 | private static final ActionMode SENTINEL_ACTION_MODE = new ActionMode() { |
| 518 | @Override |
| 519 | public void setTitle(CharSequence title) {} |
| 520 | |
| 521 | @Override |
| 522 | public void setTitle(int resId) {} |
| 523 | |
| 524 | @Override |
| 525 | public void setSubtitle(CharSequence subtitle) {} |
| 526 | |
| 527 | @Override |
| 528 | public void setSubtitle(int resId) {} |
| 529 | |
| 530 | @Override |
| 531 | public void setCustomView(View view) {} |
| 532 | |
| 533 | @Override |
| 534 | public void invalidate() {} |
| 535 | |
| 536 | @Override |
| 537 | public void finish() {} |
| 538 | |
| 539 | @Override |
| 540 | public Menu getMenu() { |
| 541 | return null; |
| 542 | } |
| 543 | |
| 544 | @Override |
| 545 | public CharSequence getTitle() { |
| 546 | return null; |
| 547 | } |
| 548 | |
| 549 | @Override |
| 550 | public CharSequence getSubtitle() { |
| 551 | return null; |
| 552 | } |
| 553 | |
| 554 | @Override |
| 555 | public View getCustomView() { |
| 556 | return null; |
| 557 | } |
| 558 | |
| 559 | @Override |
| 560 | public MenuInflater getMenuInflater() { |
| 561 | return null; |
| 562 | } |
| 563 | }; |
| 564 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 565 | public ViewGroup(Context context) { |
Alan Viverette | d6479ec | 2013-09-10 17:03:02 -0700 | [diff] [blame] | 566 | this(context, null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | public ViewGroup(Context context, AttributeSet attrs) { |
Alan Viverette | d6479ec | 2013-09-10 17:03:02 -0700 | [diff] [blame] | 570 | this(context, attrs, 0); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 571 | } |
| 572 | |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 573 | public ViewGroup(Context context, AttributeSet attrs, int defStyleAttr) { |
Alan Viverette | d6479ec | 2013-09-10 17:03:02 -0700 | [diff] [blame] | 574 | this(context, attrs, defStyleAttr, 0); |
Alan Viverette | 617feb9 | 2013-09-09 18:09:13 -0700 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | public ViewGroup(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
| 578 | super(context, attrs, defStyleAttr, defStyleRes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 579 | initViewGroup(); |
Alan Viverette | d6479ec | 2013-09-10 17:03:02 -0700 | [diff] [blame] | 580 | initFromAttributes(context, attrs, defStyleAttr, defStyleRes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 581 | } |
| 582 | |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 583 | private boolean debugDraw() { |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 584 | return DEBUG_DRAW || mAttachInfo != null && mAttachInfo.mDebugLayout; |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 585 | } |
| 586 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 587 | private void initViewGroup() { |
| 588 | // ViewGroup doesn't draw by default |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 589 | if (!debugDraw()) { |
| 590 | setFlags(WILL_NOT_DRAW, DRAW_MASK); |
| 591 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 592 | mGroupFlags |= FLAG_CLIP_CHILDREN; |
| 593 | mGroupFlags |= FLAG_CLIP_TO_PADDING; |
| 594 | mGroupFlags |= FLAG_ANIMATION_DONE; |
Chris Craik | f6ce8fd | 2015-05-11 15:33:11 -0700 | [diff] [blame] | 595 | mGroupFlags |= FLAG_ANIMATION_CACHE; |
| 596 | mGroupFlags |= FLAG_ALWAYS_DRAWN_WITH_CACHE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 597 | |
Jeff Brown | 995e774 | 2010-12-22 16:59:36 -0800 | [diff] [blame] | 598 | if (mContext.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.HONEYCOMB) { |
| 599 | mGroupFlags |= FLAG_SPLIT_MOTION_EVENTS; |
| 600 | } |
| 601 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 602 | setDescendantFocusability(FOCUS_BEFORE_DESCENDANTS); |
| 603 | |
| 604 | mChildren = new View[ARRAY_INITIAL_CAPACITY]; |
| 605 | mChildrenCount = 0; |
| 606 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 607 | mPersistentDrawingCache = PERSISTENT_SCROLLING_CACHE; |
| 608 | } |
| 609 | |
Alan Viverette | d6479ec | 2013-09-10 17:03:02 -0700 | [diff] [blame] | 610 | private void initFromAttributes( |
| 611 | Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { |
Filip Gruszczyński | b50cea0 | 2014-03-05 17:54:58 -0800 | [diff] [blame] | 612 | final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ViewGroup, defStyleAttr, |
| 613 | defStyleRes); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 614 | |
| 615 | final int N = a.getIndexCount(); |
| 616 | for (int i = 0; i < N; i++) { |
| 617 | int attr = a.getIndex(i); |
| 618 | switch (attr) { |
| 619 | case R.styleable.ViewGroup_clipChildren: |
| 620 | setClipChildren(a.getBoolean(attr, true)); |
| 621 | break; |
| 622 | case R.styleable.ViewGroup_clipToPadding: |
| 623 | setClipToPadding(a.getBoolean(attr, true)); |
| 624 | break; |
| 625 | case R.styleable.ViewGroup_animationCache: |
| 626 | setAnimationCacheEnabled(a.getBoolean(attr, true)); |
| 627 | break; |
| 628 | case R.styleable.ViewGroup_persistentDrawingCache: |
| 629 | setPersistentDrawingCache(a.getInt(attr, PERSISTENT_SCROLLING_CACHE)); |
| 630 | break; |
| 631 | case R.styleable.ViewGroup_addStatesFromChildren: |
| 632 | setAddStatesFromChildren(a.getBoolean(attr, false)); |
| 633 | break; |
| 634 | case R.styleable.ViewGroup_alwaysDrawnWithCache: |
| 635 | setAlwaysDrawnWithCacheEnabled(a.getBoolean(attr, true)); |
| 636 | break; |
| 637 | case R.styleable.ViewGroup_layoutAnimation: |
| 638 | int id = a.getResourceId(attr, -1); |
| 639 | if (id > 0) { |
| 640 | setLayoutAnimation(AnimationUtils.loadLayoutAnimation(mContext, id)); |
| 641 | } |
| 642 | break; |
| 643 | case R.styleable.ViewGroup_descendantFocusability: |
| 644 | setDescendantFocusability(DESCENDANT_FOCUSABILITY_FLAGS[a.getInt(attr, 0)]); |
| 645 | break; |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 646 | case R.styleable.ViewGroup_splitMotionEvents: |
| 647 | setMotionEventSplittingEnabled(a.getBoolean(attr, false)); |
| 648 | break; |
Chet Haase | 13cc120 | 2010-09-03 15:39:20 -0700 | [diff] [blame] | 649 | case R.styleable.ViewGroup_animateLayoutChanges: |
| 650 | boolean animateLayoutChanges = a.getBoolean(attr, false); |
| 651 | if (animateLayoutChanges) { |
| 652 | setLayoutTransition(new LayoutTransition()); |
| 653 | } |
| 654 | break; |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 655 | case R.styleable.ViewGroup_layoutMode: |
Philip Milne | cfb631b | 2012-10-26 10:51:46 -0700 | [diff] [blame] | 656 | setLayoutMode(a.getInt(attr, LAYOUT_MODE_UNDEFINED)); |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 657 | break; |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 658 | case R.styleable.ViewGroup_transitionGroup: |
| 659 | setTransitionGroup(a.getBoolean(attr, false)); |
| 660 | break; |
Adam Powell | ff0d298 | 2014-07-10 20:34:14 -0700 | [diff] [blame] | 661 | case R.styleable.ViewGroup_touchscreenBlocksFocus: |
| 662 | setTouchscreenBlocksFocus(a.getBoolean(attr, false)); |
| 663 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 664 | } |
| 665 | } |
| 666 | |
| 667 | a.recycle(); |
| 668 | } |
| 669 | |
| 670 | /** |
| 671 | * Gets the descendant focusability of this view group. The descendant |
| 672 | * focusability defines the relationship between this view group and its |
| 673 | * descendants when looking for a view to take focus in |
| 674 | * {@link #requestFocus(int, android.graphics.Rect)}. |
| 675 | * |
| 676 | * @return one of {@link #FOCUS_BEFORE_DESCENDANTS}, {@link #FOCUS_AFTER_DESCENDANTS}, |
| 677 | * {@link #FOCUS_BLOCK_DESCENDANTS}. |
| 678 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 679 | @ViewDebug.ExportedProperty(category = "focus", mapping = { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 680 | @ViewDebug.IntToString(from = FOCUS_BEFORE_DESCENDANTS, to = "FOCUS_BEFORE_DESCENDANTS"), |
| 681 | @ViewDebug.IntToString(from = FOCUS_AFTER_DESCENDANTS, to = "FOCUS_AFTER_DESCENDANTS"), |
| 682 | @ViewDebug.IntToString(from = FOCUS_BLOCK_DESCENDANTS, to = "FOCUS_BLOCK_DESCENDANTS") |
| 683 | }) |
| 684 | public int getDescendantFocusability() { |
| 685 | return mGroupFlags & FLAG_MASK_FOCUSABILITY; |
| 686 | } |
| 687 | |
| 688 | /** |
| 689 | * Set the descendant focusability of this view group. This defines the relationship |
| 690 | * between this view group and its descendants when looking for a view to |
| 691 | * take focus in {@link #requestFocus(int, android.graphics.Rect)}. |
| 692 | * |
| 693 | * @param focusability one of {@link #FOCUS_BEFORE_DESCENDANTS}, {@link #FOCUS_AFTER_DESCENDANTS}, |
| 694 | * {@link #FOCUS_BLOCK_DESCENDANTS}. |
| 695 | */ |
| 696 | public void setDescendantFocusability(int focusability) { |
| 697 | switch (focusability) { |
| 698 | case FOCUS_BEFORE_DESCENDANTS: |
| 699 | case FOCUS_AFTER_DESCENDANTS: |
| 700 | case FOCUS_BLOCK_DESCENDANTS: |
| 701 | break; |
| 702 | default: |
| 703 | throw new IllegalArgumentException("must be one of FOCUS_BEFORE_DESCENDANTS, " |
| 704 | + "FOCUS_AFTER_DESCENDANTS, FOCUS_BLOCK_DESCENDANTS"); |
| 705 | } |
| 706 | mGroupFlags &= ~FLAG_MASK_FOCUSABILITY; |
| 707 | mGroupFlags |= (focusability & FLAG_MASK_FOCUSABILITY); |
| 708 | } |
| 709 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 710 | @Override |
| 711 | void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) { |
| 712 | if (mFocused != null) { |
Alan Viverette | 223622a | 2013-12-17 13:29:02 -0800 | [diff] [blame] | 713 | mFocused.unFocus(this); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 714 | mFocused = null; |
| 715 | } |
| 716 | super.handleFocusGainInternal(direction, previouslyFocusedRect); |
| 717 | } |
| 718 | |
Alan Viverette | be463f2 | 2016-01-21 10:50:10 -0500 | [diff] [blame] | 719 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 720 | public void requestChildFocus(View child, View focused) { |
| 721 | if (DBG) { |
| 722 | System.out.println(this + " requestChildFocus()"); |
| 723 | } |
| 724 | if (getDescendantFocusability() == FOCUS_BLOCK_DESCENDANTS) { |
| 725 | return; |
| 726 | } |
| 727 | |
| 728 | // Unfocus us, if necessary |
Alan Viverette | 223622a | 2013-12-17 13:29:02 -0800 | [diff] [blame] | 729 | super.unFocus(focused); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 730 | |
| 731 | // We had a previous notion of who had focus. Clear it. |
| 732 | if (mFocused != child) { |
| 733 | if (mFocused != null) { |
Alan Viverette | 223622a | 2013-12-17 13:29:02 -0800 | [diff] [blame] | 734 | mFocused.unFocus(focused); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | mFocused = child; |
| 738 | } |
| 739 | if (mParent != null) { |
| 740 | mParent.requestChildFocus(this, focused); |
| 741 | } |
| 742 | } |
| 743 | |
Alan Viverette | be463f2 | 2016-01-21 10:50:10 -0500 | [diff] [blame] | 744 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 745 | public void focusableViewAvailable(View v) { |
| 746 | if (mParent != null |
| 747 | // shortcut: don't report a new focusable view if we block our descendants from |
| 748 | // getting focus |
| 749 | && (getDescendantFocusability() != FOCUS_BLOCK_DESCENDANTS) |
Adam Powell | 88c1175 | 2014-07-21 17:19:16 -0700 | [diff] [blame] | 750 | && (isFocusableInTouchMode() || !shouldBlockFocusForTouchscreen()) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 751 | // shortcut: don't report a new focusable view if we already are focused |
| 752 | // (and we don't prefer our descendants) |
| 753 | // |
| 754 | // note: knowing that mFocused is non-null is not a good enough reason |
| 755 | // to break the traversal since in that case we'd actually have to find |
| 756 | // the focused view and make sure it wasn't FOCUS_AFTER_DESCENDANTS and |
Joe Onorato | c6cc0f8 | 2011-04-12 11:53:13 -0700 | [diff] [blame] | 757 | // an ancestor of v; this will get checked for at ViewAncestor |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 758 | && !(isFocused() && getDescendantFocusability() != FOCUS_AFTER_DESCENDANTS)) { |
| 759 | mParent.focusableViewAvailable(v); |
| 760 | } |
| 761 | } |
| 762 | |
Alan Viverette | be463f2 | 2016-01-21 10:50:10 -0500 | [diff] [blame] | 763 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 764 | public boolean showContextMenuForChild(View originalView) { |
Adam Powell | 2af189a | 2016-02-05 15:52:02 -0800 | [diff] [blame] | 765 | if (isShowingContextMenuWithCoords()) { |
| 766 | // We're being called for compatibility. Return false and let the version |
| 767 | // with coordinates recurse up. |
| 768 | return false; |
| 769 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 770 | return mParent != null && mParent.showContextMenuForChild(originalView); |
| 771 | } |
| 772 | |
Adam Powell | 2af189a | 2016-02-05 15:52:02 -0800 | [diff] [blame] | 773 | /** |
| 774 | * @hide used internally for compatibility with existing app code only |
| 775 | */ |
| 776 | public final boolean isShowingContextMenuWithCoords() { |
| 777 | return (mGroupFlags & FLAG_SHOW_CONTEXT_MENU_WITH_COORDS) != 0; |
| 778 | } |
| 779 | |
Oren Blasberg | ed39126 | 2015-09-01 12:12:51 -0700 | [diff] [blame] | 780 | @Override |
| 781 | public boolean showContextMenuForChild(View originalView, float x, float y) { |
Adam Powell | 2af189a | 2016-02-05 15:52:02 -0800 | [diff] [blame] | 782 | try { |
| 783 | mGroupFlags |= FLAG_SHOW_CONTEXT_MENU_WITH_COORDS; |
| 784 | if (showContextMenuForChild(originalView)) { |
| 785 | return true; |
| 786 | } |
| 787 | } finally { |
| 788 | mGroupFlags &= ~FLAG_SHOW_CONTEXT_MENU_WITH_COORDS; |
| 789 | } |
Oren Blasberg | ed39126 | 2015-09-01 12:12:51 -0700 | [diff] [blame] | 790 | return mParent != null && mParent.showContextMenuForChild(originalView, x, y); |
| 791 | } |
| 792 | |
Clara Bayarri | 4423d91 | 2015-03-02 19:42:48 +0000 | [diff] [blame] | 793 | @Override |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 794 | public ActionMode startActionModeForChild(View originalView, ActionMode.Callback callback) { |
Clara Bayarri | 4423d91 | 2015-03-02 19:42:48 +0000 | [diff] [blame] | 795 | if ((mGroupFlags & FLAG_START_ACTION_MODE_FOR_CHILD_IS_TYPED) == 0) { |
| 796 | // This is the original call. |
| 797 | try { |
| 798 | mGroupFlags |= FLAG_START_ACTION_MODE_FOR_CHILD_IS_NOT_TYPED; |
| 799 | return startActionModeForChild(originalView, callback, ActionMode.TYPE_PRIMARY); |
| 800 | } finally { |
| 801 | mGroupFlags &= ~FLAG_START_ACTION_MODE_FOR_CHILD_IS_NOT_TYPED; |
| 802 | } |
| 803 | } else { |
| 804 | // We are being called from the new method with type. |
| 805 | return SENTINEL_ACTION_MODE; |
| 806 | } |
| 807 | } |
| 808 | |
Clara Bayarri | 4423d91 | 2015-03-02 19:42:48 +0000 | [diff] [blame] | 809 | @Override |
| 810 | public ActionMode startActionModeForChild( |
| 811 | View originalView, ActionMode.Callback callback, int type) { |
Adam Powell | e9fd6d2 | 2015-06-01 11:26:32 -0700 | [diff] [blame] | 812 | if ((mGroupFlags & FLAG_START_ACTION_MODE_FOR_CHILD_IS_NOT_TYPED) == 0 |
| 813 | && type == ActionMode.TYPE_PRIMARY) { |
Clara Bayarri | 4423d91 | 2015-03-02 19:42:48 +0000 | [diff] [blame] | 814 | ActionMode mode; |
| 815 | try { |
| 816 | mGroupFlags |= FLAG_START_ACTION_MODE_FOR_CHILD_IS_TYPED; |
| 817 | mode = startActionModeForChild(originalView, callback); |
| 818 | } finally { |
| 819 | mGroupFlags &= ~FLAG_START_ACTION_MODE_FOR_CHILD_IS_TYPED; |
| 820 | } |
| 821 | if (mode != SENTINEL_ACTION_MODE) { |
| 822 | return mode; |
| 823 | } |
| 824 | } |
| 825 | if (mParent != null) { |
| 826 | try { |
| 827 | return mParent.startActionModeForChild(originalView, callback, type); |
| 828 | } catch (AbstractMethodError ame) { |
| 829 | // Custom view parents might not implement this method. |
| 830 | return mParent.startActionModeForChild(originalView, callback); |
| 831 | } |
| 832 | } |
| 833 | return null; |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 834 | } |
| 835 | |
| 836 | /** |
Clara Bayarri | d5bf3ed | 2015-03-27 17:32:45 +0000 | [diff] [blame] | 837 | * @hide |
| 838 | */ |
| 839 | @Override |
| 840 | public boolean dispatchActivityResult( |
| 841 | String who, int requestCode, int resultCode, Intent data) { |
| 842 | if (super.dispatchActivityResult(who, requestCode, resultCode, data)) { |
| 843 | return true; |
| 844 | } |
| 845 | int childCount = getChildCount(); |
| 846 | for (int i = 0; i < childCount; i++) { |
| 847 | View child = getChildAt(i); |
| 848 | if (child.dispatchActivityResult(who, requestCode, resultCode, data)) { |
| 849 | return true; |
| 850 | } |
| 851 | } |
| 852 | return false; |
| 853 | } |
| 854 | |
| 855 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 856 | * Find the nearest view in the specified direction that wants to take |
| 857 | * focus. |
| 858 | * |
| 859 | * @param focused The view that currently has focus |
| 860 | * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and |
| 861 | * FOCUS_RIGHT, or 0 for not applicable. |
| 862 | */ |
Alan Viverette | be463f2 | 2016-01-21 10:50:10 -0500 | [diff] [blame] | 863 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 864 | public View focusSearch(View focused, int direction) { |
Svetoslav Ganov | 27e2da7 | 2012-07-02 18:12:00 -0700 | [diff] [blame] | 865 | if (isRootNamespace()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 866 | // root namespace means we should consider ourselves the top of the |
| 867 | // tree for focus searching; otherwise we could be focus searching |
| 868 | // into other tabs. see LocalActivityManager and TabHost for more info |
| 869 | return FocusFinder.getInstance().findNextFocus(this, focused, direction); |
| 870 | } else if (mParent != null) { |
| 871 | return mParent.focusSearch(focused, direction); |
| 872 | } |
| 873 | return null; |
| 874 | } |
| 875 | |
Alan Viverette | be463f2 | 2016-01-21 10:50:10 -0500 | [diff] [blame] | 876 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 877 | public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) { |
| 878 | return false; |
| 879 | } |
| 880 | |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 881 | @Override |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 882 | public boolean requestSendAccessibilityEvent(View child, AccessibilityEvent event) { |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 883 | ViewParent parent = mParent; |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 884 | if (parent == null) { |
| 885 | return false; |
| 886 | } |
| 887 | final boolean propagate = onRequestSendAccessibilityEvent(child, event); |
| 888 | if (!propagate) { |
| 889 | return false; |
| 890 | } |
| 891 | return parent.requestSendAccessibilityEvent(this, event); |
| 892 | } |
| 893 | |
| 894 | /** |
| 895 | * Called when a child has requested sending an {@link AccessibilityEvent} and |
| 896 | * gives an opportunity to its parent to augment the event. |
Svetoslav Ganov | 031d9c1 | 2011-09-09 16:41:13 -0700 | [diff] [blame] | 897 | * <p> |
Adam Powell | 2fcbbd0 | 2011-09-28 18:56:43 -0700 | [diff] [blame] | 898 | * If an {@link android.view.View.AccessibilityDelegate} has been specified via calling |
| 899 | * {@link android.view.View#setAccessibilityDelegate(android.view.View.AccessibilityDelegate)} its |
| 900 | * {@link android.view.View.AccessibilityDelegate#onRequestSendAccessibilityEvent(ViewGroup, View, AccessibilityEvent)} |
Svetoslav Ganov | 031d9c1 | 2011-09-09 16:41:13 -0700 | [diff] [blame] | 901 | * is responsible for handling this call. |
| 902 | * </p> |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 903 | * |
| 904 | * @param child The child which requests sending the event. |
| 905 | * @param event The event to be sent. |
| 906 | * @return True if the event should be sent. |
| 907 | * |
| 908 | * @see #requestSendAccessibilityEvent(View, AccessibilityEvent) |
| 909 | */ |
| 910 | public boolean onRequestSendAccessibilityEvent(View child, AccessibilityEvent event) { |
Svetoslav Ganov | 031d9c1 | 2011-09-09 16:41:13 -0700 | [diff] [blame] | 911 | if (mAccessibilityDelegate != null) { |
| 912 | return mAccessibilityDelegate.onRequestSendAccessibilityEvent(this, child, event); |
| 913 | } else { |
| 914 | return onRequestSendAccessibilityEventInternal(child, event); |
| 915 | } |
| 916 | } |
| 917 | |
| 918 | /** |
| 919 | * @see #onRequestSendAccessibilityEvent(View, AccessibilityEvent) |
| 920 | * |
| 921 | * Note: Called from the default {@link View.AccessibilityDelegate}. |
Alan Viverette | a54956a | 2015-01-07 16:05:02 -0800 | [diff] [blame] | 922 | * |
| 923 | * @hide |
Svetoslav Ganov | 031d9c1 | 2011-09-09 16:41:13 -0700 | [diff] [blame] | 924 | */ |
Alan Viverette | a54956a | 2015-01-07 16:05:02 -0800 | [diff] [blame] | 925 | public boolean onRequestSendAccessibilityEventInternal(View child, AccessibilityEvent event) { |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 926 | return true; |
| 927 | } |
| 928 | |
| 929 | /** |
Adam Powell | 539ee87 | 2012-02-03 19:00:49 -0800 | [diff] [blame] | 930 | * Called when a child view has changed whether or not it is tracking transient state. |
Adam Powell | 539ee87 | 2012-02-03 19:00:49 -0800 | [diff] [blame] | 931 | */ |
Alan Viverette | be463f2 | 2016-01-21 10:50:10 -0500 | [diff] [blame] | 932 | @Override |
Adam Powell | 539ee87 | 2012-02-03 19:00:49 -0800 | [diff] [blame] | 933 | public void childHasTransientStateChanged(View child, boolean childHasTransientState) { |
| 934 | final boolean oldHasTransientState = hasTransientState(); |
| 935 | if (childHasTransientState) { |
| 936 | mChildCountWithTransientState++; |
| 937 | } else { |
| 938 | mChildCountWithTransientState--; |
| 939 | } |
| 940 | |
| 941 | final boolean newHasTransientState = hasTransientState(); |
| 942 | if (mParent != null && oldHasTransientState != newHasTransientState) { |
| 943 | try { |
| 944 | mParent.childHasTransientStateChanged(this, newHasTransientState); |
| 945 | } catch (AbstractMethodError e) { |
| 946 | Log.e(TAG, mParent.getClass().getSimpleName() + |
| 947 | " does not fully implement ViewParent", e); |
| 948 | } |
| 949 | } |
| 950 | } |
| 951 | |
Adam Powell | 539ee87 | 2012-02-03 19:00:49 -0800 | [diff] [blame] | 952 | @Override |
| 953 | public boolean hasTransientState() { |
| 954 | return mChildCountWithTransientState > 0 || super.hasTransientState(); |
| 955 | } |
| 956 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 957 | @Override |
| 958 | public boolean dispatchUnhandledMove(View focused, int direction) { |
| 959 | return mFocused != null && |
| 960 | mFocused.dispatchUnhandledMove(focused, direction); |
| 961 | } |
| 962 | |
Alan Viverette | be463f2 | 2016-01-21 10:50:10 -0500 | [diff] [blame] | 963 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 964 | public void clearChildFocus(View child) { |
| 965 | if (DBG) { |
| 966 | System.out.println(this + " clearChildFocus()"); |
| 967 | } |
| 968 | |
| 969 | mFocused = null; |
| 970 | if (mParent != null) { |
| 971 | mParent.clearChildFocus(this); |
| 972 | } |
| 973 | } |
| 974 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 975 | @Override |
| 976 | public void clearFocus() { |
Svetoslav Ganov | b36a0ac | 2012-02-14 17:46:47 -0800 | [diff] [blame] | 977 | if (DBG) { |
| 978 | System.out.println(this + " clearFocus()"); |
| 979 | } |
| 980 | if (mFocused == null) { |
| 981 | super.clearFocus(); |
| 982 | } else { |
Svetoslav Ganov | b552d89 | 2012-06-02 14:35:02 -0700 | [diff] [blame] | 983 | View focused = mFocused; |
Svetoslav Ganov | b36a0ac | 2012-02-14 17:46:47 -0800 | [diff] [blame] | 984 | mFocused = null; |
Svetoslav Ganov | b552d89 | 2012-06-02 14:35:02 -0700 | [diff] [blame] | 985 | focused.clearFocus(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 986 | } |
| 987 | } |
| 988 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 989 | @Override |
Alan Viverette | 223622a | 2013-12-17 13:29:02 -0800 | [diff] [blame] | 990 | void unFocus(View focused) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 991 | if (DBG) { |
| 992 | System.out.println(this + " unFocus()"); |
| 993 | } |
Svetoslav Ganov | b36a0ac | 2012-02-14 17:46:47 -0800 | [diff] [blame] | 994 | if (mFocused == null) { |
Alan Viverette | 223622a | 2013-12-17 13:29:02 -0800 | [diff] [blame] | 995 | super.unFocus(focused); |
Svetoslav Ganov | b36a0ac | 2012-02-14 17:46:47 -0800 | [diff] [blame] | 996 | } else { |
Alan Viverette | 223622a | 2013-12-17 13:29:02 -0800 | [diff] [blame] | 997 | mFocused.unFocus(focused); |
Svetoslav Ganov | b36a0ac | 2012-02-14 17:46:47 -0800 | [diff] [blame] | 998 | mFocused = null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 999 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1000 | } |
| 1001 | |
| 1002 | /** |
| 1003 | * Returns the focused child of this view, if any. The child may have focus |
| 1004 | * or contain focus. |
| 1005 | * |
| 1006 | * @return the focused child or null. |
| 1007 | */ |
| 1008 | public View getFocusedChild() { |
| 1009 | return mFocused; |
| 1010 | } |
| 1011 | |
Adam Powell | 88c1175 | 2014-07-21 17:19:16 -0700 | [diff] [blame] | 1012 | View getDeepestFocusedChild() { |
| 1013 | View v = this; |
| 1014 | while (v != null) { |
| 1015 | if (v.isFocused()) { |
| 1016 | return v; |
| 1017 | } |
| 1018 | v = v instanceof ViewGroup ? ((ViewGroup) v).getFocusedChild() : null; |
| 1019 | } |
| 1020 | return null; |
| 1021 | } |
| 1022 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1023 | /** |
| 1024 | * Returns true if this view has or contains focus |
| 1025 | * |
| 1026 | * @return true if this view has or contains focus |
| 1027 | */ |
| 1028 | @Override |
| 1029 | public boolean hasFocus() { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 1030 | return (mPrivateFlags & PFLAG_FOCUSED) != 0 || mFocused != null; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1031 | } |
| 1032 | |
| 1033 | /* |
| 1034 | * (non-Javadoc) |
| 1035 | * |
| 1036 | * @see android.view.View#findFocus() |
| 1037 | */ |
| 1038 | @Override |
| 1039 | public View findFocus() { |
| 1040 | if (DBG) { |
| 1041 | System.out.println("Find focus in " + this + ": flags=" |
| 1042 | + isFocused() + ", child=" + mFocused); |
| 1043 | } |
| 1044 | |
| 1045 | if (isFocused()) { |
| 1046 | return this; |
| 1047 | } |
| 1048 | |
| 1049 | if (mFocused != null) { |
| 1050 | return mFocused.findFocus(); |
| 1051 | } |
| 1052 | return null; |
| 1053 | } |
| 1054 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1055 | @Override |
| 1056 | public boolean hasFocusable() { |
| 1057 | if ((mViewFlags & VISIBILITY_MASK) != VISIBLE) { |
| 1058 | return false; |
| 1059 | } |
| 1060 | |
| 1061 | if (isFocusable()) { |
| 1062 | return true; |
| 1063 | } |
| 1064 | |
| 1065 | final int descendantFocusability = getDescendantFocusability(); |
Adam Powell | 88c1175 | 2014-07-21 17:19:16 -0700 | [diff] [blame] | 1066 | if (descendantFocusability != FOCUS_BLOCK_DESCENDANTS) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1067 | final int count = mChildrenCount; |
| 1068 | final View[] children = mChildren; |
| 1069 | |
| 1070 | for (int i = 0; i < count; i++) { |
| 1071 | final View child = children[i]; |
| 1072 | if (child.hasFocusable()) { |
| 1073 | return true; |
| 1074 | } |
| 1075 | } |
| 1076 | } |
| 1077 | |
| 1078 | return false; |
| 1079 | } |
| 1080 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1081 | @Override |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 1082 | public void addFocusables(ArrayList<View> views, int direction, int focusableMode) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1083 | final int focusableCount = views.size(); |
| 1084 | |
| 1085 | final int descendantFocusability = getDescendantFocusability(); |
| 1086 | |
Adam Powell | 88c1175 | 2014-07-21 17:19:16 -0700 | [diff] [blame] | 1087 | if (descendantFocusability != FOCUS_BLOCK_DESCENDANTS) { |
| 1088 | if (shouldBlockFocusForTouchscreen()) { |
| 1089 | focusableMode |= FOCUSABLES_TOUCH_MODE; |
| 1090 | } |
| 1091 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1092 | final int count = mChildrenCount; |
| 1093 | final View[] children = mChildren; |
| 1094 | |
| 1095 | for (int i = 0; i < count; i++) { |
| 1096 | final View child = children[i]; |
| 1097 | if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) { |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 1098 | child.addFocusables(views, direction, focusableMode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1099 | } |
| 1100 | } |
| 1101 | } |
| 1102 | |
| 1103 | // we add ourselves (if focusable) in all cases except for when we are |
| 1104 | // FOCUS_AFTER_DESCENDANTS and there are some descendants focusable. this is |
| 1105 | // to avoid the focus search finding layouts when a more precise search |
| 1106 | // among the focusable children would be more interesting. |
Adam Powell | ff0d298 | 2014-07-10 20:34:14 -0700 | [diff] [blame] | 1107 | if ((descendantFocusability != FOCUS_AFTER_DESCENDANTS |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1108 | // No focusable descendants |
Adam Powell | 88c1175 | 2014-07-21 17:19:16 -0700 | [diff] [blame] | 1109 | || (focusableCount == views.size())) && |
| 1110 | (isFocusableInTouchMode() || !shouldBlockFocusForTouchscreen())) { |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 1111 | super.addFocusables(views, direction, focusableMode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1112 | } |
| 1113 | } |
| 1114 | |
Adam Powell | ff0d298 | 2014-07-10 20:34:14 -0700 | [diff] [blame] | 1115 | /** |
| 1116 | * Set whether this ViewGroup should ignore focus requests for itself and its children. |
| 1117 | * If this option is enabled and the ViewGroup or a descendant currently has focus, focus |
| 1118 | * will proceed forward. |
| 1119 | * |
| 1120 | * @param touchscreenBlocksFocus true to enable blocking focus in the presence of a touchscreen |
| 1121 | */ |
| 1122 | public void setTouchscreenBlocksFocus(boolean touchscreenBlocksFocus) { |
| 1123 | if (touchscreenBlocksFocus) { |
| 1124 | mGroupFlags |= FLAG_TOUCHSCREEN_BLOCKS_FOCUS; |
| 1125 | if (hasFocus()) { |
Adam Powell | 88c1175 | 2014-07-21 17:19:16 -0700 | [diff] [blame] | 1126 | final View focusedChild = getDeepestFocusedChild(); |
| 1127 | if (!focusedChild.isFocusableInTouchMode()) { |
| 1128 | final View newFocus = focusSearch(FOCUS_FORWARD); |
| 1129 | if (newFocus != null) { |
| 1130 | newFocus.requestFocus(); |
| 1131 | } |
Adam Powell | ff0d298 | 2014-07-10 20:34:14 -0700 | [diff] [blame] | 1132 | } |
| 1133 | } |
| 1134 | } else { |
| 1135 | mGroupFlags &= ~FLAG_TOUCHSCREEN_BLOCKS_FOCUS; |
| 1136 | } |
| 1137 | } |
| 1138 | |
| 1139 | /** |
| 1140 | * Check whether this ViewGroup should ignore focus requests for itself and its children. |
| 1141 | */ |
| 1142 | public boolean getTouchscreenBlocksFocus() { |
| 1143 | return (mGroupFlags & FLAG_TOUCHSCREEN_BLOCKS_FOCUS) != 0; |
| 1144 | } |
| 1145 | |
| 1146 | boolean shouldBlockFocusForTouchscreen() { |
| 1147 | return getTouchscreenBlocksFocus() && |
| 1148 | mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_TOUCHSCREEN); |
| 1149 | } |
| 1150 | |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 1151 | @Override |
Svetoslav Ganov | ea515ae | 2011-09-14 18:15:32 -0700 | [diff] [blame] | 1152 | public void findViewsWithText(ArrayList<View> outViews, CharSequence text, int flags) { |
| 1153 | super.findViewsWithText(outViews, text, flags); |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 1154 | final int childrenCount = mChildrenCount; |
| 1155 | final View[] children = mChildren; |
| 1156 | for (int i = 0; i < childrenCount; i++) { |
| 1157 | View child = children[i]; |
Svetoslav Ganov | ea515ae | 2011-09-14 18:15:32 -0700 | [diff] [blame] | 1158 | if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 1159 | && (child.mPrivateFlags & PFLAG_IS_ROOT_NAMESPACE) == 0) { |
Svetoslav Ganov | ea515ae | 2011-09-14 18:15:32 -0700 | [diff] [blame] | 1160 | child.findViewsWithText(outViews, text, flags); |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 1161 | } |
| 1162 | } |
| 1163 | } |
| 1164 | |
Svetoslav | 5b578da | 2013-05-08 14:23:32 -0700 | [diff] [blame] | 1165 | /** @hide */ |
Svetoslav Ganov | 2cdedff | 2011-10-03 14:18:42 -0700 | [diff] [blame] | 1166 | @Override |
Svetoslav | 5b578da | 2013-05-08 14:23:32 -0700 | [diff] [blame] | 1167 | public View findViewByAccessibilityIdTraversal(int accessibilityId) { |
Svetoslav Ganov | 2cdedff | 2011-10-03 14:18:42 -0700 | [diff] [blame] | 1168 | View foundView = super.findViewByAccessibilityIdTraversal(accessibilityId); |
| 1169 | if (foundView != null) { |
| 1170 | return foundView; |
| 1171 | } |
Svetoslav | 6cc4627 | 2015-06-03 11:38:30 -0700 | [diff] [blame] | 1172 | |
| 1173 | if (getAccessibilityNodeProvider() != null) { |
| 1174 | return null; |
| 1175 | } |
| 1176 | |
Svetoslav Ganov | 2cdedff | 2011-10-03 14:18:42 -0700 | [diff] [blame] | 1177 | final int childrenCount = mChildrenCount; |
| 1178 | final View[] children = mChildren; |
| 1179 | for (int i = 0; i < childrenCount; i++) { |
| 1180 | View child = children[i]; |
| 1181 | foundView = child.findViewByAccessibilityIdTraversal(accessibilityId); |
| 1182 | if (foundView != null) { |
| 1183 | return foundView; |
| 1184 | } |
| 1185 | } |
Svetoslav | 6cc4627 | 2015-06-03 11:38:30 -0700 | [diff] [blame] | 1186 | |
Svetoslav Ganov | 2cdedff | 2011-10-03 14:18:42 -0700 | [diff] [blame] | 1187 | return null; |
| 1188 | } |
| 1189 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1190 | @Override |
| 1191 | public void dispatchWindowFocusChanged(boolean hasFocus) { |
| 1192 | super.dispatchWindowFocusChanged(hasFocus); |
| 1193 | final int count = mChildrenCount; |
| 1194 | final View[] children = mChildren; |
| 1195 | for (int i = 0; i < count; i++) { |
| 1196 | children[i].dispatchWindowFocusChanged(hasFocus); |
| 1197 | } |
| 1198 | } |
| 1199 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1200 | @Override |
| 1201 | public void addTouchables(ArrayList<View> views) { |
| 1202 | super.addTouchables(views); |
| 1203 | |
| 1204 | final int count = mChildrenCount; |
| 1205 | final View[] children = mChildren; |
| 1206 | |
| 1207 | for (int i = 0; i < count; i++) { |
| 1208 | final View child = children[i]; |
| 1209 | if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) { |
| 1210 | child.addTouchables(views); |
| 1211 | } |
| 1212 | } |
| 1213 | } |
Dianne Hackborn | 3a3a6cf | 2012-03-26 10:24:04 -0700 | [diff] [blame] | 1214 | |
| 1215 | /** |
| 1216 | * @hide |
| 1217 | */ |
| 1218 | @Override |
| 1219 | public void makeOptionalFitsSystemWindows() { |
| 1220 | super.makeOptionalFitsSystemWindows(); |
| 1221 | final int count = mChildrenCount; |
| 1222 | final View[] children = mChildren; |
| 1223 | for (int i = 0; i < count; i++) { |
| 1224 | children[i].makeOptionalFitsSystemWindows(); |
| 1225 | } |
| 1226 | } |
| 1227 | |
Romain Guy | 43c9cdf | 2010-01-27 13:53:55 -0800 | [diff] [blame] | 1228 | @Override |
| 1229 | public void dispatchDisplayHint(int hint) { |
| 1230 | super.dispatchDisplayHint(hint); |
| 1231 | final int count = mChildrenCount; |
| 1232 | final View[] children = mChildren; |
| 1233 | for (int i = 0; i < count; i++) { |
| 1234 | children[i].dispatchDisplayHint(hint); |
| 1235 | } |
| 1236 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1237 | |
| 1238 | /** |
Chet Haase | 0d29936 | 2012-01-26 10:51:48 -0800 | [diff] [blame] | 1239 | * Called when a view's visibility has changed. Notify the parent to take any appropriate |
| 1240 | * action. |
| 1241 | * |
| 1242 | * @param child The view whose visibility has changed |
| 1243 | * @param oldVisibility The previous visibility value (GONE, INVISIBLE, or VISIBLE). |
| 1244 | * @param newVisibility The new visibility value (GONE, INVISIBLE, or VISIBLE). |
Chet Haase | 5e25c2c | 2010-09-16 11:15:56 -0700 | [diff] [blame] | 1245 | * @hide |
Chet Haase | 5e25c2c | 2010-09-16 11:15:56 -0700 | [diff] [blame] | 1246 | */ |
Chet Haase | 0d29936 | 2012-01-26 10:51:48 -0800 | [diff] [blame] | 1247 | protected void onChildVisibilityChanged(View child, int oldVisibility, int newVisibility) { |
Chet Haase | 5e25c2c | 2010-09-16 11:15:56 -0700 | [diff] [blame] | 1248 | if (mTransition != null) { |
Chet Haase | 0d29936 | 2012-01-26 10:51:48 -0800 | [diff] [blame] | 1249 | if (newVisibility == VISIBLE) { |
| 1250 | mTransition.showChild(this, child, oldVisibility); |
Chet Haase | 5e25c2c | 2010-09-16 11:15:56 -0700 | [diff] [blame] | 1251 | } else { |
Chet Haase | 0d29936 | 2012-01-26 10:51:48 -0800 | [diff] [blame] | 1252 | mTransition.hideChild(this, child, newVisibility); |
Chet Haase | 5e25c2c | 2010-09-16 11:15:56 -0700 | [diff] [blame] | 1253 | if (mTransitioningViews != null && mTransitioningViews.contains(child)) { |
Chet Haase | ddbb346 | 2012-06-19 13:54:29 -0700 | [diff] [blame] | 1254 | // Only track this on disappearing views - appearing views are already visible |
| 1255 | // and don't need special handling during drawChild() |
| 1256 | if (mVisibilityChangingChildren == null) { |
| 1257 | mVisibilityChangingChildren = new ArrayList<View>(); |
| 1258 | } |
| 1259 | mVisibilityChangingChildren.add(child); |
Chet Haase | 5e25c2c | 2010-09-16 11:15:56 -0700 | [diff] [blame] | 1260 | addDisappearingView(child); |
| 1261 | } |
| 1262 | } |
| 1263 | } |
Christopher Tate | 86cab1b | 2011-01-13 20:28:55 -0800 | [diff] [blame] | 1264 | |
| 1265 | // in all cases, for drags |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1266 | if (newVisibility == VISIBLE && mCurrentDragStartEvent != null) { |
| 1267 | if (!mChildrenInterestedInDrag.contains(child)) { |
| 1268 | notifyChildOfDragStart(child); |
Christopher Tate | 86cab1b | 2011-01-13 20:28:55 -0800 | [diff] [blame] | 1269 | } |
| 1270 | } |
Chet Haase | 5e25c2c | 2010-09-16 11:15:56 -0700 | [diff] [blame] | 1271 | } |
| 1272 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1273 | @Override |
Adam Powell | 326d808 | 2009-12-09 15:10:07 -0800 | [diff] [blame] | 1274 | protected void dispatchVisibilityChanged(View changedView, int visibility) { |
| 1275 | super.dispatchVisibilityChanged(changedView, visibility); |
| 1276 | final int count = mChildrenCount; |
| 1277 | final View[] children = mChildren; |
| 1278 | for (int i = 0; i < count; i++) { |
| 1279 | children[i].dispatchVisibilityChanged(changedView, visibility); |
| 1280 | } |
| 1281 | } |
| 1282 | |
Adam Powell | 326d808 | 2009-12-09 15:10:07 -0800 | [diff] [blame] | 1283 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1284 | public void dispatchWindowVisibilityChanged(int visibility) { |
| 1285 | super.dispatchWindowVisibilityChanged(visibility); |
| 1286 | final int count = mChildrenCount; |
| 1287 | final View[] children = mChildren; |
| 1288 | for (int i = 0; i < count; i++) { |
| 1289 | children[i].dispatchWindowVisibilityChanged(visibility); |
| 1290 | } |
| 1291 | } |
| 1292 | |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 1293 | @Override |
| 1294 | public void dispatchConfigurationChanged(Configuration newConfig) { |
| 1295 | super.dispatchConfigurationChanged(newConfig); |
| 1296 | final int count = mChildrenCount; |
| 1297 | final View[] children = mChildren; |
| 1298 | for (int i = 0; i < count; i++) { |
| 1299 | children[i].dispatchConfigurationChanged(newConfig); |
| 1300 | } |
| 1301 | } |
Filip Gruszczynski | a33bdf3 | 2015-11-19 18:22:16 -0800 | [diff] [blame] | 1302 | |
Alan Viverette | be463f2 | 2016-01-21 10:50:10 -0500 | [diff] [blame] | 1303 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1304 | public void recomputeViewAttributes(View child) { |
Joe Onorato | 664644d | 2011-01-23 17:53:23 -0800 | [diff] [blame] | 1305 | if (mAttachInfo != null && !mAttachInfo.mRecomputeGlobalAttributes) { |
| 1306 | ViewParent parent = mParent; |
| 1307 | if (parent != null) parent.recomputeViewAttributes(this); |
| 1308 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1309 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 1310 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1311 | @Override |
Dianne Hackborn | 3a3a6cf | 2012-03-26 10:24:04 -0700 | [diff] [blame] | 1312 | void dispatchCollectViewAttributes(AttachInfo attachInfo, int visibility) { |
| 1313 | if ((visibility & VISIBILITY_MASK) == VISIBLE) { |
| 1314 | super.dispatchCollectViewAttributes(attachInfo, visibility); |
| 1315 | final int count = mChildrenCount; |
| 1316 | final View[] children = mChildren; |
| 1317 | for (int i = 0; i < count; i++) { |
| 1318 | final View child = children[i]; |
| 1319 | child.dispatchCollectViewAttributes(attachInfo, |
| 1320 | visibility | (child.mViewFlags&VISIBILITY_MASK)); |
| 1321 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1322 | } |
| 1323 | } |
| 1324 | |
Alan Viverette | be463f2 | 2016-01-21 10:50:10 -0500 | [diff] [blame] | 1325 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1326 | public void bringChildToFront(View child) { |
Alan Viverette | 77bb6f1 | 2015-02-11 17:24:33 -0800 | [diff] [blame] | 1327 | final int index = indexOfChild(child); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1328 | if (index >= 0) { |
| 1329 | removeFromArray(index); |
| 1330 | addInArray(child, mChildrenCount); |
| 1331 | child.mParent = this; |
Chet Haase | cb96db8 | 2013-09-04 10:21:46 -0700 | [diff] [blame] | 1332 | requestLayout(); |
| 1333 | invalidate(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1334 | } |
| 1335 | } |
| 1336 | |
Romain Guy | 6410c0a | 2013-06-17 11:21:58 -0700 | [diff] [blame] | 1337 | private PointF getLocalPoint() { |
| 1338 | if (mLocalPoint == null) mLocalPoint = new PointF(); |
| 1339 | return mLocalPoint; |
| 1340 | } |
| 1341 | |
Steve Block | 8a7259b | 2012-03-01 11:24:41 +0000 | [diff] [blame] | 1342 | // TODO: Write real docs |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1343 | @Override |
| 1344 | public boolean dispatchDragEvent(DragEvent event) { |
| 1345 | boolean retval = false; |
| 1346 | final float tx = event.mX; |
| 1347 | final float ty = event.mY; |
| 1348 | |
Dianne Hackborn | 6dd005b | 2011-07-18 13:22:50 -0700 | [diff] [blame] | 1349 | ViewRootImpl root = getViewRootImpl(); |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1350 | |
| 1351 | // Dispatch down the view hierarchy |
Romain Guy | 6410c0a | 2013-06-17 11:21:58 -0700 | [diff] [blame] | 1352 | final PointF localPoint = getLocalPoint(); |
| 1353 | |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1354 | switch (event.mAction) { |
| 1355 | case DragEvent.ACTION_DRAG_STARTED: { |
| 1356 | // clear state to recalculate which views we drag over |
Chris Tate | 9d1ab88 | 2010-11-02 15:55:39 -0700 | [diff] [blame] | 1357 | mCurrentDragView = null; |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1358 | |
Christopher Tate | 86cab1b | 2011-01-13 20:28:55 -0800 | [diff] [blame] | 1359 | // Set up our tracking of drag-started notifications |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1360 | mCurrentDragStartEvent = DragEvent.obtain(event); |
| 1361 | if (mChildrenInterestedInDrag == null) { |
| 1362 | mChildrenInterestedInDrag = new HashSet<View>(); |
Christopher Tate | 86cab1b | 2011-01-13 20:28:55 -0800 | [diff] [blame] | 1363 | } else { |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1364 | mChildrenInterestedInDrag.clear(); |
Christopher Tate | 86cab1b | 2011-01-13 20:28:55 -0800 | [diff] [blame] | 1365 | } |
| 1366 | |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1367 | // Now dispatch down to our children, caching the responses |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1368 | final int count = mChildrenCount; |
| 1369 | final View[] children = mChildren; |
| 1370 | for (int i = 0; i < count; i++) { |
Christopher Tate | 2c095f3 | 2010-10-04 14:13:40 -0700 | [diff] [blame] | 1371 | final View child = children[i]; |
Christopher Tate | 3d4bf17 | 2011-03-28 16:16:46 -0700 | [diff] [blame] | 1372 | child.mPrivateFlags2 &= ~View.DRAG_MASK; |
Christopher Tate | 2c095f3 | 2010-10-04 14:13:40 -0700 | [diff] [blame] | 1373 | if (child.getVisibility() == VISIBLE) { |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1374 | if (notifyChildOfDragStart(children[i])) { |
| 1375 | retval = true; |
Christopher Tate | 2c095f3 | 2010-10-04 14:13:40 -0700 | [diff] [blame] | 1376 | } |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1377 | } |
| 1378 | } |
| 1379 | |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1380 | // Notify itself of the drag start. |
| 1381 | mIsInterestedInDrag = super.dispatchDragEvent(event); |
| 1382 | if (mIsInterestedInDrag) { |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1383 | retval = true; |
| 1384 | } |
Yorke Lee | 0394b21 | 2016-03-10 18:20:04 -0800 | [diff] [blame] | 1385 | |
| 1386 | if (!retval) { |
| 1387 | // Neither us nor any of our children are interested in this drag, so stop tracking |
| 1388 | // the current drag event. |
| 1389 | mCurrentDragStartEvent.recycle(); |
| 1390 | mCurrentDragStartEvent = null; |
| 1391 | } |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1392 | } break; |
| 1393 | |
| 1394 | case DragEvent.ACTION_DRAG_ENDED: { |
Christopher Tate | 86cab1b | 2011-01-13 20:28:55 -0800 | [diff] [blame] | 1395 | // Release the bookkeeping now that the drag lifecycle has ended |
Vadim Tryshev | 1517789 | 2015-12-18 08:01:00 -0800 | [diff] [blame] | 1396 | final HashSet<View> childrenInterestedInDrag = mChildrenInterestedInDrag; |
| 1397 | if (childrenInterestedInDrag != null) { |
| 1398 | for (View child : childrenInterestedInDrag) { |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1399 | // If a child was interested in the ongoing drag, it's told that it's over |
| 1400 | if (child.dispatchDragEvent(event)) { |
| 1401 | retval = true; |
| 1402 | } |
Christopher Tate | 3d4bf17 | 2011-03-28 16:16:46 -0700 | [diff] [blame] | 1403 | child.mPrivateFlags2 &= ~View.DRAG_MASK; |
| 1404 | child.refreshDrawableState(); |
Christopher Tate | 1fc014f | 2011-01-19 12:56:26 -0800 | [diff] [blame] | 1405 | } |
Vadim Tryshev | 1517789 | 2015-12-18 08:01:00 -0800 | [diff] [blame] | 1406 | childrenInterestedInDrag.clear(); |
| 1407 | } |
| 1408 | if (mCurrentDragStartEvent != null) { |
| 1409 | mCurrentDragStartEvent.recycle(); |
| 1410 | mCurrentDragStartEvent = null; |
Christopher Tate | 1fc014f | 2011-01-19 12:56:26 -0800 | [diff] [blame] | 1411 | } |
Christopher Tate | 86cab1b | 2011-01-13 20:28:55 -0800 | [diff] [blame] | 1412 | |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1413 | if (mIsInterestedInDrag) { |
| 1414 | if (super.dispatchDragEvent(event)) { |
| 1415 | retval = true; |
| 1416 | } |
| 1417 | mIsInterestedInDrag = false; |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1418 | } |
| 1419 | } break; |
| 1420 | |
| 1421 | case DragEvent.ACTION_DRAG_LOCATION: { |
| 1422 | // Find the [possibly new] drag target |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1423 | View target = findFrontmostDroppableChildAt(event.mX, event.mY, localPoint); |
| 1424 | if (target == null && mIsInterestedInDrag) { |
| 1425 | target = this; |
| 1426 | } |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1427 | |
| 1428 | // 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] | 1429 | // we're over now [for purposes of the eventual drag-recipient-changed |
| 1430 | // notifications to the framework] and tell the new target that the drag |
| 1431 | // has entered its bounds. The root will see setDragFocus() calls all |
| 1432 | // the way down to the final leaf view that is handling the LOCATION event |
| 1433 | // before reporting the new potential recipient to the framework. |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1434 | if (mCurrentDragView != target) { |
Chris Tate | 9d1ab88 | 2010-11-02 15:55:39 -0700 | [diff] [blame] | 1435 | root.setDragFocus(target); |
| 1436 | |
| 1437 | final int action = event.mAction; |
Vladislav Kaznacheev | cc010b2 | 2016-01-21 16:24:40 -0800 | [diff] [blame] | 1438 | // Position should not be available for ACTION_DRAG_ENTERED and ACTION_DRAG_EXITED. |
| 1439 | event.mX = 0; |
| 1440 | event.mY = 0; |
| 1441 | |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1442 | // 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] | 1443 | if (mCurrentDragView != null) { |
Christopher Tate | 3d4bf17 | 2011-03-28 16:16:46 -0700 | [diff] [blame] | 1444 | final View view = mCurrentDragView; |
Chris Tate | 9d1ab88 | 2010-11-02 15:55:39 -0700 | [diff] [blame] | 1445 | event.mAction = DragEvent.ACTION_DRAG_EXITED; |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1446 | if (view != this) { |
| 1447 | view.dispatchDragEvent(event); |
| 1448 | view.mPrivateFlags2 &= ~View.PFLAG2_DRAG_HOVERED; |
| 1449 | view.refreshDrawableState(); |
| 1450 | } else { |
| 1451 | super.dispatchDragEvent(event); |
| 1452 | } |
Chris Tate | 9d1ab88 | 2010-11-02 15:55:39 -0700 | [diff] [blame] | 1453 | } |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1454 | |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1455 | mCurrentDragView = target; |
Chris Tate | 9d1ab88 | 2010-11-02 15:55:39 -0700 | [diff] [blame] | 1456 | |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1457 | // If we've dragged over a new child view, send it the ENTERED message, otherwise |
| 1458 | // send it to this window. |
Chris Tate | 9d1ab88 | 2010-11-02 15:55:39 -0700 | [diff] [blame] | 1459 | if (target != null) { |
| 1460 | event.mAction = DragEvent.ACTION_DRAG_ENTERED; |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1461 | if (target != this) { |
| 1462 | target.dispatchDragEvent(event); |
| 1463 | target.mPrivateFlags2 |= View.PFLAG2_DRAG_HOVERED; |
| 1464 | target.refreshDrawableState(); |
| 1465 | } else { |
| 1466 | super.dispatchDragEvent(event); |
| 1467 | } |
Chris Tate | 9d1ab88 | 2010-11-02 15:55:39 -0700 | [diff] [blame] | 1468 | } |
| 1469 | event.mAction = action; // restore the event's original state |
Vladislav Kaznacheev | cc010b2 | 2016-01-21 16:24:40 -0800 | [diff] [blame] | 1470 | event.mX = tx; |
| 1471 | event.mY = ty; |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1472 | } |
Christopher Tate | 2c095f3 | 2010-10-04 14:13:40 -0700 | [diff] [blame] | 1473 | |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1474 | // Dispatch the actual drag location notice, localized into its coordinates |
| 1475 | if (target != null) { |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1476 | if (target != this) { |
| 1477 | event.mX = localPoint.x; |
| 1478 | event.mY = localPoint.y; |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1479 | |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1480 | retval = target.dispatchDragEvent(event); |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1481 | |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1482 | event.mX = tx; |
| 1483 | event.mY = ty; |
| 1484 | } else { |
| 1485 | retval = super.dispatchDragEvent(event); |
| 1486 | } |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1487 | } |
| 1488 | } break; |
| 1489 | |
Chris Tate | 9d1ab88 | 2010-11-02 15:55:39 -0700 | [diff] [blame] | 1490 | /* Entered / exited dispatch |
| 1491 | * |
| 1492 | * DRAG_ENTERED is not dispatched downwards from ViewGroup. The reason for this is |
| 1493 | * that we're about to get the corresponding LOCATION event, which we will use to |
| 1494 | * determine which of our children is the new target; at that point we will |
| 1495 | * 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] | 1496 | * If no suitable child is detected, dispatch to this window. |
Chris Tate | 9d1ab88 | 2010-11-02 15:55:39 -0700 | [diff] [blame] | 1497 | * |
| 1498 | * DRAG_EXITED *is* dispatched all the way down immediately: once we know the |
| 1499 | * drag has left this ViewGroup, we know by definition that every contained subview |
| 1500 | * is also no longer under the drag point. |
| 1501 | */ |
| 1502 | |
| 1503 | case DragEvent.ACTION_DRAG_EXITED: { |
| 1504 | if (mCurrentDragView != null) { |
Christopher Tate | 3d4bf17 | 2011-03-28 16:16:46 -0700 | [diff] [blame] | 1505 | final View view = mCurrentDragView; |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1506 | if (view != this) { |
| 1507 | view.dispatchDragEvent(event); |
| 1508 | view.mPrivateFlags2 &= ~View.PFLAG2_DRAG_HOVERED; |
| 1509 | view.refreshDrawableState(); |
| 1510 | } else { |
| 1511 | super.dispatchDragEvent(event); |
| 1512 | } |
Christopher Tate | 3d4bf17 | 2011-03-28 16:16:46 -0700 | [diff] [blame] | 1513 | |
Chris Tate | 9d1ab88 | 2010-11-02 15:55:39 -0700 | [diff] [blame] | 1514 | mCurrentDragView = null; |
| 1515 | } |
| 1516 | } break; |
| 1517 | |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1518 | case DragEvent.ACTION_DROP: { |
Christopher Tate | 2c095f3 | 2010-10-04 14:13:40 -0700 | [diff] [blame] | 1519 | 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] | 1520 | View target = findFrontmostDroppableChildAt(event.mX, event.mY, localPoint); |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1521 | if (target != null) { |
Christopher Tate | 5ada6cb | 2010-10-05 14:15:29 -0700 | [diff] [blame] | 1522 | 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] | 1523 | event.mX = localPoint.x; |
| 1524 | event.mY = localPoint.y; |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1525 | retval = target.dispatchDragEvent(event); |
| 1526 | event.mX = tx; |
| 1527 | event.mY = ty; |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1528 | } else if (mIsInterestedInDrag) { |
| 1529 | retval = super.dispatchDragEvent(event); |
Christopher Tate | 5ada6cb | 2010-10-05 14:15:29 -0700 | [diff] [blame] | 1530 | } else { |
| 1531 | if (ViewDebug.DEBUG_DRAG) { |
| 1532 | Log.d(View.VIEW_LOG_TAG, " not dropped on an accepting view"); |
| 1533 | } |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1534 | } |
| 1535 | } break; |
| 1536 | } |
| 1537 | |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1538 | return retval; |
| 1539 | } |
| 1540 | |
| 1541 | // Find the frontmost child view that lies under the given point, and calculate |
| 1542 | // the position within its own local coordinate system. |
| 1543 | View findFrontmostDroppableChildAt(float x, float y, PointF outLocalPoint) { |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1544 | final int count = mChildrenCount; |
| 1545 | final View[] children = mChildren; |
| 1546 | for (int i = count - 1; i >= 0; i--) { |
| 1547 | final View child = children[i]; |
Christopher Tate | 3d4bf17 | 2011-03-28 16:16:46 -0700 | [diff] [blame] | 1548 | if (!child.canAcceptDrag()) { |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1549 | continue; |
| 1550 | } |
| 1551 | |
Christopher Tate | 2c095f3 | 2010-10-04 14:13:40 -0700 | [diff] [blame] | 1552 | if (isTransformedTouchPointInView(x, y, child, outLocalPoint)) { |
Christopher Tate | a53146c | 2010-09-07 11:57:52 -0700 | [diff] [blame] | 1553 | return child; |
| 1554 | } |
| 1555 | } |
| 1556 | return null; |
| 1557 | } |
| 1558 | |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1559 | boolean notifyChildOfDragStart(View child) { |
| 1560 | // The caller guarantees that the child is not in mChildrenInterestedInDrag yet. |
| 1561 | |
Christopher Tate | 86cab1b | 2011-01-13 20:28:55 -0800 | [diff] [blame] | 1562 | if (ViewDebug.DEBUG_DRAG) { |
| 1563 | Log.d(View.VIEW_LOG_TAG, "Sending drag-started to view: " + child); |
| 1564 | } |
| 1565 | |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 1566 | final boolean canAccept = child.dispatchDragEvent(mCurrentDragStartEvent); |
| 1567 | if (canAccept) { |
| 1568 | mChildrenInterestedInDrag.add(child); |
| 1569 | if (!child.canAcceptDrag()) { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 1570 | child.mPrivateFlags2 |= View.PFLAG2_DRAG_CAN_ACCEPT; |
Christopher Tate | 3d4bf17 | 2011-03-28 16:16:46 -0700 | [diff] [blame] | 1571 | child.refreshDrawableState(); |
| 1572 | } |
Christopher Tate | 86cab1b | 2011-01-13 20:28:55 -0800 | [diff] [blame] | 1573 | } |
Christopher Tate | 3d4bf17 | 2011-03-28 16:16:46 -0700 | [diff] [blame] | 1574 | return canAccept; |
Christopher Tate | 86cab1b | 2011-01-13 20:28:55 -0800 | [diff] [blame] | 1575 | } |
| 1576 | |
Joe Onorato | 664644d | 2011-01-23 17:53:23 -0800 | [diff] [blame] | 1577 | @Override |
Dianne Hackborn | 3a3a6cf | 2012-03-26 10:24:04 -0700 | [diff] [blame] | 1578 | public void dispatchWindowSystemUiVisiblityChanged(int visible) { |
| 1579 | super.dispatchWindowSystemUiVisiblityChanged(visible); |
| 1580 | |
| 1581 | final int count = mChildrenCount; |
| 1582 | final View[] children = mChildren; |
| 1583 | for (int i=0; i <count; i++) { |
| 1584 | final View child = children[i]; |
| 1585 | child.dispatchWindowSystemUiVisiblityChanged(visible); |
| 1586 | } |
| 1587 | } |
| 1588 | |
| 1589 | @Override |
Joe Onorato | 664644d | 2011-01-23 17:53:23 -0800 | [diff] [blame] | 1590 | public void dispatchSystemUiVisibilityChanged(int visible) { |
| 1591 | super.dispatchSystemUiVisibilityChanged(visible); |
| 1592 | |
| 1593 | final int count = mChildrenCount; |
| 1594 | final View[] children = mChildren; |
| 1595 | for (int i=0; i <count; i++) { |
| 1596 | final View child = children[i]; |
| 1597 | child.dispatchSystemUiVisibilityChanged(visible); |
| 1598 | } |
| 1599 | } |
| 1600 | |
Dianne Hackborn | 9a230e0 | 2011-10-06 11:51:27 -0700 | [diff] [blame] | 1601 | @Override |
Dianne Hackborn | cf67578 | 2012-05-10 15:07:24 -0700 | [diff] [blame] | 1602 | boolean updateLocalSystemUiVisibility(int localValue, int localChanges) { |
| 1603 | boolean changed = super.updateLocalSystemUiVisibility(localValue, localChanges); |
Dianne Hackborn | 9a230e0 | 2011-10-06 11:51:27 -0700 | [diff] [blame] | 1604 | |
| 1605 | final int count = mChildrenCount; |
| 1606 | final View[] children = mChildren; |
| 1607 | for (int i=0; i <count; i++) { |
| 1608 | final View child = children[i]; |
Dianne Hackborn | cf67578 | 2012-05-10 15:07:24 -0700 | [diff] [blame] | 1609 | changed |= child.updateLocalSystemUiVisibility(localValue, localChanges); |
Dianne Hackborn | 9a230e0 | 2011-10-06 11:51:27 -0700 | [diff] [blame] | 1610 | } |
Dianne Hackborn | cf67578 | 2012-05-10 15:07:24 -0700 | [diff] [blame] | 1611 | return changed; |
Dianne Hackborn | 9a230e0 | 2011-10-06 11:51:27 -0700 | [diff] [blame] | 1612 | } |
| 1613 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1614 | @Override |
| 1615 | public boolean dispatchKeyEventPreIme(KeyEvent event) { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 1616 | if ((mPrivateFlags & (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) |
| 1617 | == (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1618 | return super.dispatchKeyEventPreIme(event); |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 1619 | } else if (mFocused != null && (mFocused.mPrivateFlags & PFLAG_HAS_BOUNDS) |
| 1620 | == PFLAG_HAS_BOUNDS) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1621 | return mFocused.dispatchKeyEventPreIme(event); |
| 1622 | } |
| 1623 | return false; |
| 1624 | } |
| 1625 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1626 | @Override |
| 1627 | public boolean dispatchKeyEvent(KeyEvent event) { |
Jeff Brown | 21bc5c9 | 2011-02-28 18:27:14 -0800 | [diff] [blame] | 1628 | if (mInputEventConsistencyVerifier != null) { |
| 1629 | mInputEventConsistencyVerifier.onKeyEvent(event, 1); |
| 1630 | } |
| 1631 | |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 1632 | if ((mPrivateFlags & (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) |
| 1633 | == (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) { |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 1634 | if (super.dispatchKeyEvent(event)) { |
| 1635 | return true; |
| 1636 | } |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 1637 | } else if (mFocused != null && (mFocused.mPrivateFlags & PFLAG_HAS_BOUNDS) |
| 1638 | == PFLAG_HAS_BOUNDS) { |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 1639 | if (mFocused.dispatchKeyEvent(event)) { |
| 1640 | return true; |
| 1641 | } |
| 1642 | } |
| 1643 | |
| 1644 | if (mInputEventConsistencyVerifier != null) { |
| 1645 | mInputEventConsistencyVerifier.onUnhandledEvent(event, 1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1646 | } |
| 1647 | return false; |
| 1648 | } |
| 1649 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1650 | @Override |
| 1651 | public boolean dispatchKeyShortcutEvent(KeyEvent event) { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 1652 | if ((mPrivateFlags & (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) |
| 1653 | == (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1654 | return super.dispatchKeyShortcutEvent(event); |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 1655 | } else if (mFocused != null && (mFocused.mPrivateFlags & PFLAG_HAS_BOUNDS) |
| 1656 | == PFLAG_HAS_BOUNDS) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1657 | return mFocused.dispatchKeyShortcutEvent(event); |
| 1658 | } |
| 1659 | return false; |
| 1660 | } |
| 1661 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1662 | @Override |
| 1663 | public boolean dispatchTrackballEvent(MotionEvent event) { |
Jeff Brown | 21bc5c9 | 2011-02-28 18:27:14 -0800 | [diff] [blame] | 1664 | if (mInputEventConsistencyVerifier != null) { |
| 1665 | mInputEventConsistencyVerifier.onTrackballEvent(event, 1); |
| 1666 | } |
| 1667 | |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 1668 | if ((mPrivateFlags & (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) |
| 1669 | == (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) { |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 1670 | if (super.dispatchTrackballEvent(event)) { |
| 1671 | return true; |
| 1672 | } |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 1673 | } else if (mFocused != null && (mFocused.mPrivateFlags & PFLAG_HAS_BOUNDS) |
| 1674 | == PFLAG_HAS_BOUNDS) { |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 1675 | if (mFocused.dispatchTrackballEvent(event)) { |
| 1676 | return true; |
| 1677 | } |
| 1678 | } |
| 1679 | |
| 1680 | if (mInputEventConsistencyVerifier != null) { |
| 1681 | mInputEventConsistencyVerifier.onUnhandledEvent(event, 1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1682 | } |
| 1683 | return false; |
| 1684 | } |
| 1685 | |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 1686 | @Override |
Jun Mukai | d4eaef7 | 2015-10-30 15:54:33 -0700 | [diff] [blame] | 1687 | public PointerIcon getPointerIcon(MotionEvent event, float x, float y) { |
Keisuke Kuroyanagi | d85bc50 | 2016-01-21 14:50:38 +0900 | [diff] [blame] | 1688 | if (isOnScrollbarThumb(x, y) || isDraggingScrollBar()) { |
| 1689 | return PointerIcon.getSystemIcon(mContext, PointerIcon.STYLE_ARROW); |
| 1690 | } |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 1691 | // Check what the child under the pointer says about the pointer. |
| 1692 | final int childrenCount = mChildrenCount; |
| 1693 | if (childrenCount != 0) { |
| 1694 | final ArrayList<View> preorderedList = buildOrderedChildList(); |
| 1695 | final boolean customOrder = preorderedList == null |
| 1696 | && isChildrenDrawingOrderEnabled(); |
| 1697 | final View[] children = mChildren; |
| 1698 | for (int i = childrenCount - 1; i >= 0; i--) { |
Alan Viverette | a7b85e6 | 2016-01-22 10:14:02 -0500 | [diff] [blame] | 1699 | final int childIndex = getAndVerifyPreorderedIndex(childrenCount, i, customOrder); |
| 1700 | final View child = getAndVerifyPreorderedView(preorderedList, children, childIndex); |
| 1701 | final PointF point = getLocalPoint(); |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 1702 | if (isTransformedTouchPointInView(x, y, child, point)) { |
Jun Mukai | d4eaef7 | 2015-10-30 15:54:33 -0700 | [diff] [blame] | 1703 | final PointerIcon pointerIcon = child.getPointerIcon(event, point.x, point.y); |
| 1704 | if (pointerIcon != null) { |
| 1705 | return pointerIcon; |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 1706 | } |
| 1707 | break; |
| 1708 | } |
| 1709 | } |
| 1710 | } |
| 1711 | |
| 1712 | // The pointer is not a child or the child has no preferences, returning the default |
| 1713 | // implementation. |
Jun Mukai | d4eaef7 | 2015-10-30 15:54:33 -0700 | [diff] [blame] | 1714 | return super.getPointerIcon(event, x, y); |
Jun Mukai | 1db5397 | 2015-09-11 18:08:31 -0700 | [diff] [blame] | 1715 | } |
| 1716 | |
Alan Viverette | a7b85e6 | 2016-01-22 10:14:02 -0500 | [diff] [blame] | 1717 | private int getAndVerifyPreorderedIndex(int childrenCount, int i, boolean customOrder) { |
| 1718 | final int childIndex; |
| 1719 | if (customOrder) { |
| 1720 | final int childIndex1 = getChildDrawingOrder(childrenCount, i); |
| 1721 | if (childIndex1 >= childrenCount) { |
| 1722 | throw new IndexOutOfBoundsException("getChildDrawingOrder() " |
| 1723 | + "returned invalid index " + childIndex1 |
| 1724 | + " (child count is " + childrenCount + ")"); |
| 1725 | } |
| 1726 | childIndex = childIndex1; |
| 1727 | } else { |
| 1728 | childIndex = i; |
| 1729 | } |
| 1730 | return childIndex; |
| 1731 | } |
| 1732 | |
Romain Guy | a948927 | 2011-06-22 20:58:11 -0700 | [diff] [blame] | 1733 | @SuppressWarnings({"ConstantConditions"}) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1734 | @Override |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 1735 | protected boolean dispatchHoverEvent(MotionEvent event) { |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 1736 | final int action = event.getAction(); |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 1737 | |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 1738 | // First check whether the view group wants to intercept the hover event. |
| 1739 | final boolean interceptHover = onInterceptHoverEvent(event); |
| 1740 | event.setAction(action); // restore action in case it was changed |
| 1741 | |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 1742 | MotionEvent eventNoHistory = event; |
| 1743 | boolean handled = false; |
| 1744 | |
| 1745 | // Send events to the hovered children and build a new list of hover targets until |
| 1746 | // one is found that handles the event. |
| 1747 | HoverTarget firstOldHoverTarget = mFirstHoverTarget; |
| 1748 | mFirstHoverTarget = null; |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 1749 | if (!interceptHover && action != MotionEvent.ACTION_HOVER_EXIT) { |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 1750 | final float x = event.getX(); |
| 1751 | final float y = event.getY(); |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 1752 | final int childrenCount = mChildrenCount; |
| 1753 | if (childrenCount != 0) { |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 1754 | final ArrayList<View> preorderedList = buildOrderedChildList(); |
| 1755 | final boolean customOrder = preorderedList == null |
| 1756 | && isChildrenDrawingOrderEnabled(); |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 1757 | final View[] children = mChildren; |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 1758 | HoverTarget lastHoverTarget = null; |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 1759 | for (int i = childrenCount - 1; i >= 0; i--) { |
Alan Viverette | a7b85e6 | 2016-01-22 10:14:02 -0500 | [diff] [blame] | 1760 | final int childIndex = getAndVerifyPreorderedIndex( |
| 1761 | childrenCount, i, customOrder); |
| 1762 | final View child = getAndVerifyPreorderedView( |
| 1763 | preorderedList, children, childIndex); |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 1764 | if (!canViewReceivePointerEvents(child) |
| 1765 | || !isTransformedTouchPointInView(x, y, child, null)) { |
| 1766 | continue; |
| 1767 | } |
| 1768 | |
| 1769 | // Obtain a hover target for this child. Dequeue it from the |
| 1770 | // old hover target list if the child was previously hovered. |
| 1771 | HoverTarget hoverTarget = firstOldHoverTarget; |
| 1772 | final boolean wasHovered; |
| 1773 | for (HoverTarget predecessor = null; ;) { |
| 1774 | if (hoverTarget == null) { |
| 1775 | hoverTarget = HoverTarget.obtain(child); |
| 1776 | wasHovered = false; |
| 1777 | break; |
| 1778 | } |
| 1779 | |
| 1780 | if (hoverTarget.child == child) { |
| 1781 | if (predecessor != null) { |
| 1782 | predecessor.next = hoverTarget.next; |
| 1783 | } else { |
| 1784 | firstOldHoverTarget = hoverTarget.next; |
| 1785 | } |
| 1786 | hoverTarget.next = null; |
| 1787 | wasHovered = true; |
| 1788 | break; |
| 1789 | } |
| 1790 | |
| 1791 | predecessor = hoverTarget; |
| 1792 | hoverTarget = hoverTarget.next; |
| 1793 | } |
| 1794 | |
| 1795 | // Enqueue the hover target onto the new hover target list. |
| 1796 | if (lastHoverTarget != null) { |
| 1797 | lastHoverTarget.next = hoverTarget; |
| 1798 | } else { |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 1799 | mFirstHoverTarget = hoverTarget; |
| 1800 | } |
Sangkyu Lee | 8725f36 | 2013-03-13 09:38:45 +0900 | [diff] [blame] | 1801 | lastHoverTarget = hoverTarget; |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 1802 | |
| 1803 | // Dispatch the event to the child. |
| 1804 | if (action == MotionEvent.ACTION_HOVER_ENTER) { |
| 1805 | if (!wasHovered) { |
| 1806 | // Send the enter as is. |
| 1807 | handled |= dispatchTransformedGenericPointerEvent( |
| 1808 | event, child); // enter |
| 1809 | } |
| 1810 | } else if (action == MotionEvent.ACTION_HOVER_MOVE) { |
| 1811 | if (!wasHovered) { |
| 1812 | // Synthesize an enter from a move. |
| 1813 | eventNoHistory = obtainMotionEventNoHistoryOrSelf(eventNoHistory); |
| 1814 | eventNoHistory.setAction(MotionEvent.ACTION_HOVER_ENTER); |
| 1815 | handled |= dispatchTransformedGenericPointerEvent( |
| 1816 | eventNoHistory, child); // enter |
| 1817 | eventNoHistory.setAction(action); |
| 1818 | |
| 1819 | handled |= dispatchTransformedGenericPointerEvent( |
| 1820 | eventNoHistory, child); // move |
| 1821 | } else { |
| 1822 | // Send the move as is. |
| 1823 | handled |= dispatchTransformedGenericPointerEvent(event, child); |
| 1824 | } |
| 1825 | } |
| 1826 | if (handled) { |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 1827 | break; |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 1828 | } |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 1829 | } |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 1830 | if (preorderedList != null) preorderedList.clear(); |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 1831 | } |
| 1832 | } |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 1833 | |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 1834 | // Send exit events to all previously hovered children that are no longer hovered. |
| 1835 | while (firstOldHoverTarget != null) { |
| 1836 | final View child = firstOldHoverTarget.child; |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 1837 | |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 1838 | // Exit the old hovered child. |
| 1839 | if (action == MotionEvent.ACTION_HOVER_EXIT) { |
| 1840 | // Send the exit as is. |
| 1841 | handled |= dispatchTransformedGenericPointerEvent( |
| 1842 | event, child); // exit |
| 1843 | } else { |
| 1844 | // Synthesize an exit from a move or enter. |
| 1845 | // Ignore the result because hover focus has moved to a different view. |
| 1846 | if (action == MotionEvent.ACTION_HOVER_MOVE) { |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 1847 | dispatchTransformedGenericPointerEvent( |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 1848 | event, child); // move |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 1849 | } |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 1850 | eventNoHistory = obtainMotionEventNoHistoryOrSelf(eventNoHistory); |
| 1851 | eventNoHistory.setAction(MotionEvent.ACTION_HOVER_EXIT); |
| 1852 | dispatchTransformedGenericPointerEvent( |
| 1853 | eventNoHistory, child); // exit |
| 1854 | eventNoHistory.setAction(action); |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 1855 | } |
| 1856 | |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 1857 | final HoverTarget nextOldHoverTarget = firstOldHoverTarget.next; |
| 1858 | firstOldHoverTarget.recycle(); |
| 1859 | firstOldHoverTarget = nextOldHoverTarget; |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 1860 | } |
| 1861 | |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 1862 | // 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] | 1863 | boolean newHoveredSelf = !handled; |
| 1864 | if (newHoveredSelf == mHoveredSelf) { |
| 1865 | if (newHoveredSelf) { |
| 1866 | // Send event to the view group as before. |
| 1867 | handled |= super.dispatchHoverEvent(event); |
| 1868 | } |
| 1869 | } else { |
| 1870 | if (mHoveredSelf) { |
| 1871 | // Exit the view group. |
| 1872 | if (action == MotionEvent.ACTION_HOVER_EXIT) { |
| 1873 | // Send the exit as is. |
| 1874 | handled |= super.dispatchHoverEvent(event); // exit |
| 1875 | } else { |
| 1876 | // Synthesize an exit from a move or enter. |
| 1877 | // Ignore the result because hover focus is moving to a different view. |
| 1878 | if (action == MotionEvent.ACTION_HOVER_MOVE) { |
| 1879 | super.dispatchHoverEvent(event); // move |
| 1880 | } |
| 1881 | eventNoHistory = obtainMotionEventNoHistoryOrSelf(eventNoHistory); |
| 1882 | eventNoHistory.setAction(MotionEvent.ACTION_HOVER_EXIT); |
| 1883 | super.dispatchHoverEvent(eventNoHistory); // exit |
| 1884 | eventNoHistory.setAction(action); |
| 1885 | } |
| 1886 | mHoveredSelf = false; |
| 1887 | } |
| 1888 | |
| 1889 | if (newHoveredSelf) { |
| 1890 | // Enter the view group. |
| 1891 | if (action == MotionEvent.ACTION_HOVER_ENTER) { |
| 1892 | // Send the enter as is. |
| 1893 | handled |= super.dispatchHoverEvent(event); // enter |
| 1894 | mHoveredSelf = true; |
| 1895 | } else if (action == MotionEvent.ACTION_HOVER_MOVE) { |
| 1896 | // Synthesize an enter from a move. |
| 1897 | eventNoHistory = obtainMotionEventNoHistoryOrSelf(eventNoHistory); |
| 1898 | eventNoHistory.setAction(MotionEvent.ACTION_HOVER_ENTER); |
| 1899 | handled |= super.dispatchHoverEvent(eventNoHistory); // enter |
| 1900 | eventNoHistory.setAction(action); |
| 1901 | |
| 1902 | handled |= super.dispatchHoverEvent(eventNoHistory); // move |
| 1903 | mHoveredSelf = true; |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 1904 | } |
| 1905 | } |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 1906 | } |
| 1907 | |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 1908 | // Recycle the copy of the event that we made. |
| 1909 | if (eventNoHistory != event) { |
| 1910 | eventNoHistory.recycle(); |
| 1911 | } |
| 1912 | |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 1913 | // Done. |
| 1914 | return handled; |
| 1915 | } |
| 1916 | |
Jeff Brown | 59a422e | 2012-04-19 15:19:19 -0700 | [diff] [blame] | 1917 | private void exitHoverTargets() { |
| 1918 | if (mHoveredSelf || mFirstHoverTarget != null) { |
| 1919 | final long now = SystemClock.uptimeMillis(); |
| 1920 | MotionEvent event = MotionEvent.obtain(now, now, |
| 1921 | MotionEvent.ACTION_HOVER_EXIT, 0.0f, 0.0f, 0); |
| 1922 | event.setSource(InputDevice.SOURCE_TOUCHSCREEN); |
| 1923 | dispatchHoverEvent(event); |
| 1924 | event.recycle(); |
| 1925 | } |
| 1926 | } |
| 1927 | |
| 1928 | private void cancelHoverTarget(View view) { |
| 1929 | HoverTarget predecessor = null; |
| 1930 | HoverTarget target = mFirstHoverTarget; |
| 1931 | while (target != null) { |
| 1932 | final HoverTarget next = target.next; |
| 1933 | if (target.child == view) { |
| 1934 | if (predecessor == null) { |
| 1935 | mFirstHoverTarget = next; |
| 1936 | } else { |
| 1937 | predecessor.next = next; |
| 1938 | } |
| 1939 | target.recycle(); |
| 1940 | |
| 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 | view.dispatchHoverEvent(event); |
| 1946 | event.recycle(); |
| 1947 | return; |
| 1948 | } |
| 1949 | predecessor = target; |
| 1950 | target = next; |
| 1951 | } |
| 1952 | } |
| 1953 | |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 1954 | /** @hide */ |
| 1955 | @Override |
| 1956 | protected boolean hasHoveredChild() { |
| 1957 | return mFirstHoverTarget != null; |
| 1958 | } |
| 1959 | |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 1960 | @Override |
Adam Powell | cf392d1 | 2015-06-25 14:48:45 -0700 | [diff] [blame] | 1961 | public void addChildrenForAccessibility(ArrayList<View> outChildren) { |
Svetoslav | 762621c | 2015-06-01 17:21:59 -0700 | [diff] [blame] | 1962 | if (getAccessibilityNodeProvider() != null) { |
| 1963 | return; |
| 1964 | } |
Svetoslav Ganov | 76f287e | 2012-04-23 11:02:36 -0700 | [diff] [blame] | 1965 | ChildListForAccessibility children = ChildListForAccessibility.obtain(this, true); |
| 1966 | try { |
| 1967 | final int childrenCount = children.getChildCount(); |
| 1968 | for (int i = 0; i < childrenCount; i++) { |
| 1969 | View child = children.getChildAt(i); |
Svetoslav Ganov | c406be9 | 2012-05-11 16:12:32 -0700 | [diff] [blame] | 1970 | if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) { |
Svetoslav Ganov | 76f287e | 2012-04-23 11:02:36 -0700 | [diff] [blame] | 1971 | if (child.includeForAccessibility()) { |
Adam Powell | cf392d1 | 2015-06-25 14:48:45 -0700 | [diff] [blame] | 1972 | outChildren.add(child); |
Svetoslav Ganov | 76f287e | 2012-04-23 11:02:36 -0700 | [diff] [blame] | 1973 | } else { |
Adam Powell | cf392d1 | 2015-06-25 14:48:45 -0700 | [diff] [blame] | 1974 | child.addChildrenForAccessibility(outChildren); |
Svetoslav Ganov | 76f287e | 2012-04-23 11:02:36 -0700 | [diff] [blame] | 1975 | } |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 1976 | } |
| 1977 | } |
Svetoslav Ganov | 76f287e | 2012-04-23 11:02:36 -0700 | [diff] [blame] | 1978 | } finally { |
| 1979 | children.recycle(); |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 1980 | } |
| 1981 | } |
| 1982 | |
| 1983 | /** |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 1984 | * Implement this method to intercept hover events before they are handled |
| 1985 | * by child views. |
| 1986 | * <p> |
| 1987 | * This method is called before dispatching a hover event to a child of |
| 1988 | * the view group or to the view group's own {@link #onHoverEvent} to allow |
| 1989 | * the view group a chance to intercept the hover event. |
| 1990 | * This method can also be used to watch all pointer motions that occur within |
| 1991 | * the bounds of the view group even when the pointer is hovering over |
| 1992 | * a child of the view group rather than over the view group itself. |
| 1993 | * </p><p> |
| 1994 | * The view group can prevent its children from receiving hover events by |
| 1995 | * implementing this method and returning <code>true</code> to indicate |
| 1996 | * that it would like to intercept hover events. The view group must |
| 1997 | * continuously return <code>true</code> from {@link #onInterceptHoverEvent} |
| 1998 | * for as long as it wishes to continue intercepting hover events from |
| 1999 | * its children. |
| 2000 | * </p><p> |
| 2001 | * Interception preserves the invariant that at most one view can be |
| 2002 | * hovered at a time by transferring hover focus from the currently hovered |
| 2003 | * child to the view group or vice-versa as needed. |
| 2004 | * </p><p> |
| 2005 | * If this method returns <code>true</code> and a child is already hovered, then the |
| 2006 | * child view will first receive a hover exit event and then the view group |
| 2007 | * itself will receive a hover enter event in {@link #onHoverEvent}. |
| 2008 | * Likewise, if this method had previously returned <code>true</code> to intercept hover |
| 2009 | * events and instead returns <code>false</code> while the pointer is hovering |
| 2010 | * within the bounds of one of a child, then the view group will first receive a |
| 2011 | * hover exit event in {@link #onHoverEvent} and then the hovered child will |
| 2012 | * receive a hover enter event. |
| 2013 | * </p><p> |
Keisuke Kuroyanagi | d85bc50 | 2016-01-21 14:50:38 +0900 | [diff] [blame] | 2014 | * The default implementation handles mouse hover on the scroll bars. |
Jeff Brown | 10b6290 | 2011-06-20 16:40:37 -0700 | [diff] [blame] | 2015 | * </p> |
| 2016 | * |
| 2017 | * @param event The motion event that describes the hover. |
| 2018 | * @return True if the view group would like to intercept the hover event |
| 2019 | * and prevent its children from receiving it. |
| 2020 | */ |
| 2021 | public boolean onInterceptHoverEvent(MotionEvent event) { |
Keisuke Kuroyanagi | d85bc50 | 2016-01-21 14:50:38 +0900 | [diff] [blame] | 2022 | if (event.isFromSource(InputDevice.SOURCE_MOUSE)) { |
| 2023 | final int action = event.getAction(); |
| 2024 | final float x = event.getX(); |
| 2025 | final float y = event.getY(); |
| 2026 | if ((action == MotionEvent.ACTION_HOVER_MOVE |
| 2027 | || action == MotionEvent.ACTION_HOVER_ENTER) && isOnScrollbar(x, y)) { |
| 2028 | return true; |
| 2029 | } |
| 2030 | } |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 2031 | return false; |
| 2032 | } |
| 2033 | |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2034 | private static MotionEvent obtainMotionEventNoHistoryOrSelf(MotionEvent event) { |
| 2035 | if (event.getHistorySize() == 0) { |
| 2036 | return event; |
| 2037 | } |
| 2038 | return MotionEvent.obtainNoHistory(event); |
| 2039 | } |
| 2040 | |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2041 | @Override |
| 2042 | protected boolean dispatchGenericPointerEvent(MotionEvent event) { |
| 2043 | // Send the event to the child under the pointer. |
| 2044 | final int childrenCount = mChildrenCount; |
| 2045 | if (childrenCount != 0) { |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2046 | final float x = event.getX(); |
| 2047 | final float y = event.getY(); |
| 2048 | |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 2049 | final ArrayList<View> preorderedList = buildOrderedChildList(); |
| 2050 | final boolean customOrder = preorderedList == null |
| 2051 | && isChildrenDrawingOrderEnabled(); |
| 2052 | final View[] children = mChildren; |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2053 | for (int i = childrenCount - 1; i >= 0; i--) { |
Alan Viverette | a7b85e6 | 2016-01-22 10:14:02 -0500 | [diff] [blame] | 2054 | final int childIndex = getAndVerifyPreorderedIndex(childrenCount, i, customOrder); |
| 2055 | final View child = getAndVerifyPreorderedView(preorderedList, children, childIndex); |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2056 | if (!canViewReceivePointerEvents(child) |
| 2057 | || !isTransformedTouchPointInView(x, y, child, null)) { |
| 2058 | continue; |
| 2059 | } |
| 2060 | |
| 2061 | if (dispatchTransformedGenericPointerEvent(event, child)) { |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 2062 | if (preorderedList != null) preorderedList.clear(); |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2063 | return true; |
| 2064 | } |
| 2065 | } |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 2066 | if (preorderedList != null) preorderedList.clear(); |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2067 | } |
| 2068 | |
| 2069 | // No child handled the event. Send it to this view group. |
| 2070 | return super.dispatchGenericPointerEvent(event); |
| 2071 | } |
| 2072 | |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2073 | @Override |
| 2074 | protected boolean dispatchGenericFocusedEvent(MotionEvent event) { |
Jeff Brown | 33bbfd2 | 2011-02-24 20:55:35 -0800 | [diff] [blame] | 2075 | // 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] | 2076 | if ((mPrivateFlags & (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) |
| 2077 | == (PFLAG_FOCUSED | PFLAG_HAS_BOUNDS)) { |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2078 | return super.dispatchGenericFocusedEvent(event); |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 2079 | } else if (mFocused != null && (mFocused.mPrivateFlags & PFLAG_HAS_BOUNDS) |
| 2080 | == PFLAG_HAS_BOUNDS) { |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 2081 | return mFocused.dispatchGenericMotionEvent(event); |
| 2082 | } |
| 2083 | return false; |
| 2084 | } |
| 2085 | |
| 2086 | /** |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2087 | * Dispatches a generic pointer event to a child, taking into account |
| 2088 | * transformations that apply to the child. |
| 2089 | * |
| 2090 | * @param event The event to send. |
| 2091 | * @param child The view to send the event to. |
| 2092 | * @return {@code true} if the child handled the event. |
| 2093 | */ |
| 2094 | private boolean dispatchTransformedGenericPointerEvent(MotionEvent event, View child) { |
| 2095 | final float offsetX = mScrollX - child.mLeft; |
| 2096 | final float offsetY = mScrollY - child.mTop; |
| 2097 | |
| 2098 | boolean handled; |
| 2099 | if (!child.hasIdentityMatrix()) { |
| 2100 | MotionEvent transformedEvent = MotionEvent.obtain(event); |
| 2101 | transformedEvent.offsetLocation(offsetX, offsetY); |
| 2102 | transformedEvent.transform(child.getInverseMatrix()); |
| 2103 | handled = child.dispatchGenericMotionEvent(transformedEvent); |
| 2104 | transformedEvent.recycle(); |
| 2105 | } else { |
| 2106 | event.offsetLocation(offsetX, offsetY); |
| 2107 | handled = child.dispatchGenericMotionEvent(event); |
| 2108 | event.offsetLocation(-offsetX, -offsetY); |
| 2109 | } |
| 2110 | return handled; |
| 2111 | } |
| 2112 | |
Jeff Brown | cb1404e | 2011-01-15 18:14:15 -0800 | [diff] [blame] | 2113 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2114 | public boolean dispatchTouchEvent(MotionEvent ev) { |
Jeff Brown | 21bc5c9 | 2011-02-28 18:27:14 -0800 | [diff] [blame] | 2115 | if (mInputEventConsistencyVerifier != null) { |
| 2116 | mInputEventConsistencyVerifier.onTouchEvent(ev, 1); |
| 2117 | } |
| 2118 | |
Svet Ganov | 0a2ccee | 2015-02-06 10:12:32 -0800 | [diff] [blame] | 2119 | // If the event targets the accessibility focused view and this is it, start |
| 2120 | // normal event dispatch. Maybe a descendant is what will handle the click. |
Svet Ganov | f33fe1f | 2015-02-06 19:23:31 -0800 | [diff] [blame] | 2121 | if (ev.isTargetAccessibilityFocus() && isAccessibilityFocusedViewOrHost()) { |
Svet Ganov | 0a2ccee | 2015-02-06 10:12:32 -0800 | [diff] [blame] | 2122 | ev.setTargetAccessibilityFocus(false); |
| 2123 | } |
| 2124 | |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2125 | boolean handled = false; |
| 2126 | if (onFilterTouchEventForSecurity(ev)) { |
| 2127 | final int action = ev.getAction(); |
| 2128 | final int actionMasked = action & MotionEvent.ACTION_MASK; |
Jeff Brown | 85a3176 | 2010-09-01 17:01:00 -0700 | [diff] [blame] | 2129 | |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2130 | // Handle an initial down. |
| 2131 | if (actionMasked == MotionEvent.ACTION_DOWN) { |
| 2132 | // Throw away all previous state when starting a new touch gesture. |
| 2133 | // The framework may have dropped the up or cancel event for the previous gesture |
| 2134 | // due to an app switch, ANR, or some other state change. |
| 2135 | cancelAndClearTouchTargets(ev); |
| 2136 | resetTouchState(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2137 | } |
Adam Powell | b08013c | 2010-09-16 16:28:11 -0700 | [diff] [blame] | 2138 | |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2139 | // Check for interception. |
| 2140 | final boolean intercepted; |
Svet Ganov | f33fe1f | 2015-02-06 19:23:31 -0800 | [diff] [blame] | 2141 | if (actionMasked == MotionEvent.ACTION_DOWN |
| 2142 | || mFirstTouchTarget != null) { |
| 2143 | final boolean disallowIntercept = (mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0; |
| 2144 | if (!disallowIntercept) { |
| 2145 | intercepted = onInterceptTouchEvent(ev); |
| 2146 | ev.setAction(action); // restore action in case it was changed |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2147 | } else { |
Svet Ganov | f33fe1f | 2015-02-06 19:23:31 -0800 | [diff] [blame] | 2148 | intercepted = false; |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2149 | } |
| 2150 | } else { |
Svet Ganov | f33fe1f | 2015-02-06 19:23:31 -0800 | [diff] [blame] | 2151 | // There are no touch targets and this action is not an initial down |
| 2152 | // so this view group continues to intercept touches. |
| 2153 | intercepted = true; |
| 2154 | } |
| 2155 | |
| 2156 | // If intercepted, start normal event dispatch. Also if there is already |
| 2157 | // a view that is handling the gesture, do normal event dispatch. |
| 2158 | if (intercepted || mFirstTouchTarget != null) { |
| 2159 | ev.setTargetAccessibilityFocus(false); |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2160 | } |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2161 | |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2162 | // Check for cancelation. |
| 2163 | final boolean canceled = resetCancelNextUpFlag(this) |
| 2164 | || actionMasked == MotionEvent.ACTION_CANCEL; |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2165 | |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2166 | // Update list of touch targets for pointer down, if needed. |
| 2167 | final boolean split = (mGroupFlags & FLAG_SPLIT_MOTION_EVENTS) != 0; |
| 2168 | TouchTarget newTouchTarget = null; |
| 2169 | boolean alreadyDispatchedToNewTouchTarget = false; |
| 2170 | if (!canceled && !intercepted) { |
Svetoslav | c73cfa0 | 2015-02-09 17:14:28 -0800 | [diff] [blame] | 2171 | |
| 2172 | // If the event is targeting accessiiblity focus we give it to the |
| 2173 | // view that has accessibility focus and if it does not handle it |
| 2174 | // we clear the flag and dispatch the event to all children as usual. |
| 2175 | // We are looking up the accessibility focused host to avoid keeping |
| 2176 | // state since these events are very rare. |
| 2177 | View childWithAccessibilityFocus = ev.isTargetAccessibilityFocus() |
| 2178 | ? findChildWithAccessibilityFocus() : null; |
| 2179 | |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2180 | if (actionMasked == MotionEvent.ACTION_DOWN |
| 2181 | || (split && actionMasked == MotionEvent.ACTION_POINTER_DOWN) |
Svet Ganov | f33fe1f | 2015-02-06 19:23:31 -0800 | [diff] [blame] | 2182 | || actionMasked == MotionEvent.ACTION_HOVER_MOVE) { |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2183 | final int actionIndex = ev.getActionIndex(); // always 0 for down |
| 2184 | final int idBitsToAssign = split ? 1 << ev.getPointerId(actionIndex) |
| 2185 | : TouchTarget.ALL_POINTER_IDS; |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2186 | |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2187 | // Clean up earlier touch targets for this pointer id in case they |
| 2188 | // have become out of sync. |
| 2189 | removePointersFromTouchTargets(idBitsToAssign); |
| 2190 | |
| 2191 | final int childrenCount = mChildrenCount; |
Chet Haase | 9c17fe6 | 2013-03-22 17:05:55 -0700 | [diff] [blame] | 2192 | if (newTouchTarget == null && childrenCount != 0) { |
Chet Haase | edf6f4b | 2013-03-26 07:55:30 -0700 | [diff] [blame] | 2193 | final float x = ev.getX(actionIndex); |
| 2194 | final float y = ev.getY(actionIndex); |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2195 | // Find a child that can receive the event. |
| 2196 | // Scan children from front to back. |
Filip Gruszczynski | a33bdf3 | 2015-11-19 18:22:16 -0800 | [diff] [blame] | 2197 | final ArrayList<View> preorderedList = buildTouchDispatchChildList(); |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 2198 | final boolean customOrder = preorderedList == null |
| 2199 | && isChildrenDrawingOrderEnabled(); |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2200 | final View[] children = mChildren; |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2201 | for (int i = childrenCount - 1; i >= 0; i--) { |
Alan Viverette | a7b85e6 | 2016-01-22 10:14:02 -0500 | [diff] [blame] | 2202 | final int childIndex = getAndVerifyPreorderedIndex( |
| 2203 | childrenCount, i, customOrder); |
| 2204 | final View child = getAndVerifyPreorderedView( |
| 2205 | preorderedList, children, childIndex); |
Svetoslav | c73cfa0 | 2015-02-09 17:14:28 -0800 | [diff] [blame] | 2206 | |
| 2207 | // If there is a view that has accessibility focus we want it |
| 2208 | // to get the event first and if not handled we will perform a |
| 2209 | // normal dispatch. We may do a double iteration but this is |
| 2210 | // safer given the timeframe. |
| 2211 | if (childWithAccessibilityFocus != null) { |
| 2212 | if (childWithAccessibilityFocus != child) { |
| 2213 | continue; |
| 2214 | } |
| 2215 | childWithAccessibilityFocus = null; |
| 2216 | i = childrenCount - 1; |
| 2217 | } |
| 2218 | |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2219 | if (!canViewReceivePointerEvents(child) |
| 2220 | || !isTransformedTouchPointInView(x, y, child, null)) { |
Svetoslav | c73cfa0 | 2015-02-09 17:14:28 -0800 | [diff] [blame] | 2221 | ev.setTargetAccessibilityFocus(false); |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2222 | continue; |
| 2223 | } |
| 2224 | |
| 2225 | newTouchTarget = getTouchTarget(child); |
| 2226 | if (newTouchTarget != null) { |
| 2227 | // Child is already receiving touch within its bounds. |
| 2228 | // Give it the new pointer in addition to the ones it is handling. |
| 2229 | newTouchTarget.pointerIdBits |= idBitsToAssign; |
| 2230 | break; |
| 2231 | } |
| 2232 | |
| 2233 | resetCancelNextUpFlag(child); |
| 2234 | if (dispatchTransformedTouchEvent(ev, false, child, idBitsToAssign)) { |
| 2235 | // Child wants to receive touch within its bounds. |
| 2236 | mLastTouchDownTime = ev.getDownTime(); |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 2237 | if (preorderedList != null) { |
| 2238 | // childIndex points into presorted list, find original index |
| 2239 | for (int j = 0; j < childrenCount; j++) { |
| 2240 | if (children[childIndex] == mChildren[j]) { |
| 2241 | mLastTouchDownIndex = j; |
| 2242 | break; |
| 2243 | } |
| 2244 | } |
| 2245 | } else { |
| 2246 | mLastTouchDownIndex = childIndex; |
| 2247 | } |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2248 | mLastTouchDownX = ev.getX(); |
| 2249 | mLastTouchDownY = ev.getY(); |
| 2250 | newTouchTarget = addTouchTarget(child, idBitsToAssign); |
| 2251 | alreadyDispatchedToNewTouchTarget = true; |
| 2252 | break; |
| 2253 | } |
Svetoslav | c73cfa0 | 2015-02-09 17:14:28 -0800 | [diff] [blame] | 2254 | |
| 2255 | // The accessibility focus didn't handle the event, so clear |
| 2256 | // the flag and do a normal dispatch to all children. |
| 2257 | ev.setTargetAccessibilityFocus(false); |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2258 | } |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 2259 | if (preorderedList != null) preorderedList.clear(); |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2260 | } |
| 2261 | |
| 2262 | if (newTouchTarget == null && mFirstTouchTarget != null) { |
| 2263 | // Did not find a child to receive the event. |
| 2264 | // Assign the pointer to the least recently added target. |
| 2265 | newTouchTarget = mFirstTouchTarget; |
| 2266 | while (newTouchTarget.next != null) { |
| 2267 | newTouchTarget = newTouchTarget.next; |
| 2268 | } |
| 2269 | newTouchTarget.pointerIdBits |= idBitsToAssign; |
| 2270 | } |
| 2271 | } |
| 2272 | } |
| 2273 | |
| 2274 | // Dispatch to touch targets. |
| 2275 | if (mFirstTouchTarget == null) { |
| 2276 | // No touch targets so treat this as an ordinary view. |
| 2277 | handled = dispatchTransformedTouchEvent(ev, canceled, null, |
| 2278 | TouchTarget.ALL_POINTER_IDS); |
| 2279 | } else { |
| 2280 | // Dispatch to touch targets, excluding the new touch target if we already |
| 2281 | // dispatched to it. Cancel touch targets if necessary. |
| 2282 | TouchTarget predecessor = null; |
| 2283 | TouchTarget target = mFirstTouchTarget; |
| 2284 | while (target != null) { |
| 2285 | final TouchTarget next = target.next; |
| 2286 | if (alreadyDispatchedToNewTouchTarget && target == newTouchTarget) { |
| 2287 | handled = true; |
| 2288 | } else { |
| 2289 | final boolean cancelChild = resetCancelNextUpFlag(target.child) |
Chet Haase | 9c17fe6 | 2013-03-22 17:05:55 -0700 | [diff] [blame] | 2290 | || intercepted; |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2291 | if (dispatchTransformedTouchEvent(ev, cancelChild, |
| 2292 | target.child, target.pointerIdBits)) { |
| 2293 | handled = true; |
| 2294 | } |
| 2295 | if (cancelChild) { |
| 2296 | if (predecessor == null) { |
| 2297 | mFirstTouchTarget = next; |
| 2298 | } else { |
| 2299 | predecessor.next = next; |
| 2300 | } |
| 2301 | target.recycle(); |
| 2302 | target = next; |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2303 | continue; |
| 2304 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2305 | } |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2306 | predecessor = target; |
| 2307 | target = next; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2308 | } |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2309 | } |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2310 | |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2311 | // Update list of touch targets for pointer up or cancel, if needed. |
| 2312 | if (canceled |
| 2313 | || actionMasked == MotionEvent.ACTION_UP |
| 2314 | || actionMasked == MotionEvent.ACTION_HOVER_MOVE) { |
| 2315 | resetTouchState(); |
| 2316 | } else if (split && actionMasked == MotionEvent.ACTION_POINTER_UP) { |
| 2317 | final int actionIndex = ev.getActionIndex(); |
| 2318 | final int idBitsToRemove = 1 << ev.getPointerId(actionIndex); |
| 2319 | removePointersFromTouchTargets(idBitsToRemove); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2320 | } |
| 2321 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 2322 | |
Jeff Brown | bbdc50b | 2011-04-19 23:46:52 -0700 | [diff] [blame] | 2323 | if (!handled && mInputEventConsistencyVerifier != null) { |
| 2324 | mInputEventConsistencyVerifier.onUnhandledEvent(ev, 1); |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2325 | } |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2326 | return handled; |
| 2327 | } |
| 2328 | |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 2329 | /** |
Filip Gruszczynski | a33bdf3 | 2015-11-19 18:22:16 -0800 | [diff] [blame] | 2330 | * Provide custom ordering of views in which the touch will be dispatched. |
| 2331 | * |
| 2332 | * This is called within a tight loop, so you are not allowed to allocate objects, including |
| 2333 | * the return array. Instead, you should return a pre-allocated list that will be cleared |
| 2334 | * after the dispatch is finished. |
| 2335 | * @hide |
| 2336 | */ |
| 2337 | public ArrayList<View> buildTouchDispatchChildList() { |
| 2338 | return buildOrderedChildList(); |
| 2339 | } |
| 2340 | |
| 2341 | /** |
Svetoslav | c73cfa0 | 2015-02-09 17:14:28 -0800 | [diff] [blame] | 2342 | * Finds the child which has accessibility focus. |
| 2343 | * |
| 2344 | * @return The child that has focus. |
| 2345 | */ |
| 2346 | private View findChildWithAccessibilityFocus() { |
| 2347 | ViewRootImpl viewRoot = getViewRootImpl(); |
| 2348 | if (viewRoot == null) { |
| 2349 | return null; |
| 2350 | } |
| 2351 | |
| 2352 | View current = viewRoot.getAccessibilityFocusedHost(); |
| 2353 | if (current == null) { |
| 2354 | return null; |
| 2355 | } |
| 2356 | |
| 2357 | ViewParent parent = current.getParent(); |
| 2358 | while (parent instanceof View) { |
| 2359 | if (parent == this) { |
| 2360 | return current; |
| 2361 | } |
| 2362 | current = (View) parent; |
| 2363 | parent = current.getParent(); |
| 2364 | } |
| 2365 | |
| 2366 | return null; |
| 2367 | } |
| 2368 | |
| 2369 | /** |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 2370 | * Resets all touch state in preparation for a new cycle. |
| 2371 | */ |
| 2372 | private void resetTouchState() { |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2373 | clearTouchTargets(); |
| 2374 | resetCancelNextUpFlag(this); |
| 2375 | mGroupFlags &= ~FLAG_DISALLOW_INTERCEPT; |
Adam Powell | 10ba277 | 2014-04-15 09:46:51 -0700 | [diff] [blame] | 2376 | mNestedScrollAxes = SCROLL_AXIS_NONE; |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2377 | } |
| 2378 | |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 2379 | /** |
| 2380 | * Resets the cancel next up flag. |
| 2381 | * Returns true if the flag was previously set. |
| 2382 | */ |
Alan Viverette | a7b85e6 | 2016-01-22 10:14:02 -0500 | [diff] [blame] | 2383 | private static boolean resetCancelNextUpFlag(@NonNull View view) { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 2384 | if ((view.mPrivateFlags & PFLAG_CANCEL_NEXT_UP_EVENT) != 0) { |
| 2385 | view.mPrivateFlags &= ~PFLAG_CANCEL_NEXT_UP_EVENT; |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2386 | return true; |
Adam Cohen | 9b07394 | 2010-08-19 16:49:52 -0700 | [diff] [blame] | 2387 | } |
| 2388 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2389 | } |
| 2390 | |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 2391 | /** |
| 2392 | * Clears all touch targets. |
| 2393 | */ |
| 2394 | private void clearTouchTargets() { |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2395 | TouchTarget target = mFirstTouchTarget; |
| 2396 | if (target != null) { |
| 2397 | do { |
| 2398 | TouchTarget next = target.next; |
| 2399 | target.recycle(); |
| 2400 | target = next; |
| 2401 | } while (target != null); |
| 2402 | mFirstTouchTarget = null; |
| 2403 | } |
| 2404 | } |
| 2405 | |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 2406 | /** |
| 2407 | * Cancels and clears all touch targets. |
| 2408 | */ |
| 2409 | private void cancelAndClearTouchTargets(MotionEvent event) { |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2410 | if (mFirstTouchTarget != null) { |
| 2411 | boolean syntheticEvent = false; |
| 2412 | if (event == null) { |
| 2413 | final long now = SystemClock.uptimeMillis(); |
| 2414 | event = MotionEvent.obtain(now, now, |
| 2415 | MotionEvent.ACTION_CANCEL, 0.0f, 0.0f, 0); |
Jeff Brown | 2fdbc5a | 2011-06-30 12:25:54 -0700 | [diff] [blame] | 2416 | event.setSource(InputDevice.SOURCE_TOUCHSCREEN); |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2417 | syntheticEvent = true; |
| 2418 | } |
| 2419 | |
| 2420 | for (TouchTarget target = mFirstTouchTarget; target != null; target = target.next) { |
| 2421 | resetCancelNextUpFlag(target.child); |
| 2422 | dispatchTransformedTouchEvent(event, true, target.child, target.pointerIdBits); |
| 2423 | } |
| 2424 | clearTouchTargets(); |
| 2425 | |
| 2426 | if (syntheticEvent) { |
| 2427 | event.recycle(); |
| 2428 | } |
| 2429 | } |
| 2430 | } |
| 2431 | |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 2432 | /** |
| 2433 | * Gets the touch target for specified child view. |
| 2434 | * Returns null if not found. |
| 2435 | */ |
Alan Viverette | a7b85e6 | 2016-01-22 10:14:02 -0500 | [diff] [blame] | 2436 | private TouchTarget getTouchTarget(@NonNull View child) { |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2437 | for (TouchTarget target = mFirstTouchTarget; target != null; target = target.next) { |
| 2438 | if (target.child == child) { |
| 2439 | return target; |
| 2440 | } |
| 2441 | } |
| 2442 | return null; |
| 2443 | } |
| 2444 | |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 2445 | /** |
| 2446 | * Adds a touch target for specified child to the beginning of the list. |
| 2447 | * Assumes the target child is not already present. |
| 2448 | */ |
Alan Viverette | a7b85e6 | 2016-01-22 10:14:02 -0500 | [diff] [blame] | 2449 | private TouchTarget addTouchTarget(@NonNull View child, int pointerIdBits) { |
| 2450 | final TouchTarget target = TouchTarget.obtain(child, pointerIdBits); |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2451 | target.next = mFirstTouchTarget; |
| 2452 | mFirstTouchTarget = target; |
| 2453 | return target; |
| 2454 | } |
| 2455 | |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 2456 | /** |
| 2457 | * Removes the pointer ids from consideration. |
| 2458 | */ |
| 2459 | private void removePointersFromTouchTargets(int pointerIdBits) { |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2460 | TouchTarget predecessor = null; |
| 2461 | TouchTarget target = mFirstTouchTarget; |
| 2462 | while (target != null) { |
| 2463 | final TouchTarget next = target.next; |
| 2464 | if ((target.pointerIdBits & pointerIdBits) != 0) { |
| 2465 | target.pointerIdBits &= ~pointerIdBits; |
| 2466 | if (target.pointerIdBits == 0) { |
| 2467 | if (predecessor == null) { |
| 2468 | mFirstTouchTarget = next; |
| 2469 | } else { |
| 2470 | predecessor.next = next; |
| 2471 | } |
| 2472 | target.recycle(); |
| 2473 | target = next; |
| 2474 | continue; |
| 2475 | } |
| 2476 | } |
| 2477 | predecessor = target; |
| 2478 | target = next; |
| 2479 | } |
| 2480 | } |
| 2481 | |
Jeff Brown | 59a422e | 2012-04-19 15:19:19 -0700 | [diff] [blame] | 2482 | private void cancelTouchTarget(View view) { |
| 2483 | TouchTarget predecessor = null; |
| 2484 | TouchTarget target = mFirstTouchTarget; |
| 2485 | while (target != null) { |
| 2486 | final TouchTarget next = target.next; |
| 2487 | if (target.child == view) { |
| 2488 | if (predecessor == null) { |
| 2489 | mFirstTouchTarget = next; |
| 2490 | } else { |
| 2491 | predecessor.next = next; |
| 2492 | } |
| 2493 | target.recycle(); |
| 2494 | |
| 2495 | final long now = SystemClock.uptimeMillis(); |
| 2496 | MotionEvent event = MotionEvent.obtain(now, now, |
| 2497 | MotionEvent.ACTION_CANCEL, 0.0f, 0.0f, 0); |
| 2498 | event.setSource(InputDevice.SOURCE_TOUCHSCREEN); |
| 2499 | view.dispatchTouchEvent(event); |
| 2500 | event.recycle(); |
| 2501 | return; |
| 2502 | } |
| 2503 | predecessor = target; |
| 2504 | target = next; |
| 2505 | } |
| 2506 | } |
| 2507 | |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 2508 | /** |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2509 | * Returns true if a child view can receive pointer events. |
| 2510 | * @hide |
| 2511 | */ |
Alan Viverette | a7b85e6 | 2016-01-22 10:14:02 -0500 | [diff] [blame] | 2512 | private static boolean canViewReceivePointerEvents(@NonNull View child) { |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2513 | return (child.mViewFlags & VISIBILITY_MASK) == VISIBLE |
| 2514 | || child.getAnimation() != null; |
| 2515 | } |
| 2516 | |
Alan Viverette | b942b6f | 2014-12-08 10:37:39 -0800 | [diff] [blame] | 2517 | private float[] getTempPoint() { |
| 2518 | if (mTempPoint == null) { |
| 2519 | mTempPoint = new float[2]; |
| 2520 | } |
| 2521 | return mTempPoint; |
| 2522 | } |
| 2523 | |
Jeff Brown | a032cc0 | 2011-03-07 16:56:21 -0800 | [diff] [blame] | 2524 | /** |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 2525 | * Returns true if a child view contains the specified point when transformed |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2526 | * into its coordinate space. |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 2527 | * Child must not be null. |
Adam Cohen | a32edd4 | 2010-10-26 10:35:01 -0700 | [diff] [blame] | 2528 | * @hide |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 2529 | */ |
Adam Cohen | a32edd4 | 2010-10-26 10:35:01 -0700 | [diff] [blame] | 2530 | protected boolean isTransformedTouchPointInView(float x, float y, View child, |
Christopher Tate | 2c095f3 | 2010-10-04 14:13:40 -0700 | [diff] [blame] | 2531 | PointF outLocalPoint) { |
Alan Viverette | b942b6f | 2014-12-08 10:37:39 -0800 | [diff] [blame] | 2532 | final float[] point = getTempPoint(); |
| 2533 | point[0] = x; |
| 2534 | point[1] = y; |
| 2535 | transformPointToViewLocal(point, child); |
| 2536 | final boolean isInView = child.pointInView(point[0], point[1]); |
Christopher Tate | 2c095f3 | 2010-10-04 14:13:40 -0700 | [diff] [blame] | 2537 | if (isInView && outLocalPoint != null) { |
Alan Viverette | b942b6f | 2014-12-08 10:37:39 -0800 | [diff] [blame] | 2538 | outLocalPoint.set(point[0], point[1]); |
Christopher Tate | 2c095f3 | 2010-10-04 14:13:40 -0700 | [diff] [blame] | 2539 | } |
| 2540 | return isInView; |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 2541 | } |
| 2542 | |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 2543 | /** |
Alan Viverette | b942b6f | 2014-12-08 10:37:39 -0800 | [diff] [blame] | 2544 | * @hide |
| 2545 | */ |
| 2546 | public void transformPointToViewLocal(float[] point, View child) { |
| 2547 | point[0] += mScrollX - child.mLeft; |
| 2548 | point[1] += mScrollY - child.mTop; |
| 2549 | |
| 2550 | if (!child.hasIdentityMatrix()) { |
| 2551 | child.getInverseMatrix().mapPoints(point); |
| 2552 | } |
| 2553 | } |
| 2554 | |
| 2555 | /** |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 2556 | * 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] | 2557 | * filters out irrelevant pointer ids, and overrides its action if necessary. |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 2558 | * If child is null, assumes the MotionEvent will be sent to this ViewGroup instead. |
| 2559 | */ |
| 2560 | private boolean dispatchTransformedTouchEvent(MotionEvent event, boolean cancel, |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2561 | View child, int desiredPointerIdBits) { |
| 2562 | final boolean handled; |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 2563 | |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2564 | // Canceling motions is a special case. We don't need to perform any transformations |
| 2565 | // or filtering. The important part is the action, not the contents. |
| 2566 | final int oldAction = event.getAction(); |
| 2567 | if (cancel || oldAction == MotionEvent.ACTION_CANCEL) { |
| 2568 | event.setAction(MotionEvent.ACTION_CANCEL); |
| 2569 | if (child == null) { |
| 2570 | handled = super.dispatchTouchEvent(event); |
| 2571 | } else { |
| 2572 | handled = child.dispatchTouchEvent(event); |
| 2573 | } |
| 2574 | event.setAction(oldAction); |
| 2575 | return handled; |
| 2576 | } |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 2577 | |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2578 | // Calculate the number of pointers to deliver. |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 2579 | final int oldPointerIdBits = event.getPointerIdBits(); |
| 2580 | final int newPointerIdBits = oldPointerIdBits & desiredPointerIdBits; |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 2581 | |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2582 | // If for some reason we ended up in an inconsistent state where it looks like we |
| 2583 | // 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] | 2584 | if (newPointerIdBits == 0) { |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2585 | return false; |
| 2586 | } |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 2587 | |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2588 | // If the number of pointers is the same and we don't need to perform any fancy |
| 2589 | // irreversible transformations, then we can reuse the motion event for this |
| 2590 | // 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] | 2591 | // Otherwise we need to make a copy. |
| 2592 | final MotionEvent transformedEvent; |
| 2593 | if (newPointerIdBits == oldPointerIdBits) { |
| 2594 | if (child == null || child.hasIdentityMatrix()) { |
| 2595 | if (child == null) { |
| 2596 | handled = super.dispatchTouchEvent(event); |
| 2597 | } else { |
| 2598 | final float offsetX = mScrollX - child.mLeft; |
| 2599 | final float offsetY = mScrollY - child.mTop; |
| 2600 | event.offsetLocation(offsetX, offsetY); |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 2601 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 2602 | handled = child.dispatchTouchEvent(event); |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2603 | |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 2604 | event.offsetLocation(-offsetX, -offsetY); |
| 2605 | } |
| 2606 | return handled; |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2607 | } |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2608 | transformedEvent = MotionEvent.obtain(event); |
| 2609 | } else { |
Jeff Brown | fe9f8ab | 2011-05-06 18:20:01 -0700 | [diff] [blame] | 2610 | transformedEvent = event.split(newPointerIdBits); |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 2611 | } |
| 2612 | |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2613 | // Perform any necessary transformations and dispatch. |
| 2614 | if (child == null) { |
| 2615 | handled = super.dispatchTouchEvent(transformedEvent); |
| 2616 | } else { |
| 2617 | final float offsetX = mScrollX - child.mLeft; |
| 2618 | final float offsetY = mScrollY - child.mTop; |
| 2619 | transformedEvent.offsetLocation(offsetX, offsetY); |
| 2620 | if (! child.hasIdentityMatrix()) { |
| 2621 | transformedEvent.transform(child.getInverseMatrix()); |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 2622 | } |
| 2623 | |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2624 | handled = child.dispatchTouchEvent(transformedEvent); |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 2625 | } |
| 2626 | |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 2627 | // Done. |
| 2628 | transformedEvent.recycle(); |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 2629 | return handled; |
| 2630 | } |
| 2631 | |
Romain Guy | 469b1db | 2010-10-05 11:49:57 -0700 | [diff] [blame] | 2632 | /** |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 2633 | * 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] | 2634 | * dispatch. This behavior is enabled by default for applications that target an |
| 2635 | * SDK version of {@link Build.VERSION_CODES#HONEYCOMB} or newer. |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 2636 | * |
| 2637 | * <p>When this option is enabled MotionEvents may be split and dispatched to different child |
| 2638 | * views depending on where each pointer initially went down. This allows for user interactions |
| 2639 | * such as scrolling two panes of content independently, chording of buttons, and performing |
| 2640 | * independent gestures on different pieces of content. |
| 2641 | * |
| 2642 | * @param split <code>true</code> to allow MotionEvents to be split and dispatched to multiple |
| 2643 | * child views. <code>false</code> to only allow one child view to be the target of |
| 2644 | * any MotionEvent received by this ViewGroup. |
Scott Main | 27a8508 | 2013-06-10 10:39:48 -0700 | [diff] [blame] | 2645 | * @attr ref android.R.styleable#ViewGroup_splitMotionEvents |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 2646 | */ |
| 2647 | public void setMotionEventSplittingEnabled(boolean split) { |
| 2648 | // TODO Applications really shouldn't change this setting mid-touch event, |
| 2649 | // but perhaps this should handle that case and send ACTION_CANCELs to any child views |
| 2650 | // with gestures in progress when this is changed. |
| 2651 | if (split) { |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 2652 | mGroupFlags |= FLAG_SPLIT_MOTION_EVENTS; |
| 2653 | } else { |
| 2654 | mGroupFlags &= ~FLAG_SPLIT_MOTION_EVENTS; |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 2655 | } |
| 2656 | } |
| 2657 | |
| 2658 | /** |
Jeff Brown | 995e774 | 2010-12-22 16:59:36 -0800 | [diff] [blame] | 2659 | * 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] | 2660 | * @return true if MotionEvents dispatched to this ViewGroup can be split to multiple children. |
| 2661 | */ |
| 2662 | public boolean isMotionEventSplittingEnabled() { |
| 2663 | return (mGroupFlags & FLAG_SPLIT_MOTION_EVENTS) == FLAG_SPLIT_MOTION_EVENTS; |
| 2664 | } |
| 2665 | |
| 2666 | /** |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 2667 | * Returns true if this ViewGroup should be considered as a single entity for removal |
| 2668 | * when executing an Activity transition. If this is false, child elements will move |
| 2669 | * individually during the transition. |
George Mount | 427c641 | 2014-11-05 16:45:36 -0800 | [diff] [blame] | 2670 | * |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 2671 | * @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] | 2672 | * The default value is true when there is a non-null background or if |
| 2673 | * {@link #getTransitionName()} is not null or if a |
| 2674 | * non-null {@link android.view.ViewOutlineProvider} other than |
| 2675 | * {@link android.view.ViewOutlineProvider#BACKGROUND} was given to |
| 2676 | * {@link #setOutlineProvider(ViewOutlineProvider)} and false otherwise. |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 2677 | */ |
| 2678 | public boolean isTransitionGroup() { |
| 2679 | if ((mGroupFlags & FLAG_IS_TRANSITION_GROUP_SET) != 0) { |
| 2680 | return ((mGroupFlags & FLAG_IS_TRANSITION_GROUP) != 0); |
| 2681 | } else { |
George Mount | 427c641 | 2014-11-05 16:45:36 -0800 | [diff] [blame] | 2682 | final ViewOutlineProvider outlineProvider = getOutlineProvider(); |
| 2683 | return getBackground() != null || getTransitionName() != null || |
| 2684 | (outlineProvider != null && outlineProvider != ViewOutlineProvider.BACKGROUND); |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 2685 | } |
| 2686 | } |
| 2687 | |
| 2688 | /** |
| 2689 | * 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] | 2690 | * Activity Transitions. |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 2691 | * @param isTransitionGroup Whether or not the ViewGroup should be treated as a unit |
| 2692 | * in Activity transitions. If false, the ViewGroup won't transition, |
| 2693 | * only its children. If true, the entire ViewGroup will transition |
| 2694 | * together. |
George Mount | 62ab9b7 | 2014-05-02 13:51:17 -0700 | [diff] [blame] | 2695 | * @see android.app.ActivityOptions#makeSceneTransitionAnimation(android.app.Activity, |
| 2696 | * android.util.Pair[]) |
George Mount | 0a778ed | 2013-12-13 13:35:36 -0800 | [diff] [blame] | 2697 | */ |
| 2698 | public void setTransitionGroup(boolean isTransitionGroup) { |
| 2699 | mGroupFlags |= FLAG_IS_TRANSITION_GROUP_SET; |
| 2700 | if (isTransitionGroup) { |
| 2701 | mGroupFlags |= FLAG_IS_TRANSITION_GROUP; |
| 2702 | } else { |
| 2703 | mGroupFlags &= ~FLAG_IS_TRANSITION_GROUP; |
| 2704 | } |
| 2705 | } |
| 2706 | |
Alan Viverette | be463f2 | 2016-01-21 10:50:10 -0500 | [diff] [blame] | 2707 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2708 | public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) { |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 2709 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2710 | if (disallowIntercept == ((mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0)) { |
| 2711 | // We're already in this state, assume our ancestors are too |
| 2712 | return; |
| 2713 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 2714 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2715 | if (disallowIntercept) { |
| 2716 | mGroupFlags |= FLAG_DISALLOW_INTERCEPT; |
| 2717 | } else { |
| 2718 | mGroupFlags &= ~FLAG_DISALLOW_INTERCEPT; |
| 2719 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 2720 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2721 | // Pass it up to our parent |
| 2722 | if (mParent != null) { |
| 2723 | mParent.requestDisallowInterceptTouchEvent(disallowIntercept); |
| 2724 | } |
| 2725 | } |
| 2726 | |
| 2727 | /** |
| 2728 | * Implement this method to intercept all touch screen motion events. This |
| 2729 | * allows you to watch events as they are dispatched to your children, and |
| 2730 | * take ownership of the current gesture at any point. |
| 2731 | * |
| 2732 | * <p>Using this function takes some care, as it has a fairly complicated |
| 2733 | * interaction with {@link View#onTouchEvent(MotionEvent) |
| 2734 | * View.onTouchEvent(MotionEvent)}, and using it requires implementing |
| 2735 | * that method as well as this one in the correct way. Events will be |
| 2736 | * received in the following order: |
| 2737 | * |
| 2738 | * <ol> |
| 2739 | * <li> You will receive the down event here. |
| 2740 | * <li> The down event will be handled either by a child of this view |
| 2741 | * group, or given to your own onTouchEvent() method to handle; this means |
| 2742 | * you should implement onTouchEvent() to return true, so you will |
| 2743 | * continue to see the rest of the gesture (instead of looking for |
| 2744 | * a parent view to handle it). Also, by returning true from |
| 2745 | * onTouchEvent(), you will not receive any following |
| 2746 | * events in onInterceptTouchEvent() and all touch processing must |
| 2747 | * happen in onTouchEvent() like normal. |
| 2748 | * <li> For as long as you return false from this function, each following |
| 2749 | * event (up to and including the final up) will be delivered first here |
| 2750 | * and then to the target's onTouchEvent(). |
| 2751 | * <li> If you return true from here, you will not receive any |
| 2752 | * following events: the target view will receive the same event but |
| 2753 | * with the action {@link MotionEvent#ACTION_CANCEL}, and all further |
| 2754 | * events will be delivered to your onTouchEvent() method and no longer |
| 2755 | * appear here. |
| 2756 | * </ol> |
| 2757 | * |
| 2758 | * @param ev The motion event being dispatched down the hierarchy. |
| 2759 | * @return Return true to steal motion events from the children and have |
| 2760 | * them dispatched to this ViewGroup through onTouchEvent(). |
| 2761 | * The current target will receive an ACTION_CANCEL event, and no further |
| 2762 | * messages will be delivered here. |
| 2763 | */ |
| 2764 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
Keisuke Kuroyanagi | d85bc50 | 2016-01-21 14:50:38 +0900 | [diff] [blame] | 2765 | if (ev.isFromSource(InputDevice.SOURCE_MOUSE) |
| 2766 | && ev.getAction() == MotionEvent.ACTION_DOWN |
| 2767 | && ev.isButtonPressed(MotionEvent.BUTTON_PRIMARY) |
| 2768 | && isOnScrollbarThumb(ev.getX(), ev.getY())) { |
| 2769 | return true; |
| 2770 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2771 | return false; |
| 2772 | } |
| 2773 | |
| 2774 | /** |
| 2775 | * {@inheritDoc} |
| 2776 | * |
| 2777 | * Looks for a view to give focus to respecting the setting specified by |
| 2778 | * {@link #getDescendantFocusability()}. |
| 2779 | * |
| 2780 | * Uses {@link #onRequestFocusInDescendants(int, android.graphics.Rect)} to |
| 2781 | * find focus within the children of this group when appropriate. |
| 2782 | * |
| 2783 | * @see #FOCUS_BEFORE_DESCENDANTS |
| 2784 | * @see #FOCUS_AFTER_DESCENDANTS |
| 2785 | * @see #FOCUS_BLOCK_DESCENDANTS |
Filip Gruszczynski | a33bdf3 | 2015-11-19 18:22:16 -0800 | [diff] [blame] | 2786 | * @see #onRequestFocusInDescendants(int, android.graphics.Rect) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2787 | */ |
| 2788 | @Override |
| 2789 | public boolean requestFocus(int direction, Rect previouslyFocusedRect) { |
| 2790 | if (DBG) { |
| 2791 | System.out.println(this + " ViewGroup.requestFocus direction=" |
| 2792 | + direction); |
| 2793 | } |
| 2794 | int descendantFocusability = getDescendantFocusability(); |
| 2795 | |
| 2796 | switch (descendantFocusability) { |
| 2797 | case FOCUS_BLOCK_DESCENDANTS: |
| 2798 | return super.requestFocus(direction, previouslyFocusedRect); |
| 2799 | case FOCUS_BEFORE_DESCENDANTS: { |
| 2800 | final boolean took = super.requestFocus(direction, previouslyFocusedRect); |
| 2801 | return took ? took : onRequestFocusInDescendants(direction, previouslyFocusedRect); |
| 2802 | } |
| 2803 | case FOCUS_AFTER_DESCENDANTS: { |
| 2804 | final boolean took = onRequestFocusInDescendants(direction, previouslyFocusedRect); |
| 2805 | return took ? took : super.requestFocus(direction, previouslyFocusedRect); |
| 2806 | } |
| 2807 | default: |
| 2808 | throw new IllegalStateException("descendant focusability must be " |
| 2809 | + "one of FOCUS_BEFORE_DESCENDANTS, FOCUS_AFTER_DESCENDANTS, FOCUS_BLOCK_DESCENDANTS " |
| 2810 | + "but is " + descendantFocusability); |
| 2811 | } |
| 2812 | } |
| 2813 | |
| 2814 | /** |
| 2815 | * Look for a descendant to call {@link View#requestFocus} on. |
| 2816 | * Called by {@link ViewGroup#requestFocus(int, android.graphics.Rect)} |
| 2817 | * when it wants to request focus within its children. Override this to |
| 2818 | * customize how your {@link ViewGroup} requests focus within its children. |
| 2819 | * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT |
| 2820 | * @param previouslyFocusedRect The rectangle (in this View's coordinate system) |
| 2821 | * to give a finer grained hint about where focus is coming from. May be null |
| 2822 | * if there is no hint. |
| 2823 | * @return Whether focus was taken. |
| 2824 | */ |
| 2825 | @SuppressWarnings({"ConstantConditions"}) |
| 2826 | protected boolean onRequestFocusInDescendants(int direction, |
| 2827 | Rect previouslyFocusedRect) { |
| 2828 | int index; |
| 2829 | int increment; |
| 2830 | int end; |
| 2831 | int count = mChildrenCount; |
| 2832 | if ((direction & FOCUS_FORWARD) != 0) { |
| 2833 | index = 0; |
| 2834 | increment = 1; |
| 2835 | end = count; |
| 2836 | } else { |
| 2837 | index = count - 1; |
| 2838 | increment = -1; |
| 2839 | end = -1; |
| 2840 | } |
| 2841 | final View[] children = mChildren; |
| 2842 | for (int i = index; i != end; i += increment) { |
| 2843 | View child = children[i]; |
| 2844 | if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) { |
| 2845 | if (child.requestFocus(direction, previouslyFocusedRect)) { |
| 2846 | return true; |
| 2847 | } |
| 2848 | } |
| 2849 | } |
| 2850 | return false; |
| 2851 | } |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 2852 | |
Romain Guy | a440b00 | 2010-02-24 15:57:54 -0800 | [diff] [blame] | 2853 | /** |
| 2854 | * {@inheritDoc} |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 2855 | * |
Romain Guy | dcc490f | 2010-02-24 17:59:35 -0800 | [diff] [blame] | 2856 | * @hide |
Romain Guy | a440b00 | 2010-02-24 15:57:54 -0800 | [diff] [blame] | 2857 | */ |
| 2858 | @Override |
| 2859 | public void dispatchStartTemporaryDetach() { |
| 2860 | super.dispatchStartTemporaryDetach(); |
| 2861 | final int count = mChildrenCount; |
| 2862 | final View[] children = mChildren; |
| 2863 | for (int i = 0; i < count; i++) { |
| 2864 | children[i].dispatchStartTemporaryDetach(); |
| 2865 | } |
| 2866 | } |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 2867 | |
Romain Guy | a440b00 | 2010-02-24 15:57:54 -0800 | [diff] [blame] | 2868 | /** |
| 2869 | * {@inheritDoc} |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 2870 | * |
Romain Guy | dcc490f | 2010-02-24 17:59:35 -0800 | [diff] [blame] | 2871 | * @hide |
Romain Guy | a440b00 | 2010-02-24 15:57:54 -0800 | [diff] [blame] | 2872 | */ |
| 2873 | @Override |
| 2874 | public void dispatchFinishTemporaryDetach() { |
| 2875 | super.dispatchFinishTemporaryDetach(); |
| 2876 | final int count = mChildrenCount; |
| 2877 | final View[] children = mChildren; |
| 2878 | for (int i = 0; i < count; i++) { |
| 2879 | children[i].dispatchFinishTemporaryDetach(); |
| 2880 | } |
| 2881 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2882 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2883 | @Override |
| 2884 | void dispatchAttachedToWindow(AttachInfo info, int visibility) { |
Adam Powell | 4b86788 | 2011-09-16 12:59:46 -0700 | [diff] [blame] | 2885 | mGroupFlags |= FLAG_PREVENT_DISPATCH_ATTACHED_TO_WINDOW; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2886 | super.dispatchAttachedToWindow(info, visibility); |
Adam Powell | 4b86788 | 2011-09-16 12:59:46 -0700 | [diff] [blame] | 2887 | mGroupFlags &= ~FLAG_PREVENT_DISPATCH_ATTACHED_TO_WINDOW; |
| 2888 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2889 | final int count = mChildrenCount; |
| 2890 | final View[] children = mChildren; |
| 2891 | for (int i = 0; i < count; i++) { |
Dianne Hackborn | 3a3a6cf | 2012-03-26 10:24:04 -0700 | [diff] [blame] | 2892 | final View child = children[i]; |
| 2893 | child.dispatchAttachedToWindow(info, |
Adam Powell | eb2b0af | 2015-05-20 18:26:35 -0700 | [diff] [blame] | 2894 | combineVisibility(visibility, child.getVisibility())); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2895 | } |
Chet Haase | c633d2f | 2015-04-07 10:29:39 -0700 | [diff] [blame] | 2896 | final int transientCount = mTransientIndices == null ? 0 : mTransientIndices.size(); |
| 2897 | for (int i = 0; i < transientCount; ++i) { |
| 2898 | View view = mTransientViews.get(i); |
Adam Powell | eb2b0af | 2015-05-20 18:26:35 -0700 | [diff] [blame] | 2899 | view.dispatchAttachedToWindow(info, |
| 2900 | combineVisibility(visibility, view.getVisibility())); |
Chet Haase | c633d2f | 2015-04-07 10:29:39 -0700 | [diff] [blame] | 2901 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2902 | } |
| 2903 | |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 2904 | @Override |
Romain Guy | bb9908b | 2012-03-08 11:14:07 -0800 | [diff] [blame] | 2905 | void dispatchScreenStateChanged(int screenState) { |
| 2906 | super.dispatchScreenStateChanged(screenState); |
| 2907 | |
| 2908 | final int count = mChildrenCount; |
| 2909 | final View[] children = mChildren; |
| 2910 | for (int i = 0; i < count; i++) { |
| 2911 | children[i].dispatchScreenStateChanged(screenState); |
| 2912 | } |
| 2913 | } |
| 2914 | |
Alan Viverette | a54956a | 2015-01-07 16:05:02 -0800 | [diff] [blame] | 2915 | /** @hide */ |
Romain Guy | bb9908b | 2012-03-08 11:14:07 -0800 | [diff] [blame] | 2916 | @Override |
Alan Viverette | a54956a | 2015-01-07 16:05:02 -0800 | [diff] [blame] | 2917 | public boolean dispatchPopulateAccessibilityEventInternal(AccessibilityEvent event) { |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 2918 | boolean handled = false; |
| 2919 | if (includeForAccessibility()) { |
| 2920 | handled = super.dispatchPopulateAccessibilityEventInternal(event); |
| 2921 | if (handled) { |
| 2922 | return handled; |
| 2923 | } |
Svetoslav Ganov | b84b94e | 2011-09-22 19:26:56 -0700 | [diff] [blame] | 2924 | } |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 2925 | // Let our children have a shot in populating the event. |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 2926 | ChildListForAccessibility children = ChildListForAccessibility.obtain(this, true); |
Svetoslav Ganov | 76f287e | 2012-04-23 11:02:36 -0700 | [diff] [blame] | 2927 | try { |
| 2928 | final int childCount = children.getChildCount(); |
| 2929 | for (int i = 0; i < childCount; i++) { |
| 2930 | View child = children.getChildAt(i); |
| 2931 | if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) { |
| 2932 | handled = child.dispatchPopulateAccessibilityEvent(event); |
| 2933 | if (handled) { |
Svetoslav Ganov | 76f287e | 2012-04-23 11:02:36 -0700 | [diff] [blame] | 2934 | return handled; |
| 2935 | } |
Svetoslav Ganov | 6179ea3 | 2011-06-28 01:12:41 -0700 | [diff] [blame] | 2936 | } |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 2937 | } |
Svetoslav Ganov | 76f287e | 2012-04-23 11:02:36 -0700 | [diff] [blame] | 2938 | } finally { |
| 2939 | children.recycle(); |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 2940 | } |
Svetoslav Ganov | 736c275 | 2011-04-22 18:30:36 -0700 | [diff] [blame] | 2941 | return false; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 2942 | } |
| 2943 | |
Dianne Hackborn | 6251f0d | 2015-04-01 16:45:03 -0700 | [diff] [blame] | 2944 | /** |
Dianne Hackborn | 49b043f | 2015-05-07 14:21:38 -0700 | [diff] [blame] | 2945 | * Dispatch creation of {@link ViewStructure} down the hierarchy. This implementation |
Dianne Hackborn | 6251f0d | 2015-04-01 16:45:03 -0700 | [diff] [blame] | 2946 | * adds in all child views of the view group, in addition to calling the default View |
| 2947 | * implementation. |
| 2948 | */ |
Alan Viverette | be463f2 | 2016-01-21 10:50:10 -0500 | [diff] [blame] | 2949 | @Override |
Dianne Hackborn | 49b043f | 2015-05-07 14:21:38 -0700 | [diff] [blame] | 2950 | public void dispatchProvideStructure(ViewStructure structure) { |
| 2951 | super.dispatchProvideStructure(structure); |
Dianne Hackborn | 799d8d0 | 2015-04-22 16:36:39 -0700 | [diff] [blame] | 2952 | if (!isAssistBlocked()) { |
| 2953 | if (structure.getChildCount() == 0) { |
| 2954 | final int childrenCount = getChildCount(); |
| 2955 | if (childrenCount > 0) { |
| 2956 | structure.setChildCount(childrenCount); |
Dianne Hackborn | 6ff55fc | 2015-08-05 18:07:31 -0700 | [diff] [blame] | 2957 | ArrayList<View> preorderedList = buildOrderedChildList(); |
| 2958 | boolean customOrder = preorderedList == null |
Dianne Hackborn | 799d8d0 | 2015-04-22 16:36:39 -0700 | [diff] [blame] | 2959 | && isChildrenDrawingOrderEnabled(); |
| 2960 | final View[] children = mChildren; |
| 2961 | for (int i=0; i<childrenCount; i++) { |
Dianne Hackborn | 6ff55fc | 2015-08-05 18:07:31 -0700 | [diff] [blame] | 2962 | int childIndex; |
| 2963 | try { |
Alan Viverette | a7b85e6 | 2016-01-22 10:14:02 -0500 | [diff] [blame] | 2964 | childIndex = getAndVerifyPreorderedIndex(childrenCount, i, customOrder); |
Dianne Hackborn | 6ff55fc | 2015-08-05 18:07:31 -0700 | [diff] [blame] | 2965 | } catch (IndexOutOfBoundsException e) { |
| 2966 | childIndex = i; |
| 2967 | if (mContext.getApplicationInfo().targetSdkVersion |
| 2968 | < Build.VERSION_CODES.M) { |
| 2969 | Log.w(TAG, "Bad getChildDrawingOrder while collecting assist @ " |
| 2970 | + i + " of " + childrenCount, e); |
| 2971 | // At least one app is failing when we call getChildDrawingOrder |
| 2972 | // at this point, so deal semi-gracefully with it by falling back |
| 2973 | // on the basic order. |
| 2974 | customOrder = false; |
| 2975 | if (i > 0) { |
| 2976 | // If we failed at the first index, there really isn't |
| 2977 | // anything to do -- we will just proceed with the simple |
| 2978 | // sequence order. |
| 2979 | // Otherwise, we failed in the middle, so need to come up |
| 2980 | // with an order for the remaining indices and use that. |
| 2981 | // Failed at the first one, easy peasy. |
| 2982 | int[] permutation = new int[childrenCount]; |
| 2983 | SparseBooleanArray usedIndices = new SparseBooleanArray(); |
| 2984 | // Go back and collected the indices we have done so far. |
| 2985 | for (int j=0; j<i; j++) { |
| 2986 | permutation[j] = getChildDrawingOrder(childrenCount, j); |
| 2987 | usedIndices.put(permutation[j], true); |
| 2988 | } |
| 2989 | // Fill in the remaining indices with indices that have not |
| 2990 | // yet been used. |
| 2991 | int nextIndex = 0; |
| 2992 | for (int j=i; j<childrenCount; j++) { |
| 2993 | while (usedIndices.get(nextIndex, false)) { |
| 2994 | nextIndex++; |
| 2995 | } |
| 2996 | permutation[j] = nextIndex; |
| 2997 | nextIndex++; |
| 2998 | } |
| 2999 | // Build the final view list. |
| 3000 | preorderedList = new ArrayList<>(childrenCount); |
| 3001 | for (int j=0; j<childrenCount; j++) { |
| 3002 | preorderedList.add(children[permutation[j]]); |
| 3003 | } |
| 3004 | } |
| 3005 | } else { |
| 3006 | throw e; |
| 3007 | } |
| 3008 | } |
Alan Viverette | a7b85e6 | 2016-01-22 10:14:02 -0500 | [diff] [blame] | 3009 | |
| 3010 | final View child = getAndVerifyPreorderedView( |
| 3011 | preorderedList, children, childIndex); |
| 3012 | final ViewStructure cstructure = structure.newChild(i); |
Dianne Hackborn | 49b043f | 2015-05-07 14:21:38 -0700 | [diff] [blame] | 3013 | child.dispatchProvideStructure(cstructure); |
Dianne Hackborn | 799d8d0 | 2015-04-22 16:36:39 -0700 | [diff] [blame] | 3014 | } |
Dianne Hackborn | 6251f0d | 2015-04-01 16:45:03 -0700 | [diff] [blame] | 3015 | } |
| 3016 | } |
| 3017 | } |
| 3018 | } |
| 3019 | |
Alan Viverette | a7b85e6 | 2016-01-22 10:14:02 -0500 | [diff] [blame] | 3020 | private static View getAndVerifyPreorderedView(ArrayList<View> preorderedList, View[] children, |
| 3021 | int childIndex) { |
| 3022 | final View child; |
| 3023 | if (preorderedList != null) { |
| 3024 | child = preorderedList.get(childIndex); |
| 3025 | if (child == null) { |
| 3026 | throw new RuntimeException("Invalid preorderedList contained null child at index " |
| 3027 | + childIndex); |
| 3028 | } |
| 3029 | } else { |
| 3030 | child = children[childIndex]; |
| 3031 | } |
| 3032 | return child; |
| 3033 | } |
| 3034 | |
Alan Viverette | a54956a | 2015-01-07 16:05:02 -0800 | [diff] [blame] | 3035 | /** @hide */ |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 3036 | @Override |
Alan Viverette | a54956a | 2015-01-07 16:05:02 -0800 | [diff] [blame] | 3037 | public void onInitializeAccessibilityNodeInfoInternal(AccessibilityNodeInfo info) { |
Svetoslav Ganov | 031d9c1 | 2011-09-09 16:41:13 -0700 | [diff] [blame] | 3038 | super.onInitializeAccessibilityNodeInfoInternal(info); |
Svet Ganov | 55bdb10 | 2015-02-06 12:41:17 -0800 | [diff] [blame] | 3039 | if (getAccessibilityNodeProvider() != null) { |
| 3040 | return; |
| 3041 | } |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 3042 | if (mAttachInfo != null) { |
Alan Viverette | f0aed09 | 2013-11-06 15:33:03 -0800 | [diff] [blame] | 3043 | final ArrayList<View> childrenForAccessibility = mAttachInfo.mTempArrayList; |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 3044 | childrenForAccessibility.clear(); |
| 3045 | addChildrenForAccessibility(childrenForAccessibility); |
| 3046 | final int childrenForAccessibilityCount = childrenForAccessibility.size(); |
| 3047 | for (int i = 0; i < childrenForAccessibilityCount; i++) { |
Alan Viverette | f0aed09 | 2013-11-06 15:33:03 -0800 | [diff] [blame] | 3048 | final View child = childrenForAccessibility.get(i); |
| 3049 | info.addChildUnchecked(child); |
Svetoslav Ganov | ea1da3d | 2011-06-15 17:16:02 -0700 | [diff] [blame] | 3050 | } |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 3051 | childrenForAccessibility.clear(); |
Svetoslav Ganov | 8643aa0 | 2011-04-20 12:12:33 -0700 | [diff] [blame] | 3052 | } |
| 3053 | } |
| 3054 | |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 3055 | @Override |
Maxim Bogatov | f399af3 | 2015-06-16 15:15:49 -0700 | [diff] [blame] | 3056 | public CharSequence getAccessibilityClassName() { |
| 3057 | return ViewGroup.class.getName(); |
Svetoslav Ganov | 8a78fd4 | 2012-01-17 14:36:46 -0800 | [diff] [blame] | 3058 | } |
| 3059 | |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 3060 | @Override |
Alan Viverette | 77e9a28 | 2013-09-12 17:16:09 -0700 | [diff] [blame] | 3061 | public void notifySubtreeAccessibilityStateChanged(View child, View source, int changeType) { |
| 3062 | // If this is a live region, we should send a subtree change event |
| 3063 | // from this view. Otherwise, we can let it propagate up. |
| 3064 | if (getAccessibilityLiveRegion() != ACCESSIBILITY_LIVE_REGION_NONE) { |
| 3065 | notifyViewAccessibilityStateChangedIfNeeded(changeType); |
| 3066 | } else if (mParent != null) { |
Adam Powell | 504a10f | 2013-07-11 15:25:59 -0700 | [diff] [blame] | 3067 | try { |
Alan Viverette | 77e9a28 | 2013-09-12 17:16:09 -0700 | [diff] [blame] | 3068 | mParent.notifySubtreeAccessibilityStateChanged(this, source, changeType); |
Adam Powell | 504a10f | 2013-07-11 15:25:59 -0700 | [diff] [blame] | 3069 | } catch (AbstractMethodError e) { |
| 3070 | Log.e(VIEW_LOG_TAG, mParent.getClass().getSimpleName() + |
| 3071 | " does not fully implement ViewParent", e); |
| 3072 | } |
Svetoslav | 6254f48 | 2013-06-04 17:22:14 -0700 | [diff] [blame] | 3073 | } |
| 3074 | } |
| 3075 | |
| 3076 | @Override |
| 3077 | void resetSubtreeAccessibilityStateChanged() { |
| 3078 | super.resetSubtreeAccessibilityStateChanged(); |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 3079 | View[] children = mChildren; |
| 3080 | final int childCount = mChildrenCount; |
| 3081 | for (int i = 0; i < childCount; i++) { |
Svetoslav | 6254f48 | 2013-06-04 17:22:14 -0700 | [diff] [blame] | 3082 | children[i].resetSubtreeAccessibilityStateChanged(); |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 3083 | } |
| 3084 | } |
| 3085 | |
| 3086 | /** |
Phil Weaver | 1f22254 | 2016-01-08 11:49:32 -0800 | [diff] [blame] | 3087 | * Counts the number of children of this View that will be sent to an accessibility service. |
| 3088 | * |
| 3089 | * @return The number of children an {@code AccessibilityNodeInfo} rooted at this View |
| 3090 | * would have. |
| 3091 | */ |
| 3092 | int getNumChildrenForAccessibility() { |
| 3093 | int numChildrenForAccessibility = 0; |
| 3094 | for (int i = 0; i < getChildCount(); i++) { |
| 3095 | View child = getChildAt(i); |
| 3096 | if (child.includeForAccessibility()) { |
| 3097 | numChildrenForAccessibility++; |
| 3098 | } else if (child instanceof ViewGroup) { |
| 3099 | numChildrenForAccessibility += ((ViewGroup) child) |
| 3100 | .getNumChildrenForAccessibility(); |
| 3101 | } |
| 3102 | } |
| 3103 | return numChildrenForAccessibility; |
| 3104 | } |
| 3105 | |
| 3106 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3107 | * {@inheritDoc} |
Adam Powell | b6ab098 | 2015-01-07 17:00:12 -0800 | [diff] [blame] | 3108 | * |
| 3109 | * <p>Subclasses should always call <code>super.onNestedPrePerformAccessibilityAction</code></p> |
| 3110 | * |
| 3111 | * @param target The target view dispatching this action |
| 3112 | * @param action Action being performed; see |
| 3113 | * {@link android.view.accessibility.AccessibilityNodeInfo} |
| 3114 | * @param args Optional action arguments |
| 3115 | * @return false by default. Subclasses should return true if they handle the event. |
| 3116 | */ |
| 3117 | @Override |
| 3118 | public boolean onNestedPrePerformAccessibilityAction(View target, int action, Bundle args) { |
| 3119 | return false; |
| 3120 | } |
| 3121 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3122 | @Override |
| 3123 | void dispatchDetachedFromWindow() { |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 3124 | // 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] | 3125 | // dispatching motion events to a child; we need to get rid of that |
| 3126 | // child to avoid dispatching events to it after the window is torn |
| 3127 | // down. To make sure we keep the child in a consistent state, we |
| 3128 | // first send it an ACTION_CANCEL motion event. |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 3129 | cancelAndClearTouchTargets(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3130 | |
Jeff Brown | 59a422e | 2012-04-19 15:19:19 -0700 | [diff] [blame] | 3131 | // Similarly, set ACTION_EXIT to all hover targets and clear them. |
| 3132 | exitHoverTargets(); |
| 3133 | |
Chet Haase | 9c08744 | 2011-01-12 16:20:16 -0800 | [diff] [blame] | 3134 | // In case view is detached while transition is running |
Chet Haase | b989502 | 2013-04-02 15:10:58 -0700 | [diff] [blame] | 3135 | mLayoutCalledWhileSuppressed = false; |
Chet Haase | 9c08744 | 2011-01-12 16:20:16 -0800 | [diff] [blame] | 3136 | |
Christopher Tate | 86cab1b | 2011-01-13 20:28:55 -0800 | [diff] [blame] | 3137 | // Tear down our drag tracking |
Vadim Tryshev | 1a68dc9 | 2015-07-20 17:01:50 -0700 | [diff] [blame] | 3138 | mChildrenInterestedInDrag = null; |
| 3139 | mIsInterestedInDrag = false; |
| 3140 | if (mCurrentDragStartEvent != null) { |
| 3141 | mCurrentDragStartEvent.recycle(); |
| 3142 | mCurrentDragStartEvent = null; |
Christopher Tate | 86cab1b | 2011-01-13 20:28:55 -0800 | [diff] [blame] | 3143 | } |
| 3144 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3145 | final int count = mChildrenCount; |
| 3146 | final View[] children = mChildren; |
| 3147 | for (int i = 0; i < count; i++) { |
| 3148 | children[i].dispatchDetachedFromWindow(); |
| 3149 | } |
John Reck | ca7a9da | 2014-03-05 16:29:07 -0800 | [diff] [blame] | 3150 | clearDisappearingChildren(); |
Chet Haase | c633d2f | 2015-04-07 10:29:39 -0700 | [diff] [blame] | 3151 | final int transientCount = mTransientViews == null ? 0 : mTransientIndices.size(); |
| 3152 | for (int i = 0; i < transientCount; ++i) { |
| 3153 | View view = mTransientViews.get(i); |
| 3154 | view.dispatchDetachedFromWindow(); |
| 3155 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3156 | super.dispatchDetachedFromWindow(); |
| 3157 | } |
| 3158 | |
Fabrice Di Meglio | 23c89fd | 2012-08-13 12:17:42 -0700 | [diff] [blame] | 3159 | /** |
| 3160 | * @hide |
| 3161 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3162 | @Override |
Fabrice Di Meglio | 23c89fd | 2012-08-13 12:17:42 -0700 | [diff] [blame] | 3163 | protected void internalSetPadding(int left, int top, int right, int bottom) { |
Romain Guy | 2440e67 | 2012-08-07 14:43:43 -0700 | [diff] [blame] | 3164 | super.internalSetPadding(left, top, right, bottom); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3165 | |
Romain Guy | 13f35f3 | 2011-03-24 12:03:17 -0700 | [diff] [blame] | 3166 | if ((mPaddingLeft | mPaddingTop | mPaddingRight | mPaddingBottom) != 0) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3167 | mGroupFlags |= FLAG_PADDING_NOT_NULL; |
| 3168 | } else { |
| 3169 | mGroupFlags &= ~FLAG_PADDING_NOT_NULL; |
| 3170 | } |
| 3171 | } |
| 3172 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3173 | @Override |
| 3174 | protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) { |
| 3175 | super.dispatchSaveInstanceState(container); |
| 3176 | final int count = mChildrenCount; |
| 3177 | final View[] children = mChildren; |
| 3178 | for (int i = 0; i < count; i++) { |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 3179 | View c = children[i]; |
| 3180 | if ((c.mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED) { |
| 3181 | c.dispatchSaveInstanceState(container); |
| 3182 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3183 | } |
| 3184 | } |
| 3185 | |
| 3186 | /** |
Romain Guy | 9fc2781 | 2011-04-27 14:21:41 -0700 | [diff] [blame] | 3187 | * Perform dispatching of a {@link #saveHierarchyState(android.util.SparseArray)} freeze()} |
| 3188 | * to only this view, not to its children. For use when overriding |
| 3189 | * {@link #dispatchSaveInstanceState(android.util.SparseArray)} dispatchFreeze()} to allow |
| 3190 | * 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] | 3191 | * |
| 3192 | * @param container the container |
| 3193 | */ |
| 3194 | protected void dispatchFreezeSelfOnly(SparseArray<Parcelable> container) { |
| 3195 | super.dispatchSaveInstanceState(container); |
| 3196 | } |
| 3197 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3198 | @Override |
| 3199 | protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) { |
| 3200 | super.dispatchRestoreInstanceState(container); |
| 3201 | final int count = mChildrenCount; |
| 3202 | final View[] children = mChildren; |
| 3203 | for (int i = 0; i < count; i++) { |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 3204 | View c = children[i]; |
| 3205 | if ((c.mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED) { |
| 3206 | c.dispatchRestoreInstanceState(container); |
| 3207 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3208 | } |
| 3209 | } |
| 3210 | |
| 3211 | /** |
Romain Guy | 02739a8 | 2011-05-16 11:43:18 -0700 | [diff] [blame] | 3212 | * Perform dispatching of a {@link #restoreHierarchyState(android.util.SparseArray)} |
| 3213 | * to only this view, not to its children. For use when overriding |
| 3214 | * {@link #dispatchRestoreInstanceState(android.util.SparseArray)} to allow |
| 3215 | * 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] | 3216 | * |
| 3217 | * @param container the container |
| 3218 | */ |
| 3219 | protected void dispatchThawSelfOnly(SparseArray<Parcelable> container) { |
| 3220 | super.dispatchRestoreInstanceState(container); |
| 3221 | } |
| 3222 | |
| 3223 | /** |
| 3224 | * Enables or disables the drawing cache for each child of this view group. |
| 3225 | * |
| 3226 | * @param enabled true to enable the cache, false to dispose of it |
| 3227 | */ |
| 3228 | protected void setChildrenDrawingCacheEnabled(boolean enabled) { |
| 3229 | if (enabled || (mPersistentDrawingCache & PERSISTENT_ALL_CACHES) != PERSISTENT_ALL_CACHES) { |
| 3230 | final View[] children = mChildren; |
| 3231 | final int count = mChildrenCount; |
| 3232 | for (int i = 0; i < count; i++) { |
| 3233 | children[i].setDrawingCacheEnabled(enabled); |
| 3234 | } |
| 3235 | } |
| 3236 | } |
| 3237 | |
| 3238 | @Override |
Romain Guy | 223ff5c | 2010-03-02 17:07:47 -0800 | [diff] [blame] | 3239 | Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) { |
Romain Guy | 65554f2 | 2010-03-22 18:58:21 -0700 | [diff] [blame] | 3240 | int count = mChildrenCount; |
| 3241 | int[] visibilities = null; |
| 3242 | |
Romain Guy | 223ff5c | 2010-03-02 17:07:47 -0800 | [diff] [blame] | 3243 | if (skipChildren) { |
Romain Guy | 65554f2 | 2010-03-22 18:58:21 -0700 | [diff] [blame] | 3244 | visibilities = new int[count]; |
| 3245 | for (int i = 0; i < count; i++) { |
| 3246 | View child = getChildAt(i); |
| 3247 | visibilities[i] = child.getVisibility(); |
| 3248 | if (visibilities[i] == View.VISIBLE) { |
| 3249 | child.setVisibility(INVISIBLE); |
| 3250 | } |
| 3251 | } |
Romain Guy | 223ff5c | 2010-03-02 17:07:47 -0800 | [diff] [blame] | 3252 | } |
| 3253 | |
| 3254 | Bitmap b = super.createSnapshot(quality, backgroundColor, skipChildren); |
Romain Guy | 65554f2 | 2010-03-22 18:58:21 -0700 | [diff] [blame] | 3255 | |
| 3256 | if (skipChildren) { |
| 3257 | for (int i = 0; i < count; i++) { |
| 3258 | getChildAt(i).setVisibility(visibilities[i]); |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 3259 | } |
Romain Guy | 65554f2 | 2010-03-22 18:58:21 -0700 | [diff] [blame] | 3260 | } |
Romain Guy | 223ff5c | 2010-03-02 17:07:47 -0800 | [diff] [blame] | 3261 | |
| 3262 | return b; |
| 3263 | } |
| 3264 | |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 3265 | /** Return true if this ViewGroup is laying out using optical bounds. */ |
| 3266 | boolean isLayoutModeOptical() { |
| 3267 | return mLayoutMode == LAYOUT_MODE_OPTICAL_BOUNDS; |
| 3268 | } |
Romain Guy | cbc6774 | 2012-04-27 16:12:57 -0700 | [diff] [blame] | 3269 | |
Alan Viverette | be463f2 | 2016-01-21 10:50:10 -0500 | [diff] [blame] | 3270 | @Override |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 3271 | Insets computeOpticalInsets() { |
| 3272 | if (isLayoutModeOptical()) { |
| 3273 | int left = 0; |
| 3274 | int top = 0; |
| 3275 | int right = 0; |
| 3276 | int bottom = 0; |
| 3277 | for (int i = 0; i < mChildrenCount; i++) { |
| 3278 | View child = getChildAt(i); |
| 3279 | if (child.getVisibility() == VISIBLE) { |
| 3280 | Insets insets = child.getOpticalInsets(); |
| 3281 | left = Math.max(left, insets.left); |
| 3282 | top = Math.max(top, insets.top); |
| 3283 | right = Math.max(right, insets.right); |
| 3284 | bottom = Math.max(bottom, insets.bottom); |
| 3285 | } |
| 3286 | } |
| 3287 | return Insets.of(left, top, right, bottom); |
| 3288 | } else { |
| 3289 | return Insets.NONE; |
| 3290 | } |
| 3291 | } |
| 3292 | |
| 3293 | private static void fillRect(Canvas canvas, Paint paint, int x1, int y1, int x2, int y2) { |
| 3294 | if (x1 != x2 && y1 != y2) { |
| 3295 | if (x1 > x2) { |
| 3296 | int tmp = x1; x1 = x2; x2 = tmp; |
| 3297 | } |
| 3298 | if (y1 > y2) { |
| 3299 | int tmp = y1; y1 = y2; y2 = tmp; |
| 3300 | } |
| 3301 | canvas.drawRect(x1, y1, x2, y2, paint); |
| 3302 | } |
| 3303 | } |
| 3304 | |
| 3305 | private static int sign(int x) { |
| 3306 | return (x >= 0) ? 1 : -1; |
| 3307 | } |
| 3308 | |
| 3309 | private static void drawCorner(Canvas c, Paint paint, int x1, int y1, int dx, int dy, int lw) { |
| 3310 | fillRect(c, paint, x1, y1, x1 + dx, y1 + lw * sign(dy)); |
| 3311 | fillRect(c, paint, x1, y1, x1 + lw * sign(dx), y1 + dy); |
| 3312 | } |
| 3313 | |
| 3314 | private int dipsToPixels(int dips) { |
| 3315 | float scale = getContext().getResources().getDisplayMetrics().density; |
| 3316 | return (int) (dips * scale + 0.5f); |
| 3317 | } |
| 3318 | |
Romain Guy | 6410c0a | 2013-06-17 11:21:58 -0700 | [diff] [blame] | 3319 | private static void drawRectCorners(Canvas canvas, int x1, int y1, int x2, int y2, Paint paint, |
| 3320 | int lineLength, int lineWidth) { |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 3321 | drawCorner(canvas, paint, x1, y1, lineLength, lineLength, lineWidth); |
| 3322 | drawCorner(canvas, paint, x1, y2, lineLength, -lineLength, lineWidth); |
| 3323 | drawCorner(canvas, paint, x2, y1, -lineLength, lineLength, lineWidth); |
| 3324 | drawCorner(canvas, paint, x2, y2, -lineLength, -lineLength, lineWidth); |
| 3325 | } |
| 3326 | |
| 3327 | private static void fillDifference(Canvas canvas, |
| 3328 | int x2, int y2, int x3, int y3, |
| 3329 | int dx1, int dy1, int dx2, int dy2, Paint paint) { |
| 3330 | int x1 = x2 - dx1; |
| 3331 | int y1 = y2 - dy1; |
| 3332 | |
| 3333 | int x4 = x3 + dx2; |
| 3334 | int y4 = y3 + dy2; |
| 3335 | |
| 3336 | fillRect(canvas, paint, x1, y1, x4, y2); |
| 3337 | fillRect(canvas, paint, x1, y2, x2, y3); |
| 3338 | fillRect(canvas, paint, x3, y2, x4, y3); |
| 3339 | fillRect(canvas, paint, x1, y3, x4, y4); |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 3340 | } |
| 3341 | |
| 3342 | /** |
| 3343 | * @hide |
| 3344 | */ |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 3345 | protected void onDebugDrawMargins(Canvas canvas, Paint paint) { |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 3346 | for (int i = 0; i < getChildCount(); i++) { |
| 3347 | View c = getChildAt(i); |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 3348 | c.getLayoutParams().onDebugDraw(c, canvas, paint); |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 3349 | } |
| 3350 | } |
| 3351 | |
| 3352 | /** |
| 3353 | * @hide |
| 3354 | */ |
| 3355 | protected void onDebugDraw(Canvas canvas) { |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 3356 | Paint paint = getDebugPaint(); |
| 3357 | |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 3358 | // Draw optical bounds |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 3359 | { |
| 3360 | paint.setColor(Color.RED); |
| 3361 | paint.setStyle(Paint.Style.STROKE); |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 3362 | |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 3363 | for (int i = 0; i < getChildCount(); i++) { |
| 3364 | View c = getChildAt(i); |
Chris Craik | a1dab8b | 2015-06-30 13:51:25 -0700 | [diff] [blame] | 3365 | if (c.getVisibility() != View.GONE) { |
| 3366 | Insets insets = c.getOpticalInsets(); |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 3367 | |
Chris Craik | a1dab8b | 2015-06-30 13:51:25 -0700 | [diff] [blame] | 3368 | drawRect(canvas, paint, |
| 3369 | c.getLeft() + insets.left, |
| 3370 | c.getTop() + insets.top, |
| 3371 | c.getRight() - insets.right - 1, |
| 3372 | c.getBottom() - insets.bottom - 1); |
| 3373 | } |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 3374 | } |
| 3375 | } |
| 3376 | |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 3377 | // Draw margins |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 3378 | { |
| 3379 | paint.setColor(Color.argb(63, 255, 0, 255)); |
| 3380 | paint.setStyle(Paint.Style.FILL); |
Philip Milne | 604f440 | 2012-04-24 19:27:11 -0700 | [diff] [blame] | 3381 | |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 3382 | onDebugDrawMargins(canvas, paint); |
| 3383 | } |
| 3384 | |
| 3385 | // Draw clip bounds |
| 3386 | { |
| 3387 | paint.setColor(Color.rgb(63, 127, 255)); |
| 3388 | paint.setStyle(Paint.Style.FILL); |
| 3389 | |
| 3390 | int lineLength = dipsToPixels(8); |
| 3391 | int lineWidth = dipsToPixels(1); |
| 3392 | for (int i = 0; i < getChildCount(); i++) { |
| 3393 | View c = getChildAt(i); |
Chris Craik | a1dab8b | 2015-06-30 13:51:25 -0700 | [diff] [blame] | 3394 | if (c.getVisibility() != View.GONE) { |
| 3395 | drawRectCorners(canvas, c.getLeft(), c.getTop(), c.getRight(), c.getBottom(), |
| 3396 | paint, lineLength, lineWidth); |
| 3397 | } |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 3398 | } |
Philip Milne | 604f440 | 2012-04-24 19:27:11 -0700 | [diff] [blame] | 3399 | } |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 3400 | } |
| 3401 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3402 | @Override |
| 3403 | protected void dispatchDraw(Canvas canvas) { |
Chris Craik | a753f4c | 2014-07-24 12:39:17 -0700 | [diff] [blame] | 3404 | boolean usingRenderNodeProperties = canvas.isRecordingFor(mRenderNode); |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 3405 | final int childrenCount = mChildrenCount; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3406 | final View[] children = mChildren; |
| 3407 | int flags = mGroupFlags; |
| 3408 | |
| 3409 | if ((flags & FLAG_RUN_ANIMATION) != 0 && canAnimate()) { |
Romain Guy | 0d9275e | 2010-10-26 14:22:30 -0700 | [diff] [blame] | 3410 | final boolean buildCache = !isHardwareAccelerated(); |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 3411 | for (int i = 0; i < childrenCount; i++) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3412 | final View child = children[i]; |
| 3413 | if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) { |
| 3414 | final LayoutParams params = child.getLayoutParams(); |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 3415 | attachLayoutAnimationParameters(child, params, i, childrenCount); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3416 | bindLayoutAnimation(child); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3417 | } |
| 3418 | } |
| 3419 | |
| 3420 | final LayoutAnimationController controller = mLayoutAnimationController; |
| 3421 | if (controller.willOverlap()) { |
| 3422 | mGroupFlags |= FLAG_OPTIMIZE_INVALIDATE; |
| 3423 | } |
| 3424 | |
| 3425 | controller.start(); |
| 3426 | |
| 3427 | mGroupFlags &= ~FLAG_RUN_ANIMATION; |
| 3428 | mGroupFlags &= ~FLAG_ANIMATION_DONE; |
| 3429 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3430 | if (mAnimationListener != null) { |
| 3431 | mAnimationListener.onAnimationStart(controller.getAnimation()); |
| 3432 | } |
| 3433 | } |
| 3434 | |
Selim Cinek | 19cadc2 | 2014-04-16 17:27:19 +0200 | [diff] [blame] | 3435 | int clipSaveCount = 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3436 | final boolean clipToPadding = (flags & CLIP_TO_PADDING_MASK) == CLIP_TO_PADDING_MASK; |
| 3437 | if (clipToPadding) { |
Chris Craik | e4cf152 | 2014-08-04 17:55:22 -0700 | [diff] [blame] | 3438 | clipSaveCount = canvas.save(); |
Romain Guy | 8f2d94f | 2009-03-25 18:04:42 -0700 | [diff] [blame] | 3439 | canvas.clipRect(mScrollX + mPaddingLeft, mScrollY + mPaddingTop, |
| 3440 | mScrollX + mRight - mLeft - mPaddingRight, |
| 3441 | mScrollY + mBottom - mTop - mPaddingBottom); |
Selim Cinek | 19cadc2 | 2014-04-16 17:27:19 +0200 | [diff] [blame] | 3442 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3443 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3444 | // We will draw our child's animation, let's reset the flag |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 3445 | mPrivateFlags &= ~PFLAG_DRAW_ANIMATION; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3446 | mGroupFlags &= ~FLAG_INVALIDATE_REQUIRED; |
| 3447 | |
| 3448 | boolean more = false; |
| 3449 | final long drawingTime = getDrawingTime(); |
| 3450 | |
Chris Craik | 8afd0f2 | 2014-08-21 17:41:57 -0700 | [diff] [blame] | 3451 | if (usingRenderNodeProperties) canvas.insertReorderBarrier(); |
Chet Haase | c633d2f | 2015-04-07 10:29:39 -0700 | [diff] [blame] | 3452 | final int transientCount = mTransientIndices == null ? 0 : mTransientIndices.size(); |
| 3453 | int transientIndex = transientCount != 0 ? 0 : -1; |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 3454 | // Only use the preordered list if not HW accelerated, since the HW pipeline will do the |
| 3455 | // draw reordering internally |
Chris Craik | a753f4c | 2014-07-24 12:39:17 -0700 | [diff] [blame] | 3456 | final ArrayList<View> preorderedList = usingRenderNodeProperties |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 3457 | ? null : buildOrderedChildList(); |
| 3458 | final boolean customOrder = preorderedList == null |
| 3459 | && isChildrenDrawingOrderEnabled(); |
| 3460 | for (int i = 0; i < childrenCount; i++) { |
Chet Haase | c633d2f | 2015-04-07 10:29:39 -0700 | [diff] [blame] | 3461 | while (transientIndex >= 0 && mTransientIndices.get(transientIndex) == i) { |
| 3462 | final View transientChild = mTransientViews.get(transientIndex); |
| 3463 | if ((transientChild.mViewFlags & VISIBILITY_MASK) == VISIBLE || |
| 3464 | transientChild.getAnimation() != null) { |
| 3465 | more |= drawChild(canvas, transientChild, drawingTime); |
| 3466 | } |
| 3467 | transientIndex++; |
| 3468 | if (transientIndex >= transientCount) { |
| 3469 | transientIndex = -1; |
| 3470 | } |
| 3471 | } |
Alan Viverette | a7b85e6 | 2016-01-22 10:14:02 -0500 | [diff] [blame] | 3472 | |
| 3473 | final int childIndex = getAndVerifyPreorderedIndex(childrenCount, i, customOrder); |
| 3474 | final View child = getAndVerifyPreorderedView(preorderedList, children, childIndex); |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 3475 | if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null) { |
| 3476 | more |= drawChild(canvas, child, drawingTime); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3477 | } |
| 3478 | } |
Chet Haase | c633d2f | 2015-04-07 10:29:39 -0700 | [diff] [blame] | 3479 | while (transientIndex >= 0) { |
| 3480 | // there may be additional transient views after the normal views |
| 3481 | final View transientChild = mTransientViews.get(transientIndex); |
| 3482 | if ((transientChild.mViewFlags & VISIBILITY_MASK) == VISIBLE || |
| 3483 | transientChild.getAnimation() != null) { |
| 3484 | more |= drawChild(canvas, transientChild, drawingTime); |
| 3485 | } |
| 3486 | transientIndex++; |
| 3487 | if (transientIndex >= transientCount) { |
| 3488 | break; |
| 3489 | } |
| 3490 | } |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 3491 | if (preorderedList != null) preorderedList.clear(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3492 | |
| 3493 | // Draw any disappearing views that have animations |
| 3494 | if (mDisappearingChildren != null) { |
| 3495 | final ArrayList<View> disappearingChildren = mDisappearingChildren; |
| 3496 | final int disappearingCount = disappearingChildren.size() - 1; |
| 3497 | // Go backwards -- we may delete as animations finish |
| 3498 | for (int i = disappearingCount; i >= 0; i--) { |
| 3499 | final View child = disappearingChildren.get(i); |
| 3500 | more |= drawChild(canvas, child, drawingTime); |
| 3501 | } |
| 3502 | } |
Chris Craik | 8afd0f2 | 2014-08-21 17:41:57 -0700 | [diff] [blame] | 3503 | if (usingRenderNodeProperties) canvas.insertInorderBarrier(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3504 | |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 3505 | if (debugDraw()) { |
| 3506 | onDebugDraw(canvas); |
| 3507 | } |
| 3508 | |
Chris Craik | e4cf152 | 2014-08-04 17:55:22 -0700 | [diff] [blame] | 3509 | if (clipToPadding) { |
Selim Cinek | 19cadc2 | 2014-04-16 17:27:19 +0200 | [diff] [blame] | 3510 | canvas.restoreToCount(clipSaveCount); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3511 | } |
| 3512 | |
| 3513 | // mGroupFlags might have been updated by drawChild() |
| 3514 | flags = mGroupFlags; |
| 3515 | |
| 3516 | if ((flags & FLAG_INVALIDATE_REQUIRED) == FLAG_INVALIDATE_REQUIRED) { |
Romain Guy | 849d0a3 | 2011-02-01 17:20:48 -0800 | [diff] [blame] | 3517 | invalidate(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3518 | } |
| 3519 | |
| 3520 | if ((flags & FLAG_ANIMATION_DONE) == 0 && (flags & FLAG_NOTIFY_ANIMATION_LISTENER) == 0 && |
| 3521 | mLayoutAnimationController.isDone() && !more) { |
| 3522 | // We want to erase the drawing cache and notify the listener after the |
| 3523 | // next frame is drawn because one extra invalidate() is caused by |
| 3524 | // drawChild() after the animation is over |
| 3525 | mGroupFlags |= FLAG_NOTIFY_ANIMATION_LISTENER; |
| 3526 | final Runnable end = new Runnable() { |
Alan Viverette | be463f2 | 2016-01-21 10:50:10 -0500 | [diff] [blame] | 3527 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3528 | public void run() { |
| 3529 | notifyAnimationListener(); |
| 3530 | } |
| 3531 | }; |
| 3532 | post(end); |
| 3533 | } |
| 3534 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 3535 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3536 | /** |
Chet Haase | edf6f4b | 2013-03-26 07:55:30 -0700 | [diff] [blame] | 3537 | * Returns the ViewGroupOverlay for this view group, creating it if it does |
| 3538 | * not yet exist. In addition to {@link ViewOverlay}'s support for drawables, |
| 3539 | * {@link ViewGroupOverlay} allows views to be added to the overlay. These |
| 3540 | * views, like overlay drawables, are visual-only; they do not receive input |
| 3541 | * events and should not be used as anything other than a temporary |
| 3542 | * representation of a view in a parent container, such as might be used |
| 3543 | * by an animation effect. |
| 3544 | * |
Chet Haase | 9539949 | 2013-04-08 14:30:31 -0700 | [diff] [blame] | 3545 | * <p>Note: Overlays do not currently work correctly with {@link |
| 3546 | * SurfaceView} or {@link TextureView}; contents in overlays for these |
| 3547 | * types of views may not display correctly.</p> |
| 3548 | * |
Chet Haase | edf6f4b | 2013-03-26 07:55:30 -0700 | [diff] [blame] | 3549 | * @return The ViewGroupOverlay object for this view. |
| 3550 | * @see ViewGroupOverlay |
| 3551 | */ |
| 3552 | @Override |
| 3553 | public ViewGroupOverlay getOverlay() { |
| 3554 | if (mOverlay == null) { |
| 3555 | mOverlay = new ViewGroupOverlay(mContext, this); |
| 3556 | } |
| 3557 | return (ViewGroupOverlay) mOverlay; |
| 3558 | } |
| 3559 | |
| 3560 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3561 | * Returns the index of the child to draw for this iteration. Override this |
| 3562 | * if you want to change the drawing order of children. By default, it |
| 3563 | * returns i. |
| 3564 | * <p> |
Romain Guy | 293451e | 2009-11-04 13:59:48 -0800 | [diff] [blame] | 3565 | * NOTE: In order for this method to be called, you must enable child ordering |
| 3566 | * first by calling {@link #setChildrenDrawingOrderEnabled(boolean)}. |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 3567 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3568 | * @param i The current iteration. |
| 3569 | * @return The index of the child to draw this iteration. |
Chet Haase | 5c13d89 | 2010-10-08 08:37:55 -0700 | [diff] [blame] | 3570 | * |
Romain Guy | 293451e | 2009-11-04 13:59:48 -0800 | [diff] [blame] | 3571 | * @see #setChildrenDrawingOrderEnabled(boolean) |
| 3572 | * @see #isChildrenDrawingOrderEnabled() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3573 | */ |
| 3574 | protected int getChildDrawingOrder(int childCount, int i) { |
| 3575 | return i; |
| 3576 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 3577 | |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 3578 | private boolean hasChildWithZ() { |
| 3579 | for (int i = 0; i < mChildrenCount; i++) { |
| 3580 | if (mChildren[i].getZ() != 0) return true; |
| 3581 | } |
| 3582 | return false; |
| 3583 | } |
| 3584 | |
| 3585 | /** |
| 3586 | * 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] | 3587 | * sorted first by Z, then by child drawing order (if applicable). This list must be cleared |
| 3588 | * after use to avoid leaking child Views. |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 3589 | * |
| 3590 | * Uses a stable, insertion sort which is commonly O(n) for ViewGroups with very few elevated |
| 3591 | * children. |
| 3592 | */ |
George Mount | 8120652 | 2014-09-26 21:53:39 -0700 | [diff] [blame] | 3593 | ArrayList<View> buildOrderedChildList() { |
Alan Viverette | a7b85e6 | 2016-01-22 10:14:02 -0500 | [diff] [blame] | 3594 | final int childrenCount = mChildrenCount; |
| 3595 | if (childrenCount <= 1 || !hasChildWithZ()) return null; |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 3596 | |
| 3597 | if (mPreSortedChildren == null) { |
Alan Viverette | a7b85e6 | 2016-01-22 10:14:02 -0500 | [diff] [blame] | 3598 | mPreSortedChildren = new ArrayList<>(childrenCount); |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 3599 | } else { |
Alan Viverette | a7b85e6 | 2016-01-22 10:14:02 -0500 | [diff] [blame] | 3600 | mPreSortedChildren.ensureCapacity(childrenCount); |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 3601 | } |
| 3602 | |
Alan Viverette | a7b85e6 | 2016-01-22 10:14:02 -0500 | [diff] [blame] | 3603 | final boolean customOrder = isChildrenDrawingOrderEnabled(); |
| 3604 | for (int i = 0; i < childrenCount; i++) { |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 3605 | // add next child (in child order) to end of list |
Alan Viverette | a7b85e6 | 2016-01-22 10:14:02 -0500 | [diff] [blame] | 3606 | final int childIndex = getAndVerifyPreorderedIndex(childrenCount, i, customOrder); |
| 3607 | final View nextChild = mChildren[childIndex]; |
| 3608 | final float currentZ = nextChild.getZ(); |
Chris Craik | ab008f0 | 2014-05-23 17:55:03 -0700 | [diff] [blame] | 3609 | |
| 3610 | // insert ahead of any Views with greater Z |
| 3611 | int insertIndex = i; |
| 3612 | while (insertIndex > 0 && mPreSortedChildren.get(insertIndex - 1).getZ() > currentZ) { |
| 3613 | insertIndex--; |
| 3614 | } |
| 3615 | mPreSortedChildren.add(insertIndex, nextChild); |
| 3616 | } |
| 3617 | return mPreSortedChildren; |
| 3618 | } |
| 3619 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3620 | private void notifyAnimationListener() { |
| 3621 | mGroupFlags &= ~FLAG_NOTIFY_ANIMATION_LISTENER; |
| 3622 | mGroupFlags |= FLAG_ANIMATION_DONE; |
| 3623 | |
| 3624 | if (mAnimationListener != null) { |
| 3625 | final Runnable end = new Runnable() { |
Alan Viverette | be463f2 | 2016-01-21 10:50:10 -0500 | [diff] [blame] | 3626 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3627 | public void run() { |
| 3628 | mAnimationListener.onAnimationEnd(mLayoutAnimationController.getAnimation()); |
| 3629 | } |
| 3630 | }; |
| 3631 | post(end); |
| 3632 | } |
| 3633 | |
Romain Guy | 849d0a3 | 2011-02-01 17:20:48 -0800 | [diff] [blame] | 3634 | invalidate(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3635 | } |
| 3636 | |
| 3637 | /** |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 3638 | * This method is used to cause children of this ViewGroup to restore or recreate their |
| 3639 | * display lists. It is called by getDisplayList() when the parent ViewGroup does not need |
| 3640 | * to recreate its own display list, which would happen if it went through the normal |
| 3641 | * draw/dispatchDraw mechanisms. |
| 3642 | * |
| 3643 | * @hide |
| 3644 | */ |
| 3645 | @Override |
| 3646 | protected void dispatchGetDisplayList() { |
| 3647 | final int count = mChildrenCount; |
| 3648 | final View[] children = mChildren; |
| 3649 | for (int i = 0; i < count; i++) { |
| 3650 | final View child = children[i]; |
John Reck | c2330f5 | 2015-04-28 13:18:52 -0700 | [diff] [blame] | 3651 | if (((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null)) { |
Chet Haase | 6c0665f | 2014-08-01 13:32:27 -0700 | [diff] [blame] | 3652 | recreateChildDisplayList(child); |
Romain Guy | 2f57ba5 | 2011-02-03 18:03:29 -0800 | [diff] [blame] | 3653 | } |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 3654 | } |
Chet Haase | 91cedf1 | 2013-03-11 07:56:30 -0700 | [diff] [blame] | 3655 | if (mOverlay != null) { |
Chet Haase | edf6f4b | 2013-03-26 07:55:30 -0700 | [diff] [blame] | 3656 | View overlayView = mOverlay.getOverlayView(); |
Chet Haase | 6c0665f | 2014-08-01 13:32:27 -0700 | [diff] [blame] | 3657 | recreateChildDisplayList(overlayView); |
Chet Haase | 91cedf1 | 2013-03-11 07:56:30 -0700 | [diff] [blame] | 3658 | } |
Chet Haase | 6c0665f | 2014-08-01 13:32:27 -0700 | [diff] [blame] | 3659 | if (mDisappearingChildren != null) { |
| 3660 | final ArrayList<View> disappearingChildren = mDisappearingChildren; |
| 3661 | final int disappearingCount = disappearingChildren.size(); |
| 3662 | for (int i = 0; i < disappearingCount; ++i) { |
| 3663 | final View child = disappearingChildren.get(i); |
| 3664 | recreateChildDisplayList(child); |
| 3665 | } |
| 3666 | } |
| 3667 | } |
| 3668 | |
| 3669 | private void recreateChildDisplayList(View child) { |
Chris Craik | 31a2d06 | 2015-05-01 14:22:47 -0700 | [diff] [blame] | 3670 | child.mRecreateDisplayList = (child.mPrivateFlags & PFLAG_INVALIDATED) != 0; |
Chet Haase | 6c0665f | 2014-08-01 13:32:27 -0700 | [diff] [blame] | 3671 | child.mPrivateFlags &= ~PFLAG_INVALIDATED; |
Chris Craik | 31a2d06 | 2015-05-01 14:22:47 -0700 | [diff] [blame] | 3672 | child.updateDisplayListIfDirty(); |
Chet Haase | 6c0665f | 2014-08-01 13:32:27 -0700 | [diff] [blame] | 3673 | child.mRecreateDisplayList = false; |
Chet Haase | daf98e9 | 2011-01-10 14:10:36 -0800 | [diff] [blame] | 3674 | } |
| 3675 | |
| 3676 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3677 | * Draw one child of this View Group. This method is responsible for getting |
| 3678 | * the canvas in the right state. This includes clipping, translating so |
| 3679 | * that the child's scrolled origin is at 0, 0, and applying any animation |
| 3680 | * transformations. |
| 3681 | * |
| 3682 | * @param canvas The canvas on which to draw the child |
| 3683 | * @param child Who to draw |
Chet Haase | bcca79a | 2012-02-14 08:45:14 -0800 | [diff] [blame] | 3684 | * @param drawingTime The time at which draw is occurring |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3685 | * @return True if an invalidate() was issued |
| 3686 | */ |
| 3687 | protected boolean drawChild(Canvas canvas, View child, long drawingTime) { |
Chet Haase | 64a48c1 | 2012-02-13 16:33:29 -0800 | [diff] [blame] | 3688 | return child.draw(canvas, this, drawingTime); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3689 | } |
| 3690 | |
Alan Viverette | 922e1c6 | 2015-05-05 17:18:27 -0700 | [diff] [blame] | 3691 | @Override |
| 3692 | void getScrollIndicatorBounds(@NonNull Rect out) { |
| 3693 | super.getScrollIndicatorBounds(out); |
| 3694 | |
| 3695 | // If we have padding and we're supposed to clip children to that |
| 3696 | // padding, offset the scroll indicators to match our clip bounds. |
| 3697 | final boolean clipToPadding = (mGroupFlags & CLIP_TO_PADDING_MASK) == CLIP_TO_PADDING_MASK; |
| 3698 | if (clipToPadding) { |
| 3699 | out.left += mPaddingLeft; |
| 3700 | out.right -= mPaddingRight; |
| 3701 | out.top += mPaddingTop; |
| 3702 | out.bottom -= mPaddingBottom; |
| 3703 | } |
| 3704 | } |
| 3705 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3706 | /** |
Chris Craik | d863a10 | 2013-12-19 13:31:15 -0800 | [diff] [blame] | 3707 | * 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] | 3708 | * The default value is true. |
| 3709 | * @see #setClipChildren(boolean) |
| 3710 | * |
| 3711 | * @return True if the group's children will be clipped to their bounds, |
| 3712 | * false otherwise. |
| 3713 | */ |
Chris Craik | 5c75c52 | 2014-09-05 14:08:08 -0700 | [diff] [blame] | 3714 | @ViewDebug.ExportedProperty(category = "drawing") |
Chet Haase | 430742f | 2013-04-12 11:18:36 -0700 | [diff] [blame] | 3715 | public boolean getClipChildren() { |
| 3716 | return ((mGroupFlags & FLAG_CLIP_CHILDREN) != 0); |
| 3717 | } |
| 3718 | |
| 3719 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3720 | * By default, children are clipped to their bounds before drawing. This |
| 3721 | * allows view groups to override this behavior for animations, etc. |
| 3722 | * |
| 3723 | * @param clipChildren true to clip children to their bounds, |
| 3724 | * false otherwise |
| 3725 | * @attr ref android.R.styleable#ViewGroup_clipChildren |
| 3726 | */ |
| 3727 | public void setClipChildren(boolean clipChildren) { |
Chet Haase | a1cff50 | 2012-02-21 13:43:44 -0800 | [diff] [blame] | 3728 | boolean previousValue = (mGroupFlags & FLAG_CLIP_CHILDREN) == FLAG_CLIP_CHILDREN; |
| 3729 | if (clipChildren != previousValue) { |
| 3730 | setBooleanFlag(FLAG_CLIP_CHILDREN, clipChildren); |
Chet Haase | 1271e2c | 2012-04-20 09:54:27 -0700 | [diff] [blame] | 3731 | for (int i = 0; i < mChildrenCount; ++i) { |
| 3732 | View child = getChildAt(i); |
Chris Craik | 64a12e1 | 2014-03-28 18:12:12 -0700 | [diff] [blame] | 3733 | if (child.mRenderNode != null) { |
| 3734 | child.mRenderNode.setClipToBounds(clipChildren); |
Chet Haase | a1cff50 | 2012-02-21 13:43:44 -0800 | [diff] [blame] | 3735 | } |
| 3736 | } |
John Reck | aae9f3b | 2014-07-28 09:30:36 -0700 | [diff] [blame] | 3737 | invalidate(true); |
Chet Haase | a1cff50 | 2012-02-21 13:43:44 -0800 | [diff] [blame] | 3738 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3739 | } |
| 3740 | |
| 3741 | /** |
Doris Liu | b134b5e | 2015-05-28 17:26:46 -0700 | [diff] [blame] | 3742 | * Sets whether this ViewGroup will clip its children to its padding and resize (but not |
| 3743 | * clip) any EdgeEffect to the padded region, if padding is present. |
Chris Craik | b165296 | 2014-11-14 17:05:06 -0800 | [diff] [blame] | 3744 | * <p> |
| 3745 | * By default, children are clipped to the padding of their parent |
Doris Liu | b134b5e | 2015-05-28 17:26:46 -0700 | [diff] [blame] | 3746 | * 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] | 3747 | * |
Doris Liu | b134b5e | 2015-05-28 17:26:46 -0700 | [diff] [blame] | 3748 | * @param clipToPadding true to clip children to the padding of the group, and resize (but |
| 3749 | * 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] | 3750 | * @attr ref android.R.styleable#ViewGroup_clipToPadding |
| 3751 | */ |
| 3752 | public void setClipToPadding(boolean clipToPadding) { |
John Reck | 9fa3a24 | 2014-06-27 15:57:19 -0700 | [diff] [blame] | 3753 | if (hasBooleanFlag(FLAG_CLIP_TO_PADDING) != clipToPadding) { |
| 3754 | setBooleanFlag(FLAG_CLIP_TO_PADDING, clipToPadding); |
John Reck | aae9f3b | 2014-07-28 09:30:36 -0700 | [diff] [blame] | 3755 | invalidate(true); |
John Reck | 9fa3a24 | 2014-06-27 15:57:19 -0700 | [diff] [blame] | 3756 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3757 | } |
| 3758 | |
| 3759 | /** |
Doris Liu | b134b5e | 2015-05-28 17:26:46 -0700 | [diff] [blame] | 3760 | * Returns whether this ViewGroup will clip its children to its padding, and resize (but |
| 3761 | * not clip) any EdgeEffect to the padded region, if padding is present. |
Chris Craik | b165296 | 2014-11-14 17:05:06 -0800 | [diff] [blame] | 3762 | * <p> |
| 3763 | * By default, children are clipped to the padding of their parent |
| 3764 | * Viewgroup. This clipping behavior is only enabled if padding is non-zero. |
Adam Powell | 1c35b08 | 2014-07-11 15:37:15 -0700 | [diff] [blame] | 3765 | * |
Doris Liu | b134b5e | 2015-05-28 17:26:46 -0700 | [diff] [blame] | 3766 | * @return true if this ViewGroup clips children to its padding and resizes (but doesn't |
| 3767 | * clip) any EdgeEffect to the padded region, false otherwise. |
Adam Powell | 1c35b08 | 2014-07-11 15:37:15 -0700 | [diff] [blame] | 3768 | * |
| 3769 | * @attr ref android.R.styleable#ViewGroup_clipToPadding |
| 3770 | */ |
Chris Craik | 5c75c52 | 2014-09-05 14:08:08 -0700 | [diff] [blame] | 3771 | @ViewDebug.ExportedProperty(category = "drawing") |
Adam Powell | 1c35b08 | 2014-07-11 15:37:15 -0700 | [diff] [blame] | 3772 | public boolean getClipToPadding() { |
| 3773 | return hasBooleanFlag(FLAG_CLIP_TO_PADDING); |
| 3774 | } |
| 3775 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3776 | @Override |
| 3777 | public void dispatchSetSelected(boolean selected) { |
| 3778 | final View[] children = mChildren; |
| 3779 | final int count = mChildrenCount; |
| 3780 | for (int i = 0; i < count; i++) { |
| 3781 | children[i].setSelected(selected); |
| 3782 | } |
| 3783 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 3784 | |
Dianne Hackborn | d0fa371 | 2010-09-14 18:57:14 -0700 | [diff] [blame] | 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 | */ |
Alan Viverette | be463f2 | 2016-01-21 10:50:10 -0500 | [diff] [blame] | 4189 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4190 | public void addView(View child, LayoutParams params) { |
| 4191 | addView(child, -1, params); |
| 4192 | } |
| 4193 | |
| 4194 | /** |
| 4195 | * Adds a child view with the specified layout parameters. |
| 4196 | * |
Romain Guy | 393a52c | 2012-05-22 20:21:08 -0700 | [diff] [blame] | 4197 | * <p><strong>Note:</strong> do not invoke this method from |
| 4198 | * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)}, |
| 4199 | * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p> |
| 4200 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4201 | * @param child the child view to add |
Alan Viverette | 77bb6f1 | 2015-02-11 17:24:33 -0800 | [diff] [blame] | 4202 | * @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] | 4203 | * @param params the layout parameters to set on the child |
| 4204 | */ |
| 4205 | public void addView(View child, int index, LayoutParams params) { |
| 4206 | if (DBG) { |
| 4207 | System.out.println(this + " addView"); |
| 4208 | } |
| 4209 | |
Adam Powell | 45a9da5 | 2014-10-09 09:44:18 -0700 | [diff] [blame] | 4210 | if (child == null) { |
| 4211 | throw new IllegalArgumentException("Cannot add a null child view to a ViewGroup"); |
| 4212 | } |
| 4213 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4214 | // addViewInner() will call child.requestLayout() when setting the new LayoutParams |
| 4215 | // therefore, we call requestLayout() on ourselves before, so that the child's request |
| 4216 | // will be blocked at our level |
| 4217 | requestLayout(); |
Romain Guy | 849d0a3 | 2011-02-01 17:20:48 -0800 | [diff] [blame] | 4218 | invalidate(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4219 | addViewInner(child, index, params, false); |
| 4220 | } |
| 4221 | |
Alan Viverette | be463f2 | 2016-01-21 10:50:10 -0500 | [diff] [blame] | 4222 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4223 | public void updateViewLayout(View view, ViewGroup.LayoutParams params) { |
| 4224 | if (!checkLayoutParams(params)) { |
| 4225 | throw new IllegalArgumentException("Invalid LayoutParams supplied to " + this); |
| 4226 | } |
| 4227 | if (view.mParent != this) { |
| 4228 | throw new IllegalArgumentException("Given view not a child of " + this); |
| 4229 | } |
| 4230 | view.setLayoutParams(params); |
| 4231 | } |
| 4232 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4233 | protected boolean checkLayoutParams(ViewGroup.LayoutParams p) { |
| 4234 | return p != null; |
| 4235 | } |
| 4236 | |
| 4237 | /** |
| 4238 | * Interface definition for a callback to be invoked when the hierarchy |
| 4239 | * within this view changed. The hierarchy changes whenever a child is added |
| 4240 | * to or removed from this view. |
| 4241 | */ |
| 4242 | public interface OnHierarchyChangeListener { |
| 4243 | /** |
| 4244 | * Called when a new child is added to a parent view. |
| 4245 | * |
| 4246 | * @param parent the view in which a child was added |
| 4247 | * @param child the new child view added in the hierarchy |
| 4248 | */ |
| 4249 | void onChildViewAdded(View parent, View child); |
| 4250 | |
| 4251 | /** |
| 4252 | * Called when a child is removed from a parent view. |
| 4253 | * |
| 4254 | * @param parent the view from which the child was removed |
| 4255 | * @param child the child removed from the hierarchy |
| 4256 | */ |
| 4257 | void onChildViewRemoved(View parent, View child); |
| 4258 | } |
| 4259 | |
| 4260 | /** |
| 4261 | * Register a callback to be invoked when a child is added to or removed |
| 4262 | * from this view. |
| 4263 | * |
| 4264 | * @param listener the callback to invoke on hierarchy change |
| 4265 | */ |
| 4266 | public void setOnHierarchyChangeListener(OnHierarchyChangeListener listener) { |
| 4267 | mOnHierarchyChangeListener = listener; |
| 4268 | } |
| 4269 | |
Adam Powell | 6690d01 | 2015-06-17 16:41:56 -0700 | [diff] [blame] | 4270 | void dispatchViewAdded(View child) { |
| 4271 | onViewAdded(child); |
Philip Milne | f51d91c | 2011-07-18 16:12:19 -0700 | [diff] [blame] | 4272 | if (mOnHierarchyChangeListener != null) { |
| 4273 | mOnHierarchyChangeListener.onChildViewAdded(this, child); |
| 4274 | } |
| 4275 | } |
| 4276 | |
| 4277 | /** |
Adam Powell | 6690d01 | 2015-06-17 16:41:56 -0700 | [diff] [blame] | 4278 | * Called when a new child is added to this ViewGroup. Overrides should always |
| 4279 | * call super.onViewAdded. |
| 4280 | * |
| 4281 | * @param child the added child view |
Philip Milne | f51d91c | 2011-07-18 16:12:19 -0700 | [diff] [blame] | 4282 | */ |
Adam Powell | 6690d01 | 2015-06-17 16:41:56 -0700 | [diff] [blame] | 4283 | public void onViewAdded(View child) { |
| 4284 | } |
| 4285 | |
| 4286 | void dispatchViewRemoved(View child) { |
| 4287 | onViewRemoved(child); |
Philip Milne | f51d91c | 2011-07-18 16:12:19 -0700 | [diff] [blame] | 4288 | if (mOnHierarchyChangeListener != null) { |
| 4289 | mOnHierarchyChangeListener.onChildViewRemoved(this, child); |
| 4290 | } |
| 4291 | } |
| 4292 | |
Adam Powell | 6690d01 | 2015-06-17 16:41:56 -0700 | [diff] [blame] | 4293 | /** |
| 4294 | * Called when a child view is removed from this ViewGroup. Overrides should always |
| 4295 | * call super.onViewRemoved. |
| 4296 | * |
| 4297 | * @param child the removed child view |
| 4298 | */ |
| 4299 | public void onViewRemoved(View child) { |
| 4300 | } |
| 4301 | |
Philip Milne | cfb631b | 2012-10-26 10:51:46 -0700 | [diff] [blame] | 4302 | private void clearCachedLayoutMode() { |
Svetoslav | 6254f48 | 2013-06-04 17:22:14 -0700 | [diff] [blame] | 4303 | if (!hasBooleanFlag(FLAG_LAYOUT_MODE_WAS_EXPLICITLY_SET)) { |
Philip Milne | cfb631b | 2012-10-26 10:51:46 -0700 | [diff] [blame] | 4304 | mLayoutMode = LAYOUT_MODE_UNDEFINED; |
| 4305 | } |
| 4306 | } |
| 4307 | |
| 4308 | @Override |
| 4309 | protected void onAttachedToWindow() { |
| 4310 | super.onAttachedToWindow(); |
| 4311 | clearCachedLayoutMode(); |
| 4312 | } |
| 4313 | |
| 4314 | @Override |
| 4315 | protected void onDetachedFromWindow() { |
| 4316 | super.onDetachedFromWindow(); |
| 4317 | clearCachedLayoutMode(); |
| 4318 | } |
| 4319 | |
Philip Milne | f51d91c | 2011-07-18 16:12:19 -0700 | [diff] [blame] | 4320 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4321 | * Adds a view during layout. This is useful if in your onLayout() method, |
| 4322 | * you need to add more views (as does the list view for example). |
| 4323 | * |
| 4324 | * If index is negative, it means put it at the end of the list. |
| 4325 | * |
| 4326 | * @param child the view to add to the group |
Alan Viverette | 77bb6f1 | 2015-02-11 17:24:33 -0800 | [diff] [blame] | 4327 | * @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] | 4328 | * @param params the layout parameters to associate with the child |
| 4329 | * @return true if the child was added, false otherwise |
| 4330 | */ |
| 4331 | protected boolean addViewInLayout(View child, int index, LayoutParams params) { |
| 4332 | return addViewInLayout(child, index, params, false); |
| 4333 | } |
| 4334 | |
| 4335 | /** |
| 4336 | * Adds a view during layout. This is useful if in your onLayout() method, |
| 4337 | * you need to add more views (as does the list view for example). |
| 4338 | * |
| 4339 | * If index is negative, it means put it at the end of the list. |
| 4340 | * |
| 4341 | * @param child the view to add to the group |
Alan Viverette | 77bb6f1 | 2015-02-11 17:24:33 -0800 | [diff] [blame] | 4342 | * @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] | 4343 | * @param params the layout parameters to associate with the child |
| 4344 | * @param preventRequestLayout if true, calling this method will not trigger a |
| 4345 | * layout request on child |
| 4346 | * @return true if the child was added, false otherwise |
| 4347 | */ |
| 4348 | protected boolean addViewInLayout(View child, int index, LayoutParams params, |
| 4349 | boolean preventRequestLayout) { |
Adam Powell | 45a9da5 | 2014-10-09 09:44:18 -0700 | [diff] [blame] | 4350 | if (child == null) { |
| 4351 | throw new IllegalArgumentException("Cannot add a null child view to a ViewGroup"); |
| 4352 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4353 | child.mParent = null; |
| 4354 | addViewInner(child, index, params, preventRequestLayout); |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 4355 | child.mPrivateFlags = (child.mPrivateFlags & ~PFLAG_DIRTY_MASK) | PFLAG_DRAWN; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4356 | return true; |
| 4357 | } |
| 4358 | |
| 4359 | /** |
| 4360 | * Prevents the specified child to be laid out during the next layout pass. |
| 4361 | * |
| 4362 | * @param child the child on which to perform the cleanup |
| 4363 | */ |
| 4364 | protected void cleanupLayoutState(View child) { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 4365 | child.mPrivateFlags &= ~View.PFLAG_FORCE_LAYOUT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4366 | } |
| 4367 | |
| 4368 | private void addViewInner(View child, int index, LayoutParams params, |
| 4369 | boolean preventRequestLayout) { |
| 4370 | |
Chet Haase | e8e45d3 | 2011-03-02 17:07:35 -0800 | [diff] [blame] | 4371 | if (mTransition != null) { |
| 4372 | // Don't prevent other add transitions from completing, but cancel remove |
| 4373 | // transitions to let them complete the process before we add to the container |
| 4374 | mTransition.cancel(LayoutTransition.DISAPPEARING); |
Chet Haase | add6577 | 2011-02-09 16:47:29 -0800 | [diff] [blame] | 4375 | } |
| 4376 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4377 | if (child.getParent() != null) { |
| 4378 | throw new IllegalStateException("The specified child already has a parent. " + |
| 4379 | "You must call removeView() on the child's parent first."); |
| 4380 | } |
| 4381 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4382 | if (mTransition != null) { |
Chet Haase | 5e25c2c | 2010-09-16 11:15:56 -0700 | [diff] [blame] | 4383 | mTransition.addChild(this, child); |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4384 | } |
| 4385 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4386 | if (!checkLayoutParams(params)) { |
| 4387 | params = generateLayoutParams(params); |
| 4388 | } |
| 4389 | |
| 4390 | if (preventRequestLayout) { |
| 4391 | child.mLayoutParams = params; |
| 4392 | } else { |
| 4393 | child.setLayoutParams(params); |
| 4394 | } |
| 4395 | |
| 4396 | if (index < 0) { |
| 4397 | index = mChildrenCount; |
| 4398 | } |
| 4399 | |
| 4400 | addInArray(child, index); |
| 4401 | |
| 4402 | // tell our children |
| 4403 | if (preventRequestLayout) { |
| 4404 | child.assignParent(this); |
| 4405 | } else { |
| 4406 | child.mParent = this; |
| 4407 | } |
| 4408 | |
| 4409 | if (child.hasFocus()) { |
| 4410 | requestChildFocus(child, child.findFocus()); |
| 4411 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 4412 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4413 | AttachInfo ai = mAttachInfo; |
Adam Powell | 4b86788 | 2011-09-16 12:59:46 -0700 | [diff] [blame] | 4414 | if (ai != null && (mGroupFlags & FLAG_PREVENT_DISPATCH_ATTACHED_TO_WINDOW) == 0) { |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 4415 | boolean lastKeepOn = ai.mKeepScreenOn; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4416 | ai.mKeepScreenOn = false; |
| 4417 | child.dispatchAttachedToWindow(mAttachInfo, (mViewFlags&VISIBILITY_MASK)); |
| 4418 | if (ai.mKeepScreenOn) { |
| 4419 | needGlobalAttributesUpdate(true); |
| 4420 | } |
| 4421 | ai.mKeepScreenOn = lastKeepOn; |
| 4422 | } |
| 4423 | |
Fabrice Di Meglio | 9a04856 | 2012-09-26 14:55:56 -0700 | [diff] [blame] | 4424 | if (child.isLayoutDirectionInherited()) { |
Fabrice Di Meglio | a7e0bcd | 2012-10-16 19:55:01 -0700 | [diff] [blame] | 4425 | child.resetRtlProperties(); |
Fabrice Di Meglio | 9a04856 | 2012-09-26 14:55:56 -0700 | [diff] [blame] | 4426 | } |
| 4427 | |
Adam Powell | 6690d01 | 2015-06-17 16:41:56 -0700 | [diff] [blame] | 4428 | dispatchViewAdded(child); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4429 | |
| 4430 | if ((child.mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE) { |
| 4431 | mGroupFlags |= FLAG_NOTIFY_CHILDREN_ON_DRAWABLE_STATE_CHANGE; |
| 4432 | } |
Adam Powell | 539ee87 | 2012-02-03 19:00:49 -0800 | [diff] [blame] | 4433 | |
| 4434 | if (child.hasTransientState()) { |
| 4435 | childHasTransientStateChanged(child, true); |
| 4436 | } |
Svetoslav | 6254f48 | 2013-06-04 17:22:14 -0700 | [diff] [blame] | 4437 | |
Svetoslav | 8e3feb1 | 2014-02-24 13:46:47 -0800 | [diff] [blame] | 4438 | if (child.getVisibility() != View.GONE) { |
Svetoslav | 00dbe81 | 2013-06-10 12:51:09 -0700 | [diff] [blame] | 4439 | notifySubtreeAccessibilityStateChangedIfNeeded(); |
Svetoslav | 6254f48 | 2013-06-04 17:22:14 -0700 | [diff] [blame] | 4440 | } |
Chet Haase | c633d2f | 2015-04-07 10:29:39 -0700 | [diff] [blame] | 4441 | |
| 4442 | if (mTransientIndices != null) { |
| 4443 | final int transientCount = mTransientIndices.size(); |
| 4444 | for (int i = 0; i < transientCount; ++i) { |
| 4445 | final int oldIndex = mTransientIndices.get(i); |
| 4446 | if (index <= oldIndex) { |
| 4447 | mTransientIndices.set(i, oldIndex + 1); |
| 4448 | } |
| 4449 | } |
| 4450 | } |
Vadim Tryshev | 02ed4a0 | 2015-10-23 17:39:33 -0700 | [diff] [blame] | 4451 | |
| 4452 | if (mCurrentDragStartEvent != null && child.getVisibility() == VISIBLE) { |
| 4453 | notifyChildOfDragStart(child); |
| 4454 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4455 | } |
| 4456 | |
| 4457 | private void addInArray(View child, int index) { |
| 4458 | View[] children = mChildren; |
| 4459 | final int count = mChildrenCount; |
| 4460 | final int size = children.length; |
| 4461 | if (index == count) { |
| 4462 | if (size == count) { |
| 4463 | mChildren = new View[size + ARRAY_CAPACITY_INCREMENT]; |
| 4464 | System.arraycopy(children, 0, mChildren, 0, size); |
| 4465 | children = mChildren; |
| 4466 | } |
| 4467 | children[mChildrenCount++] = child; |
| 4468 | } else if (index < count) { |
| 4469 | if (size == count) { |
| 4470 | mChildren = new View[size + ARRAY_CAPACITY_INCREMENT]; |
| 4471 | System.arraycopy(children, 0, mChildren, 0, index); |
| 4472 | System.arraycopy(children, index, mChildren, index + 1, count - index); |
| 4473 | children = mChildren; |
| 4474 | } else { |
| 4475 | System.arraycopy(children, index, children, index + 1, count - index); |
| 4476 | } |
| 4477 | children[index] = child; |
| 4478 | mChildrenCount++; |
Joe Onorato | 03ab0c7 | 2011-01-06 15:46:27 -0800 | [diff] [blame] | 4479 | if (mLastTouchDownIndex >= index) { |
| 4480 | mLastTouchDownIndex++; |
| 4481 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4482 | } else { |
| 4483 | throw new IndexOutOfBoundsException("index=" + index + " count=" + count); |
| 4484 | } |
| 4485 | } |
| 4486 | |
| 4487 | // This method also sets the child's mParent to null |
| 4488 | private void removeFromArray(int index) { |
| 4489 | final View[] children = mChildren; |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4490 | if (!(mTransitioningViews != null && mTransitioningViews.contains(children[index]))) { |
| 4491 | children[index].mParent = null; |
| 4492 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4493 | final int count = mChildrenCount; |
| 4494 | if (index == count - 1) { |
| 4495 | children[--mChildrenCount] = null; |
| 4496 | } else if (index >= 0 && index < count) { |
| 4497 | System.arraycopy(children, index + 1, children, index, count - index - 1); |
| 4498 | children[--mChildrenCount] = null; |
| 4499 | } else { |
| 4500 | throw new IndexOutOfBoundsException(); |
| 4501 | } |
Joe Onorato | 03ab0c7 | 2011-01-06 15:46:27 -0800 | [diff] [blame] | 4502 | if (mLastTouchDownIndex == index) { |
| 4503 | mLastTouchDownTime = 0; |
| 4504 | mLastTouchDownIndex = -1; |
| 4505 | } else if (mLastTouchDownIndex > index) { |
| 4506 | mLastTouchDownIndex--; |
| 4507 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4508 | } |
| 4509 | |
| 4510 | // This method also sets the children's mParent to null |
| 4511 | private void removeFromArray(int start, int count) { |
| 4512 | final View[] children = mChildren; |
| 4513 | final int childrenCount = mChildrenCount; |
| 4514 | |
| 4515 | start = Math.max(0, start); |
| 4516 | final int end = Math.min(childrenCount, start + count); |
| 4517 | |
| 4518 | if (start == end) { |
| 4519 | return; |
| 4520 | } |
| 4521 | |
| 4522 | if (end == childrenCount) { |
| 4523 | for (int i = start; i < end; i++) { |
| 4524 | children[i].mParent = null; |
| 4525 | children[i] = null; |
| 4526 | } |
| 4527 | } else { |
| 4528 | for (int i = start; i < end; i++) { |
| 4529 | children[i].mParent = null; |
| 4530 | } |
| 4531 | |
| 4532 | // Since we're looping above, we might as well do the copy, but is arraycopy() |
| 4533 | // faster than the extra 2 bounds checks we would do in the loop? |
| 4534 | System.arraycopy(children, end, children, start, childrenCount - end); |
| 4535 | |
| 4536 | for (int i = childrenCount - (end - start); i < childrenCount; i++) { |
| 4537 | children[i] = null; |
| 4538 | } |
| 4539 | } |
| 4540 | |
| 4541 | mChildrenCount -= (end - start); |
| 4542 | } |
| 4543 | |
| 4544 | private void bindLayoutAnimation(View child) { |
| 4545 | Animation a = mLayoutAnimationController.getAnimationForView(child); |
| 4546 | child.setAnimation(a); |
| 4547 | } |
| 4548 | |
| 4549 | /** |
| 4550 | * Subclasses should override this method to set layout animation |
| 4551 | * parameters on the supplied child. |
| 4552 | * |
| 4553 | * @param child the child to associate with animation parameters |
| 4554 | * @param params the child's layout parameters which hold the animation |
| 4555 | * parameters |
| 4556 | * @param index the index of the child in the view group |
| 4557 | * @param count the number of children in the view group |
| 4558 | */ |
| 4559 | protected void attachLayoutAnimationParameters(View child, |
| 4560 | LayoutParams params, int index, int count) { |
| 4561 | LayoutAnimationController.AnimationParameters animationParams = |
| 4562 | params.layoutAnimationParameters; |
| 4563 | if (animationParams == null) { |
| 4564 | animationParams = new LayoutAnimationController.AnimationParameters(); |
| 4565 | params.layoutAnimationParameters = animationParams; |
| 4566 | } |
| 4567 | |
| 4568 | animationParams.count = count; |
| 4569 | animationParams.index = index; |
| 4570 | } |
| 4571 | |
| 4572 | /** |
| 4573 | * {@inheritDoc} |
Filip Gruszczynski | a33bdf3 | 2015-11-19 18:22:16 -0800 | [diff] [blame] | 4574 | * |
Romain Guy | 393a52c | 2012-05-22 20:21:08 -0700 | [diff] [blame] | 4575 | * <p><strong>Note:</strong> do not invoke this method from |
| 4576 | * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)}, |
| 4577 | * {@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] | 4578 | */ |
Alan Viverette | be463f2 | 2016-01-21 10:50:10 -0500 | [diff] [blame] | 4579 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4580 | public void removeView(View view) { |
Alan Viverette | 177ec460 | 2014-10-17 13:34:50 -0700 | [diff] [blame] | 4581 | if (removeViewInternal(view)) { |
| 4582 | requestLayout(); |
| 4583 | invalidate(true); |
| 4584 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4585 | } |
| 4586 | |
| 4587 | /** |
| 4588 | * Removes a view during layout. This is useful if in your onLayout() method, |
| 4589 | * you need to remove more views. |
| 4590 | * |
Romain Guy | 393a52c | 2012-05-22 20:21:08 -0700 | [diff] [blame] | 4591 | * <p><strong>Note:</strong> do not invoke this method from |
| 4592 | * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)}, |
| 4593 | * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p> |
Filip Gruszczynski | a33bdf3 | 2015-11-19 18:22:16 -0800 | [diff] [blame] | 4594 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4595 | * @param view the view to remove from the group |
| 4596 | */ |
| 4597 | public void removeViewInLayout(View view) { |
| 4598 | removeViewInternal(view); |
| 4599 | } |
| 4600 | |
| 4601 | /** |
| 4602 | * Removes a range of views during layout. This is useful if in your onLayout() method, |
| 4603 | * you need to remove more views. |
| 4604 | * |
Romain Guy | 393a52c | 2012-05-22 20:21:08 -0700 | [diff] [blame] | 4605 | * <p><strong>Note:</strong> do not invoke this method from |
| 4606 | * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)}, |
| 4607 | * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p> |
| 4608 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4609 | * @param start the index of the first view to remove from the group |
| 4610 | * @param count the number of views to remove from the group |
| 4611 | */ |
| 4612 | public void removeViewsInLayout(int start, int count) { |
| 4613 | removeViewsInternal(start, count); |
| 4614 | } |
| 4615 | |
| 4616 | /** |
| 4617 | * Removes the view at the specified position in the group. |
| 4618 | * |
Romain Guy | 393a52c | 2012-05-22 20:21:08 -0700 | [diff] [blame] | 4619 | * <p><strong>Note:</strong> do not invoke this method from |
| 4620 | * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)}, |
| 4621 | * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p> |
Filip Gruszczynski | a33bdf3 | 2015-11-19 18:22:16 -0800 | [diff] [blame] | 4622 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4623 | * @param index the position in the group of the view to remove |
| 4624 | */ |
| 4625 | public void removeViewAt(int index) { |
| 4626 | removeViewInternal(index, getChildAt(index)); |
| 4627 | requestLayout(); |
Romain Guy | 849d0a3 | 2011-02-01 17:20:48 -0800 | [diff] [blame] | 4628 | invalidate(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4629 | } |
| 4630 | |
| 4631 | /** |
| 4632 | * Removes the specified range of views from the group. |
| 4633 | * |
Romain Guy | 393a52c | 2012-05-22 20:21:08 -0700 | [diff] [blame] | 4634 | * <p><strong>Note:</strong> do not invoke this method from |
| 4635 | * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)}, |
| 4636 | * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p> |
| 4637 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4638 | * @param start the first position in the group of the range of views to remove |
| 4639 | * @param count the number of views to remove |
| 4640 | */ |
| 4641 | public void removeViews(int start, int count) { |
| 4642 | removeViewsInternal(start, count); |
| 4643 | requestLayout(); |
Romain Guy | 849d0a3 | 2011-02-01 17:20:48 -0800 | [diff] [blame] | 4644 | invalidate(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4645 | } |
| 4646 | |
Alan Viverette | 177ec460 | 2014-10-17 13:34:50 -0700 | [diff] [blame] | 4647 | private boolean removeViewInternal(View view) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4648 | final int index = indexOfChild(view); |
| 4649 | if (index >= 0) { |
| 4650 | removeViewInternal(index, view); |
Alan Viverette | 177ec460 | 2014-10-17 13:34:50 -0700 | [diff] [blame] | 4651 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4652 | } |
Alan Viverette | 177ec460 | 2014-10-17 13:34:50 -0700 | [diff] [blame] | 4653 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4654 | } |
| 4655 | |
| 4656 | private void removeViewInternal(int index, View view) { |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4657 | if (mTransition != null) { |
Chet Haase | 5e25c2c | 2010-09-16 11:15:56 -0700 | [diff] [blame] | 4658 | mTransition.removeChild(this, view); |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4659 | } |
| 4660 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4661 | boolean clearChildFocus = false; |
| 4662 | if (view == mFocused) { |
Alan Viverette | 223622a | 2013-12-17 13:29:02 -0800 | [diff] [blame] | 4663 | view.unFocus(null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4664 | clearChildFocus = true; |
| 4665 | } |
| 4666 | |
Alan Viverette | 632af84 | 2014-10-28 13:45:11 -0700 | [diff] [blame] | 4667 | view.clearAccessibilityFocus(); |
Svetoslav Ganov | 961bf0e | 2012-05-08 09:40:03 -0700 | [diff] [blame] | 4668 | |
Jeff Brown | 59a422e | 2012-04-19 15:19:19 -0700 | [diff] [blame] | 4669 | cancelTouchTarget(view); |
| 4670 | cancelHoverTarget(view); |
| 4671 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4672 | if (view.getAnimation() != null || |
| 4673 | (mTransitioningViews != null && mTransitioningViews.contains(view))) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4674 | addDisappearingView(view); |
| 4675 | } else if (view.mAttachInfo != null) { |
| 4676 | view.dispatchDetachedFromWindow(); |
| 4677 | } |
| 4678 | |
Adam Powell | 539ee87 | 2012-02-03 19:00:49 -0800 | [diff] [blame] | 4679 | if (view.hasTransientState()) { |
| 4680 | childHasTransientStateChanged(view, false); |
| 4681 | } |
| 4682 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4683 | needGlobalAttributesUpdate(false); |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 4684 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4685 | removeFromArray(index); |
| 4686 | |
| 4687 | if (clearChildFocus) { |
| 4688 | clearChildFocus(view); |
Svetoslav Ganov | 149567f | 2013-01-08 15:23:34 -0800 | [diff] [blame] | 4689 | if (!rootViewRequestFocus()) { |
| 4690 | notifyGlobalFocusCleared(this); |
| 4691 | } |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 4692 | } |
Romain Guy | 6fb0563 | 2012-11-29 10:50:33 -0800 | [diff] [blame] | 4693 | |
Adam Powell | 6690d01 | 2015-06-17 16:41:56 -0700 | [diff] [blame] | 4694 | dispatchViewRemoved(view); |
Svetoslav | 6254f48 | 2013-06-04 17:22:14 -0700 | [diff] [blame] | 4695 | |
Svetoslav | 8e3feb1 | 2014-02-24 13:46:47 -0800 | [diff] [blame] | 4696 | if (view.getVisibility() != View.GONE) { |
Svetoslav | 00dbe81 | 2013-06-10 12:51:09 -0700 | [diff] [blame] | 4697 | notifySubtreeAccessibilityStateChangedIfNeeded(); |
Svetoslav | 6254f48 | 2013-06-04 17:22:14 -0700 | [diff] [blame] | 4698 | } |
Chet Haase | c633d2f | 2015-04-07 10:29:39 -0700 | [diff] [blame] | 4699 | |
| 4700 | int transientCount = mTransientIndices == null ? 0 : mTransientIndices.size(); |
| 4701 | for (int i = 0; i < transientCount; ++i) { |
| 4702 | final int oldIndex = mTransientIndices.get(i); |
| 4703 | if (index < oldIndex) { |
| 4704 | mTransientIndices.set(i, oldIndex - 1); |
| 4705 | } |
| 4706 | } |
Vadim Tryshev | 02ed4a0 | 2015-10-23 17:39:33 -0700 | [diff] [blame] | 4707 | |
| 4708 | if (mCurrentDragStartEvent != null) { |
| 4709 | mChildrenInterestedInDrag.remove(view); |
| 4710 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4711 | } |
| 4712 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4713 | /** |
| 4714 | * Sets the LayoutTransition object for this ViewGroup. If the LayoutTransition object is |
| 4715 | * not null, changes in layout which occur because of children being added to or removed from |
| 4716 | * the ViewGroup will be animated according to the animations defined in that LayoutTransition |
| 4717 | * object. By default, the transition object is null (so layout changes are not animated). |
| 4718 | * |
Chet Haase | ef3cbfd | 2013-08-21 14:01:02 -0700 | [diff] [blame] | 4719 | * <p>Replacing a non-null transition will cause that previous transition to be |
| 4720 | * canceled, if it is currently running, to restore this container to |
| 4721 | * its correct post-transition state.</p> |
| 4722 | * |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4723 | * @param transition The LayoutTransition object that will animated changes in layout. A value |
| 4724 | * of <code>null</code> means no transition will run on layout changes. |
Chet Haase | 13cc120 | 2010-09-03 15:39:20 -0700 | [diff] [blame] | 4725 | * @attr ref android.R.styleable#ViewGroup_animateLayoutChanges |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4726 | */ |
| 4727 | public void setLayoutTransition(LayoutTransition transition) { |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 4728 | if (mTransition != null) { |
Chet Haase | fee6f2b | 2013-08-27 12:22:29 -0700 | [diff] [blame] | 4729 | LayoutTransition previousTransition = mTransition; |
| 4730 | previousTransition.cancel(); |
| 4731 | previousTransition.removeTransitionListener(mLayoutTransitionListener); |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 4732 | } |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4733 | mTransition = transition; |
Chet Haase | 13cc120 | 2010-09-03 15:39:20 -0700 | [diff] [blame] | 4734 | if (mTransition != null) { |
| 4735 | mTransition.addTransitionListener(mLayoutTransitionListener); |
| 4736 | } |
| 4737 | } |
| 4738 | |
| 4739 | /** |
| 4740 | * Gets the LayoutTransition object for this ViewGroup. If the LayoutTransition object is |
| 4741 | * not null, changes in layout which occur because of children being added to or removed from |
| 4742 | * the ViewGroup will be animated according to the animations defined in that LayoutTransition |
| 4743 | * object. By default, the transition object is null (so layout changes are not animated). |
| 4744 | * |
| 4745 | * @return LayoutTranstion The LayoutTransition object that will animated changes in layout. |
| 4746 | * A value of <code>null</code> means no transition will run on layout changes. |
| 4747 | */ |
| 4748 | public LayoutTransition getLayoutTransition() { |
| 4749 | return mTransition; |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4750 | } |
| 4751 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4752 | private void removeViewsInternal(int start, int count) { |
Chris Craik | 18a759d | 2015-10-14 14:16:33 -0700 | [diff] [blame] | 4753 | final int end = start + count; |
| 4754 | |
| 4755 | if (start < 0 || count < 0 || end > mChildrenCount) { |
| 4756 | throw new IndexOutOfBoundsException(); |
| 4757 | } |
| 4758 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4759 | final View focused = mFocused; |
| 4760 | final boolean detach = mAttachInfo != null; |
Svetoslav Ganov | 149567f | 2013-01-08 15:23:34 -0800 | [diff] [blame] | 4761 | boolean clearChildFocus = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4762 | |
| 4763 | final View[] children = mChildren; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4764 | |
| 4765 | for (int i = start; i < end; i++) { |
| 4766 | final View view = children[i]; |
| 4767 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4768 | if (mTransition != null) { |
Chet Haase | 5e25c2c | 2010-09-16 11:15:56 -0700 | [diff] [blame] | 4769 | mTransition.removeChild(this, view); |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4770 | } |
| 4771 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4772 | if (view == focused) { |
Alan Viverette | 223622a | 2013-12-17 13:29:02 -0800 | [diff] [blame] | 4773 | view.unFocus(null); |
Svetoslav Ganov | 149567f | 2013-01-08 15:23:34 -0800 | [diff] [blame] | 4774 | clearChildFocus = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4775 | } |
| 4776 | |
Alan Viverette | 632af84 | 2014-10-28 13:45:11 -0700 | [diff] [blame] | 4777 | view.clearAccessibilityFocus(); |
Svetoslav Ganov | 961bf0e | 2012-05-08 09:40:03 -0700 | [diff] [blame] | 4778 | |
Jeff Brown | 59a422e | 2012-04-19 15:19:19 -0700 | [diff] [blame] | 4779 | cancelTouchTarget(view); |
| 4780 | cancelHoverTarget(view); |
| 4781 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4782 | if (view.getAnimation() != null || |
| 4783 | (mTransitioningViews != null && mTransitioningViews.contains(view))) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4784 | addDisappearingView(view); |
| 4785 | } else if (detach) { |
| 4786 | view.dispatchDetachedFromWindow(); |
| 4787 | } |
| 4788 | |
Adam Powell | 539ee87 | 2012-02-03 19:00:49 -0800 | [diff] [blame] | 4789 | if (view.hasTransientState()) { |
| 4790 | childHasTransientStateChanged(view, false); |
| 4791 | } |
| 4792 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4793 | needGlobalAttributesUpdate(false); |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 4794 | |
Adam Powell | 6690d01 | 2015-06-17 16:41:56 -0700 | [diff] [blame] | 4795 | dispatchViewRemoved(view); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4796 | } |
| 4797 | |
| 4798 | removeFromArray(start, count); |
| 4799 | |
Svetoslav Ganov | 149567f | 2013-01-08 15:23:34 -0800 | [diff] [blame] | 4800 | if (clearChildFocus) { |
| 4801 | clearChildFocus(focused); |
| 4802 | if (!rootViewRequestFocus()) { |
| 4803 | notifyGlobalFocusCleared(focused); |
| 4804 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4805 | } |
| 4806 | } |
| 4807 | |
| 4808 | /** |
| 4809 | * Call this method to remove all child views from the |
| 4810 | * ViewGroup. |
Filip Gruszczynski | a33bdf3 | 2015-11-19 18:22:16 -0800 | [diff] [blame] | 4811 | * |
Romain Guy | 393a52c | 2012-05-22 20:21:08 -0700 | [diff] [blame] | 4812 | * <p><strong>Note:</strong> do not invoke this method from |
| 4813 | * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)}, |
| 4814 | * {@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] | 4815 | */ |
| 4816 | public void removeAllViews() { |
| 4817 | removeAllViewsInLayout(); |
| 4818 | requestLayout(); |
Romain Guy | 849d0a3 | 2011-02-01 17:20:48 -0800 | [diff] [blame] | 4819 | invalidate(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4820 | } |
| 4821 | |
| 4822 | /** |
| 4823 | * Called by a ViewGroup subclass to remove child views from itself, |
| 4824 | * when it must first know its size on screen before it can calculate how many |
| 4825 | * child views it will render. An example is a Gallery or a ListView, which |
| 4826 | * may "have" 50 children, but actually only render the number of children |
| 4827 | * that can currently fit inside the object on screen. Do not call |
| 4828 | * this method unless you are extending ViewGroup and understand the |
| 4829 | * view measuring and layout pipeline. |
Romain Guy | 393a52c | 2012-05-22 20:21:08 -0700 | [diff] [blame] | 4830 | * |
| 4831 | * <p><strong>Note:</strong> do not invoke this method from |
| 4832 | * {@link #draw(android.graphics.Canvas)}, {@link #onDraw(android.graphics.Canvas)}, |
| 4833 | * {@link #dispatchDraw(android.graphics.Canvas)} or any related method.</p> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4834 | */ |
| 4835 | public void removeAllViewsInLayout() { |
| 4836 | final int count = mChildrenCount; |
| 4837 | if (count <= 0) { |
| 4838 | return; |
| 4839 | } |
| 4840 | |
| 4841 | final View[] children = mChildren; |
| 4842 | mChildrenCount = 0; |
| 4843 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4844 | final View focused = mFocused; |
| 4845 | final boolean detach = mAttachInfo != null; |
Svetoslav Ganov | 149567f | 2013-01-08 15:23:34 -0800 | [diff] [blame] | 4846 | boolean clearChildFocus = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4847 | |
| 4848 | needGlobalAttributesUpdate(false); |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 4849 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4850 | for (int i = count - 1; i >= 0; i--) { |
| 4851 | final View view = children[i]; |
| 4852 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4853 | if (mTransition != null) { |
Chet Haase | 5e25c2c | 2010-09-16 11:15:56 -0700 | [diff] [blame] | 4854 | mTransition.removeChild(this, view); |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4855 | } |
| 4856 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4857 | if (view == focused) { |
Alan Viverette | 223622a | 2013-12-17 13:29:02 -0800 | [diff] [blame] | 4858 | view.unFocus(null); |
Svetoslav Ganov | 149567f | 2013-01-08 15:23:34 -0800 | [diff] [blame] | 4859 | clearChildFocus = true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4860 | } |
| 4861 | |
Alan Viverette | 632af84 | 2014-10-28 13:45:11 -0700 | [diff] [blame] | 4862 | view.clearAccessibilityFocus(); |
Svetoslav Ganov | 961bf0e | 2012-05-08 09:40:03 -0700 | [diff] [blame] | 4863 | |
Jeff Brown | 59a422e | 2012-04-19 15:19:19 -0700 | [diff] [blame] | 4864 | cancelTouchTarget(view); |
| 4865 | cancelHoverTarget(view); |
| 4866 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4867 | if (view.getAnimation() != null || |
| 4868 | (mTransitioningViews != null && mTransitioningViews.contains(view))) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4869 | addDisappearingView(view); |
| 4870 | } else if (detach) { |
| 4871 | view.dispatchDetachedFromWindow(); |
| 4872 | } |
| 4873 | |
Adam Powell | 539ee87 | 2012-02-03 19:00:49 -0800 | [diff] [blame] | 4874 | if (view.hasTransientState()) { |
| 4875 | childHasTransientStateChanged(view, false); |
| 4876 | } |
| 4877 | |
Adam Powell | 6690d01 | 2015-06-17 16:41:56 -0700 | [diff] [blame] | 4878 | dispatchViewRemoved(view); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4879 | |
| 4880 | view.mParent = null; |
| 4881 | children[i] = null; |
| 4882 | } |
| 4883 | |
Svetoslav Ganov | 149567f | 2013-01-08 15:23:34 -0800 | [diff] [blame] | 4884 | if (clearChildFocus) { |
| 4885 | clearChildFocus(focused); |
| 4886 | if (!rootViewRequestFocus()) { |
| 4887 | notifyGlobalFocusCleared(focused); |
| 4888 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4889 | } |
| 4890 | } |
| 4891 | |
| 4892 | /** |
| 4893 | * Finishes the removal of a detached view. This method will dispatch the detached from |
| 4894 | * window event and notify the hierarchy change listener. |
Chet Haase | ca479d4 | 2012-08-30 17:20:08 -0700 | [diff] [blame] | 4895 | * <p> |
| 4896 | * This method is intended to be lightweight and makes no assumptions about whether the |
| 4897 | * parent or child should be redrawn. Proper use of this method will include also making |
| 4898 | * any appropriate {@link #requestLayout()} or {@link #invalidate()} calls. |
| 4899 | * For example, callers can {@link #post(Runnable) post} a {@link Runnable} |
| 4900 | * which performs a {@link #requestLayout()} on the next frame, after all detach/remove |
| 4901 | * 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] | 4902 | * |
| 4903 | * @param child the child to be definitely removed from the view hierarchy |
| 4904 | * @param animate if true and the view has an animation, the view is placed in the |
| 4905 | * disappearing views list, otherwise, it is detached from the window |
| 4906 | * |
| 4907 | * @see #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams) |
| 4908 | * @see #detachAllViewsFromParent() |
| 4909 | * @see #detachViewFromParent(View) |
| 4910 | * @see #detachViewFromParent(int) |
| 4911 | */ |
| 4912 | protected void removeDetachedView(View child, boolean animate) { |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4913 | if (mTransition != null) { |
Chet Haase | 5e25c2c | 2010-09-16 11:15:56 -0700 | [diff] [blame] | 4914 | mTransition.removeChild(this, child); |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4915 | } |
| 4916 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4917 | if (child == mFocused) { |
| 4918 | child.clearFocus(); |
| 4919 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 4920 | |
Svetoslav Ganov | 961bf0e | 2012-05-08 09:40:03 -0700 | [diff] [blame] | 4921 | child.clearAccessibilityFocus(); |
| 4922 | |
Jeff Brown | 59a422e | 2012-04-19 15:19:19 -0700 | [diff] [blame] | 4923 | cancelTouchTarget(child); |
| 4924 | cancelHoverTarget(child); |
| 4925 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 4926 | if ((animate && child.getAnimation() != null) || |
| 4927 | (mTransitioningViews != null && mTransitioningViews.contains(child))) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4928 | addDisappearingView(child); |
| 4929 | } else if (child.mAttachInfo != null) { |
| 4930 | child.dispatchDetachedFromWindow(); |
| 4931 | } |
| 4932 | |
Adam Powell | 539ee87 | 2012-02-03 19:00:49 -0800 | [diff] [blame] | 4933 | if (child.hasTransientState()) { |
| 4934 | childHasTransientStateChanged(child, false); |
| 4935 | } |
| 4936 | |
Adam Powell | 6690d01 | 2015-06-17 16:41:56 -0700 | [diff] [blame] | 4937 | dispatchViewRemoved(child); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4938 | } |
| 4939 | |
| 4940 | /** |
| 4941 | * 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] | 4942 | * sets the layout parameters and puts the view in the list of children so that |
| 4943 | * it can be retrieved by calling {@link #getChildAt(int)}. |
| 4944 | * <p> |
| 4945 | * This method is intended to be lightweight and makes no assumptions about whether the |
| 4946 | * parent or child should be redrawn. Proper use of this method will include also making |
| 4947 | * any appropriate {@link #requestLayout()} or {@link #invalidate()} calls. |
| 4948 | * For example, callers can {@link #post(Runnable) post} a {@link Runnable} |
| 4949 | * which performs a {@link #requestLayout()} on the next frame, after all detach/attach |
| 4950 | * calls are finished, causing layout to be run prior to redrawing the view hierarchy. |
| 4951 | * <p> |
| 4952 | * 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] | 4953 | * |
| 4954 | * @param child the child to attach |
| 4955 | * @param index the index at which the child should be attached |
| 4956 | * @param params the layout parameters of the child |
| 4957 | * |
| 4958 | * @see #removeDetachedView(View, boolean) |
| 4959 | * @see #detachAllViewsFromParent() |
| 4960 | * @see #detachViewFromParent(View) |
| 4961 | * @see #detachViewFromParent(int) |
| 4962 | */ |
| 4963 | protected void attachViewToParent(View child, int index, LayoutParams params) { |
| 4964 | child.mLayoutParams = params; |
| 4965 | |
| 4966 | if (index < 0) { |
| 4967 | index = mChildrenCount; |
| 4968 | } |
| 4969 | |
| 4970 | addInArray(child, index); |
| 4971 | |
| 4972 | child.mParent = this; |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 4973 | child.mPrivateFlags = (child.mPrivateFlags & ~PFLAG_DIRTY_MASK |
| 4974 | & ~PFLAG_DRAWING_CACHE_VALID) |
| 4975 | | PFLAG_DRAWN | PFLAG_INVALIDATED; |
| 4976 | this.mPrivateFlags |= PFLAG_INVALIDATED; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4977 | |
| 4978 | if (child.hasFocus()) { |
| 4979 | requestChildFocus(child, child.findFocus()); |
| 4980 | } |
| 4981 | } |
| 4982 | |
| 4983 | /** |
Chet Haase | ca479d4 | 2012-08-30 17:20:08 -0700 | [diff] [blame] | 4984 | * Detaches a view from its parent. Detaching a view should be followed |
| 4985 | * either by a call to |
| 4986 | * {@link #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)} |
| 4987 | * or a call to {@link #removeDetachedView(View, boolean)}. Detachment should only be |
| 4988 | * temporary; reattachment or removal should happen within the same drawing cycle as |
| 4989 | * detachment. When a view is detached, its parent is null and cannot be retrieved by a |
| 4990 | * call to {@link #getChildAt(int)}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4991 | * |
| 4992 | * @param child the child to detach |
| 4993 | * |
| 4994 | * @see #detachViewFromParent(int) |
| 4995 | * @see #detachViewsFromParent(int, int) |
| 4996 | * @see #detachAllViewsFromParent() |
| 4997 | * @see #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams) |
| 4998 | * @see #removeDetachedView(View, boolean) |
| 4999 | */ |
| 5000 | protected void detachViewFromParent(View child) { |
| 5001 | removeFromArray(indexOfChild(child)); |
| 5002 | } |
| 5003 | |
| 5004 | /** |
Chet Haase | ca479d4 | 2012-08-30 17:20:08 -0700 | [diff] [blame] | 5005 | * Detaches a view from its parent. Detaching a view should be followed |
| 5006 | * either by a call to |
| 5007 | * {@link #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)} |
| 5008 | * or a call to {@link #removeDetachedView(View, boolean)}. Detachment should only be |
| 5009 | * temporary; reattachment or removal should happen within the same drawing cycle as |
| 5010 | * detachment. When a view is detached, its parent is null and cannot be retrieved by a |
| 5011 | * call to {@link #getChildAt(int)}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5012 | * |
| 5013 | * @param index the index of the child to detach |
| 5014 | * |
| 5015 | * @see #detachViewFromParent(View) |
| 5016 | * @see #detachAllViewsFromParent() |
| 5017 | * @see #detachViewsFromParent(int, int) |
| 5018 | * @see #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams) |
| 5019 | * @see #removeDetachedView(View, boolean) |
| 5020 | */ |
| 5021 | protected void detachViewFromParent(int index) { |
| 5022 | removeFromArray(index); |
| 5023 | } |
| 5024 | |
| 5025 | /** |
Chet Haase | ca479d4 | 2012-08-30 17:20:08 -0700 | [diff] [blame] | 5026 | * Detaches a range of views from their parents. Detaching a view should be followed |
| 5027 | * either by a call to |
| 5028 | * {@link #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)} |
| 5029 | * or a call to {@link #removeDetachedView(View, boolean)}. Detachment should only be |
| 5030 | * temporary; reattachment or removal should happen within the same drawing cycle as |
| 5031 | * detachment. When a view is detached, its parent is null and cannot be retrieved by a |
| 5032 | * call to {@link #getChildAt(int)}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5033 | * |
| 5034 | * @param start the first index of the childrend range to detach |
| 5035 | * @param count the number of children to detach |
| 5036 | * |
| 5037 | * @see #detachViewFromParent(View) |
| 5038 | * @see #detachViewFromParent(int) |
| 5039 | * @see #detachAllViewsFromParent() |
| 5040 | * @see #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams) |
| 5041 | * @see #removeDetachedView(View, boolean) |
| 5042 | */ |
| 5043 | protected void detachViewsFromParent(int start, int count) { |
| 5044 | removeFromArray(start, count); |
| 5045 | } |
| 5046 | |
| 5047 | /** |
Chet Haase | ca479d4 | 2012-08-30 17:20:08 -0700 | [diff] [blame] | 5048 | * Detaches all views from the parent. Detaching a view should be followed |
| 5049 | * either by a call to |
| 5050 | * {@link #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)} |
| 5051 | * or a call to {@link #removeDetachedView(View, boolean)}. Detachment should only be |
| 5052 | * temporary; reattachment or removal should happen within the same drawing cycle as |
| 5053 | * detachment. When a view is detached, its parent is null and cannot be retrieved by a |
| 5054 | * call to {@link #getChildAt(int)}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5055 | * |
| 5056 | * @see #detachViewFromParent(View) |
| 5057 | * @see #detachViewFromParent(int) |
| 5058 | * @see #detachViewsFromParent(int, int) |
| 5059 | * @see #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams) |
| 5060 | * @see #removeDetachedView(View, boolean) |
| 5061 | */ |
| 5062 | protected void detachAllViewsFromParent() { |
| 5063 | final int count = mChildrenCount; |
| 5064 | if (count <= 0) { |
| 5065 | return; |
| 5066 | } |
| 5067 | |
| 5068 | final View[] children = mChildren; |
| 5069 | mChildrenCount = 0; |
| 5070 | |
| 5071 | for (int i = count - 1; i >= 0; i--) { |
| 5072 | children[i].mParent = null; |
| 5073 | children[i] = null; |
| 5074 | } |
| 5075 | } |
| 5076 | |
| 5077 | /** |
| 5078 | * Don't call or override this method. It is used for the implementation of |
| 5079 | * the view hierarchy. |
| 5080 | */ |
Alan Viverette | be463f2 | 2016-01-21 10:50:10 -0500 | [diff] [blame] | 5081 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5082 | public final void invalidateChild(View child, final Rect dirty) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5083 | ViewParent parent = this; |
| 5084 | |
| 5085 | final AttachInfo attachInfo = mAttachInfo; |
| 5086 | if (attachInfo != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5087 | // If the child is drawing an animation, we want to copy this flag onto |
| 5088 | // ourselves and the parent to make sure the invalidate request goes |
| 5089 | // through |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 5090 | final boolean drawAnimation = (child.mPrivateFlags & PFLAG_DRAW_ANIMATION) |
| 5091 | == PFLAG_DRAW_ANIMATION; |
Romain Guy | 24443ea | 2009-05-11 11:56:30 -0700 | [diff] [blame] | 5092 | |
Romain Guy | fe455af | 2012-02-15 16:40:20 -0800 | [diff] [blame] | 5093 | // Check whether the child that requests the invalidate is fully opaque |
| 5094 | // Views being animated or transformed are not considered opaque because we may |
| 5095 | // be invalidating their old position and need the parent to paint behind them. |
| 5096 | Matrix childMatrix = child.getMatrix(); |
| 5097 | final boolean isOpaque = child.isOpaque() && !drawAnimation && |
| 5098 | child.getAnimation() == null && childMatrix.isIdentity(); |
| 5099 | // Mark the child as dirty, using the appropriate flag |
| 5100 | // Make sure we do not set both flags at the same time |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 5101 | int opaqueFlag = isOpaque ? PFLAG_DIRTY_OPAQUE : PFLAG_DIRTY; |
Romain Guy | fe455af | 2012-02-15 16:40:20 -0800 | [diff] [blame] | 5102 | |
John Reck | 96bb8ad | 2014-06-19 10:53:03 -0700 | [diff] [blame] | 5103 | if (child.mLayerType != LAYER_TYPE_NONE) { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 5104 | mPrivateFlags |= PFLAG_INVALIDATED; |
| 5105 | mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID; |
Romain Guy | fe455af | 2012-02-15 16:40:20 -0800 | [diff] [blame] | 5106 | } |
| 5107 | |
| 5108 | final int[] location = attachInfo.mInvalidateChildLocation; |
| 5109 | location[CHILD_LEFT_INDEX] = child.mLeft; |
| 5110 | location[CHILD_TOP_INDEX] = child.mTop; |
Chet Haase | 599913d | 2012-07-23 16:22:05 -0700 | [diff] [blame] | 5111 | if (!childMatrix.isIdentity() || |
| 5112 | (mGroupFlags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) { |
Romain Guy | fe455af | 2012-02-15 16:40:20 -0800 | [diff] [blame] | 5113 | RectF boundingRect = attachInfo.mTmpTransformRect; |
| 5114 | boundingRect.set(dirty); |
Chet Haase | 599913d | 2012-07-23 16:22:05 -0700 | [diff] [blame] | 5115 | Matrix transformMatrix; |
| 5116 | if ((mGroupFlags & ViewGroup.FLAG_SUPPORT_STATIC_TRANSFORMATIONS) != 0) { |
| 5117 | Transformation t = attachInfo.mTmpTransformation; |
| 5118 | boolean transformed = getChildStaticTransformation(child, t); |
| 5119 | if (transformed) { |
| 5120 | transformMatrix = attachInfo.mTmpMatrix; |
| 5121 | transformMatrix.set(t.getMatrix()); |
| 5122 | if (!childMatrix.isIdentity()) { |
| 5123 | transformMatrix.preConcat(childMatrix); |
| 5124 | } |
| 5125 | } else { |
| 5126 | transformMatrix = childMatrix; |
| 5127 | } |
| 5128 | } else { |
| 5129 | transformMatrix = childMatrix; |
| 5130 | } |
| 5131 | transformMatrix.mapRect(boundingRect); |
Alan Viverette | c45b1d4 | 2015-11-16 15:38:59 -0500 | [diff] [blame] | 5132 | dirty.set((int) Math.floor(boundingRect.left), |
| 5133 | (int) Math.floor(boundingRect.top), |
| 5134 | (int) Math.ceil(boundingRect.right), |
| 5135 | (int) Math.ceil(boundingRect.bottom)); |
Romain Guy | fe455af | 2012-02-15 16:40:20 -0800 | [diff] [blame] | 5136 | } |
| 5137 | |
| 5138 | do { |
| 5139 | View view = null; |
| 5140 | if (parent instanceof View) { |
| 5141 | view = (View) parent; |
Romain Guy | fe455af | 2012-02-15 16:40:20 -0800 | [diff] [blame] | 5142 | } |
| 5143 | |
| 5144 | if (drawAnimation) { |
| 5145 | if (view != null) { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 5146 | view.mPrivateFlags |= PFLAG_DRAW_ANIMATION; |
Romain Guy | fe455af | 2012-02-15 16:40:20 -0800 | [diff] [blame] | 5147 | } else if (parent instanceof ViewRootImpl) { |
| 5148 | ((ViewRootImpl) parent).mIsAnimating = true; |
| 5149 | } |
| 5150 | } |
| 5151 | |
| 5152 | // If the parent is dirty opaque or not dirty, mark it dirty with the opaque |
| 5153 | // flag coming from the child that initiated the invalidate |
| 5154 | if (view != null) { |
| 5155 | if ((view.mViewFlags & FADING_EDGE_MASK) != 0 && |
| 5156 | view.getSolidColor() == 0) { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 5157 | opaqueFlag = PFLAG_DIRTY; |
Romain Guy | fe455af | 2012-02-15 16:40:20 -0800 | [diff] [blame] | 5158 | } |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 5159 | if ((view.mPrivateFlags & PFLAG_DIRTY_MASK) != PFLAG_DIRTY) { |
| 5160 | view.mPrivateFlags = (view.mPrivateFlags & ~PFLAG_DIRTY_MASK) | opaqueFlag; |
Romain Guy | fe455af | 2012-02-15 16:40:20 -0800 | [diff] [blame] | 5161 | } |
| 5162 | } |
| 5163 | |
| 5164 | parent = parent.invalidateChildInParent(location, dirty); |
| 5165 | if (view != null) { |
| 5166 | // Account for transform on current parent |
| 5167 | Matrix m = view.getMatrix(); |
| 5168 | if (!m.isIdentity()) { |
| 5169 | RectF boundingRect = attachInfo.mTmpTransformRect; |
| 5170 | boundingRect.set(dirty); |
| 5171 | m.mapRect(boundingRect); |
Alan Viverette | c45b1d4 | 2015-11-16 15:38:59 -0500 | [diff] [blame] | 5172 | dirty.set((int) Math.floor(boundingRect.left), |
| 5173 | (int) Math.floor(boundingRect.top), |
| 5174 | (int) Math.ceil(boundingRect.right), |
| 5175 | (int) Math.ceil(boundingRect.bottom)); |
Romain Guy | fe455af | 2012-02-15 16:40:20 -0800 | [diff] [blame] | 5176 | } |
| 5177 | } |
| 5178 | } while (parent != null); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5179 | } |
| 5180 | } |
| 5181 | |
| 5182 | /** |
| 5183 | * Don't call or override this method. It is used for the implementation of |
| 5184 | * the view hierarchy. |
| 5185 | * |
| 5186 | * This implementation returns null if this ViewGroup does not have a parent, |
| 5187 | * if this ViewGroup is already fully invalidated or if the dirty rectangle |
| 5188 | * does not intersect with this ViewGroup's bounds. |
| 5189 | */ |
Alan Viverette | be463f2 | 2016-01-21 10:50:10 -0500 | [diff] [blame] | 5190 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5191 | public ViewParent invalidateChildInParent(final int[] location, final Rect dirty) { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 5192 | if ((mPrivateFlags & PFLAG_DRAWN) == PFLAG_DRAWN || |
| 5193 | (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) == PFLAG_DRAWING_CACHE_VALID) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5194 | if ((mGroupFlags & (FLAG_OPTIMIZE_INVALIDATE | FLAG_ANIMATION_DONE)) != |
| 5195 | FLAG_OPTIMIZE_INVALIDATE) { |
| 5196 | dirty.offset(location[CHILD_LEFT_INDEX] - mScrollX, |
| 5197 | location[CHILD_TOP_INDEX] - mScrollY); |
Chet Haase | a4f14eb | 2013-04-22 11:11:39 -0700 | [diff] [blame] | 5198 | if ((mGroupFlags & FLAG_CLIP_CHILDREN) == 0) { |
| 5199 | dirty.union(0, 0, mRight - mLeft, mBottom - mTop); |
| 5200 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5201 | |
| 5202 | final int left = mLeft; |
| 5203 | final int top = mTop; |
| 5204 | |
Chet Haase | 05e91ed | 2012-07-03 14:17:57 -0700 | [diff] [blame] | 5205 | if ((mGroupFlags & FLAG_CLIP_CHILDREN) == FLAG_CLIP_CHILDREN) { |
| 5206 | if (!dirty.intersect(0, 0, mRight - left, mBottom - top)) { |
| 5207 | dirty.setEmpty(); |
Romain Guy | 3a3133d | 2011-02-01 22:59:58 -0800 | [diff] [blame] | 5208 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5209 | } |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 5210 | mPrivateFlags &= ~PFLAG_DRAWING_CACHE_VALID; |
Chet Haase | 05e91ed | 2012-07-03 14:17:57 -0700 | [diff] [blame] | 5211 | |
| 5212 | location[CHILD_LEFT_INDEX] = left; |
| 5213 | location[CHILD_TOP_INDEX] = top; |
| 5214 | |
John Reck | 96bb8ad | 2014-06-19 10:53:03 -0700 | [diff] [blame] | 5215 | if (mLayerType != LAYER_TYPE_NONE) { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 5216 | mPrivateFlags |= PFLAG_INVALIDATED; |
Chet Haase | 05e91ed | 2012-07-03 14:17:57 -0700 | [diff] [blame] | 5217 | } |
| 5218 | |
| 5219 | return mParent; |
| 5220 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5221 | } else { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 5222 | mPrivateFlags &= ~PFLAG_DRAWN & ~PFLAG_DRAWING_CACHE_VALID; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5223 | |
| 5224 | location[CHILD_LEFT_INDEX] = mLeft; |
| 5225 | location[CHILD_TOP_INDEX] = mTop; |
Chet Haase | a3db866 | 2011-07-19 10:36:05 -0700 | [diff] [blame] | 5226 | if ((mGroupFlags & FLAG_CLIP_CHILDREN) == FLAG_CLIP_CHILDREN) { |
| 5227 | dirty.set(0, 0, mRight - mLeft, mBottom - mTop); |
| 5228 | } else { |
| 5229 | // in case the dirty rect extends outside the bounds of this container |
| 5230 | dirty.union(0, 0, mRight - mLeft, mBottom - mTop); |
| 5231 | } |
Romain Guy | 3a3133d | 2011-02-01 22:59:58 -0800 | [diff] [blame] | 5232 | |
John Reck | 96bb8ad | 2014-06-19 10:53:03 -0700 | [diff] [blame] | 5233 | if (mLayerType != LAYER_TYPE_NONE) { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 5234 | mPrivateFlags |= PFLAG_INVALIDATED; |
Romain Guy | 3a3133d | 2011-02-01 22:59:58 -0800 | [diff] [blame] | 5235 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5236 | |
| 5237 | return mParent; |
| 5238 | } |
| 5239 | } |
| 5240 | |
| 5241 | return null; |
| 5242 | } |
| 5243 | |
| 5244 | /** |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 5245 | * Native-calculated damage path |
| 5246 | * Returns false if this path was unable to complete successfully. This means |
| 5247 | * it hit a ViewParent it doesn't recognize and needs to fall back to calculating |
| 5248 | * damage area |
| 5249 | * @hide |
| 5250 | */ |
| 5251 | public boolean damageChildDeferred(View child) { |
| 5252 | ViewParent parent = getParent(); |
| 5253 | while (parent != null) { |
| 5254 | if (parent instanceof ViewGroup) { |
| 5255 | parent = parent.getParent(); |
| 5256 | } else if (parent instanceof ViewRootImpl) { |
| 5257 | ((ViewRootImpl) parent).invalidate(); |
| 5258 | return true; |
| 5259 | } else { |
| 5260 | parent = null; |
| 5261 | } |
| 5262 | } |
| 5263 | return false; |
| 5264 | } |
| 5265 | |
| 5266 | /** |
Chet Haase | 9d1992d | 2012-03-13 11:03:25 -0700 | [diff] [blame] | 5267 | * Quick invalidation method called by View.invalidateViewProperty. This doesn't set the |
| 5268 | * DRAWN flags and doesn't handle the Animation logic that the default invalidation methods |
| 5269 | * do; all we want to do here is schedule a traversal with the appropriate dirty rect. |
| 5270 | * |
| 5271 | * @hide |
| 5272 | */ |
Chris Craik | 49e6c73 | 2014-03-31 12:34:11 -0700 | [diff] [blame] | 5273 | public void damageChild(View child, final Rect dirty) { |
John Reck | e4267ea | 2014-06-03 15:53:15 -0700 | [diff] [blame] | 5274 | if (damageChildDeferred(child)) { |
| 5275 | return; |
| 5276 | } |
| 5277 | |
Chet Haase | 9d1992d | 2012-03-13 11:03:25 -0700 | [diff] [blame] | 5278 | ViewParent parent = this; |
| 5279 | |
| 5280 | final AttachInfo attachInfo = mAttachInfo; |
| 5281 | if (attachInfo != null) { |
Chet Haase | 9d1992d | 2012-03-13 11:03:25 -0700 | [diff] [blame] | 5282 | int left = child.mLeft; |
| 5283 | int top = child.mTop; |
| 5284 | if (!child.getMatrix().isIdentity()) { |
| 5285 | child.transformRect(dirty); |
| 5286 | } |
| 5287 | |
| 5288 | do { |
| 5289 | if (parent instanceof ViewGroup) { |
| 5290 | ViewGroup parentVG = (ViewGroup) parent; |
Chet Haase | b85967b | 2012-03-26 14:37:51 -0700 | [diff] [blame] | 5291 | if (parentVG.mLayerType != LAYER_TYPE_NONE) { |
| 5292 | // Layered parents should be recreated, not just re-issued |
| 5293 | parentVG.invalidate(); |
| 5294 | parent = null; |
| 5295 | } else { |
Chris Craik | 49e6c73 | 2014-03-31 12:34:11 -0700 | [diff] [blame] | 5296 | parent = parentVG.damageChildInParent(left, top, dirty); |
Chet Haase | b85967b | 2012-03-26 14:37:51 -0700 | [diff] [blame] | 5297 | left = parentVG.mLeft; |
| 5298 | top = parentVG.mTop; |
| 5299 | } |
Chet Haase | 9d1992d | 2012-03-13 11:03:25 -0700 | [diff] [blame] | 5300 | } else { |
| 5301 | // Reached the top; this calls into the usual invalidate method in |
| 5302 | // ViewRootImpl, which schedules a traversal |
| 5303 | final int[] location = attachInfo.mInvalidateChildLocation; |
| 5304 | location[0] = left; |
| 5305 | location[1] = top; |
| 5306 | parent = parent.invalidateChildInParent(location, dirty); |
| 5307 | } |
| 5308 | } while (parent != null); |
| 5309 | } |
| 5310 | } |
| 5311 | |
| 5312 | /** |
| 5313 | * Quick invalidation method that simply transforms the dirty rect into the parent's |
| 5314 | * coordinate system, pruning the invalidation if the parent has already been invalidated. |
Chet Haase | e4a2d7c | 2013-06-21 17:49:36 -0700 | [diff] [blame] | 5315 | * |
| 5316 | * @hide |
Chet Haase | 9d1992d | 2012-03-13 11:03:25 -0700 | [diff] [blame] | 5317 | */ |
Chris Craik | 49e6c73 | 2014-03-31 12:34:11 -0700 | [diff] [blame] | 5318 | protected ViewParent damageChildInParent(int left, int top, final Rect dirty) { |
| 5319 | if ((mPrivateFlags & PFLAG_DRAWN) != 0 |
| 5320 | || (mPrivateFlags & PFLAG_DRAWING_CACHE_VALID) != 0) { |
Chet Haase | 9d1992d | 2012-03-13 11:03:25 -0700 | [diff] [blame] | 5321 | dirty.offset(left - mScrollX, top - mScrollY); |
Chet Haase | a4f14eb | 2013-04-22 11:11:39 -0700 | [diff] [blame] | 5322 | if ((mGroupFlags & FLAG_CLIP_CHILDREN) == 0) { |
| 5323 | dirty.union(0, 0, mRight - mLeft, mBottom - mTop); |
| 5324 | } |
Chet Haase | 9d1992d | 2012-03-13 11:03:25 -0700 | [diff] [blame] | 5325 | |
| 5326 | if ((mGroupFlags & FLAG_CLIP_CHILDREN) == 0 || |
| 5327 | dirty.intersect(0, 0, mRight - mLeft, mBottom - mTop)) { |
| 5328 | |
Chet Haase | 9d1992d | 2012-03-13 11:03:25 -0700 | [diff] [blame] | 5329 | if (!getMatrix().isIdentity()) { |
| 5330 | transformRect(dirty); |
| 5331 | } |
| 5332 | |
| 5333 | return mParent; |
| 5334 | } |
| 5335 | } |
| 5336 | |
| 5337 | return null; |
| 5338 | } |
| 5339 | |
| 5340 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5341 | * Offset a rectangle that is in a descendant's coordinate |
| 5342 | * space into our coordinate space. |
| 5343 | * @param descendant A descendant of this view |
| 5344 | * @param rect A rectangle defined in descendant's coordinate space. |
| 5345 | */ |
| 5346 | public final void offsetDescendantRectToMyCoords(View descendant, Rect rect) { |
| 5347 | offsetRectBetweenParentAndChild(descendant, rect, true, false); |
| 5348 | } |
| 5349 | |
| 5350 | /** |
| 5351 | * Offset a rectangle that is in our coordinate space into an ancestor's |
| 5352 | * coordinate space. |
| 5353 | * @param descendant A descendant of this view |
| 5354 | * @param rect A rectangle defined in descendant's coordinate space. |
| 5355 | */ |
| 5356 | public final void offsetRectIntoDescendantCoords(View descendant, Rect rect) { |
| 5357 | offsetRectBetweenParentAndChild(descendant, rect, false, false); |
| 5358 | } |
| 5359 | |
| 5360 | /** |
| 5361 | * Helper method that offsets a rect either from parent to descendant or |
| 5362 | * descendant to parent. |
| 5363 | */ |
| 5364 | void offsetRectBetweenParentAndChild(View descendant, Rect rect, |
| 5365 | boolean offsetFromChildToParent, boolean clipToBounds) { |
| 5366 | |
| 5367 | // already in the same coord system :) |
| 5368 | if (descendant == this) { |
| 5369 | return; |
| 5370 | } |
| 5371 | |
| 5372 | ViewParent theParent = descendant.mParent; |
| 5373 | |
| 5374 | // search and offset up to the parent |
| 5375 | while ((theParent != null) |
| 5376 | && (theParent instanceof View) |
| 5377 | && (theParent != this)) { |
| 5378 | |
| 5379 | if (offsetFromChildToParent) { |
Hyunyoung Song | 4eb1a4e | 2016-03-09 22:51:02 +0000 | [diff] [blame] | 5380 | rect.offset(descendant.mLeft - descendant.mScrollX, |
| 5381 | descendant.mTop - descendant.mScrollY); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5382 | if (clipToBounds) { |
| 5383 | View p = (View) theParent; |
Doris Liu | 9607fbe | 2015-05-28 17:17:28 -0700 | [diff] [blame] | 5384 | boolean intersected = rect.intersect(0, 0, p.mRight - p.mLeft, |
| 5385 | p.mBottom - p.mTop); |
| 5386 | if (!intersected) { |
| 5387 | rect.setEmpty(); |
| 5388 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5389 | } |
| 5390 | } else { |
| 5391 | if (clipToBounds) { |
| 5392 | View p = (View) theParent; |
Doris Liu | 9607fbe | 2015-05-28 17:17:28 -0700 | [diff] [blame] | 5393 | boolean intersected = rect.intersect(0, 0, p.mRight - p.mLeft, |
| 5394 | p.mBottom - p.mTop); |
| 5395 | if (!intersected) { |
| 5396 | rect.setEmpty(); |
| 5397 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5398 | } |
Hyunyoung Song | 4eb1a4e | 2016-03-09 22:51:02 +0000 | [diff] [blame] | 5399 | rect.offset(descendant.mScrollX - descendant.mLeft, |
| 5400 | descendant.mScrollY - descendant.mTop); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5401 | } |
| 5402 | |
| 5403 | descendant = (View) theParent; |
| 5404 | theParent = descendant.mParent; |
| 5405 | } |
| 5406 | |
| 5407 | // now that we are up to this view, need to offset one more time |
| 5408 | // to get into our coordinate space |
| 5409 | if (theParent == this) { |
| 5410 | if (offsetFromChildToParent) { |
Hyunyoung Song | 4eb1a4e | 2016-03-09 22:51:02 +0000 | [diff] [blame] | 5411 | rect.offset(descendant.mLeft - descendant.mScrollX, |
| 5412 | descendant.mTop - descendant.mScrollY); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5413 | } else { |
Hyunyoung Song | 4eb1a4e | 2016-03-09 22:51:02 +0000 | [diff] [blame] | 5414 | rect.offset(descendant.mScrollX - descendant.mLeft, |
| 5415 | descendant.mScrollY - descendant.mTop); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5416 | } |
| 5417 | } else { |
| 5418 | throw new IllegalArgumentException("parameter must be a descendant of this view"); |
| 5419 | } |
| 5420 | } |
| 5421 | |
| 5422 | /** |
| 5423 | * Offset the vertical location of all children of this view by the specified number of pixels. |
| 5424 | * |
| 5425 | * @param offset the number of pixels to offset |
| 5426 | * |
| 5427 | * @hide |
| 5428 | */ |
| 5429 | public void offsetChildrenTopAndBottom(int offset) { |
| 5430 | final int count = mChildrenCount; |
| 5431 | final View[] children = mChildren; |
Romain Guy | 5549cb5 | 2013-05-06 18:42:08 -0700 | [diff] [blame] | 5432 | boolean invalidate = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5433 | |
| 5434 | for (int i = 0; i < count; i++) { |
| 5435 | final View v = children[i]; |
| 5436 | v.mTop += offset; |
| 5437 | v.mBottom += offset; |
Chris Craik | 64a12e1 | 2014-03-28 18:12:12 -0700 | [diff] [blame] | 5438 | if (v.mRenderNode != null) { |
Romain Guy | 5549cb5 | 2013-05-06 18:42:08 -0700 | [diff] [blame] | 5439 | invalidate = true; |
Chris Craik | 64a12e1 | 2014-03-28 18:12:12 -0700 | [diff] [blame] | 5440 | v.mRenderNode.offsetTopAndBottom(offset); |
Chet Haase | a1cff50 | 2012-02-21 13:43:44 -0800 | [diff] [blame] | 5441 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5442 | } |
Romain Guy | 5549cb5 | 2013-05-06 18:42:08 -0700 | [diff] [blame] | 5443 | |
| 5444 | if (invalidate) { |
| 5445 | invalidateViewProperty(false, false); |
| 5446 | } |
Guang Zhu | 84e2509 | 2014-05-01 21:12:55 -0700 | [diff] [blame] | 5447 | notifySubtreeAccessibilityStateChangedIfNeeded(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5448 | } |
| 5449 | |
Alan Viverette | be463f2 | 2016-01-21 10:50:10 -0500 | [diff] [blame] | 5450 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5451 | public boolean getChildVisibleRect(View child, Rect r, android.graphics.Point offset) { |
Adam Powell | f93bb6d | 2011-12-12 15:21:57 -0800 | [diff] [blame] | 5452 | // It doesn't make a whole lot of sense to call this on a view that isn't attached, |
| 5453 | // but for some simple tests it can be useful. If we don't have attach info this |
| 5454 | // will allocate memory. |
| 5455 | final RectF rect = mAttachInfo != null ? mAttachInfo.mTmpTransformRect : new RectF(); |
Gilles Debunne | cea4513 | 2011-11-24 02:19:27 +0100 | [diff] [blame] | 5456 | rect.set(r); |
| 5457 | |
| 5458 | if (!child.hasIdentityMatrix()) { |
George Mount | 002d43d | 2014-11-11 12:54:43 -0800 | [diff] [blame] | 5459 | child.getMatrix().mapRect(rect); |
Gilles Debunne | cea4513 | 2011-11-24 02:19:27 +0100 | [diff] [blame] | 5460 | } |
| 5461 | |
Alan Viverette | ab2cf6d | 2014-11-26 14:34:36 -0800 | [diff] [blame] | 5462 | final int dx = child.mLeft - mScrollX; |
| 5463 | final int dy = child.mTop - mScrollY; |
Gilles Debunne | cea4513 | 2011-11-24 02:19:27 +0100 | [diff] [blame] | 5464 | |
| 5465 | rect.offset(dx, dy); |
| 5466 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5467 | if (offset != null) { |
Gilles Debunne | cea4513 | 2011-11-24 02:19:27 +0100 | [diff] [blame] | 5468 | if (!child.hasIdentityMatrix()) { |
Adam Powell | f93bb6d | 2011-12-12 15:21:57 -0800 | [diff] [blame] | 5469 | float[] position = mAttachInfo != null ? mAttachInfo.mTmpTransformLocation |
| 5470 | : new float[2]; |
Gilles Debunne | cea4513 | 2011-11-24 02:19:27 +0100 | [diff] [blame] | 5471 | position[0] = offset.x; |
| 5472 | position[1] = offset.y; |
| 5473 | child.getMatrix().mapPoints(position); |
Alan Viverette | c45b1d4 | 2015-11-16 15:38:59 -0500 | [diff] [blame] | 5474 | offset.x = Math.round(position[0]); |
| 5475 | offset.y = Math.round(position[1]); |
Gilles Debunne | cea4513 | 2011-11-24 02:19:27 +0100 | [diff] [blame] | 5476 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5477 | offset.x += dx; |
| 5478 | offset.y += dy; |
| 5479 | } |
Gilles Debunne | cea4513 | 2011-11-24 02:19:27 +0100 | [diff] [blame] | 5480 | |
Alan Viverette | ab2cf6d | 2014-11-26 14:34:36 -0800 | [diff] [blame] | 5481 | final int width = mRight - mLeft; |
| 5482 | final int height = mBottom - mTop; |
| 5483 | |
George Mount | 002d43d | 2014-11-11 12:54:43 -0800 | [diff] [blame] | 5484 | boolean rectIsVisible = true; |
Adam Powell | 35da41e | 2014-12-10 18:59:34 -0800 | [diff] [blame] | 5485 | if (mParent == null || |
| 5486 | (mParent instanceof ViewGroup && ((ViewGroup) mParent).getClipChildren())) { |
Alan Viverette | ab2cf6d | 2014-11-26 14:34:36 -0800 | [diff] [blame] | 5487 | // Clip to bounds. |
| 5488 | rectIsVisible = rect.intersect(0, 0, width, height); |
Gilles Debunne | cea4513 | 2011-11-24 02:19:27 +0100 | [diff] [blame] | 5489 | } |
| 5490 | |
George Mount | 002d43d | 2014-11-11 12:54:43 -0800 | [diff] [blame] | 5491 | if (rectIsVisible && (mGroupFlags & CLIP_TO_PADDING_MASK) == CLIP_TO_PADDING_MASK) { |
Alan Viverette | ab2cf6d | 2014-11-26 14:34:36 -0800 | [diff] [blame] | 5492 | // Clip to padding. |
George Mount | 002d43d | 2014-11-11 12:54:43 -0800 | [diff] [blame] | 5493 | rectIsVisible = rect.intersect(mPaddingLeft, mPaddingTop, |
Alan Viverette | ab2cf6d | 2014-11-26 14:34:36 -0800 | [diff] [blame] | 5494 | width - mPaddingRight, height - mPaddingBottom); |
George Mount | 002d43d | 2014-11-11 12:54:43 -0800 | [diff] [blame] | 5495 | } |
| 5496 | |
| 5497 | if (rectIsVisible && mClipBounds != null) { |
Alan Viverette | ab2cf6d | 2014-11-26 14:34:36 -0800 | [diff] [blame] | 5498 | // Clip to clipBounds. |
George Mount | 002d43d | 2014-11-11 12:54:43 -0800 | [diff] [blame] | 5499 | rectIsVisible = rect.intersect(mClipBounds.left, mClipBounds.top, mClipBounds.right, |
| 5500 | mClipBounds.bottom); |
| 5501 | } |
Alan Viverette | c45b1d4 | 2015-11-16 15:38:59 -0500 | [diff] [blame] | 5502 | r.set((int) Math.floor(rect.left), (int) Math.floor(rect.top), |
| 5503 | (int) Math.ceil(rect.right), (int) Math.ceil(rect.bottom)); |
George Mount | 002d43d | 2014-11-11 12:54:43 -0800 | [diff] [blame] | 5504 | if (rectIsVisible && mParent != null) { |
| 5505 | rectIsVisible = mParent.getChildVisibleRect(this, r, offset); |
| 5506 | } |
| 5507 | return rectIsVisible; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5508 | } |
| 5509 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5510 | @Override |
Chet Haase | 9c08744 | 2011-01-12 16:20:16 -0800 | [diff] [blame] | 5511 | public final void layout(int l, int t, int r, int b) { |
Chet Haase | 430742f | 2013-04-12 11:18:36 -0700 | [diff] [blame] | 5512 | if (!mSuppressLayout && (mTransition == null || !mTransition.isChangingLayout())) { |
Chet Haase | 7dd4a53 | 2012-04-16 13:35:09 -0700 | [diff] [blame] | 5513 | if (mTransition != null) { |
| 5514 | mTransition.layoutChange(this); |
| 5515 | } |
Chet Haase | 9c08744 | 2011-01-12 16:20:16 -0800 | [diff] [blame] | 5516 | super.layout(l, t, r, b); |
| 5517 | } else { |
| 5518 | // 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] | 5519 | mLayoutCalledWhileSuppressed = true; |
Chet Haase | 9c08744 | 2011-01-12 16:20:16 -0800 | [diff] [blame] | 5520 | } |
| 5521 | } |
| 5522 | |
Chet Haase | 9c08744 | 2011-01-12 16:20:16 -0800 | [diff] [blame] | 5523 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5524 | protected abstract void onLayout(boolean changed, |
| 5525 | int l, int t, int r, int b); |
| 5526 | |
| 5527 | /** |
| 5528 | * Indicates whether the view group has the ability to animate its children |
| 5529 | * after the first layout. |
| 5530 | * |
| 5531 | * @return true if the children can be animated, false otherwise |
| 5532 | */ |
| 5533 | protected boolean canAnimate() { |
| 5534 | return mLayoutAnimationController != null; |
| 5535 | } |
| 5536 | |
| 5537 | /** |
| 5538 | * Runs the layout animation. Calling this method triggers a relayout of |
| 5539 | * this view group. |
| 5540 | */ |
| 5541 | public void startLayoutAnimation() { |
| 5542 | if (mLayoutAnimationController != null) { |
| 5543 | mGroupFlags |= FLAG_RUN_ANIMATION; |
| 5544 | requestLayout(); |
| 5545 | } |
| 5546 | } |
| 5547 | |
| 5548 | /** |
| 5549 | * Schedules the layout animation to be played after the next layout pass |
| 5550 | * of this view group. This can be used to restart the layout animation |
| 5551 | * when the content of the view group changes or when the activity is |
| 5552 | * paused and resumed. |
| 5553 | */ |
| 5554 | public void scheduleLayoutAnimation() { |
| 5555 | mGroupFlags |= FLAG_RUN_ANIMATION; |
| 5556 | } |
| 5557 | |
| 5558 | /** |
| 5559 | * Sets the layout animation controller used to animate the group's |
| 5560 | * children after the first layout. |
| 5561 | * |
| 5562 | * @param controller the animation controller |
| 5563 | */ |
| 5564 | public void setLayoutAnimation(LayoutAnimationController controller) { |
| 5565 | mLayoutAnimationController = controller; |
| 5566 | if (mLayoutAnimationController != null) { |
| 5567 | mGroupFlags |= FLAG_RUN_ANIMATION; |
| 5568 | } |
| 5569 | } |
| 5570 | |
| 5571 | /** |
| 5572 | * Returns the layout animation controller used to animate the group's |
| 5573 | * children. |
| 5574 | * |
| 5575 | * @return the current animation controller |
| 5576 | */ |
| 5577 | public LayoutAnimationController getLayoutAnimation() { |
| 5578 | return mLayoutAnimationController; |
| 5579 | } |
| 5580 | |
| 5581 | /** |
| 5582 | * Indicates whether the children's drawing cache is used during a layout |
| 5583 | * animation. By default, the drawing cache is enabled but this will prevent |
| 5584 | * nested layout animations from working. To nest animations, you must disable |
| 5585 | * the cache. |
| 5586 | * |
| 5587 | * @return true if the animation cache is enabled, false otherwise |
| 5588 | * |
| 5589 | * @see #setAnimationCacheEnabled(boolean) |
| 5590 | * @see View#setDrawingCacheEnabled(boolean) |
Chris Craik | 5a6bbae | 2015-04-10 17:41:34 -0700 | [diff] [blame] | 5591 | * |
Dianne Hackborn | 0e3de6c | 2015-07-29 15:20:21 -0700 | [diff] [blame] | 5592 | * @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] | 5593 | * 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] | 5594 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5595 | public boolean isAnimationCacheEnabled() { |
| 5596 | return (mGroupFlags & FLAG_ANIMATION_CACHE) == FLAG_ANIMATION_CACHE; |
| 5597 | } |
| 5598 | |
| 5599 | /** |
| 5600 | * Enables or disables the children's drawing cache during a layout animation. |
| 5601 | * By default, the drawing cache is enabled but this will prevent nested |
| 5602 | * layout animations from working. To nest animations, you must disable the |
| 5603 | * cache. |
| 5604 | * |
| 5605 | * @param enabled true to enable the animation cache, false otherwise |
| 5606 | * |
| 5607 | * @see #isAnimationCacheEnabled() |
| 5608 | * @see View#setDrawingCacheEnabled(boolean) |
Chris Craik | 5a6bbae | 2015-04-10 17:41:34 -0700 | [diff] [blame] | 5609 | * |
Dianne Hackborn | 0e3de6c | 2015-07-29 15:20:21 -0700 | [diff] [blame] | 5610 | * @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] | 5611 | * 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] | 5612 | */ |
| 5613 | public void setAnimationCacheEnabled(boolean enabled) { |
| 5614 | setBooleanFlag(FLAG_ANIMATION_CACHE, enabled); |
| 5615 | } |
| 5616 | |
| 5617 | /** |
| 5618 | * Indicates whether this ViewGroup will always try to draw its children using their |
| 5619 | * drawing cache. By default this property is enabled. |
| 5620 | * |
| 5621 | * @return true if the animation cache is enabled, false otherwise |
| 5622 | * |
| 5623 | * @see #setAlwaysDrawnWithCacheEnabled(boolean) |
| 5624 | * @see #setChildrenDrawnWithCacheEnabled(boolean) |
| 5625 | * @see View#setDrawingCacheEnabled(boolean) |
Chris Craik | 5a6bbae | 2015-04-10 17:41:34 -0700 | [diff] [blame] | 5626 | * |
Dianne Hackborn | 0e3de6c | 2015-07-29 15:20:21 -0700 | [diff] [blame] | 5627 | * @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] | 5628 | * 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] | 5629 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5630 | public boolean isAlwaysDrawnWithCacheEnabled() { |
| 5631 | return (mGroupFlags & FLAG_ALWAYS_DRAWN_WITH_CACHE) == FLAG_ALWAYS_DRAWN_WITH_CACHE; |
| 5632 | } |
| 5633 | |
| 5634 | /** |
| 5635 | * Indicates whether this ViewGroup will always try to draw its children using their |
| 5636 | * drawing cache. This property can be set to true when the cache rendering is |
| 5637 | * slightly different from the children's normal rendering. Renderings can be different, |
| 5638 | * for instance, when the cache's quality is set to low. |
| 5639 | * |
| 5640 | * When this property is disabled, the ViewGroup will use the drawing cache of its |
| 5641 | * children only when asked to. It's usually the task of subclasses to tell ViewGroup |
| 5642 | * when to start using the drawing cache and when to stop using it. |
| 5643 | * |
| 5644 | * @param always true to always draw with the drawing cache, false otherwise |
| 5645 | * |
| 5646 | * @see #isAlwaysDrawnWithCacheEnabled() |
| 5647 | * @see #setChildrenDrawnWithCacheEnabled(boolean) |
| 5648 | * @see View#setDrawingCacheEnabled(boolean) |
| 5649 | * @see View#setDrawingCacheQuality(int) |
Chris Craik | 5a6bbae | 2015-04-10 17:41:34 -0700 | [diff] [blame] | 5650 | * |
Dianne Hackborn | 0e3de6c | 2015-07-29 15:20:21 -0700 | [diff] [blame] | 5651 | * @deprecated As of {@link android.os.Build.VERSION_CODES#M}, this property is ignored. |
Chris Craik | 5a6bbae | 2015-04-10 17:41:34 -0700 | [diff] [blame] | 5652 | * Child views may no longer have their caching behavior disabled by parents. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5653 | */ |
| 5654 | public void setAlwaysDrawnWithCacheEnabled(boolean always) { |
| 5655 | setBooleanFlag(FLAG_ALWAYS_DRAWN_WITH_CACHE, always); |
| 5656 | } |
| 5657 | |
| 5658 | /** |
| 5659 | * Indicates whether the ViewGroup is currently drawing its children using |
| 5660 | * their drawing cache. |
| 5661 | * |
| 5662 | * @return true if children should be drawn with their cache, false otherwise |
| 5663 | * |
| 5664 | * @see #setAlwaysDrawnWithCacheEnabled(boolean) |
| 5665 | * @see #setChildrenDrawnWithCacheEnabled(boolean) |
Chris Craik | 5a6bbae | 2015-04-10 17:41:34 -0700 | [diff] [blame] | 5666 | * |
Dianne Hackborn | 0e3de6c | 2015-07-29 15:20:21 -0700 | [diff] [blame] | 5667 | * @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] | 5668 | * Child views may no longer be forced to cache their rendering state by their parents. |
| 5669 | * 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] | 5670 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5671 | protected boolean isChildrenDrawnWithCacheEnabled() { |
| 5672 | return (mGroupFlags & FLAG_CHILDREN_DRAWN_WITH_CACHE) == FLAG_CHILDREN_DRAWN_WITH_CACHE; |
| 5673 | } |
| 5674 | |
| 5675 | /** |
| 5676 | * Tells the ViewGroup to draw its children using their drawing cache. This property |
| 5677 | * is ignored when {@link #isAlwaysDrawnWithCacheEnabled()} is true. A child's drawing cache |
| 5678 | * will be used only if it has been enabled. |
| 5679 | * |
| 5680 | * Subclasses should call this method to start and stop using the drawing cache when |
| 5681 | * they perform performance sensitive operations, like scrolling or animating. |
| 5682 | * |
| 5683 | * @param enabled true if children should be drawn with their cache, false otherwise |
| 5684 | * |
| 5685 | * @see #setAlwaysDrawnWithCacheEnabled(boolean) |
| 5686 | * @see #isChildrenDrawnWithCacheEnabled() |
Chris Craik | 5a6bbae | 2015-04-10 17:41:34 -0700 | [diff] [blame] | 5687 | * |
Dianne Hackborn | 0e3de6c | 2015-07-29 15:20:21 -0700 | [diff] [blame] | 5688 | * @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] | 5689 | * Child views may no longer be forced to cache their rendering state by their parents. |
| 5690 | * 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] | 5691 | */ |
| 5692 | protected void setChildrenDrawnWithCacheEnabled(boolean enabled) { |
| 5693 | setBooleanFlag(FLAG_CHILDREN_DRAWN_WITH_CACHE, enabled); |
| 5694 | } |
| 5695 | |
Romain Guy | 293451e | 2009-11-04 13:59:48 -0800 | [diff] [blame] | 5696 | /** |
| 5697 | * Indicates whether the ViewGroup is drawing its children in the order defined by |
| 5698 | * {@link #getChildDrawingOrder(int, int)}. |
| 5699 | * |
| 5700 | * @return true if children drawing order is defined by {@link #getChildDrawingOrder(int, int)}, |
| 5701 | * false otherwise |
| 5702 | * |
| 5703 | * @see #setChildrenDrawingOrderEnabled(boolean) |
| 5704 | * @see #getChildDrawingOrder(int, int) |
| 5705 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 5706 | @ViewDebug.ExportedProperty(category = "drawing") |
Romain Guy | 293451e | 2009-11-04 13:59:48 -0800 | [diff] [blame] | 5707 | protected boolean isChildrenDrawingOrderEnabled() { |
| 5708 | return (mGroupFlags & FLAG_USE_CHILD_DRAWING_ORDER) == FLAG_USE_CHILD_DRAWING_ORDER; |
| 5709 | } |
| 5710 | |
| 5711 | /** |
| 5712 | * Tells the ViewGroup whether to draw its children in the order defined by the method |
| 5713 | * {@link #getChildDrawingOrder(int, int)}. |
Chris Craik | e83cbd4 | 2014-09-03 17:52:24 -0700 | [diff] [blame] | 5714 | * <p> |
| 5715 | * Note that {@link View#getZ() Z} reordering, done by {@link #dispatchDraw(Canvas)}, |
| 5716 | * will override custom child ordering done via this method. |
Romain Guy | 293451e | 2009-11-04 13:59:48 -0800 | [diff] [blame] | 5717 | * |
| 5718 | * @param enabled true if the order of the children when drawing is determined by |
| 5719 | * {@link #getChildDrawingOrder(int, int)}, false otherwise |
| 5720 | * |
| 5721 | * @see #isChildrenDrawingOrderEnabled() |
| 5722 | * @see #getChildDrawingOrder(int, int) |
| 5723 | */ |
| 5724 | protected void setChildrenDrawingOrderEnabled(boolean enabled) { |
| 5725 | setBooleanFlag(FLAG_USE_CHILD_DRAWING_ORDER, enabled); |
| 5726 | } |
| 5727 | |
Svetoslav | 6254f48 | 2013-06-04 17:22:14 -0700 | [diff] [blame] | 5728 | private boolean hasBooleanFlag(int flag) { |
Philip Milne | f091b66 | 2013-02-27 11:15:21 -0800 | [diff] [blame] | 5729 | return (mGroupFlags & flag) == flag; |
| 5730 | } |
| 5731 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5732 | private void setBooleanFlag(int flag, boolean value) { |
| 5733 | if (value) { |
| 5734 | mGroupFlags |= flag; |
| 5735 | } else { |
| 5736 | mGroupFlags &= ~flag; |
| 5737 | } |
| 5738 | } |
| 5739 | |
| 5740 | /** |
| 5741 | * Returns an integer indicating what types of drawing caches are kept in memory. |
| 5742 | * |
| 5743 | * @see #setPersistentDrawingCache(int) |
| 5744 | * @see #setAnimationCacheEnabled(boolean) |
| 5745 | * |
| 5746 | * @return one or a combination of {@link #PERSISTENT_NO_CACHE}, |
| 5747 | * {@link #PERSISTENT_ANIMATION_CACHE}, {@link #PERSISTENT_SCROLLING_CACHE} |
| 5748 | * and {@link #PERSISTENT_ALL_CACHES} |
| 5749 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 5750 | @ViewDebug.ExportedProperty(category = "drawing", mapping = { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5751 | @ViewDebug.IntToString(from = PERSISTENT_NO_CACHE, to = "NONE"), |
Romain Guy | 203688c | 2010-05-12 15:41:32 -0700 | [diff] [blame] | 5752 | @ViewDebug.IntToString(from = PERSISTENT_ANIMATION_CACHE, to = "ANIMATION"), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5753 | @ViewDebug.IntToString(from = PERSISTENT_SCROLLING_CACHE, to = "SCROLLING"), |
| 5754 | @ViewDebug.IntToString(from = PERSISTENT_ALL_CACHES, to = "ALL") |
| 5755 | }) |
| 5756 | public int getPersistentDrawingCache() { |
| 5757 | return mPersistentDrawingCache; |
| 5758 | } |
| 5759 | |
| 5760 | /** |
| 5761 | * Indicates what types of drawing caches should be kept in memory after |
| 5762 | * they have been created. |
| 5763 | * |
| 5764 | * @see #getPersistentDrawingCache() |
| 5765 | * @see #setAnimationCacheEnabled(boolean) |
| 5766 | * |
| 5767 | * @param drawingCacheToKeep one or a combination of {@link #PERSISTENT_NO_CACHE}, |
| 5768 | * {@link #PERSISTENT_ANIMATION_CACHE}, {@link #PERSISTENT_SCROLLING_CACHE} |
| 5769 | * and {@link #PERSISTENT_ALL_CACHES} |
| 5770 | */ |
| 5771 | public void setPersistentDrawingCache(int drawingCacheToKeep) { |
| 5772 | mPersistentDrawingCache = drawingCacheToKeep & PERSISTENT_ALL_CACHES; |
| 5773 | } |
| 5774 | |
Philip Milne | f091b66 | 2013-02-27 11:15:21 -0800 | [diff] [blame] | 5775 | private void setLayoutMode(int layoutMode, boolean explicitly) { |
| 5776 | mLayoutMode = layoutMode; |
| 5777 | setBooleanFlag(FLAG_LAYOUT_MODE_WAS_EXPLICITLY_SET, explicitly); |
| 5778 | } |
| 5779 | |
| 5780 | /** |
| 5781 | * Recursively traverse the view hierarchy, resetting the layoutMode of any |
| 5782 | * descendants that had inherited a different layoutMode from a previous parent. |
| 5783 | * Recursion terminates when a descendant's mode is: |
| 5784 | * <ul> |
| 5785 | * <li>Undefined</li> |
| 5786 | * <li>The same as the root node's</li> |
| 5787 | * <li>A mode that had been explicitly set</li> |
| 5788 | * <ul/> |
| 5789 | * The first two clauses are optimizations. |
| 5790 | * @param layoutModeOfRoot |
| 5791 | */ |
| 5792 | @Override |
| 5793 | void invalidateInheritedLayoutMode(int layoutModeOfRoot) { |
| 5794 | if (mLayoutMode == LAYOUT_MODE_UNDEFINED || |
| 5795 | mLayoutMode == layoutModeOfRoot || |
Svetoslav | 6254f48 | 2013-06-04 17:22:14 -0700 | [diff] [blame] | 5796 | hasBooleanFlag(FLAG_LAYOUT_MODE_WAS_EXPLICITLY_SET)) { |
Philip Milne | f091b66 | 2013-02-27 11:15:21 -0800 | [diff] [blame] | 5797 | return; |
| 5798 | } |
| 5799 | setLayoutMode(LAYOUT_MODE_UNDEFINED, false); |
| 5800 | |
| 5801 | // apply recursively |
| 5802 | for (int i = 0, N = getChildCount(); i < N; i++) { |
| 5803 | getChildAt(i).invalidateInheritedLayoutMode(layoutModeOfRoot); |
| 5804 | } |
| 5805 | } |
| 5806 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5807 | /** |
Philip Milne | cfb631b | 2012-10-26 10:51:46 -0700 | [diff] [blame] | 5808 | * Returns the basis of alignment during layout operations on this ViewGroup: |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 5809 | * either {@link #LAYOUT_MODE_CLIP_BOUNDS} or {@link #LAYOUT_MODE_OPTICAL_BOUNDS}. |
Philip Milne | cfb631b | 2012-10-26 10:51:46 -0700 | [diff] [blame] | 5810 | * <p> |
| 5811 | * If no layoutMode was explicitly set, either programmatically or in an XML resource, |
| 5812 | * the method returns the layoutMode of the view's parent ViewGroup if such a parent exists, |
| 5813 | * 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] | 5814 | * |
Philip Milne | fcc6a0f | 2012-04-16 16:12:19 -0700 | [diff] [blame] | 5815 | * @return the layout mode to use during layout operations |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 5816 | * |
| 5817 | * @see #setLayoutMode(int) |
| 5818 | */ |
| 5819 | public int getLayoutMode() { |
Philip Milne | cfb631b | 2012-10-26 10:51:46 -0700 | [diff] [blame] | 5820 | if (mLayoutMode == LAYOUT_MODE_UNDEFINED) { |
Philip Milne | f091b66 | 2013-02-27 11:15:21 -0800 | [diff] [blame] | 5821 | int inheritedLayoutMode = (mParent instanceof ViewGroup) ? |
| 5822 | ((ViewGroup) mParent).getLayoutMode() : LAYOUT_MODE_DEFAULT; |
| 5823 | setLayoutMode(inheritedLayoutMode, false); |
Philip Milne | cfb631b | 2012-10-26 10:51:46 -0700 | [diff] [blame] | 5824 | } |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 5825 | return mLayoutMode; |
| 5826 | } |
| 5827 | |
| 5828 | /** |
Philip Milne | cfb631b | 2012-10-26 10:51:46 -0700 | [diff] [blame] | 5829 | * Sets the basis of alignment during the layout of this ViewGroup. |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 5830 | * Valid values are either {@link #LAYOUT_MODE_CLIP_BOUNDS} or |
| 5831 | * {@link #LAYOUT_MODE_OPTICAL_BOUNDS}. |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 5832 | * |
Philip Milne | fcc6a0f | 2012-04-16 16:12:19 -0700 | [diff] [blame] | 5833 | * @param layoutMode the layout mode to use during layout operations |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 5834 | * |
| 5835 | * @see #getLayoutMode() |
Scott Main | 27a8508 | 2013-06-10 10:39:48 -0700 | [diff] [blame] | 5836 | * @attr ref android.R.styleable#ViewGroup_layoutMode |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 5837 | */ |
| 5838 | public void setLayoutMode(int layoutMode) { |
| 5839 | if (mLayoutMode != layoutMode) { |
Philip Milne | f091b66 | 2013-02-27 11:15:21 -0800 | [diff] [blame] | 5840 | invalidateInheritedLayoutMode(layoutMode); |
| 5841 | setLayoutMode(layoutMode, layoutMode != LAYOUT_MODE_UNDEFINED); |
Philip Milne | 1557fd7 | 2012-04-04 23:41:34 -0700 | [diff] [blame] | 5842 | requestLayout(); |
| 5843 | } |
| 5844 | } |
| 5845 | |
| 5846 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5847 | * Returns a new set of layout parameters based on the supplied attributes set. |
| 5848 | * |
| 5849 | * @param attrs the attributes to build the layout parameters from |
| 5850 | * |
| 5851 | * @return an instance of {@link android.view.ViewGroup.LayoutParams} or one |
| 5852 | * of its descendants |
| 5853 | */ |
| 5854 | public LayoutParams generateLayoutParams(AttributeSet attrs) { |
| 5855 | return new LayoutParams(getContext(), attrs); |
| 5856 | } |
| 5857 | |
| 5858 | /** |
| 5859 | * Returns a safe set of layout parameters based on the supplied layout params. |
| 5860 | * When a ViewGroup is passed a View whose layout params do not pass the test of |
| 5861 | * {@link #checkLayoutParams(android.view.ViewGroup.LayoutParams)}, this method |
| 5862 | * is invoked. This method should return a new set of layout params suitable for |
| 5863 | * this ViewGroup, possibly by copying the appropriate attributes from the |
| 5864 | * specified set of layout params. |
| 5865 | * |
| 5866 | * @param p The layout parameters to convert into a suitable set of layout parameters |
| 5867 | * for this ViewGroup. |
| 5868 | * |
| 5869 | * @return an instance of {@link android.view.ViewGroup.LayoutParams} or one |
| 5870 | * of its descendants |
| 5871 | */ |
| 5872 | protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) { |
Chet Haase | 4610eef | 2015-12-03 07:38:11 -0800 | [diff] [blame] | 5873 | return p; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5874 | } |
| 5875 | |
| 5876 | /** |
| 5877 | * Returns a set of default layout parameters. These parameters are requested |
| 5878 | * when the View passed to {@link #addView(View)} has no layout parameters |
| 5879 | * already set. If null is returned, an exception is thrown from addView. |
| 5880 | * |
| 5881 | * @return a set of default layout parameters or null |
| 5882 | */ |
| 5883 | protected LayoutParams generateDefaultLayoutParams() { |
| 5884 | return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); |
| 5885 | } |
| 5886 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5887 | @Override |
| 5888 | protected void debug(int depth) { |
| 5889 | super.debug(depth); |
| 5890 | String output; |
| 5891 | |
| 5892 | if (mFocused != null) { |
| 5893 | output = debugIndent(depth); |
| 5894 | output += "mFocused"; |
| 5895 | Log.d(VIEW_LOG_TAG, output); |
| 5896 | } |
| 5897 | if (mChildrenCount != 0) { |
| 5898 | output = debugIndent(depth); |
| 5899 | output += "{"; |
| 5900 | Log.d(VIEW_LOG_TAG, output); |
| 5901 | } |
| 5902 | int count = mChildrenCount; |
| 5903 | for (int i = 0; i < count; i++) { |
| 5904 | View child = mChildren[i]; |
| 5905 | child.debug(depth + 1); |
| 5906 | } |
| 5907 | |
| 5908 | if (mChildrenCount != 0) { |
| 5909 | output = debugIndent(depth); |
| 5910 | output += "}"; |
| 5911 | Log.d(VIEW_LOG_TAG, output); |
| 5912 | } |
| 5913 | } |
| 5914 | |
| 5915 | /** |
| 5916 | * Returns the position in the group of the specified child view. |
| 5917 | * |
| 5918 | * @param child the view for which to get the position |
| 5919 | * @return a positive integer representing the position of the view in the |
| 5920 | * group, or -1 if the view does not exist in the group |
| 5921 | */ |
| 5922 | public int indexOfChild(View child) { |
| 5923 | final int count = mChildrenCount; |
| 5924 | final View[] children = mChildren; |
| 5925 | for (int i = 0; i < count; i++) { |
| 5926 | if (children[i] == child) { |
| 5927 | return i; |
| 5928 | } |
| 5929 | } |
| 5930 | return -1; |
| 5931 | } |
| 5932 | |
| 5933 | /** |
| 5934 | * Returns the number of children in the group. |
| 5935 | * |
| 5936 | * @return a positive integer representing the number of children in |
| 5937 | * the group |
| 5938 | */ |
| 5939 | public int getChildCount() { |
| 5940 | return mChildrenCount; |
| 5941 | } |
| 5942 | |
| 5943 | /** |
| 5944 | * Returns the view at the specified position in the group. |
| 5945 | * |
| 5946 | * @param index the position at which to get the view from |
| 5947 | * @return the view at the specified position or null if the position |
| 5948 | * does not exist within the group |
| 5949 | */ |
| 5950 | public View getChildAt(int index) { |
Adam Powell | 3ba8f5d6 | 2011-03-07 15:36:33 -0800 | [diff] [blame] | 5951 | if (index < 0 || index >= mChildrenCount) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5952 | return null; |
| 5953 | } |
Adam Powell | 3ba8f5d6 | 2011-03-07 15:36:33 -0800 | [diff] [blame] | 5954 | return mChildren[index]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5955 | } |
| 5956 | |
| 5957 | /** |
| 5958 | * Ask all of the children of this view to measure themselves, taking into |
| 5959 | * account both the MeasureSpec requirements for this view and its padding. |
| 5960 | * We skip children that are in the GONE state The heavy lifting is done in |
| 5961 | * getChildMeasureSpec. |
| 5962 | * |
| 5963 | * @param widthMeasureSpec The width requirements for this view |
| 5964 | * @param heightMeasureSpec The height requirements for this view |
| 5965 | */ |
| 5966 | protected void measureChildren(int widthMeasureSpec, int heightMeasureSpec) { |
| 5967 | final int size = mChildrenCount; |
| 5968 | final View[] children = mChildren; |
| 5969 | for (int i = 0; i < size; ++i) { |
| 5970 | final View child = children[i]; |
| 5971 | if ((child.mViewFlags & VISIBILITY_MASK) != GONE) { |
| 5972 | measureChild(child, widthMeasureSpec, heightMeasureSpec); |
| 5973 | } |
| 5974 | } |
| 5975 | } |
| 5976 | |
| 5977 | /** |
| 5978 | * Ask one of the children of this view to measure itself, taking into |
| 5979 | * account both the MeasureSpec requirements for this view and its padding. |
| 5980 | * The heavy lifting is done in getChildMeasureSpec. |
| 5981 | * |
| 5982 | * @param child The child to measure |
| 5983 | * @param parentWidthMeasureSpec The width requirements for this view |
| 5984 | * @param parentHeightMeasureSpec The height requirements for this view |
| 5985 | */ |
| 5986 | protected void measureChild(View child, int parentWidthMeasureSpec, |
| 5987 | int parentHeightMeasureSpec) { |
| 5988 | final LayoutParams lp = child.getLayoutParams(); |
| 5989 | |
| 5990 | final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec, |
| 5991 | mPaddingLeft + mPaddingRight, lp.width); |
| 5992 | final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec, |
| 5993 | mPaddingTop + mPaddingBottom, lp.height); |
| 5994 | |
| 5995 | child.measure(childWidthMeasureSpec, childHeightMeasureSpec); |
| 5996 | } |
| 5997 | |
| 5998 | /** |
| 5999 | * Ask one of the children of this view to measure itself, taking into |
| 6000 | * account both the MeasureSpec requirements for this view and its padding |
| 6001 | * and margins. The child must have MarginLayoutParams The heavy lifting is |
| 6002 | * done in getChildMeasureSpec. |
| 6003 | * |
| 6004 | * @param child The child to measure |
| 6005 | * @param parentWidthMeasureSpec The width requirements for this view |
| 6006 | * @param widthUsed Extra space that has been used up by the parent |
| 6007 | * horizontally (possibly by other children of the parent) |
| 6008 | * @param parentHeightMeasureSpec The height requirements for this view |
| 6009 | * @param heightUsed Extra space that has been used up by the parent |
| 6010 | * vertically (possibly by other children of the parent) |
| 6011 | */ |
| 6012 | protected void measureChildWithMargins(View child, |
| 6013 | int parentWidthMeasureSpec, int widthUsed, |
| 6014 | int parentHeightMeasureSpec, int heightUsed) { |
| 6015 | final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams(); |
| 6016 | |
| 6017 | final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec, |
| 6018 | mPaddingLeft + mPaddingRight + lp.leftMargin + lp.rightMargin |
| 6019 | + widthUsed, lp.width); |
| 6020 | final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec, |
| 6021 | mPaddingTop + mPaddingBottom + lp.topMargin + lp.bottomMargin |
| 6022 | + heightUsed, lp.height); |
| 6023 | |
| 6024 | child.measure(childWidthMeasureSpec, childHeightMeasureSpec); |
| 6025 | } |
| 6026 | |
| 6027 | /** |
| 6028 | * Does the hard part of measureChildren: figuring out the MeasureSpec to |
| 6029 | * pass to a particular child. This method figures out the right MeasureSpec |
| 6030 | * for one dimension (height or width) of one child view. |
| 6031 | * |
| 6032 | * The goal is to combine information from our MeasureSpec with the |
| 6033 | * LayoutParams of the child to get the best possible results. For example, |
| 6034 | * if the this view knows its size (because its MeasureSpec has a mode of |
| 6035 | * EXACTLY), and the child has indicated in its LayoutParams that it wants |
| 6036 | * to be the same size as the parent, the parent should ask the child to |
| 6037 | * layout given an exact size. |
| 6038 | * |
| 6039 | * @param spec The requirements for this view |
| 6040 | * @param padding The padding of this view for the current dimension and |
| 6041 | * margins, if applicable |
| 6042 | * @param childDimension How big the child wants to be in the current |
| 6043 | * dimension |
| 6044 | * @return a MeasureSpec integer for the child |
| 6045 | */ |
| 6046 | public static int getChildMeasureSpec(int spec, int padding, int childDimension) { |
| 6047 | int specMode = MeasureSpec.getMode(spec); |
| 6048 | int specSize = MeasureSpec.getSize(spec); |
| 6049 | |
| 6050 | int size = Math.max(0, specSize - padding); |
| 6051 | |
| 6052 | int resultSize = 0; |
| 6053 | int resultMode = 0; |
| 6054 | |
| 6055 | switch (specMode) { |
| 6056 | // Parent has imposed an exact size on us |
| 6057 | case MeasureSpec.EXACTLY: |
| 6058 | if (childDimension >= 0) { |
| 6059 | resultSize = childDimension; |
| 6060 | resultMode = MeasureSpec.EXACTLY; |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 6061 | } else if (childDimension == LayoutParams.MATCH_PARENT) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6062 | // Child wants to be our size. So be it. |
| 6063 | resultSize = size; |
| 6064 | resultMode = MeasureSpec.EXACTLY; |
| 6065 | } else if (childDimension == LayoutParams.WRAP_CONTENT) { |
| 6066 | // Child wants to determine its own size. It can't be |
| 6067 | // bigger than us. |
| 6068 | resultSize = size; |
| 6069 | resultMode = MeasureSpec.AT_MOST; |
| 6070 | } |
| 6071 | break; |
| 6072 | |
| 6073 | // Parent has imposed a maximum size on us |
| 6074 | case MeasureSpec.AT_MOST: |
| 6075 | if (childDimension >= 0) { |
| 6076 | // Child wants a specific size... so be it |
| 6077 | resultSize = childDimension; |
| 6078 | resultMode = MeasureSpec.EXACTLY; |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 6079 | } else if (childDimension == LayoutParams.MATCH_PARENT) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6080 | // Child wants to be our size, but our size is not fixed. |
| 6081 | // Constrain child to not be bigger than us. |
| 6082 | resultSize = size; |
| 6083 | resultMode = MeasureSpec.AT_MOST; |
| 6084 | } else if (childDimension == LayoutParams.WRAP_CONTENT) { |
| 6085 | // Child wants to determine its own size. It can't be |
| 6086 | // bigger than us. |
| 6087 | resultSize = size; |
| 6088 | resultMode = MeasureSpec.AT_MOST; |
| 6089 | } |
| 6090 | break; |
| 6091 | |
| 6092 | // Parent asked to see how big we want to be |
| 6093 | case MeasureSpec.UNSPECIFIED: |
| 6094 | if (childDimension >= 0) { |
| 6095 | // Child wants a specific size... let him have it |
| 6096 | resultSize = childDimension; |
| 6097 | resultMode = MeasureSpec.EXACTLY; |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 6098 | } else if (childDimension == LayoutParams.MATCH_PARENT) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6099 | // Child wants to be our size... find out how big it should |
| 6100 | // be |
Adam Powell | d5dbf4b | 2015-06-11 13:19:24 -0700 | [diff] [blame] | 6101 | resultSize = View.sUseZeroUnspecifiedMeasureSpec ? 0 : size; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6102 | resultMode = MeasureSpec.UNSPECIFIED; |
| 6103 | } else if (childDimension == LayoutParams.WRAP_CONTENT) { |
| 6104 | // Child wants to determine its own size.... find out how |
| 6105 | // big it should be |
Adam Powell | d5dbf4b | 2015-06-11 13:19:24 -0700 | [diff] [blame] | 6106 | resultSize = View.sUseZeroUnspecifiedMeasureSpec ? 0 : size; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6107 | resultMode = MeasureSpec.UNSPECIFIED; |
| 6108 | } |
| 6109 | break; |
| 6110 | } |
Tor Norbye | 6756864 | 2015-03-31 07:47:23 -0700 | [diff] [blame] | 6111 | //noinspection ResourceType |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6112 | return MeasureSpec.makeMeasureSpec(resultSize, resultMode); |
| 6113 | } |
| 6114 | |
| 6115 | |
| 6116 | /** |
| 6117 | * Removes any pending animations for views that have been removed. Call |
| 6118 | * this if you don't want animations for exiting views to stack up. |
| 6119 | */ |
| 6120 | public void clearDisappearingChildren() { |
John Reck | ca7a9da | 2014-03-05 16:29:07 -0800 | [diff] [blame] | 6121 | final ArrayList<View> disappearingChildren = mDisappearingChildren; |
| 6122 | if (disappearingChildren != null) { |
| 6123 | final int count = disappearingChildren.size(); |
| 6124 | for (int i = 0; i < count; i++) { |
| 6125 | final View view = disappearingChildren.get(i); |
| 6126 | if (view.mAttachInfo != null) { |
| 6127 | view.dispatchDetachedFromWindow(); |
| 6128 | } |
| 6129 | view.clearAnimation(); |
| 6130 | } |
| 6131 | disappearingChildren.clear(); |
Chet Haase | b85967b | 2012-03-26 14:37:51 -0700 | [diff] [blame] | 6132 | invalidate(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6133 | } |
| 6134 | } |
| 6135 | |
| 6136 | /** |
| 6137 | * Add a view which is removed from mChildren but still needs animation |
| 6138 | * |
| 6139 | * @param v View to add |
| 6140 | */ |
| 6141 | private void addDisappearingView(View v) { |
| 6142 | ArrayList<View> disappearingChildren = mDisappearingChildren; |
| 6143 | |
| 6144 | if (disappearingChildren == null) { |
| 6145 | disappearingChildren = mDisappearingChildren = new ArrayList<View>(); |
| 6146 | } |
| 6147 | |
| 6148 | disappearingChildren.add(v); |
| 6149 | } |
| 6150 | |
| 6151 | /** |
| 6152 | * Cleanup a view when its animation is done. This may mean removing it from |
| 6153 | * the list of disappearing views. |
| 6154 | * |
| 6155 | * @param view The view whose animation has finished |
| 6156 | * @param animation The animation, cannot be null |
| 6157 | */ |
Chet Haase | 64a48c1 | 2012-02-13 16:33:29 -0800 | [diff] [blame] | 6158 | void finishAnimatingView(final View view, Animation animation) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6159 | final ArrayList<View> disappearingChildren = mDisappearingChildren; |
| 6160 | if (disappearingChildren != null) { |
| 6161 | if (disappearingChildren.contains(view)) { |
| 6162 | disappearingChildren.remove(view); |
| 6163 | |
| 6164 | if (view.mAttachInfo != null) { |
| 6165 | view.dispatchDetachedFromWindow(); |
| 6166 | } |
| 6167 | |
| 6168 | view.clearAnimation(); |
| 6169 | mGroupFlags |= FLAG_INVALIDATE_REQUIRED; |
| 6170 | } |
| 6171 | } |
| 6172 | |
| 6173 | if (animation != null && !animation.getFillAfter()) { |
| 6174 | view.clearAnimation(); |
| 6175 | } |
| 6176 | |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 6177 | if ((view.mPrivateFlags & PFLAG_ANIMATION_STARTED) == PFLAG_ANIMATION_STARTED) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6178 | view.onAnimationEnd(); |
| 6179 | // Should be performed by onAnimationEnd() but this avoid an infinite loop, |
| 6180 | // so we'd rather be safe than sorry |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 6181 | view.mPrivateFlags &= ~PFLAG_ANIMATION_STARTED; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6182 | // Draw one more frame after the animation is done |
| 6183 | mGroupFlags |= FLAG_INVALIDATE_REQUIRED; |
| 6184 | } |
| 6185 | } |
| 6186 | |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 6187 | /** |
Chet Haase | aceafe6 | 2011-08-26 15:44:33 -0700 | [diff] [blame] | 6188 | * Utility function called by View during invalidation to determine whether a view that |
| 6189 | * is invisible or gone should still be invalidated because it is being transitioned (and |
| 6190 | * therefore still needs to be drawn). |
| 6191 | */ |
| 6192 | boolean isViewTransitioning(View view) { |
| 6193 | return (mTransitioningViews != null && mTransitioningViews.contains(view)); |
| 6194 | } |
| 6195 | |
| 6196 | /** |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 6197 | * This method tells the ViewGroup that the given View object, which should have this |
| 6198 | * ViewGroup as its parent, |
| 6199 | * should be kept around (re-displayed when the ViewGroup draws its children) even if it |
| 6200 | * is removed from its parent. This allows animations, such as those used by |
| 6201 | * {@link android.app.Fragment} and {@link android.animation.LayoutTransition} to animate |
| 6202 | * the removal of views. A call to this method should always be accompanied by a later call |
| 6203 | * to {@link #endViewTransition(View)}, such as after an animation on the View has finished, |
| 6204 | * so that the View finally gets removed. |
| 6205 | * |
| 6206 | * @param view The View object to be kept visible even if it gets removed from its parent. |
| 6207 | */ |
| 6208 | public void startViewTransition(View view) { |
| 6209 | if (view.mParent == this) { |
| 6210 | if (mTransitioningViews == null) { |
| 6211 | mTransitioningViews = new ArrayList<View>(); |
| 6212 | } |
| 6213 | mTransitioningViews.add(view); |
| 6214 | } |
| 6215 | } |
| 6216 | |
| 6217 | /** |
| 6218 | * This method should always be called following an earlier call to |
| 6219 | * {@link #startViewTransition(View)}. The given View is finally removed from its parent |
| 6220 | * and will no longer be displayed. Note that this method does not perform the functionality |
| 6221 | * of removing a view from its parent; it just discontinues the display of a View that |
| 6222 | * has previously been removed. |
| 6223 | * |
| 6224 | * @return view The View object that has been removed but is being kept around in the visible |
| 6225 | * hierarchy by an earlier call to {@link #startViewTransition(View)}. |
| 6226 | */ |
| 6227 | public void endViewTransition(View view) { |
| 6228 | if (mTransitioningViews != null) { |
| 6229 | mTransitioningViews.remove(view); |
| 6230 | final ArrayList<View> disappearingChildren = mDisappearingChildren; |
| 6231 | if (disappearingChildren != null && disappearingChildren.contains(view)) { |
| 6232 | disappearingChildren.remove(view); |
Chet Haase | 5e25c2c | 2010-09-16 11:15:56 -0700 | [diff] [blame] | 6233 | if (mVisibilityChangingChildren != null && |
| 6234 | mVisibilityChangingChildren.contains(view)) { |
| 6235 | mVisibilityChangingChildren.remove(view); |
| 6236 | } else { |
| 6237 | if (view.mAttachInfo != null) { |
| 6238 | view.dispatchDetachedFromWindow(); |
| 6239 | } |
| 6240 | if (view.mParent != null) { |
| 6241 | view.mParent = null; |
| 6242 | } |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 6243 | } |
Chet Haase | b85967b | 2012-03-26 14:37:51 -0700 | [diff] [blame] | 6244 | invalidate(); |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 6245 | } |
| 6246 | } |
| 6247 | } |
| 6248 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 6249 | private LayoutTransition.TransitionListener mLayoutTransitionListener = |
| 6250 | new LayoutTransition.TransitionListener() { |
| 6251 | @Override |
| 6252 | public void startTransition(LayoutTransition transition, ViewGroup container, |
| 6253 | View view, int transitionType) { |
| 6254 | // We only care about disappearing items, since we need special logic to keep |
| 6255 | // those items visible after they've been 'removed' |
| 6256 | if (transitionType == LayoutTransition.DISAPPEARING) { |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 6257 | startViewTransition(view); |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 6258 | } |
| 6259 | } |
| 6260 | |
| 6261 | @Override |
| 6262 | public void endTransition(LayoutTransition transition, ViewGroup container, |
| 6263 | View view, int transitionType) { |
Chet Haase | b989502 | 2013-04-02 15:10:58 -0700 | [diff] [blame] | 6264 | if (mLayoutCalledWhileSuppressed && !transition.isChangingLayout()) { |
Chet Haase | 9c08744 | 2011-01-12 16:20:16 -0800 | [diff] [blame] | 6265 | requestLayout(); |
Chet Haase | b989502 | 2013-04-02 15:10:58 -0700 | [diff] [blame] | 6266 | mLayoutCalledWhileSuppressed = false; |
Chet Haase | 9c08744 | 2011-01-12 16:20:16 -0800 | [diff] [blame] | 6267 | } |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 6268 | if (transitionType == LayoutTransition.DISAPPEARING && mTransitioningViews != null) { |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 6269 | endViewTransition(view); |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 6270 | } |
| 6271 | } |
| 6272 | }; |
| 6273 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6274 | /** |
Chet Haase | b989502 | 2013-04-02 15:10:58 -0700 | [diff] [blame] | 6275 | * Tells this ViewGroup to suppress all layout() calls until layout |
| 6276 | * suppression is disabled with a later call to suppressLayout(false). |
| 6277 | * When layout suppression is disabled, a requestLayout() call is sent |
| 6278 | * if layout() was attempted while layout was being suppressed. |
| 6279 | * |
| 6280 | * @hide |
| 6281 | */ |
| 6282 | public void suppressLayout(boolean suppress) { |
| 6283 | mSuppressLayout = suppress; |
| 6284 | if (!suppress) { |
| 6285 | if (mLayoutCalledWhileSuppressed) { |
| 6286 | requestLayout(); |
| 6287 | mLayoutCalledWhileSuppressed = false; |
| 6288 | } |
| 6289 | } |
| 6290 | } |
| 6291 | |
| 6292 | /** |
Chet Haase | 199acdf | 2013-07-24 18:40:55 -0700 | [diff] [blame] | 6293 | * Returns whether layout calls on this container are currently being |
| 6294 | * suppressed, due to an earlier call to {@link #suppressLayout(boolean)}. |
| 6295 | * |
| 6296 | * @return true if layout calls are currently suppressed, false otherwise. |
| 6297 | * |
| 6298 | * @hide |
| 6299 | */ |
| 6300 | public boolean isLayoutSuppressed() { |
| 6301 | return mSuppressLayout; |
| 6302 | } |
| 6303 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6304 | @Override |
| 6305 | public boolean gatherTransparentRegion(Region region) { |
| 6306 | // If no transparent regions requested, we are always opaque. |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 6307 | 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] | 6308 | if (meOpaque && region == null) { |
| 6309 | // The caller doesn't care about the region, so stop now. |
| 6310 | return true; |
| 6311 | } |
| 6312 | super.gatherTransparentRegion(region); |
| 6313 | final View[] children = mChildren; |
| 6314 | final int count = mChildrenCount; |
| 6315 | boolean noneOfTheChildrenAreTransparent = true; |
| 6316 | for (int i = 0; i < count; i++) { |
| 6317 | final View child = children[i]; |
Mathias Agopian | e338115 | 2010-12-02 15:19:36 -0800 | [diff] [blame] | 6318 | if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6319 | if (!child.gatherTransparentRegion(region)) { |
| 6320 | noneOfTheChildrenAreTransparent = false; |
| 6321 | } |
| 6322 | } |
| 6323 | } |
| 6324 | return meOpaque || noneOfTheChildrenAreTransparent; |
| 6325 | } |
| 6326 | |
Alan Viverette | be463f2 | 2016-01-21 10:50:10 -0500 | [diff] [blame] | 6327 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6328 | public void requestTransparentRegion(View child) { |
| 6329 | if (child != null) { |
Dianne Hackborn | 4702a85 | 2012-08-17 15:18:29 -0700 | [diff] [blame] | 6330 | child.mPrivateFlags |= View.PFLAG_REQUEST_TRANSPARENT_REGIONS; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6331 | if (mParent != null) { |
| 6332 | mParent.requestTransparentRegion(this); |
| 6333 | } |
| 6334 | } |
| 6335 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 6336 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6337 | @Override |
Adam Powell | 46e38fd | 2014-02-03 10:16:49 -0800 | [diff] [blame] | 6338 | public WindowInsets dispatchApplyWindowInsets(WindowInsets insets) { |
| 6339 | insets = super.dispatchApplyWindowInsets(insets); |
Adam Powell | 0d9fdba | 2014-06-11 15:33:08 -0700 | [diff] [blame] | 6340 | if (!insets.isConsumed()) { |
Adam Powell | 46e38fd | 2014-02-03 10:16:49 -0800 | [diff] [blame] | 6341 | final int count = getChildCount(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6342 | for (int i = 0; i < count; i++) { |
Adam Powell | 46e38fd | 2014-02-03 10:16:49 -0800 | [diff] [blame] | 6343 | insets = getChildAt(i).dispatchApplyWindowInsets(insets); |
Adam Powell | 0d9fdba | 2014-06-11 15:33:08 -0700 | [diff] [blame] | 6344 | if (insets.isConsumed()) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6345 | break; |
| 6346 | } |
| 6347 | } |
| 6348 | } |
Adam Powell | 46e38fd | 2014-02-03 10:16:49 -0800 | [diff] [blame] | 6349 | return insets; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6350 | } |
| 6351 | |
| 6352 | /** |
| 6353 | * Returns the animation listener to which layout animation events are |
| 6354 | * sent. |
| 6355 | * |
| 6356 | * @return an {@link android.view.animation.Animation.AnimationListener} |
| 6357 | */ |
| 6358 | public Animation.AnimationListener getLayoutAnimationListener() { |
| 6359 | return mAnimationListener; |
| 6360 | } |
| 6361 | |
| 6362 | @Override |
| 6363 | protected void drawableStateChanged() { |
| 6364 | super.drawableStateChanged(); |
| 6365 | |
| 6366 | if ((mGroupFlags & FLAG_NOTIFY_CHILDREN_ON_DRAWABLE_STATE_CHANGE) != 0) { |
| 6367 | if ((mGroupFlags & FLAG_ADD_STATES_FROM_CHILDREN) != 0) { |
| 6368 | throw new IllegalStateException("addStateFromChildren cannot be enabled if a" |
| 6369 | + " child has duplicateParentState set to true"); |
| 6370 | } |
| 6371 | |
| 6372 | final View[] children = mChildren; |
| 6373 | final int count = mChildrenCount; |
| 6374 | |
| 6375 | for (int i = 0; i < count; i++) { |
| 6376 | final View child = children[i]; |
| 6377 | if ((child.mViewFlags & DUPLICATE_PARENT_STATE) != 0) { |
| 6378 | child.refreshDrawableState(); |
| 6379 | } |
| 6380 | } |
| 6381 | } |
| 6382 | } |
| 6383 | |
| 6384 | @Override |
Dianne Hackborn | e213677 | 2010-11-04 15:08:59 -0700 | [diff] [blame] | 6385 | public void jumpDrawablesToCurrentState() { |
| 6386 | super.jumpDrawablesToCurrentState(); |
| 6387 | final View[] children = mChildren; |
| 6388 | final int count = mChildrenCount; |
| 6389 | for (int i = 0; i < count; i++) { |
| 6390 | children[i].jumpDrawablesToCurrentState(); |
| 6391 | } |
| 6392 | } |
| 6393 | |
| 6394 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6395 | protected int[] onCreateDrawableState(int extraSpace) { |
| 6396 | if ((mGroupFlags & FLAG_ADD_STATES_FROM_CHILDREN) == 0) { |
| 6397 | return super.onCreateDrawableState(extraSpace); |
| 6398 | } |
| 6399 | |
| 6400 | int need = 0; |
| 6401 | int n = getChildCount(); |
| 6402 | for (int i = 0; i < n; i++) { |
| 6403 | int[] childState = getChildAt(i).getDrawableState(); |
| 6404 | |
| 6405 | if (childState != null) { |
| 6406 | need += childState.length; |
| 6407 | } |
| 6408 | } |
| 6409 | |
| 6410 | int[] state = super.onCreateDrawableState(extraSpace + need); |
| 6411 | |
| 6412 | for (int i = 0; i < n; i++) { |
| 6413 | int[] childState = getChildAt(i).getDrawableState(); |
| 6414 | |
| 6415 | if (childState != null) { |
| 6416 | state = mergeDrawableStates(state, childState); |
| 6417 | } |
| 6418 | } |
| 6419 | |
| 6420 | return state; |
| 6421 | } |
| 6422 | |
| 6423 | /** |
| 6424 | * Sets whether this ViewGroup's drawable states also include |
| 6425 | * its children's drawable states. This is used, for example, to |
| 6426 | * make a group appear to be focused when its child EditText or button |
| 6427 | * is focused. |
| 6428 | */ |
| 6429 | public void setAddStatesFromChildren(boolean addsStates) { |
| 6430 | if (addsStates) { |
| 6431 | mGroupFlags |= FLAG_ADD_STATES_FROM_CHILDREN; |
| 6432 | } else { |
| 6433 | mGroupFlags &= ~FLAG_ADD_STATES_FROM_CHILDREN; |
| 6434 | } |
| 6435 | |
| 6436 | refreshDrawableState(); |
| 6437 | } |
| 6438 | |
| 6439 | /** |
| 6440 | * Returns whether this ViewGroup's drawable states also include |
| 6441 | * its children's drawable states. This is used, for example, to |
| 6442 | * make a group appear to be focused when its child EditText or button |
| 6443 | * is focused. |
| 6444 | */ |
| 6445 | public boolean addStatesFromChildren() { |
| 6446 | return (mGroupFlags & FLAG_ADD_STATES_FROM_CHILDREN) != 0; |
| 6447 | } |
| 6448 | |
| 6449 | /** |
Jeff Smith | a45746e | 2012-07-19 14:19:24 -0500 | [diff] [blame] | 6450 | * If {@link #addStatesFromChildren} is true, refreshes this group's |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6451 | * drawable state (to include the states from its children). |
| 6452 | */ |
Alan Viverette | be463f2 | 2016-01-21 10:50:10 -0500 | [diff] [blame] | 6453 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6454 | public void childDrawableStateChanged(View child) { |
| 6455 | if ((mGroupFlags & FLAG_ADD_STATES_FROM_CHILDREN) != 0) { |
| 6456 | refreshDrawableState(); |
| 6457 | } |
| 6458 | } |
| 6459 | |
| 6460 | /** |
| 6461 | * Specifies the animation listener to which layout animation events must |
| 6462 | * be sent. Only |
| 6463 | * {@link android.view.animation.Animation.AnimationListener#onAnimationStart(Animation)} |
| 6464 | * and |
| 6465 | * {@link android.view.animation.Animation.AnimationListener#onAnimationEnd(Animation)} |
| 6466 | * are invoked. |
| 6467 | * |
| 6468 | * @param animationListener the layout animation listener |
| 6469 | */ |
| 6470 | public void setLayoutAnimationListener(Animation.AnimationListener animationListener) { |
| 6471 | mAnimationListener = animationListener; |
| 6472 | } |
| 6473 | |
| 6474 | /** |
Chet Haase | cca2c98 | 2011-05-20 14:34:18 -0700 | [diff] [blame] | 6475 | * This method is called by LayoutTransition when there are 'changing' animations that need |
| 6476 | * to start after the layout/setup phase. The request is forwarded to the ViewAncestor, who |
| 6477 | * starts all pending transitions prior to the drawing phase in the current traversal. |
| 6478 | * |
| 6479 | * @param transition The LayoutTransition to be started on the next traversal. |
| 6480 | * |
| 6481 | * @hide |
| 6482 | */ |
| 6483 | public void requestTransitionStart(LayoutTransition transition) { |
Dianne Hackborn | 6dd005b | 2011-07-18 13:22:50 -0700 | [diff] [blame] | 6484 | ViewRootImpl viewAncestor = getViewRootImpl(); |
Chet Haase | 1abf7fa | 2011-08-17 18:31:56 -0700 | [diff] [blame] | 6485 | if (viewAncestor != null) { |
| 6486 | viewAncestor.requestTransitionStart(transition); |
| 6487 | } |
Chet Haase | cca2c98 | 2011-05-20 14:34:18 -0700 | [diff] [blame] | 6488 | } |
| 6489 | |
Fabrice Di Meglio | 4457e85 | 2012-09-18 19:23:12 -0700 | [diff] [blame] | 6490 | /** |
| 6491 | * @hide |
| 6492 | */ |
Fabrice Di Meglio | 80dc53d | 2011-06-21 18:36:33 -0700 | [diff] [blame] | 6493 | @Override |
Fabrice Di Meglio | 09ecb25 | 2013-05-03 16:51:55 -0700 | [diff] [blame] | 6494 | public boolean resolveRtlPropertiesIfNeeded() { |
| 6495 | final boolean result = super.resolveRtlPropertiesIfNeeded(); |
| 6496 | // We dont need to resolve the children RTL properties if nothing has changed for the parent |
| 6497 | if (result) { |
| 6498 | int count = getChildCount(); |
| 6499 | for (int i = 0; i < count; i++) { |
| 6500 | final View child = getChildAt(i); |
| 6501 | if (child.isLayoutDirectionInherited()) { |
| 6502 | child.resolveRtlPropertiesIfNeeded(); |
| 6503 | } |
Fabrice Di Meglio | 84ebb35 | 2012-10-11 16:27:37 -0700 | [diff] [blame] | 6504 | } |
| 6505 | } |
Fabrice Di Meglio | 09ecb25 | 2013-05-03 16:51:55 -0700 | [diff] [blame] | 6506 | return result; |
Fabrice Di Meglio | 84ebb35 | 2012-10-11 16:27:37 -0700 | [diff] [blame] | 6507 | } |
| 6508 | |
| 6509 | /** |
| 6510 | * @hide |
| 6511 | */ |
| 6512 | @Override |
Fabrice Di Meglio | 9a04856 | 2012-09-26 14:55:56 -0700 | [diff] [blame] | 6513 | public boolean resolveLayoutDirection() { |
| 6514 | final boolean result = super.resolveLayoutDirection(); |
| 6515 | if (result) { |
| 6516 | int count = getChildCount(); |
| 6517 | for (int i = 0; i < count; i++) { |
| 6518 | final View child = getChildAt(i); |
| 6519 | if (child.isLayoutDirectionInherited()) { |
| 6520 | child.resolveLayoutDirection(); |
| 6521 | } |
Fabrice Di Meglio | 1f88ba8 | 2012-09-24 14:56:49 -0700 | [diff] [blame] | 6522 | } |
| 6523 | } |
Fabrice Di Meglio | 9a04856 | 2012-09-26 14:55:56 -0700 | [diff] [blame] | 6524 | return result; |
Fabrice Di Meglio | 1f88ba8 | 2012-09-24 14:56:49 -0700 | [diff] [blame] | 6525 | } |
| 6526 | |
| 6527 | /** |
| 6528 | * @hide |
| 6529 | */ |
| 6530 | @Override |
Fabrice Di Meglio | 9a04856 | 2012-09-26 14:55:56 -0700 | [diff] [blame] | 6531 | public boolean resolveTextDirection() { |
| 6532 | final boolean result = super.resolveTextDirection(); |
| 6533 | if (result) { |
| 6534 | int count = getChildCount(); |
| 6535 | for (int i = 0; i < count; i++) { |
| 6536 | final View child = getChildAt(i); |
| 6537 | if (child.isTextDirectionInherited()) { |
| 6538 | child.resolveTextDirection(); |
| 6539 | } |
Fabrice Di Meglio | 1f88ba8 | 2012-09-24 14:56:49 -0700 | [diff] [blame] | 6540 | } |
| 6541 | } |
Fabrice Di Meglio | 9a04856 | 2012-09-26 14:55:56 -0700 | [diff] [blame] | 6542 | return result; |
Fabrice Di Meglio | 1f88ba8 | 2012-09-24 14:56:49 -0700 | [diff] [blame] | 6543 | } |
| 6544 | |
| 6545 | /** |
| 6546 | * @hide |
| 6547 | */ |
| 6548 | @Override |
Fabrice Di Meglio | 9a04856 | 2012-09-26 14:55:56 -0700 | [diff] [blame] | 6549 | public boolean resolveTextAlignment() { |
| 6550 | final boolean result = super.resolveTextAlignment(); |
| 6551 | if (result) { |
| 6552 | int count = getChildCount(); |
| 6553 | for (int i = 0; i < count; i++) { |
| 6554 | final View child = getChildAt(i); |
| 6555 | if (child.isTextAlignmentInherited()) { |
| 6556 | child.resolveTextAlignment(); |
| 6557 | } |
Fabrice Di Meglio | 1f88ba8 | 2012-09-24 14:56:49 -0700 | [diff] [blame] | 6558 | } |
| 6559 | } |
Fabrice Di Meglio | 9a04856 | 2012-09-26 14:55:56 -0700 | [diff] [blame] | 6560 | return result; |
Fabrice Di Meglio | 1f88ba8 | 2012-09-24 14:56:49 -0700 | [diff] [blame] | 6561 | } |
| 6562 | |
| 6563 | /** |
| 6564 | * @hide |
| 6565 | */ |
| 6566 | @Override |
Fabrice Di Meglio | 84ebb35 | 2012-10-11 16:27:37 -0700 | [diff] [blame] | 6567 | public void resolvePadding() { |
| 6568 | super.resolvePadding(); |
| 6569 | int count = getChildCount(); |
| 6570 | for (int i = 0; i < count; i++) { |
| 6571 | final View child = getChildAt(i); |
Adam Powell | 05f3512 | 2014-11-10 17:47:37 -0800 | [diff] [blame] | 6572 | if (child.isLayoutDirectionInherited() && !child.isPaddingResolved()) { |
Fabrice Di Meglio | 84ebb35 | 2012-10-11 16:27:37 -0700 | [diff] [blame] | 6573 | child.resolvePadding(); |
| 6574 | } |
| 6575 | } |
| 6576 | } |
| 6577 | |
| 6578 | /** |
| 6579 | * @hide |
| 6580 | */ |
| 6581 | @Override |
| 6582 | protected void resolveDrawables() { |
| 6583 | super.resolveDrawables(); |
| 6584 | int count = getChildCount(); |
| 6585 | for (int i = 0; i < count; i++) { |
| 6586 | final View child = getChildAt(i); |
Adam Powell | 05f3512 | 2014-11-10 17:47:37 -0800 | [diff] [blame] | 6587 | if (child.isLayoutDirectionInherited() && !child.areDrawablesResolved()) { |
Fabrice Di Meglio | 84ebb35 | 2012-10-11 16:27:37 -0700 | [diff] [blame] | 6588 | child.resolveDrawables(); |
| 6589 | } |
| 6590 | } |
| 6591 | } |
| 6592 | |
Fabrice Di Meglio | 1e0ed6b | 2012-10-18 16:06:52 -0700 | [diff] [blame] | 6593 | /** |
| 6594 | * @hide |
| 6595 | */ |
Fabrice Di Meglio | fcc3348 | 2012-10-18 11:11:51 -0700 | [diff] [blame] | 6596 | @Override |
| 6597 | public void resolveLayoutParams() { |
| 6598 | super.resolveLayoutParams(); |
| 6599 | int count = getChildCount(); |
| 6600 | for (int i = 0; i < count; i++) { |
| 6601 | final View child = getChildAt(i); |
| 6602 | child.resolveLayoutParams(); |
| 6603 | } |
| 6604 | } |
| 6605 | |
Fabrice Di Meglio | 84ebb35 | 2012-10-11 16:27:37 -0700 | [diff] [blame] | 6606 | /** |
| 6607 | * @hide |
| 6608 | */ |
| 6609 | @Override |
Fabrice Di Meglio | 4457e85 | 2012-09-18 19:23:12 -0700 | [diff] [blame] | 6610 | public void resetResolvedLayoutDirection() { |
| 6611 | super.resetResolvedLayoutDirection(); |
| 6612 | |
Fabrice Di Meglio | 4457e85 | 2012-09-18 19:23:12 -0700 | [diff] [blame] | 6613 | int count = getChildCount(); |
Fabrice Di Meglio | 80dc53d | 2011-06-21 18:36:33 -0700 | [diff] [blame] | 6614 | for (int i = 0; i < count; i++) { |
| 6615 | final View child = getChildAt(i); |
Fabrice Di Meglio | e56ffdc | 2012-09-23 14:51:16 -0700 | [diff] [blame] | 6616 | if (child.isLayoutDirectionInherited()) { |
Fabrice Di Meglio | 7f86c80 | 2011-07-01 15:09:24 -0700 | [diff] [blame] | 6617 | child.resetResolvedLayoutDirection(); |
Fabrice Di Meglio | 80dc53d | 2011-06-21 18:36:33 -0700 | [diff] [blame] | 6618 | } |
Fabrice Di Meglio | 1f88ba8 | 2012-09-24 14:56:49 -0700 | [diff] [blame] | 6619 | } |
| 6620 | } |
| 6621 | |
| 6622 | /** |
| 6623 | * @hide |
| 6624 | */ |
| 6625 | @Override |
| 6626 | public void resetResolvedTextDirection() { |
| 6627 | super.resetResolvedTextDirection(); |
| 6628 | |
| 6629 | int count = getChildCount(); |
| 6630 | for (int i = 0; i < count; i++) { |
| 6631 | final View child = getChildAt(i); |
Fabrice Di Meglio | 97e146c | 2012-09-23 15:45:16 -0700 | [diff] [blame] | 6632 | if (child.isTextDirectionInherited()) { |
Fabrice Di Meglio | 2226886 | 2011-06-27 18:13:18 -0700 | [diff] [blame] | 6633 | child.resetResolvedTextDirection(); |
| 6634 | } |
Fabrice Di Meglio | 1f88ba8 | 2012-09-24 14:56:49 -0700 | [diff] [blame] | 6635 | } |
| 6636 | } |
| 6637 | |
| 6638 | /** |
| 6639 | * @hide |
| 6640 | */ |
| 6641 | @Override |
| 6642 | public void resetResolvedTextAlignment() { |
| 6643 | super.resetResolvedTextAlignment(); |
| 6644 | |
| 6645 | int count = getChildCount(); |
| 6646 | for (int i = 0; i < count; i++) { |
| 6647 | final View child = getChildAt(i); |
Fabrice Di Meglio | 1a7d487 | 2012-09-23 16:19:58 -0700 | [diff] [blame] | 6648 | if (child.isTextAlignmentInherited()) { |
Fabrice Di Meglio | 9da0f8a | 2012-03-13 19:37:57 -0700 | [diff] [blame] | 6649 | child.resetResolvedTextAlignment(); |
| 6650 | } |
| 6651 | } |
| 6652 | } |
| 6653 | |
Fabrice Di Meglio | 2226886 | 2011-06-27 18:13:18 -0700 | [diff] [blame] | 6654 | /** |
Fabrice Di Meglio | 84ebb35 | 2012-10-11 16:27:37 -0700 | [diff] [blame] | 6655 | * @hide |
| 6656 | */ |
| 6657 | @Override |
| 6658 | public void resetResolvedPadding() { |
| 6659 | super.resetResolvedPadding(); |
| 6660 | |
| 6661 | int count = getChildCount(); |
| 6662 | for (int i = 0; i < count; i++) { |
| 6663 | final View child = getChildAt(i); |
| 6664 | if (child.isLayoutDirectionInherited()) { |
| 6665 | child.resetResolvedPadding(); |
| 6666 | } |
| 6667 | } |
| 6668 | } |
| 6669 | |
| 6670 | /** |
| 6671 | * @hide |
| 6672 | */ |
| 6673 | @Override |
| 6674 | protected void resetResolvedDrawables() { |
| 6675 | super.resetResolvedDrawables(); |
| 6676 | |
| 6677 | int count = getChildCount(); |
| 6678 | for (int i = 0; i < count; i++) { |
| 6679 | final View child = getChildAt(i); |
| 6680 | if (child.isLayoutDirectionInherited()) { |
| 6681 | child.resetResolvedDrawables(); |
| 6682 | } |
| 6683 | } |
| 6684 | } |
| 6685 | |
| 6686 | /** |
Patrick Dubroy | e0a799a | 2011-05-04 16:19:22 -0700 | [diff] [blame] | 6687 | * Return true if the pressed state should be delayed for children or descendants of this |
| 6688 | * ViewGroup. Generally, this should be done for containers that can scroll, such as a List. |
| 6689 | * This prevents the pressed state from appearing when the user is actually trying to scroll |
| 6690 | * the content. |
| 6691 | * |
| 6692 | * The default implementation returns true for compatibility reasons. Subclasses that do |
| 6693 | * not scroll should generally override this method and return false. |
| 6694 | */ |
| 6695 | public boolean shouldDelayChildPressedState() { |
| 6696 | return true; |
| 6697 | } |
| 6698 | |
Adam Powell | 10ba277 | 2014-04-15 09:46:51 -0700 | [diff] [blame] | 6699 | /** |
| 6700 | * @inheritDoc |
| 6701 | */ |
| 6702 | @Override |
| 6703 | public boolean onStartNestedScroll(View child, View target, int nestedScrollAxes) { |
| 6704 | return false; |
| 6705 | } |
| 6706 | |
| 6707 | /** |
| 6708 | * @inheritDoc |
| 6709 | */ |
| 6710 | @Override |
| 6711 | public void onNestedScrollAccepted(View child, View target, int axes) { |
| 6712 | mNestedScrollAxes = axes; |
| 6713 | } |
| 6714 | |
| 6715 | /** |
| 6716 | * @inheritDoc |
| 6717 | * |
| 6718 | * <p>The default implementation of onStopNestedScroll calls |
| 6719 | * {@link #stopNestedScroll()} to halt any recursive nested scrolling in progress.</p> |
| 6720 | */ |
| 6721 | @Override |
| 6722 | public void onStopNestedScroll(View child) { |
| 6723 | // Stop any recursive nested scrolling. |
| 6724 | stopNestedScroll(); |
Adam Powell | d4a22d4 | 2015-04-16 15:44:10 -0700 | [diff] [blame] | 6725 | mNestedScrollAxes = 0; |
Adam Powell | 10ba277 | 2014-04-15 09:46:51 -0700 | [diff] [blame] | 6726 | } |
| 6727 | |
| 6728 | /** |
| 6729 | * @inheritDoc |
| 6730 | */ |
| 6731 | @Override |
| 6732 | public void onNestedScroll(View target, int dxConsumed, int dyConsumed, |
| 6733 | int dxUnconsumed, int dyUnconsumed) { |
| 6734 | // Do nothing |
| 6735 | } |
| 6736 | |
| 6737 | /** |
| 6738 | * @inheritDoc |
| 6739 | */ |
| 6740 | @Override |
| 6741 | public void onNestedPreScroll(View target, int dx, int dy, int[] consumed) { |
| 6742 | // Do nothing |
| 6743 | } |
| 6744 | |
| 6745 | /** |
| 6746 | * @inheritDoc |
| 6747 | */ |
| 6748 | @Override |
Adam Powell | b36e4f9 | 2014-05-01 10:23:33 -0700 | [diff] [blame] | 6749 | public boolean onNestedFling(View target, float velocityX, float velocityY, boolean consumed) { |
Adam Powell | 10ba277 | 2014-04-15 09:46:51 -0700 | [diff] [blame] | 6750 | return false; |
| 6751 | } |
| 6752 | |
| 6753 | /** |
Adam Powell | b72be59 | 2014-07-16 21:41:31 -0700 | [diff] [blame] | 6754 | * @inheritDoc |
| 6755 | */ |
| 6756 | @Override |
| 6757 | public boolean onNestedPreFling(View target, float velocityX, float velocityY) { |
| 6758 | return false; |
| 6759 | } |
| 6760 | |
| 6761 | /** |
Adam Powell | 10ba277 | 2014-04-15 09:46:51 -0700 | [diff] [blame] | 6762 | * Return the current axes of nested scrolling for this ViewGroup. |
| 6763 | * |
| 6764 | * <p>A ViewGroup returning something other than {@link #SCROLL_AXIS_NONE} is currently |
| 6765 | * acting as a nested scrolling parent for one or more descendant views in the hierarchy.</p> |
| 6766 | * |
| 6767 | * @return Flags indicating the current axes of nested scrolling |
| 6768 | * @see #SCROLL_AXIS_HORIZONTAL |
| 6769 | * @see #SCROLL_AXIS_VERTICAL |
| 6770 | * @see #SCROLL_AXIS_NONE |
| 6771 | */ |
| 6772 | public int getNestedScrollAxes() { |
| 6773 | return mNestedScrollAxes; |
| 6774 | } |
| 6775 | |
Philip Milne | d7dd890 | 2012-01-26 16:55:30 -0800 | [diff] [blame] | 6776 | /** @hide */ |
| 6777 | protected void onSetLayoutParams(View child, LayoutParams layoutParams) { |
| 6778 | } |
| 6779 | |
George Mount | e180337 | 2014-02-26 19:00:52 +0000 | [diff] [blame] | 6780 | /** @hide */ |
| 6781 | @Override |
| 6782 | public void captureTransitioningViews(List<View> transitioningViews) { |
| 6783 | if (getVisibility() != View.VISIBLE) { |
| 6784 | return; |
| 6785 | } |
| 6786 | if (isTransitionGroup()) { |
| 6787 | transitioningViews.add(this); |
| 6788 | } else { |
| 6789 | int count = getChildCount(); |
| 6790 | for (int i = 0; i < count; i++) { |
| 6791 | View child = getChildAt(i); |
| 6792 | child.captureTransitioningViews(transitioningViews); |
| 6793 | } |
| 6794 | } |
| 6795 | } |
| 6796 | |
| 6797 | /** @hide */ |
| 6798 | @Override |
George Mount | abb352a | 2014-05-09 10:27:20 -0700 | [diff] [blame] | 6799 | public void findNamedViews(Map<String, View> namedElements) { |
George Mount | fe361d2 | 2014-07-08 17:25:25 -0700 | [diff] [blame] | 6800 | if (getVisibility() != VISIBLE && mGhostView == null) { |
George Mount | e180337 | 2014-02-26 19:00:52 +0000 | [diff] [blame] | 6801 | return; |
| 6802 | } |
George Mount | abb352a | 2014-05-09 10:27:20 -0700 | [diff] [blame] | 6803 | super.findNamedViews(namedElements); |
George Mount | e180337 | 2014-02-26 19:00:52 +0000 | [diff] [blame] | 6804 | int count = getChildCount(); |
| 6805 | for (int i = 0; i < count; i++) { |
| 6806 | View child = getChildAt(i); |
George Mount | abb352a | 2014-05-09 10:27:20 -0700 | [diff] [blame] | 6807 | child.findNamedViews(namedElements); |
George Mount | e180337 | 2014-02-26 19:00:52 +0000 | [diff] [blame] | 6808 | } |
| 6809 | } |
| 6810 | |
Patrick Dubroy | e0a799a | 2011-05-04 16:19:22 -0700 | [diff] [blame] | 6811 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6812 | * LayoutParams are used by views to tell their parents how they want to be |
| 6813 | * laid out. See |
| 6814 | * {@link android.R.styleable#ViewGroup_Layout ViewGroup Layout Attributes} |
| 6815 | * for a list of all child view attributes that this class supports. |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 6816 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6817 | * <p> |
| 6818 | * The base LayoutParams class just describes how big the view wants to be |
| 6819 | * for both width and height. For each dimension, it can specify one of: |
| 6820 | * <ul> |
Dirk Dougherty | 75c66da | 2010-03-25 16:33:33 -0700 | [diff] [blame] | 6821 | * <li>FILL_PARENT (renamed MATCH_PARENT in API Level 8 and higher), which |
| 6822 | * 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] | 6823 | * <li> WRAP_CONTENT, which means that the view wants to be just big enough |
| 6824 | * to enclose its content (plus padding) |
Dirk Dougherty | 75c66da | 2010-03-25 16:33:33 -0700 | [diff] [blame] | 6825 | * <li> an exact number |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6826 | * </ul> |
| 6827 | * There are subclasses of LayoutParams for different subclasses of |
| 6828 | * ViewGroup. For example, AbsoluteLayout has its own subclass of |
Joe Fernandez | 558459f | 2011-10-13 16:47:36 -0700 | [diff] [blame] | 6829 | * LayoutParams which adds an X and Y value.</p> |
| 6830 | * |
| 6831 | * <div class="special reference"> |
| 6832 | * <h3>Developer Guides</h3> |
| 6833 | * <p>For more information about creating user interface layouts, read the |
| 6834 | * <a href="{@docRoot}guide/topics/ui/declaring-layout.html">XML Layouts</a> developer |
| 6835 | * guide.</p></div> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6836 | * |
| 6837 | * @attr ref android.R.styleable#ViewGroup_Layout_layout_height |
| 6838 | * @attr ref android.R.styleable#ViewGroup_Layout_layout_width |
| 6839 | */ |
| 6840 | public static class LayoutParams { |
| 6841 | /** |
Dirk Dougherty | 75c66da | 2010-03-25 16:33:33 -0700 | [diff] [blame] | 6842 | * Special value for the height or width requested by a View. |
| 6843 | * FILL_PARENT means that the view wants to be as big as its parent, |
| 6844 | * minus the parent's padding, if any. This value is deprecated |
| 6845 | * 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] | 6846 | */ |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 6847 | @SuppressWarnings({"UnusedDeclaration"}) |
| 6848 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6849 | public static final int FILL_PARENT = -1; |
| 6850 | |
| 6851 | /** |
| 6852 | * Special value for the height or width requested by a View. |
Gilles Debunne | f5c6eff | 2010-02-09 19:08:36 -0800 | [diff] [blame] | 6853 | * 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] | 6854 | * minus the parent's padding, if any. Introduced in API Level 8. |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 6855 | */ |
| 6856 | public static final int MATCH_PARENT = -1; |
| 6857 | |
| 6858 | /** |
| 6859 | * 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] | 6860 | * WRAP_CONTENT means that the view wants to be just large enough to fit |
| 6861 | * its own internal content, taking its own padding into account. |
| 6862 | */ |
| 6863 | public static final int WRAP_CONTENT = -2; |
| 6864 | |
| 6865 | /** |
Dirk Dougherty | 75c66da | 2010-03-25 16:33:33 -0700 | [diff] [blame] | 6866 | * 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] | 6867 | * constants FILL_PARENT (replaced by MATCH_PARENT |
| 6868 | * 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] | 6869 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 6870 | @ViewDebug.ExportedProperty(category = "layout", mapping = { |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 6871 | @ViewDebug.IntToString(from = MATCH_PARENT, to = "MATCH_PARENT"), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6872 | @ViewDebug.IntToString(from = WRAP_CONTENT, to = "WRAP_CONTENT") |
| 6873 | }) |
| 6874 | public int width; |
| 6875 | |
| 6876 | /** |
Dirk Dougherty | 75c66da | 2010-03-25 16:33:33 -0700 | [diff] [blame] | 6877 | * 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] | 6878 | * constants FILL_PARENT (replaced by MATCH_PARENT |
| 6879 | * 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] | 6880 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 6881 | @ViewDebug.ExportedProperty(category = "layout", mapping = { |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 6882 | @ViewDebug.IntToString(from = MATCH_PARENT, to = "MATCH_PARENT"), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6883 | @ViewDebug.IntToString(from = WRAP_CONTENT, to = "WRAP_CONTENT") |
| 6884 | }) |
| 6885 | public int height; |
| 6886 | |
| 6887 | /** |
| 6888 | * Used to animate layouts. |
| 6889 | */ |
| 6890 | public LayoutAnimationController.AnimationParameters layoutAnimationParameters; |
| 6891 | |
| 6892 | /** |
| 6893 | * Creates a new set of layout parameters. The values are extracted from |
| 6894 | * the supplied attributes set and context. The XML attributes mapped |
| 6895 | * to this set of layout parameters are: |
| 6896 | * |
| 6897 | * <ul> |
| 6898 | * <li><code>layout_width</code>: the width, either an exact value, |
Dirk Dougherty | 75c66da | 2010-03-25 16:33:33 -0700 | [diff] [blame] | 6899 | * {@link #WRAP_CONTENT}, or {@link #FILL_PARENT} (replaced by |
| 6900 | * {@link #MATCH_PARENT} in API Level 8)</li> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6901 | * <li><code>layout_height</code>: the height, either an exact value, |
Dirk Dougherty | 75c66da | 2010-03-25 16:33:33 -0700 | [diff] [blame] | 6902 | * {@link #WRAP_CONTENT}, or {@link #FILL_PARENT} (replaced by |
| 6903 | * {@link #MATCH_PARENT} in API Level 8)</li> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6904 | * </ul> |
| 6905 | * |
| 6906 | * @param c the application environment |
| 6907 | * @param attrs the set of attributes from which to extract the layout |
| 6908 | * parameters' values |
| 6909 | */ |
| 6910 | public LayoutParams(Context c, AttributeSet attrs) { |
| 6911 | TypedArray a = c.obtainStyledAttributes(attrs, R.styleable.ViewGroup_Layout); |
| 6912 | setBaseAttributes(a, |
| 6913 | R.styleable.ViewGroup_Layout_layout_width, |
| 6914 | R.styleable.ViewGroup_Layout_layout_height); |
| 6915 | a.recycle(); |
| 6916 | } |
| 6917 | |
| 6918 | /** |
| 6919 | * Creates a new set of layout parameters with the specified width |
| 6920 | * and height. |
| 6921 | * |
Dirk Dougherty | 75c66da | 2010-03-25 16:33:33 -0700 | [diff] [blame] | 6922 | * @param width the width, either {@link #WRAP_CONTENT}, |
| 6923 | * {@link #FILL_PARENT} (replaced by {@link #MATCH_PARENT} in |
| 6924 | * API Level 8), or a fixed size in pixels |
| 6925 | * @param height the height, either {@link #WRAP_CONTENT}, |
| 6926 | * {@link #FILL_PARENT} (replaced by {@link #MATCH_PARENT} in |
| 6927 | * API Level 8), or a fixed size in pixels |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6928 | */ |
| 6929 | public LayoutParams(int width, int height) { |
| 6930 | this.width = width; |
| 6931 | this.height = height; |
| 6932 | } |
| 6933 | |
| 6934 | /** |
| 6935 | * Copy constructor. Clones the width and height values of the source. |
| 6936 | * |
| 6937 | * @param source The layout params to copy from. |
| 6938 | */ |
| 6939 | public LayoutParams(LayoutParams source) { |
| 6940 | this.width = source.width; |
| 6941 | this.height = source.height; |
| 6942 | } |
| 6943 | |
| 6944 | /** |
| 6945 | * Used internally by MarginLayoutParams. |
| 6946 | * @hide |
| 6947 | */ |
| 6948 | LayoutParams() { |
| 6949 | } |
| 6950 | |
| 6951 | /** |
Dave Burke | 579e140 | 2012-10-18 20:41:55 -0700 | [diff] [blame] | 6952 | * Extracts the layout parameters from the supplied attributes. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6953 | * |
| 6954 | * @param a the style attributes to extract the parameters from |
| 6955 | * @param widthAttr the identifier of the width attribute |
| 6956 | * @param heightAttr the identifier of the height attribute |
| 6957 | */ |
| 6958 | protected void setBaseAttributes(TypedArray a, int widthAttr, int heightAttr) { |
Dave Burke | 579e140 | 2012-10-18 20:41:55 -0700 | [diff] [blame] | 6959 | width = a.getLayoutDimension(widthAttr, "layout_width"); |
| 6960 | height = a.getLayoutDimension(heightAttr, "layout_height"); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6961 | } |
| 6962 | |
| 6963 | /** |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 6964 | * Resolve layout parameters depending on the layout direction. Subclasses that care about |
| 6965 | * layoutDirection changes should override this method. The default implementation does |
| 6966 | * nothing. |
| 6967 | * |
| 6968 | * @param layoutDirection the direction of the layout |
| 6969 | * |
| 6970 | * {@link View#LAYOUT_DIRECTION_LTR} |
| 6971 | * {@link View#LAYOUT_DIRECTION_RTL} |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 6972 | */ |
Fabrice Di Meglio | 2918ab6 | 2012-10-10 16:39:25 -0700 | [diff] [blame] | 6973 | public void resolveLayoutDirection(int layoutDirection) { |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 6974 | } |
| 6975 | |
| 6976 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6977 | * Returns a String representation of this set of layout parameters. |
| 6978 | * |
| 6979 | * @param output the String to prepend to the internal representation |
| 6980 | * @return a String with the following format: output + |
| 6981 | * "ViewGroup.LayoutParams={ width=WIDTH, height=HEIGHT }" |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 6982 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 6983 | * @hide |
| 6984 | */ |
| 6985 | public String debug(String output) { |
| 6986 | return output + "ViewGroup.LayoutParams={ width=" |
| 6987 | + sizeToString(width) + ", height=" + sizeToString(height) + " }"; |
| 6988 | } |
| 6989 | |
| 6990 | /** |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 6991 | * Use {@code canvas} to draw suitable debugging annotations for these LayoutParameters. |
| 6992 | * |
| 6993 | * @param view the view that contains these layout parameters |
| 6994 | * @param canvas the canvas on which to draw |
| 6995 | * |
| 6996 | * @hide |
| 6997 | */ |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 6998 | public void onDebugDraw(View view, Canvas canvas, Paint paint) { |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 6999 | } |
| 7000 | |
| 7001 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7002 | * Converts the specified size to a readable String. |
| 7003 | * |
| 7004 | * @param size the size to convert |
| 7005 | * @return a String instance representing the supplied size |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 7006 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7007 | * @hide |
| 7008 | */ |
| 7009 | protected static String sizeToString(int size) { |
| 7010 | if (size == WRAP_CONTENT) { |
| 7011 | return "wrap-content"; |
| 7012 | } |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 7013 | if (size == MATCH_PARENT) { |
| 7014 | return "match-parent"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7015 | } |
| 7016 | return String.valueOf(size); |
| 7017 | } |
Siva Velusamy | 0d857b9 | 2015-04-22 10:23:56 -0700 | [diff] [blame] | 7018 | |
| 7019 | /** @hide */ |
| 7020 | void encode(@NonNull ViewHierarchyEncoder encoder) { |
| 7021 | encoder.beginObject(this); |
| 7022 | encodeProperties(encoder); |
| 7023 | encoder.endObject(); |
| 7024 | } |
| 7025 | |
| 7026 | /** @hide */ |
| 7027 | protected void encodeProperties(@NonNull ViewHierarchyEncoder encoder) { |
| 7028 | encoder.addProperty("width", width); |
| 7029 | encoder.addProperty("height", height); |
| 7030 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7031 | } |
| 7032 | |
| 7033 | /** |
| 7034 | * Per-child layout information for layouts that support margins. |
| 7035 | * See |
| 7036 | * {@link android.R.styleable#ViewGroup_MarginLayout ViewGroup Margin Layout Attributes} |
| 7037 | * for a list of all child view attributes that this class supports. |
| 7038 | */ |
| 7039 | public static class MarginLayoutParams extends ViewGroup.LayoutParams { |
| 7040 | /** |
Adam Powell | a7a735f | 2014-10-09 12:54:52 -0700 | [diff] [blame] | 7041 | * 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] | 7042 | * Call {@link ViewGroup#setLayoutParams(LayoutParams)} after reassigning a new value |
| 7043 | * to this field. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7044 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 7045 | @ViewDebug.ExportedProperty(category = "layout") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7046 | public int leftMargin; |
| 7047 | |
| 7048 | /** |
Adam Powell | a7a735f | 2014-10-09 12:54:52 -0700 | [diff] [blame] | 7049 | * 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] | 7050 | * Call {@link ViewGroup#setLayoutParams(LayoutParams)} after reassigning a new value |
| 7051 | * to this field. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7052 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 7053 | @ViewDebug.ExportedProperty(category = "layout") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7054 | public int topMargin; |
| 7055 | |
| 7056 | /** |
Adam Powell | a7a735f | 2014-10-09 12:54:52 -0700 | [diff] [blame] | 7057 | * 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] | 7058 | * Call {@link ViewGroup#setLayoutParams(LayoutParams)} after reassigning a new value |
| 7059 | * to this field. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7060 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 7061 | @ViewDebug.ExportedProperty(category = "layout") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7062 | public int rightMargin; |
| 7063 | |
| 7064 | /** |
Adam Powell | a7a735f | 2014-10-09 12:54:52 -0700 | [diff] [blame] | 7065 | * 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] | 7066 | * Call {@link ViewGroup#setLayoutParams(LayoutParams)} after reassigning a new value |
| 7067 | * to this field. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7068 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 7069 | @ViewDebug.ExportedProperty(category = "layout") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7070 | public int bottomMargin; |
| 7071 | |
| 7072 | /** |
Adam Powell | a7a735f | 2014-10-09 12:54:52 -0700 | [diff] [blame] | 7073 | * 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] | 7074 | * Call {@link ViewGroup#setLayoutParams(LayoutParams)} after reassigning a new value |
| 7075 | * to this field. |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7076 | */ |
| 7077 | @ViewDebug.ExportedProperty(category = "layout") |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 7078 | private int startMargin = DEFAULT_MARGIN_RELATIVE; |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7079 | |
| 7080 | /** |
Adam Powell | a7a735f | 2014-10-09 12:54:52 -0700 | [diff] [blame] | 7081 | * 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] | 7082 | * Call {@link ViewGroup#setLayoutParams(LayoutParams)} after reassigning a new value |
| 7083 | * to this field. |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7084 | */ |
| 7085 | @ViewDebug.ExportedProperty(category = "layout") |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 7086 | private int endMargin = DEFAULT_MARGIN_RELATIVE; |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7087 | |
| 7088 | /** |
| 7089 | * The default start and end margin. |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 7090 | * @hide |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7091 | */ |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 7092 | public static final int DEFAULT_MARGIN_RELATIVE = Integer.MIN_VALUE; |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7093 | |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7094 | /** |
| 7095 | * Bit 0: layout direction |
| 7096 | * Bit 1: layout direction |
| 7097 | * Bit 2: left margin undefined |
| 7098 | * Bit 3: right margin undefined |
| 7099 | * Bit 4: is RTL compatibility mode |
| 7100 | * Bit 5: need resolution |
| 7101 | * |
| 7102 | * Bit 6 to 7 not used |
| 7103 | * |
| 7104 | * @hide |
| 7105 | */ |
| 7106 | @ViewDebug.ExportedProperty(category = "layout", flagMapping = { |
| 7107 | @ViewDebug.FlagToString(mask = LAYOUT_DIRECTION_MASK, |
| 7108 | equals = LAYOUT_DIRECTION_MASK, name = "LAYOUT_DIRECTION"), |
| 7109 | @ViewDebug.FlagToString(mask = LEFT_MARGIN_UNDEFINED_MASK, |
| 7110 | equals = LEFT_MARGIN_UNDEFINED_MASK, name = "LEFT_MARGIN_UNDEFINED_MASK"), |
| 7111 | @ViewDebug.FlagToString(mask = RIGHT_MARGIN_UNDEFINED_MASK, |
| 7112 | equals = RIGHT_MARGIN_UNDEFINED_MASK, name = "RIGHT_MARGIN_UNDEFINED_MASK"), |
| 7113 | @ViewDebug.FlagToString(mask = RTL_COMPATIBILITY_MODE_MASK, |
| 7114 | equals = RTL_COMPATIBILITY_MODE_MASK, name = "RTL_COMPATIBILITY_MODE_MASK"), |
| 7115 | @ViewDebug.FlagToString(mask = NEED_RESOLUTION_MASK, |
| 7116 | equals = NEED_RESOLUTION_MASK, name = "NEED_RESOLUTION_MASK") |
Jon Miranda | 4597e98 | 2014-07-29 07:25:49 -0700 | [diff] [blame] | 7117 | }, formatToHexString = true) |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7118 | byte mMarginFlags; |
Fabrice Di Meglio | 69bd558 | 2012-07-02 13:17:24 -0700 | [diff] [blame] | 7119 | |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7120 | private static final int LAYOUT_DIRECTION_MASK = 0x00000003; |
| 7121 | private static final int LEFT_MARGIN_UNDEFINED_MASK = 0x00000004; |
| 7122 | private static final int RIGHT_MARGIN_UNDEFINED_MASK = 0x00000008; |
| 7123 | private static final int RTL_COMPATIBILITY_MODE_MASK = 0x00000010; |
| 7124 | private static final int NEED_RESOLUTION_MASK = 0x00000020; |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 7125 | |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7126 | private static final int DEFAULT_MARGIN_RESOLVED = 0; |
| 7127 | private static final int UNDEFINED_MARGIN = DEFAULT_MARGIN_RELATIVE; |
Fabrice Di Meglio | 02a7d56 | 2013-03-27 12:41:22 -0700 | [diff] [blame] | 7128 | |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7129 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7130 | * Creates a new set of layout parameters. The values are extracted from |
| 7131 | * the supplied attributes set and context. |
| 7132 | * |
| 7133 | * @param c the application environment |
| 7134 | * @param attrs the set of attributes from which to extract the layout |
| 7135 | * parameters' values |
| 7136 | */ |
| 7137 | public MarginLayoutParams(Context c, AttributeSet attrs) { |
| 7138 | super(); |
| 7139 | |
| 7140 | TypedArray a = c.obtainStyledAttributes(attrs, R.styleable.ViewGroup_MarginLayout); |
| 7141 | setBaseAttributes(a, |
| 7142 | R.styleable.ViewGroup_MarginLayout_layout_width, |
| 7143 | R.styleable.ViewGroup_MarginLayout_layout_height); |
| 7144 | |
| 7145 | int margin = a.getDimensionPixelSize( |
| 7146 | com.android.internal.R.styleable.ViewGroup_MarginLayout_layout_margin, -1); |
| 7147 | if (margin >= 0) { |
| 7148 | leftMargin = margin; |
| 7149 | topMargin = margin; |
| 7150 | rightMargin= margin; |
| 7151 | bottomMargin = margin; |
| 7152 | } else { |
| 7153 | leftMargin = a.getDimensionPixelSize( |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 7154 | R.styleable.ViewGroup_MarginLayout_layout_marginLeft, |
Fabrice Di Meglio | 02a7d56 | 2013-03-27 12:41:22 -0700 | [diff] [blame] | 7155 | UNDEFINED_MARGIN); |
| 7156 | if (leftMargin == UNDEFINED_MARGIN) { |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7157 | mMarginFlags |= LEFT_MARGIN_UNDEFINED_MASK; |
Fabrice Di Meglio | 02a7d56 | 2013-03-27 12:41:22 -0700 | [diff] [blame] | 7158 | leftMargin = DEFAULT_MARGIN_RESOLVED; |
| 7159 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7160 | rightMargin = a.getDimensionPixelSize( |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 7161 | R.styleable.ViewGroup_MarginLayout_layout_marginRight, |
Fabrice Di Meglio | 02a7d56 | 2013-03-27 12:41:22 -0700 | [diff] [blame] | 7162 | UNDEFINED_MARGIN); |
| 7163 | if (rightMargin == UNDEFINED_MARGIN) { |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7164 | mMarginFlags |= RIGHT_MARGIN_UNDEFINED_MASK; |
Fabrice Di Meglio | 02a7d56 | 2013-03-27 12:41:22 -0700 | [diff] [blame] | 7165 | rightMargin = DEFAULT_MARGIN_RESOLVED; |
| 7166 | } |
| 7167 | |
| 7168 | topMargin = a.getDimensionPixelSize( |
| 7169 | R.styleable.ViewGroup_MarginLayout_layout_marginTop, |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 7170 | DEFAULT_MARGIN_RESOLVED); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7171 | bottomMargin = a.getDimensionPixelSize( |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 7172 | R.styleable.ViewGroup_MarginLayout_layout_marginBottom, |
| 7173 | DEFAULT_MARGIN_RESOLVED); |
Fabrice Di Meglio | 02a7d56 | 2013-03-27 12:41:22 -0700 | [diff] [blame] | 7174 | |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7175 | startMargin = a.getDimensionPixelSize( |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 7176 | R.styleable.ViewGroup_MarginLayout_layout_marginStart, |
| 7177 | DEFAULT_MARGIN_RELATIVE); |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7178 | endMargin = a.getDimensionPixelSize( |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 7179 | R.styleable.ViewGroup_MarginLayout_layout_marginEnd, |
| 7180 | DEFAULT_MARGIN_RELATIVE); |
| 7181 | |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7182 | if (isMarginRelative()) { |
| 7183 | mMarginFlags |= NEED_RESOLUTION_MASK; |
| 7184 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7185 | } |
| 7186 | |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 7187 | final boolean hasRtlSupport = c.getApplicationInfo().hasRtlSupport(); |
| 7188 | final int targetSdkVersion = c.getApplicationInfo().targetSdkVersion; |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7189 | if (targetSdkVersion < JELLY_BEAN_MR1 || !hasRtlSupport) { |
| 7190 | mMarginFlags |= RTL_COMPATIBILITY_MODE_MASK; |
| 7191 | } |
| 7192 | |
| 7193 | // Layout direction is LTR by default |
| 7194 | mMarginFlags |= LAYOUT_DIRECTION_LTR; |
Fabrice Di Meglio | 69bd558 | 2012-07-02 13:17:24 -0700 | [diff] [blame] | 7195 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7196 | a.recycle(); |
| 7197 | } |
| 7198 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7199 | public MarginLayoutParams(int width, int height) { |
| 7200 | super(width, height); |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 7201 | |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7202 | mMarginFlags |= LEFT_MARGIN_UNDEFINED_MASK; |
| 7203 | mMarginFlags |= RIGHT_MARGIN_UNDEFINED_MASK; |
Fabrice Di Meglio | 02a7d56 | 2013-03-27 12:41:22 -0700 | [diff] [blame] | 7204 | |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7205 | mMarginFlags &= ~NEED_RESOLUTION_MASK; |
| 7206 | mMarginFlags &= ~RTL_COMPATIBILITY_MODE_MASK; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7207 | } |
| 7208 | |
| 7209 | /** |
| 7210 | * Copy constructor. Clones the width, height and margin values of the source. |
| 7211 | * |
| 7212 | * @param source The layout params to copy from. |
| 7213 | */ |
| 7214 | public MarginLayoutParams(MarginLayoutParams source) { |
| 7215 | this.width = source.width; |
| 7216 | this.height = source.height; |
| 7217 | |
| 7218 | this.leftMargin = source.leftMargin; |
| 7219 | this.topMargin = source.topMargin; |
| 7220 | this.rightMargin = source.rightMargin; |
| 7221 | this.bottomMargin = source.bottomMargin; |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7222 | this.startMargin = source.startMargin; |
| 7223 | this.endMargin = source.endMargin; |
Fabrice Di Meglio | 69bd558 | 2012-07-02 13:17:24 -0700 | [diff] [blame] | 7224 | |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7225 | this.mMarginFlags = source.mMarginFlags; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7226 | } |
| 7227 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7228 | public MarginLayoutParams(LayoutParams source) { |
| 7229 | super(source); |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 7230 | |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7231 | mMarginFlags |= LEFT_MARGIN_UNDEFINED_MASK; |
| 7232 | mMarginFlags |= RIGHT_MARGIN_UNDEFINED_MASK; |
Fabrice Di Meglio | 02a7d56 | 2013-03-27 12:41:22 -0700 | [diff] [blame] | 7233 | |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7234 | mMarginFlags &= ~NEED_RESOLUTION_MASK; |
| 7235 | mMarginFlags &= ~RTL_COMPATIBILITY_MODE_MASK; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7236 | } |
| 7237 | |
| 7238 | /** |
Adam Powell | d760083 | 2014-07-01 15:22:50 -0700 | [diff] [blame] | 7239 | * @hide Used internally. |
| 7240 | */ |
| 7241 | public final void copyMarginsFrom(MarginLayoutParams source) { |
| 7242 | this.leftMargin = source.leftMargin; |
| 7243 | this.topMargin = source.topMargin; |
| 7244 | this.rightMargin = source.rightMargin; |
| 7245 | this.bottomMargin = source.bottomMargin; |
| 7246 | this.startMargin = source.startMargin; |
| 7247 | this.endMargin = source.endMargin; |
| 7248 | |
| 7249 | this.mMarginFlags = source.mMarginFlags; |
| 7250 | } |
| 7251 | |
| 7252 | /** |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7253 | * Sets the margins, in pixels. A call to {@link android.view.View#requestLayout()} needs |
| 7254 | * to be done so that the new margins are taken into account. Left and right margins may be |
| 7255 | * overriden by {@link android.view.View#requestLayout()} depending on layout direction. |
Adam Powell | a7a735f | 2014-10-09 12:54:52 -0700 | [diff] [blame] | 7256 | * Margin values should be positive. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7257 | * |
| 7258 | * @param left the left margin size |
| 7259 | * @param top the top margin size |
| 7260 | * @param right the right margin size |
| 7261 | * @param bottom the bottom margin size |
| 7262 | * |
| 7263 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginLeft |
| 7264 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginTop |
| 7265 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginRight |
| 7266 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginBottom |
| 7267 | */ |
| 7268 | public void setMargins(int left, int top, int right, int bottom) { |
| 7269 | leftMargin = left; |
| 7270 | topMargin = top; |
| 7271 | rightMargin = right; |
| 7272 | bottomMargin = bottom; |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7273 | mMarginFlags &= ~LEFT_MARGIN_UNDEFINED_MASK; |
| 7274 | mMarginFlags &= ~RIGHT_MARGIN_UNDEFINED_MASK; |
| 7275 | if (isMarginRelative()) { |
| 7276 | mMarginFlags |= NEED_RESOLUTION_MASK; |
| 7277 | } else { |
| 7278 | mMarginFlags &= ~NEED_RESOLUTION_MASK; |
| 7279 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7280 | } |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7281 | |
| 7282 | /** |
| 7283 | * Sets the relative margins, in pixels. A call to {@link android.view.View#requestLayout()} |
| 7284 | * needs to be done so that the new relative margins are taken into account. Left and right |
| 7285 | * 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] | 7286 | * direction. Margin values should be positive. |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7287 | * |
| 7288 | * @param start the start margin size |
| 7289 | * @param top the top margin size |
| 7290 | * @param end the right margin size |
| 7291 | * @param bottom the bottom margin size |
| 7292 | * |
| 7293 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginStart |
| 7294 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginTop |
| 7295 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginEnd |
| 7296 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginBottom |
| 7297 | * |
| 7298 | * @hide |
| 7299 | */ |
| 7300 | public void setMarginsRelative(int start, int top, int end, int bottom) { |
| 7301 | startMargin = start; |
| 7302 | topMargin = top; |
| 7303 | endMargin = end; |
| 7304 | bottomMargin = bottom; |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7305 | mMarginFlags |= NEED_RESOLUTION_MASK; |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7306 | } |
| 7307 | |
| 7308 | /** |
Adam Powell | a7a735f | 2014-10-09 12:54:52 -0700 | [diff] [blame] | 7309 | * Sets the relative start margin. Margin values should be positive. |
Fabrice Di Meglio | a40627d | 2012-09-11 16:47:21 -0700 | [diff] [blame] | 7310 | * |
Fabrice Di Meglio | 61a2177 | 2012-09-12 16:33:13 -0700 | [diff] [blame] | 7311 | * @param start the start margin size |
Fabrice Di Meglio | a40627d | 2012-09-11 16:47:21 -0700 | [diff] [blame] | 7312 | * |
| 7313 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginStart |
| 7314 | */ |
| 7315 | public void setMarginStart(int start) { |
| 7316 | startMargin = start; |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7317 | mMarginFlags |= NEED_RESOLUTION_MASK; |
Fabrice Di Meglio | a40627d | 2012-09-11 16:47:21 -0700 | [diff] [blame] | 7318 | } |
| 7319 | |
| 7320 | /** |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7321 | * Returns the start margin in pixels. |
| 7322 | * |
| 7323 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginStart |
| 7324 | * |
| 7325 | * @return the start margin in pixels. |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7326 | */ |
| 7327 | public int getMarginStart() { |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 7328 | if (startMargin != DEFAULT_MARGIN_RELATIVE) return startMargin; |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7329 | if ((mMarginFlags & NEED_RESOLUTION_MASK) == NEED_RESOLUTION_MASK) { |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 7330 | doResolveMargins(); |
| 7331 | } |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7332 | switch(mMarginFlags & LAYOUT_DIRECTION_MASK) { |
Fabrice Di Meglio | 69bd558 | 2012-07-02 13:17:24 -0700 | [diff] [blame] | 7333 | case View.LAYOUT_DIRECTION_RTL: |
| 7334 | return rightMargin; |
| 7335 | case View.LAYOUT_DIRECTION_LTR: |
| 7336 | default: |
| 7337 | return leftMargin; |
| 7338 | } |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7339 | } |
| 7340 | |
| 7341 | /** |
Adam Powell | a7a735f | 2014-10-09 12:54:52 -0700 | [diff] [blame] | 7342 | * Sets the relative end margin. Margin values should be positive. |
Fabrice Di Meglio | a40627d | 2012-09-11 16:47:21 -0700 | [diff] [blame] | 7343 | * |
Fabrice Di Meglio | 61a2177 | 2012-09-12 16:33:13 -0700 | [diff] [blame] | 7344 | * @param end the end margin size |
Fabrice Di Meglio | a40627d | 2012-09-11 16:47:21 -0700 | [diff] [blame] | 7345 | * |
| 7346 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginEnd |
| 7347 | */ |
| 7348 | public void setMarginEnd(int end) { |
| 7349 | endMargin = end; |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7350 | mMarginFlags |= NEED_RESOLUTION_MASK; |
Fabrice Di Meglio | a40627d | 2012-09-11 16:47:21 -0700 | [diff] [blame] | 7351 | } |
| 7352 | |
| 7353 | /** |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7354 | * Returns the end margin in pixels. |
| 7355 | * |
| 7356 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginEnd |
| 7357 | * |
| 7358 | * @return the end margin in pixels. |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7359 | */ |
| 7360 | public int getMarginEnd() { |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 7361 | if (endMargin != DEFAULT_MARGIN_RELATIVE) return endMargin; |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7362 | if ((mMarginFlags & NEED_RESOLUTION_MASK) == NEED_RESOLUTION_MASK) { |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 7363 | doResolveMargins(); |
| 7364 | } |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7365 | switch(mMarginFlags & LAYOUT_DIRECTION_MASK) { |
Fabrice Di Meglio | 69bd558 | 2012-07-02 13:17:24 -0700 | [diff] [blame] | 7366 | case View.LAYOUT_DIRECTION_RTL: |
| 7367 | return leftMargin; |
| 7368 | case View.LAYOUT_DIRECTION_LTR: |
| 7369 | default: |
| 7370 | return rightMargin; |
| 7371 | } |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7372 | } |
| 7373 | |
| 7374 | /** |
| 7375 | * Check if margins are relative. |
| 7376 | * |
| 7377 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginStart |
| 7378 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginEnd |
| 7379 | * |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 7380 | * @return true if either marginStart or marginEnd has been set. |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7381 | */ |
| 7382 | public boolean isMarginRelative() { |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 7383 | return (startMargin != DEFAULT_MARGIN_RELATIVE || endMargin != DEFAULT_MARGIN_RELATIVE); |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7384 | } |
| 7385 | |
| 7386 | /** |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 7387 | * Set the layout direction |
| 7388 | * @param layoutDirection the layout direction. |
| 7389 | * Should be either {@link View#LAYOUT_DIRECTION_LTR} |
| 7390 | * or {@link View#LAYOUT_DIRECTION_RTL}. |
| 7391 | */ |
| 7392 | public void setLayoutDirection(int layoutDirection) { |
| 7393 | if (layoutDirection != View.LAYOUT_DIRECTION_LTR && |
| 7394 | layoutDirection != View.LAYOUT_DIRECTION_RTL) return; |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7395 | if (layoutDirection != (mMarginFlags & LAYOUT_DIRECTION_MASK)) { |
| 7396 | mMarginFlags &= ~LAYOUT_DIRECTION_MASK; |
| 7397 | mMarginFlags |= (layoutDirection & LAYOUT_DIRECTION_MASK); |
| 7398 | if (isMarginRelative()) { |
| 7399 | mMarginFlags |= NEED_RESOLUTION_MASK; |
| 7400 | } else { |
| 7401 | mMarginFlags &= ~NEED_RESOLUTION_MASK; |
| 7402 | } |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 7403 | } |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 7404 | } |
| 7405 | |
| 7406 | /** |
| 7407 | * Retuns the layout direction. Can be either {@link View#LAYOUT_DIRECTION_LTR} or |
| 7408 | * {@link View#LAYOUT_DIRECTION_RTL}. |
| 7409 | * |
| 7410 | * @return the layout direction. |
| 7411 | */ |
| 7412 | public int getLayoutDirection() { |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7413 | return (mMarginFlags & LAYOUT_DIRECTION_MASK); |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 7414 | } |
| 7415 | |
| 7416 | /** |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7417 | * 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] | 7418 | * may be overridden depending on layout direction. |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7419 | */ |
| 7420 | @Override |
Fabrice Di Meglio | 2918ab6 | 2012-10-10 16:39:25 -0700 | [diff] [blame] | 7421 | public void resolveLayoutDirection(int layoutDirection) { |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 7422 | setLayoutDirection(layoutDirection); |
Fabrice Di Meglio | 69bd558 | 2012-07-02 13:17:24 -0700 | [diff] [blame] | 7423 | |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 7424 | // No relative margin or pre JB-MR1 case or no need to resolve, just dont do anything |
| 7425 | // 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] | 7426 | if (!isMarginRelative() || |
| 7427 | (mMarginFlags & NEED_RESOLUTION_MASK) != NEED_RESOLUTION_MASK) return; |
Fabrice Di Meglio | 69bd558 | 2012-07-02 13:17:24 -0700 | [diff] [blame] | 7428 | |
Fabrice Di Meglio | 0072f64 | 2013-03-26 15:50:24 -0700 | [diff] [blame] | 7429 | // Proceed with resolution |
| 7430 | doResolveMargins(); |
| 7431 | } |
| 7432 | |
| 7433 | private void doResolveMargins() { |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7434 | if ((mMarginFlags & RTL_COMPATIBILITY_MODE_MASK) == RTL_COMPATIBILITY_MODE_MASK) { |
Fabrice Di Meglio | 02a7d56 | 2013-03-27 12:41:22 -0700 | [diff] [blame] | 7435 | // if left or right margins are not defined and if we have some start or end margin |
| 7436 | // defined then use those start and end margins. |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7437 | if ((mMarginFlags & LEFT_MARGIN_UNDEFINED_MASK) == LEFT_MARGIN_UNDEFINED_MASK |
| 7438 | && startMargin > DEFAULT_MARGIN_RELATIVE) { |
Fabrice Di Meglio | 02a7d56 | 2013-03-27 12:41:22 -0700 | [diff] [blame] | 7439 | leftMargin = startMargin; |
| 7440 | } |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7441 | if ((mMarginFlags & RIGHT_MARGIN_UNDEFINED_MASK) == RIGHT_MARGIN_UNDEFINED_MASK |
| 7442 | && endMargin > DEFAULT_MARGIN_RELATIVE) { |
Fabrice Di Meglio | 02a7d56 | 2013-03-27 12:41:22 -0700 | [diff] [blame] | 7443 | rightMargin = endMargin; |
| 7444 | } |
| 7445 | } else { |
| 7446 | // We have some relative margins (either the start one or the end one or both). So use |
| 7447 | // them and override what has been defined for left and right margins. If either start |
| 7448 | // 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] | 7449 | switch(mMarginFlags & LAYOUT_DIRECTION_MASK) { |
Fabrice Di Meglio | 02a7d56 | 2013-03-27 12:41:22 -0700 | [diff] [blame] | 7450 | case View.LAYOUT_DIRECTION_RTL: |
| 7451 | leftMargin = (endMargin > DEFAULT_MARGIN_RELATIVE) ? |
| 7452 | endMargin : DEFAULT_MARGIN_RESOLVED; |
| 7453 | rightMargin = (startMargin > DEFAULT_MARGIN_RELATIVE) ? |
| 7454 | startMargin : DEFAULT_MARGIN_RESOLVED; |
| 7455 | break; |
| 7456 | case View.LAYOUT_DIRECTION_LTR: |
| 7457 | default: |
| 7458 | leftMargin = (startMargin > DEFAULT_MARGIN_RELATIVE) ? |
| 7459 | startMargin : DEFAULT_MARGIN_RESOLVED; |
| 7460 | rightMargin = (endMargin > DEFAULT_MARGIN_RELATIVE) ? |
| 7461 | endMargin : DEFAULT_MARGIN_RESOLVED; |
| 7462 | break; |
| 7463 | } |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7464 | } |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7465 | mMarginFlags &= ~NEED_RESOLUTION_MASK; |
Fabrice Di Meglio | b76023a | 2011-06-20 17:41:21 -0700 | [diff] [blame] | 7466 | } |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 7467 | |
Fabrice Di Meglio | 03b8d3a | 2012-09-27 17:05:27 -0700 | [diff] [blame] | 7468 | /** |
| 7469 | * @hide |
| 7470 | */ |
| 7471 | public boolean isLayoutRtl() { |
Fabrice Di Meglio | b365f91 | 2013-03-27 16:36:21 -0700 | [diff] [blame] | 7472 | return ((mMarginFlags & LAYOUT_DIRECTION_MASK) == View.LAYOUT_DIRECTION_RTL); |
Fabrice Di Meglio | f443f98 | 2012-07-13 20:24:03 -0700 | [diff] [blame] | 7473 | } |
| 7474 | |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 7475 | /** |
| 7476 | * @hide |
| 7477 | */ |
| 7478 | @Override |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 7479 | public void onDebugDraw(View view, Canvas canvas, Paint paint) { |
| 7480 | Insets oi = isLayoutModeOptical(view.mParent) ? view.getOpticalInsets() : Insets.NONE; |
| 7481 | |
| 7482 | fillDifference(canvas, |
| 7483 | view.getLeft() + oi.left, |
| 7484 | view.getTop() + oi.top, |
| 7485 | view.getRight() - oi.right, |
| 7486 | view.getBottom() - oi.bottom, |
| 7487 | leftMargin, |
| 7488 | topMargin, |
| 7489 | rightMargin, |
| 7490 | bottomMargin, |
| 7491 | paint); |
Philip Milne | 10ca24a | 2012-04-23 15:38:27 -0700 | [diff] [blame] | 7492 | } |
Siva Velusamy | 0d857b9 | 2015-04-22 10:23:56 -0700 | [diff] [blame] | 7493 | |
| 7494 | /** @hide */ |
| 7495 | @Override |
| 7496 | protected void encodeProperties(@NonNull ViewHierarchyEncoder encoder) { |
| 7497 | super.encodeProperties(encoder); |
| 7498 | encoder.addProperty("leftMargin", leftMargin); |
| 7499 | encoder.addProperty("topMargin", topMargin); |
| 7500 | encoder.addProperty("rightMargin", rightMargin); |
| 7501 | encoder.addProperty("bottomMargin", bottomMargin); |
| 7502 | encoder.addProperty("startMargin", startMargin); |
| 7503 | encoder.addProperty("endMargin", endMargin); |
| 7504 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7505 | } |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 7506 | |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 7507 | /* Describes a touched view and the ids of the pointers that it has captured. |
| 7508 | * |
| 7509 | * This code assumes that pointer ids are always in the range 0..31 such that |
| 7510 | * it can use a bitfield to track which pointer ids are present. |
| 7511 | * As it happens, the lower layers of the input dispatch pipeline also use the |
| 7512 | * same trick so the assumption should be safe here... |
| 7513 | */ |
| 7514 | private static final class TouchTarget { |
| 7515 | private static final int MAX_RECYCLED = 32; |
Romain Guy | 6410c0a | 2013-06-17 11:21:58 -0700 | [diff] [blame] | 7516 | private static final Object sRecycleLock = new Object[0]; |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 7517 | private static TouchTarget sRecycleBin; |
| 7518 | private static int sRecycledCount; |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 7519 | |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 7520 | public static final int ALL_POINTER_IDS = -1; // all ones |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 7521 | |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 7522 | // The touched child view. |
| 7523 | public View child; |
| 7524 | |
| 7525 | // The combined bit mask of pointer ids for all pointers captured by the target. |
| 7526 | public int pointerIdBits; |
| 7527 | |
| 7528 | // The next target in the target list. |
| 7529 | public TouchTarget next; |
| 7530 | |
| 7531 | private TouchTarget() { |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 7532 | } |
| 7533 | |
Alan Viverette | a7b85e6 | 2016-01-22 10:14:02 -0500 | [diff] [blame] | 7534 | public static TouchTarget obtain(@NonNull View child, int pointerIdBits) { |
| 7535 | if (child == null) { |
| 7536 | throw new IllegalArgumentException("child must be non-null"); |
| 7537 | } |
| 7538 | |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 7539 | final TouchTarget target; |
| 7540 | synchronized (sRecycleLock) { |
Adam Powell | 816c3be | 2010-08-23 18:00:05 -0700 | [diff] [blame] | 7541 | if (sRecycleBin == null) { |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 7542 | target = new TouchTarget(); |
Adam Powell | 816c3be | 2010-08-23 18:00:05 -0700 | [diff] [blame] | 7543 | } else { |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 7544 | target = sRecycleBin; |
| 7545 | sRecycleBin = target.next; |
| 7546 | sRecycledCount--; |
| 7547 | target.next = null; |
Adam Powell | 816c3be | 2010-08-23 18:00:05 -0700 | [diff] [blame] | 7548 | } |
Adam Powell | 816c3be | 2010-08-23 18:00:05 -0700 | [diff] [blame] | 7549 | } |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 7550 | target.child = child; |
| 7551 | target.pointerIdBits = pointerIdBits; |
| 7552 | return target; |
| 7553 | } |
Adam Powell | 816c3be | 2010-08-23 18:00:05 -0700 | [diff] [blame] | 7554 | |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 7555 | public void recycle() { |
Alan Viverette | a7b85e6 | 2016-01-22 10:14:02 -0500 | [diff] [blame] | 7556 | if (child == null) { |
| 7557 | throw new IllegalStateException("already recycled once"); |
| 7558 | } |
| 7559 | |
Jeff Brown | 20e987b | 2010-08-23 12:01:02 -0700 | [diff] [blame] | 7560 | synchronized (sRecycleLock) { |
| 7561 | if (sRecycledCount < MAX_RECYCLED) { |
| 7562 | next = sRecycleBin; |
| 7563 | sRecycleBin = this; |
| 7564 | sRecycledCount += 1; |
Patrick Dubroy | fb0547d2 | 2010-10-19 17:36:18 -0700 | [diff] [blame] | 7565 | } else { |
| 7566 | next = null; |
Adam Powell | 816c3be | 2010-08-23 18:00:05 -0700 | [diff] [blame] | 7567 | } |
Patrick Dubroy | fb0547d2 | 2010-10-19 17:36:18 -0700 | [diff] [blame] | 7568 | child = null; |
Adam Powell | 816c3be | 2010-08-23 18:00:05 -0700 | [diff] [blame] | 7569 | } |
| 7570 | } |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 7571 | } |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 7572 | |
| 7573 | /* Describes a hovered view. */ |
| 7574 | private static final class HoverTarget { |
| 7575 | private static final int MAX_RECYCLED = 32; |
Romain Guy | 6410c0a | 2013-06-17 11:21:58 -0700 | [diff] [blame] | 7576 | private static final Object sRecycleLock = new Object[0]; |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 7577 | private static HoverTarget sRecycleBin; |
| 7578 | private static int sRecycledCount; |
| 7579 | |
| 7580 | // The hovered child view. |
| 7581 | public View child; |
| 7582 | |
| 7583 | // The next target in the target list. |
| 7584 | public HoverTarget next; |
| 7585 | |
| 7586 | private HoverTarget() { |
| 7587 | } |
| 7588 | |
Alan Viverette | a7b85e6 | 2016-01-22 10:14:02 -0500 | [diff] [blame] | 7589 | public static HoverTarget obtain(@NonNull View child) { |
| 7590 | if (child == null) { |
| 7591 | throw new IllegalArgumentException("child must be non-null"); |
| 7592 | } |
| 7593 | |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 7594 | final HoverTarget target; |
| 7595 | synchronized (sRecycleLock) { |
| 7596 | if (sRecycleBin == null) { |
| 7597 | target = new HoverTarget(); |
| 7598 | } else { |
| 7599 | target = sRecycleBin; |
| 7600 | sRecycleBin = target.next; |
Alan Viverette | a7b85e6 | 2016-01-22 10:14:02 -0500 | [diff] [blame] | 7601 | sRecycledCount--; |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 7602 | target.next = null; |
| 7603 | } |
| 7604 | } |
| 7605 | target.child = child; |
| 7606 | return target; |
| 7607 | } |
| 7608 | |
| 7609 | public void recycle() { |
Alan Viverette | a7b85e6 | 2016-01-22 10:14:02 -0500 | [diff] [blame] | 7610 | if (child == null) { |
| 7611 | throw new IllegalStateException("already recycled once"); |
| 7612 | } |
| 7613 | |
Jeff Brown | 87b7f80 | 2011-06-21 18:35:45 -0700 | [diff] [blame] | 7614 | synchronized (sRecycleLock) { |
| 7615 | if (sRecycledCount < MAX_RECYCLED) { |
| 7616 | next = sRecycleBin; |
| 7617 | sRecycleBin = this; |
| 7618 | sRecycledCount += 1; |
| 7619 | } else { |
| 7620 | next = null; |
| 7621 | } |
| 7622 | child = null; |
| 7623 | } |
| 7624 | } |
| 7625 | } |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 7626 | |
| 7627 | /** |
| 7628 | * Pooled class that orderes the children of a ViewGroup from start |
| 7629 | * to end based on how they are laid out and the layout direction. |
| 7630 | */ |
| 7631 | static class ChildListForAccessibility { |
| 7632 | |
| 7633 | private static final int MAX_POOL_SIZE = 32; |
| 7634 | |
Svetoslav Ganov | be922dc | 2012-11-30 16:46:26 -0800 | [diff] [blame] | 7635 | private static final SynchronizedPool<ChildListForAccessibility> sPool = |
| 7636 | new SynchronizedPool<ChildListForAccessibility>(MAX_POOL_SIZE); |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 7637 | |
| 7638 | private final ArrayList<View> mChildren = new ArrayList<View>(); |
| 7639 | |
| 7640 | private final ArrayList<ViewLocationHolder> mHolders = new ArrayList<ViewLocationHolder>(); |
| 7641 | |
| 7642 | public static ChildListForAccessibility obtain(ViewGroup parent, boolean sort) { |
Svetoslav Ganov | be922dc | 2012-11-30 16:46:26 -0800 | [diff] [blame] | 7643 | ChildListForAccessibility list = sPool.acquire(); |
| 7644 | if (list == null) { |
| 7645 | list = new ChildListForAccessibility(); |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 7646 | } |
Svetoslav Ganov | be922dc | 2012-11-30 16:46:26 -0800 | [diff] [blame] | 7647 | list.init(parent, sort); |
| 7648 | return list; |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 7649 | } |
| 7650 | |
| 7651 | public void recycle() { |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 7652 | clear(); |
Svetoslav Ganov | be922dc | 2012-11-30 16:46:26 -0800 | [diff] [blame] | 7653 | sPool.release(this); |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 7654 | } |
| 7655 | |
| 7656 | public int getChildCount() { |
| 7657 | return mChildren.size(); |
| 7658 | } |
| 7659 | |
| 7660 | public View getChildAt(int index) { |
| 7661 | return mChildren.get(index); |
| 7662 | } |
| 7663 | |
| 7664 | public int getChildIndex(View child) { |
| 7665 | return mChildren.indexOf(child); |
| 7666 | } |
| 7667 | |
| 7668 | private void init(ViewGroup parent, boolean sort) { |
| 7669 | ArrayList<View> children = mChildren; |
| 7670 | final int childCount = parent.getChildCount(); |
| 7671 | for (int i = 0; i < childCount; i++) { |
| 7672 | View child = parent.getChildAt(i); |
| 7673 | children.add(child); |
| 7674 | } |
| 7675 | if (sort) { |
| 7676 | ArrayList<ViewLocationHolder> holders = mHolders; |
| 7677 | for (int i = 0; i < childCount; i++) { |
| 7678 | View child = children.get(i); |
| 7679 | ViewLocationHolder holder = ViewLocationHolder.obtain(parent, child); |
| 7680 | holders.add(holder); |
| 7681 | } |
Svetoslav | 88e447b | 2014-10-09 15:49:02 -0700 | [diff] [blame] | 7682 | sort(holders); |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 7683 | for (int i = 0; i < childCount; i++) { |
| 7684 | ViewLocationHolder holder = holders.get(i); |
| 7685 | children.set(i, holder.mView); |
| 7686 | holder.recycle(); |
| 7687 | } |
| 7688 | holders.clear(); |
| 7689 | } |
| 7690 | } |
| 7691 | |
Svetoslav | 88e447b | 2014-10-09 15:49:02 -0700 | [diff] [blame] | 7692 | private void sort(ArrayList<ViewLocationHolder> holders) { |
| 7693 | // This is gross but the least risky solution. The current comparison |
| 7694 | // strategy breaks transitivity but produces very good results. Coming |
| 7695 | // up with a new strategy requires time which we do not have, so ... |
| 7696 | try { |
| 7697 | ViewLocationHolder.setComparisonStrategy( |
| 7698 | ViewLocationHolder.COMPARISON_STRATEGY_STRIPE); |
| 7699 | Collections.sort(holders); |
| 7700 | } catch (IllegalArgumentException iae) { |
| 7701 | // Note that in practice this occurs extremely rarely in a couple |
| 7702 | // of pathological cases. |
| 7703 | ViewLocationHolder.setComparisonStrategy( |
| 7704 | ViewLocationHolder.COMPARISON_STRATEGY_LOCATION); |
| 7705 | Collections.sort(holders); |
| 7706 | } |
| 7707 | } |
| 7708 | |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 7709 | private void clear() { |
| 7710 | mChildren.clear(); |
| 7711 | } |
| 7712 | } |
| 7713 | |
| 7714 | /** |
| 7715 | * Pooled class that holds a View and its location with respect to |
| 7716 | * a specified root. This enables sorting of views based on their |
| 7717 | * coordinates without recomputing the position relative to the root |
| 7718 | * on every comparison. |
| 7719 | */ |
| 7720 | static class ViewLocationHolder implements Comparable<ViewLocationHolder> { |
| 7721 | |
| 7722 | private static final int MAX_POOL_SIZE = 32; |
| 7723 | |
Svetoslav Ganov | be922dc | 2012-11-30 16:46:26 -0800 | [diff] [blame] | 7724 | private static final SynchronizedPool<ViewLocationHolder> sPool = |
| 7725 | new SynchronizedPool<ViewLocationHolder>(MAX_POOL_SIZE); |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 7726 | |
Svetoslav | 88e447b | 2014-10-09 15:49:02 -0700 | [diff] [blame] | 7727 | public static final int COMPARISON_STRATEGY_STRIPE = 1; |
| 7728 | |
| 7729 | public static final int COMPARISON_STRATEGY_LOCATION = 2; |
| 7730 | |
| 7731 | private static int sComparisonStrategy = COMPARISON_STRATEGY_STRIPE; |
| 7732 | |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 7733 | private final Rect mLocation = new Rect(); |
| 7734 | |
| 7735 | public View mView; |
| 7736 | |
| 7737 | private int mLayoutDirection; |
| 7738 | |
| 7739 | public static ViewLocationHolder obtain(ViewGroup root, View view) { |
Svetoslav Ganov | be922dc | 2012-11-30 16:46:26 -0800 | [diff] [blame] | 7740 | ViewLocationHolder holder = sPool.acquire(); |
| 7741 | if (holder == null) { |
| 7742 | holder = new ViewLocationHolder(); |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 7743 | } |
Svetoslav Ganov | be922dc | 2012-11-30 16:46:26 -0800 | [diff] [blame] | 7744 | holder.init(root, view); |
| 7745 | return holder; |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 7746 | } |
| 7747 | |
Svetoslav | 88e447b | 2014-10-09 15:49:02 -0700 | [diff] [blame] | 7748 | public static void setComparisonStrategy(int strategy) { |
| 7749 | sComparisonStrategy = strategy; |
| 7750 | } |
| 7751 | |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 7752 | public void recycle() { |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 7753 | clear(); |
Svetoslav Ganov | be922dc | 2012-11-30 16:46:26 -0800 | [diff] [blame] | 7754 | sPool.release(this); |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 7755 | } |
| 7756 | |
| 7757 | @Override |
| 7758 | public int compareTo(ViewLocationHolder another) { |
| 7759 | // This instance is greater than an invalid argument. |
| 7760 | if (another == null) { |
| 7761 | return 1; |
| 7762 | } |
Svetoslav | 88e447b | 2014-10-09 15:49:02 -0700 | [diff] [blame] | 7763 | |
| 7764 | if (sComparisonStrategy == COMPARISON_STRATEGY_STRIPE) { |
| 7765 | // First is above second. |
| 7766 | if (mLocation.bottom - another.mLocation.top <= 0) { |
| 7767 | return -1; |
| 7768 | } |
| 7769 | // First is below second. |
| 7770 | if (mLocation.top - another.mLocation.bottom >= 0) { |
| 7771 | return 1; |
| 7772 | } |
| 7773 | } |
| 7774 | |
Svetoslav | 04cab1b | 2014-08-25 18:35:57 -0700 | [diff] [blame] | 7775 | // We are ordering left-to-right, top-to-bottom. |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 7776 | if (mLayoutDirection == LAYOUT_DIRECTION_LTR) { |
| 7777 | final int leftDifference = mLocation.left - another.mLocation.left; |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 7778 | if (leftDifference != 0) { |
| 7779 | return leftDifference; |
| 7780 | } |
| 7781 | } else { // RTL |
| 7782 | final int rightDifference = mLocation.right - another.mLocation.right; |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 7783 | if (rightDifference != 0) { |
| 7784 | return -rightDifference; |
| 7785 | } |
| 7786 | } |
Svetoslav | 04cab1b | 2014-08-25 18:35:57 -0700 | [diff] [blame] | 7787 | // We are ordering left-to-right, top-to-bottom. |
| 7788 | final int topDifference = mLocation.top - another.mLocation.top; |
| 7789 | if (topDifference != 0) { |
| 7790 | return topDifference; |
| 7791 | } |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 7792 | // Break tie by height. |
| 7793 | final int heightDiference = mLocation.height() - another.mLocation.height(); |
| 7794 | if (heightDiference != 0) { |
| 7795 | return -heightDiference; |
| 7796 | } |
| 7797 | // Break tie by width. |
| 7798 | final int widthDiference = mLocation.width() - another.mLocation.width(); |
| 7799 | if (widthDiference != 0) { |
| 7800 | return -widthDiference; |
| 7801 | } |
Svetoslav Ganov | 005b83b | 2012-04-16 18:17:17 -0700 | [diff] [blame] | 7802 | // Just break the tie somehow. The accessibliity ids are unique |
| 7803 | // and stable, hence this is deterministic tie breaking. |
| 7804 | return mView.getAccessibilityViewId() - another.mView.getAccessibilityViewId(); |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 7805 | } |
| 7806 | |
| 7807 | private void init(ViewGroup root, View view) { |
| 7808 | Rect viewLocation = mLocation; |
| 7809 | view.getDrawingRect(viewLocation); |
| 7810 | root.offsetDescendantRectToMyCoords(view, viewLocation); |
| 7811 | mView = view; |
Fabrice Di Meglio | e56ffdc | 2012-09-23 14:51:16 -0700 | [diff] [blame] | 7812 | mLayoutDirection = root.getLayoutDirection(); |
Svetoslav Ganov | 4213804 | 2012-03-20 11:51:39 -0700 | [diff] [blame] | 7813 | } |
| 7814 | |
| 7815 | private void clear() { |
| 7816 | mView = null; |
| 7817 | mLocation.set(0, 0, 0, 0); |
| 7818 | } |
| 7819 | } |
Romain Guy | cbc6774 | 2012-04-27 16:12:57 -0700 | [diff] [blame] | 7820 | |
| 7821 | private static Paint getDebugPaint() { |
| 7822 | if (sDebugPaint == null) { |
| 7823 | sDebugPaint = new Paint(); |
| 7824 | sDebugPaint.setAntiAlias(false); |
| 7825 | } |
| 7826 | return sDebugPaint; |
| 7827 | } |
| 7828 | |
Romain Guy | 6410c0a | 2013-06-17 11:21:58 -0700 | [diff] [blame] | 7829 | 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] | 7830 | if (sDebugLines== null) { |
Romain Guy | 6410c0a | 2013-06-17 11:21:58 -0700 | [diff] [blame] | 7831 | // 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] | 7832 | sDebugLines = new float[16]; |
| 7833 | } |
| 7834 | |
Romain Guy | cbc6774 | 2012-04-27 16:12:57 -0700 | [diff] [blame] | 7835 | sDebugLines[0] = x1; |
| 7836 | sDebugLines[1] = y1; |
| 7837 | sDebugLines[2] = x2; |
| 7838 | sDebugLines[3] = y1; |
| 7839 | |
| 7840 | sDebugLines[4] = x2; |
| 7841 | sDebugLines[5] = y1; |
| 7842 | sDebugLines[6] = x2; |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 7843 | sDebugLines[7] = y2; |
Romain Guy | cbc6774 | 2012-04-27 16:12:57 -0700 | [diff] [blame] | 7844 | |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 7845 | sDebugLines[8] = x2; |
Romain Guy | cbc6774 | 2012-04-27 16:12:57 -0700 | [diff] [blame] | 7846 | sDebugLines[9] = y2; |
| 7847 | sDebugLines[10] = x1; |
| 7848 | sDebugLines[11] = y2; |
| 7849 | |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 7850 | sDebugLines[12] = x1; |
| 7851 | sDebugLines[13] = y2; |
Romain Guy | cbc6774 | 2012-04-27 16:12:57 -0700 | [diff] [blame] | 7852 | sDebugLines[14] = x1; |
| 7853 | sDebugLines[15] = y1; |
| 7854 | |
Philip Milne | 7b75781 | 2012-09-19 18:13:44 -0700 | [diff] [blame] | 7855 | canvas.drawLines(sDebugLines, paint); |
Romain Guy | cbc6774 | 2012-04-27 16:12:57 -0700 | [diff] [blame] | 7856 | } |
Siva Velusamy | 0d857b9 | 2015-04-22 10:23:56 -0700 | [diff] [blame] | 7857 | |
| 7858 | /** @hide */ |
| 7859 | @Override |
| 7860 | protected void encodeProperties(@NonNull ViewHierarchyEncoder encoder) { |
| 7861 | super.encodeProperties(encoder); |
| 7862 | |
| 7863 | encoder.addProperty("focus:descendantFocusability", getDescendantFocusability()); |
| 7864 | encoder.addProperty("drawing:clipChildren", getClipChildren()); |
| 7865 | encoder.addProperty("drawing:clipToPadding", getClipToPadding()); |
| 7866 | encoder.addProperty("drawing:childrenDrawingOrderEnabled", isChildrenDrawingOrderEnabled()); |
| 7867 | encoder.addProperty("drawing:persistentDrawingCache", getPersistentDrawingCache()); |
| 7868 | |
| 7869 | int n = getChildCount(); |
| 7870 | encoder.addProperty("meta:__childCount__", (short)n); |
| 7871 | for (int i = 0; i < n; i++) { |
| 7872 | encoder.addPropertyKey("meta:__child__" + i); |
| 7873 | getChildAt(i).encode(encoder); |
| 7874 | } |
| 7875 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 7876 | } |