The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2006 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.view; |
| 18 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 19 | import android.animation.LayoutTransition; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | import com.android.internal.R; |
| 21 | |
| 22 | import android.content.Context; |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 23 | import android.content.res.Configuration; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | import android.content.res.TypedArray; |
| 25 | import android.graphics.Bitmap; |
| 26 | import android.graphics.Canvas; |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 27 | import android.graphics.Matrix; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 28 | import android.graphics.Paint; |
| 29 | import android.graphics.Rect; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | import android.graphics.RectF; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 31 | import android.graphics.Region; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 32 | import android.os.Parcelable; |
| 33 | import android.os.SystemClock; |
| 34 | import android.util.AttributeSet; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 35 | import android.util.Log; |
| 36 | import android.util.SparseArray; |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 37 | import android.view.accessibility.AccessibilityEvent; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | import android.view.animation.Animation; |
| 39 | import android.view.animation.AnimationUtils; |
| 40 | import android.view.animation.LayoutAnimationController; |
| 41 | import android.view.animation.Transformation; |
| 42 | |
| 43 | import java.util.ArrayList; |
| 44 | |
| 45 | /** |
| 46 | * <p> |
| 47 | * A <code>ViewGroup</code> is a special view that can contain other views |
| 48 | * (called children.) The view group is the base class for layouts and views |
| 49 | * containers. This class also defines the |
| 50 | * {@link android.view.ViewGroup.LayoutParams} class which serves as the base |
| 51 | * class for layouts parameters. |
| 52 | * </p> |
| 53 | * |
| 54 | * <p> |
| 55 | * Also see {@link LayoutParams} for layout attributes. |
| 56 | * </p> |
Romain Guy | d6a463a | 2009-05-21 23:10:10 -0700 | [diff] [blame] | 57 | * |
| 58 | * @attr ref android.R.styleable#ViewGroup_clipChildren |
| 59 | * @attr ref android.R.styleable#ViewGroup_clipToPadding |
| 60 | * @attr ref android.R.styleable#ViewGroup_layoutAnimation |
| 61 | * @attr ref android.R.styleable#ViewGroup_animationCache |
| 62 | * @attr ref android.R.styleable#ViewGroup_persistentDrawingCache |
| 63 | * @attr ref android.R.styleable#ViewGroup_alwaysDrawnWithCache |
| 64 | * @attr ref android.R.styleable#ViewGroup_addStatesFromChildren |
| 65 | * @attr ref android.R.styleable#ViewGroup_descendantFocusability |
Chet Haase | 13cc120 | 2010-09-03 15:39:20 -0700 | [diff] [blame] | 66 | * @attr ref android.R.styleable#ViewGroup_animateLayoutChanges |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | */ |
| 68 | public abstract class ViewGroup extends View implements ViewParent, ViewManager { |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 69 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | private static final boolean DBG = false; |
| 71 | |
| 72 | /** |
| 73 | * Views which have been hidden or removed which need to be animated on |
| 74 | * their way out. |
| 75 | * This field should be made private, so it is hidden from the SDK. |
| 76 | * {@hide} |
| 77 | */ |
| 78 | protected ArrayList<View> mDisappearingChildren; |
| 79 | |
| 80 | /** |
| 81 | * Listener used to propagate events indicating when children are added |
| 82 | * and/or removed from a view group. |
| 83 | * This field should be made private, so it is hidden from the SDK. |
| 84 | * {@hide} |
| 85 | */ |
| 86 | protected OnHierarchyChangeListener mOnHierarchyChangeListener; |
| 87 | |
| 88 | // The view contained within this ViewGroup that has or contains focus. |
| 89 | private View mFocused; |
| 90 | |
Chet Haase | 4846032 | 2010-06-11 14:22:25 -0700 | [diff] [blame] | 91 | /** |
| 92 | * A Transformation used when drawing children, to |
| 93 | * apply on the child being drawn. |
| 94 | */ |
| 95 | private final Transformation mChildTransformation = new Transformation(); |
| 96 | |
| 97 | /** |
| 98 | * Used to track the current invalidation region. |
| 99 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 100 | private RectF mInvalidateRegion; |
| 101 | |
Chet Haase | 4846032 | 2010-06-11 14:22:25 -0700 | [diff] [blame] | 102 | /** |
| 103 | * A Transformation used to calculate a correct |
| 104 | * invalidation area when the application is autoscaled. |
| 105 | */ |
| 106 | private Transformation mInvalidationTransformation; |
| 107 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 108 | // Target of Motion events |
| 109 | private View mMotionTarget; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 110 | |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 111 | // Targets of MotionEvents in split mode |
| 112 | private SplitMotionTargets mSplitMotionTargets; |
| 113 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 114 | // Layout animation |
| 115 | private LayoutAnimationController mLayoutAnimationController; |
| 116 | private Animation.AnimationListener mAnimationListener; |
| 117 | |
| 118 | /** |
| 119 | * Internal flags. |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 120 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 121 | * This field should be made private, so it is hidden from the SDK. |
| 122 | * {@hide} |
| 123 | */ |
| 124 | protected int mGroupFlags; |
| 125 | |
| 126 | // When set, ViewGroup invalidates only the child's rectangle |
| 127 | // Set by default |
| 128 | private static final int FLAG_CLIP_CHILDREN = 0x1; |
| 129 | |
| 130 | // When set, ViewGroup excludes the padding area from the invalidate rectangle |
| 131 | // Set by default |
| 132 | private static final int FLAG_CLIP_TO_PADDING = 0x2; |
| 133 | |
| 134 | // When set, dispatchDraw() will invoke invalidate(); this is set by drawChild() when |
| 135 | // a child needs to be invalidated and FLAG_OPTIMIZE_INVALIDATE is set |
| 136 | private static final int FLAG_INVALIDATE_REQUIRED = 0x4; |
| 137 | |
| 138 | // When set, dispatchDraw() will run the layout animation and unset the flag |
| 139 | private static final int FLAG_RUN_ANIMATION = 0x8; |
| 140 | |
| 141 | // When set, there is either no layout animation on the ViewGroup or the layout |
| 142 | // animation is over |
| 143 | // Set by default |
| 144 | private static final int FLAG_ANIMATION_DONE = 0x10; |
| 145 | |
| 146 | // If set, this ViewGroup has padding; if unset there is no padding and we don't need |
| 147 | // to clip it, even if FLAG_CLIP_TO_PADDING is set |
| 148 | private static final int FLAG_PADDING_NOT_NULL = 0x20; |
| 149 | |
| 150 | // When set, this ViewGroup caches its children in a Bitmap before starting a layout animation |
| 151 | // Set by default |
| 152 | private static final int FLAG_ANIMATION_CACHE = 0x40; |
| 153 | |
| 154 | // When set, this ViewGroup converts calls to invalidate(Rect) to invalidate() during a |
| 155 | // layout animation; this avoid clobbering the hierarchy |
| 156 | // Automatically set when the layout animation starts, depending on the animation's |
| 157 | // characteristics |
| 158 | private static final int FLAG_OPTIMIZE_INVALIDATE = 0x80; |
| 159 | |
| 160 | // When set, the next call to drawChild() will clear mChildTransformation's matrix |
| 161 | private static final int FLAG_CLEAR_TRANSFORMATION = 0x100; |
| 162 | |
| 163 | // When set, this ViewGroup invokes mAnimationListener.onAnimationEnd() and removes |
| 164 | // the children's Bitmap caches if necessary |
| 165 | // This flag is set when the layout animation is over (after FLAG_ANIMATION_DONE is set) |
| 166 | private static final int FLAG_NOTIFY_ANIMATION_LISTENER = 0x200; |
| 167 | |
| 168 | /** |
| 169 | * When set, the drawing method will call {@link #getChildDrawingOrder(int, int)} |
| 170 | * to get the index of the child to draw for that iteration. |
Romain Guy | 293451e | 2009-11-04 13:59:48 -0800 | [diff] [blame] | 171 | * |
| 172 | * @hide |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 173 | */ |
| 174 | protected static final int FLAG_USE_CHILD_DRAWING_ORDER = 0x400; |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 175 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 176 | /** |
| 177 | * When set, this ViewGroup supports static transformations on children; this causes |
| 178 | * {@link #getChildStaticTransformation(View, android.view.animation.Transformation)} to be |
| 179 | * invoked when a child is drawn. |
| 180 | * |
| 181 | * Any subclass overriding |
| 182 | * {@link #getChildStaticTransformation(View, android.view.animation.Transformation)} should |
| 183 | * set this flags in {@link #mGroupFlags}. |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 184 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 185 | * {@hide} |
| 186 | */ |
| 187 | protected static final int FLAG_SUPPORT_STATIC_TRANSFORMATIONS = 0x800; |
| 188 | |
| 189 | // When the previous drawChild() invocation used an alpha value that was lower than |
| 190 | // 1.0 and set it in mCachePaint |
| 191 | private static final int FLAG_ALPHA_LOWER_THAN_ONE = 0x1000; |
| 192 | |
| 193 | /** |
| 194 | * When set, this ViewGroup's drawable states also include those |
| 195 | * of its children. |
| 196 | */ |
| 197 | private static final int FLAG_ADD_STATES_FROM_CHILDREN = 0x2000; |
| 198 | |
| 199 | /** |
| 200 | * When set, this ViewGroup tries to always draw its children using their drawing cache. |
| 201 | */ |
| 202 | private static final int FLAG_ALWAYS_DRAWN_WITH_CACHE = 0x4000; |
| 203 | |
| 204 | /** |
| 205 | * When set, and if FLAG_ALWAYS_DRAWN_WITH_CACHE is not set, this ViewGroup will try to |
| 206 | * draw its children with their drawing cache. |
| 207 | */ |
| 208 | private static final int FLAG_CHILDREN_DRAWN_WITH_CACHE = 0x8000; |
| 209 | |
| 210 | /** |
| 211 | * When set, this group will go through its list of children to notify them of |
| 212 | * any drawable state change. |
| 213 | */ |
| 214 | private static final int FLAG_NOTIFY_CHILDREN_ON_DRAWABLE_STATE_CHANGE = 0x10000; |
| 215 | |
| 216 | private static final int FLAG_MASK_FOCUSABILITY = 0x60000; |
| 217 | |
| 218 | /** |
| 219 | * This view will get focus before any of its descendants. |
| 220 | */ |
| 221 | public static final int FOCUS_BEFORE_DESCENDANTS = 0x20000; |
| 222 | |
| 223 | /** |
| 224 | * This view will get focus only if none of its descendants want it. |
| 225 | */ |
| 226 | public static final int FOCUS_AFTER_DESCENDANTS = 0x40000; |
| 227 | |
| 228 | /** |
| 229 | * This view will block any of its descendants from getting focus, even |
| 230 | * if they are focusable. |
| 231 | */ |
| 232 | public static final int FOCUS_BLOCK_DESCENDANTS = 0x60000; |
| 233 | |
| 234 | /** |
| 235 | * Used to map between enum in attrubutes and flag values. |
| 236 | */ |
| 237 | private static final int[] DESCENDANT_FOCUSABILITY_FLAGS = |
| 238 | {FOCUS_BEFORE_DESCENDANTS, FOCUS_AFTER_DESCENDANTS, |
| 239 | FOCUS_BLOCK_DESCENDANTS}; |
| 240 | |
| 241 | /** |
| 242 | * When set, this ViewGroup should not intercept touch events. |
Adam Powell | 110486f | 2010-06-22 17:14:44 -0700 | [diff] [blame] | 243 | * {@hide} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 244 | */ |
Adam Powell | 110486f | 2010-06-22 17:14:44 -0700 | [diff] [blame] | 245 | protected static final int FLAG_DISALLOW_INTERCEPT = 0x80000; |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 246 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 247 | /** |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 248 | * When set, this ViewGroup will split MotionEvents to multiple child Views when appropriate. |
| 249 | */ |
| 250 | private static final int FLAG_SPLIT_MOTION_EVENTS = 0x100000; |
| 251 | |
| 252 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 253 | * Indicates which types of drawing caches are to be kept in memory. |
| 254 | * This field should be made private, so it is hidden from the SDK. |
| 255 | * {@hide} |
| 256 | */ |
| 257 | protected int mPersistentDrawingCache; |
| 258 | |
| 259 | /** |
| 260 | * Used to indicate that no drawing cache should be kept in memory. |
| 261 | */ |
| 262 | public static final int PERSISTENT_NO_CACHE = 0x0; |
| 263 | |
| 264 | /** |
| 265 | * Used to indicate that the animation drawing cache should be kept in memory. |
| 266 | */ |
| 267 | public static final int PERSISTENT_ANIMATION_CACHE = 0x1; |
| 268 | |
| 269 | /** |
| 270 | * Used to indicate that the scrolling drawing cache should be kept in memory. |
| 271 | */ |
| 272 | public static final int PERSISTENT_SCROLLING_CACHE = 0x2; |
| 273 | |
| 274 | /** |
| 275 | * Used to indicate that all drawing caches should be kept in memory. |
| 276 | */ |
| 277 | public static final int PERSISTENT_ALL_CACHES = 0x3; |
| 278 | |
| 279 | /** |
| 280 | * We clip to padding when FLAG_CLIP_TO_PADDING and FLAG_PADDING_NOT_NULL |
| 281 | * are set at the same time. |
| 282 | */ |
| 283 | protected static final int CLIP_TO_PADDING_MASK = FLAG_CLIP_TO_PADDING | FLAG_PADDING_NOT_NULL; |
| 284 | |
| 285 | // Index of the child's left position in the mLocation array |
| 286 | private static final int CHILD_LEFT_INDEX = 0; |
| 287 | // Index of the child's top position in the mLocation array |
| 288 | private static final int CHILD_TOP_INDEX = 1; |
| 289 | |
| 290 | // Child views of this ViewGroup |
| 291 | private View[] mChildren; |
| 292 | // Number of valid children in the mChildren array, the rest should be null or not |
| 293 | // considered as children |
| 294 | private int mChildrenCount; |
| 295 | |
| 296 | private static final int ARRAY_INITIAL_CAPACITY = 12; |
| 297 | private static final int ARRAY_CAPACITY_INCREMENT = 12; |
| 298 | |
| 299 | // Used to draw cached views |
| 300 | private final Paint mCachePaint = new Paint(); |
| 301 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 302 | // Used to animate add/remove changes in layout |
| 303 | private LayoutTransition mTransition; |
| 304 | |
| 305 | // The set of views that are currently being transitioned. This list is used to track views |
| 306 | // being removed that should not actually be removed from the parent yet because they are |
| 307 | // being animated. |
| 308 | private ArrayList<View> mTransitioningViews; |
| 309 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 310 | public ViewGroup(Context context) { |
| 311 | super(context); |
| 312 | initViewGroup(); |
| 313 | } |
| 314 | |
| 315 | public ViewGroup(Context context, AttributeSet attrs) { |
| 316 | super(context, attrs); |
| 317 | initViewGroup(); |
| 318 | initFromAttributes(context, attrs); |
| 319 | } |
| 320 | |
| 321 | public ViewGroup(Context context, AttributeSet attrs, int defStyle) { |
| 322 | super(context, attrs, defStyle); |
| 323 | initViewGroup(); |
| 324 | initFromAttributes(context, attrs); |
| 325 | } |
| 326 | |
| 327 | private void initViewGroup() { |
| 328 | // ViewGroup doesn't draw by default |
| 329 | setFlags(WILL_NOT_DRAW, DRAW_MASK); |
| 330 | mGroupFlags |= FLAG_CLIP_CHILDREN; |
| 331 | mGroupFlags |= FLAG_CLIP_TO_PADDING; |
| 332 | mGroupFlags |= FLAG_ANIMATION_DONE; |
| 333 | mGroupFlags |= FLAG_ANIMATION_CACHE; |
| 334 | mGroupFlags |= FLAG_ALWAYS_DRAWN_WITH_CACHE; |
| 335 | |
| 336 | setDescendantFocusability(FOCUS_BEFORE_DESCENDANTS); |
| 337 | |
| 338 | mChildren = new View[ARRAY_INITIAL_CAPACITY]; |
| 339 | mChildrenCount = 0; |
| 340 | |
| 341 | mCachePaint.setDither(false); |
| 342 | |
| 343 | mPersistentDrawingCache = PERSISTENT_SCROLLING_CACHE; |
| 344 | } |
| 345 | |
| 346 | private void initFromAttributes(Context context, AttributeSet attrs) { |
| 347 | TypedArray a = context.obtainStyledAttributes(attrs, |
| 348 | R.styleable.ViewGroup); |
| 349 | |
| 350 | final int N = a.getIndexCount(); |
| 351 | for (int i = 0; i < N; i++) { |
| 352 | int attr = a.getIndex(i); |
| 353 | switch (attr) { |
| 354 | case R.styleable.ViewGroup_clipChildren: |
| 355 | setClipChildren(a.getBoolean(attr, true)); |
| 356 | break; |
| 357 | case R.styleable.ViewGroup_clipToPadding: |
| 358 | setClipToPadding(a.getBoolean(attr, true)); |
| 359 | break; |
| 360 | case R.styleable.ViewGroup_animationCache: |
| 361 | setAnimationCacheEnabled(a.getBoolean(attr, true)); |
| 362 | break; |
| 363 | case R.styleable.ViewGroup_persistentDrawingCache: |
| 364 | setPersistentDrawingCache(a.getInt(attr, PERSISTENT_SCROLLING_CACHE)); |
| 365 | break; |
| 366 | case R.styleable.ViewGroup_addStatesFromChildren: |
| 367 | setAddStatesFromChildren(a.getBoolean(attr, false)); |
| 368 | break; |
| 369 | case R.styleable.ViewGroup_alwaysDrawnWithCache: |
| 370 | setAlwaysDrawnWithCacheEnabled(a.getBoolean(attr, true)); |
| 371 | break; |
| 372 | case R.styleable.ViewGroup_layoutAnimation: |
| 373 | int id = a.getResourceId(attr, -1); |
| 374 | if (id > 0) { |
| 375 | setLayoutAnimation(AnimationUtils.loadLayoutAnimation(mContext, id)); |
| 376 | } |
| 377 | break; |
| 378 | case R.styleable.ViewGroup_descendantFocusability: |
| 379 | setDescendantFocusability(DESCENDANT_FOCUSABILITY_FLAGS[a.getInt(attr, 0)]); |
| 380 | break; |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 381 | case R.styleable.ViewGroup_splitMotionEvents: |
| 382 | setMotionEventSplittingEnabled(a.getBoolean(attr, false)); |
| 383 | break; |
Chet Haase | 13cc120 | 2010-09-03 15:39:20 -0700 | [diff] [blame] | 384 | case R.styleable.ViewGroup_animateLayoutChanges: |
| 385 | boolean animateLayoutChanges = a.getBoolean(attr, false); |
| 386 | if (animateLayoutChanges) { |
| 387 | setLayoutTransition(new LayoutTransition()); |
| 388 | } |
| 389 | break; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 390 | } |
| 391 | } |
| 392 | |
| 393 | a.recycle(); |
| 394 | } |
| 395 | |
| 396 | /** |
| 397 | * Gets the descendant focusability of this view group. The descendant |
| 398 | * focusability defines the relationship between this view group and its |
| 399 | * descendants when looking for a view to take focus in |
| 400 | * {@link #requestFocus(int, android.graphics.Rect)}. |
| 401 | * |
| 402 | * @return one of {@link #FOCUS_BEFORE_DESCENDANTS}, {@link #FOCUS_AFTER_DESCENDANTS}, |
| 403 | * {@link #FOCUS_BLOCK_DESCENDANTS}. |
| 404 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 405 | @ViewDebug.ExportedProperty(category = "focus", mapping = { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 406 | @ViewDebug.IntToString(from = FOCUS_BEFORE_DESCENDANTS, to = "FOCUS_BEFORE_DESCENDANTS"), |
| 407 | @ViewDebug.IntToString(from = FOCUS_AFTER_DESCENDANTS, to = "FOCUS_AFTER_DESCENDANTS"), |
| 408 | @ViewDebug.IntToString(from = FOCUS_BLOCK_DESCENDANTS, to = "FOCUS_BLOCK_DESCENDANTS") |
| 409 | }) |
| 410 | public int getDescendantFocusability() { |
| 411 | return mGroupFlags & FLAG_MASK_FOCUSABILITY; |
| 412 | } |
| 413 | |
| 414 | /** |
| 415 | * Set the descendant focusability of this view group. This defines the relationship |
| 416 | * between this view group and its descendants when looking for a view to |
| 417 | * take focus in {@link #requestFocus(int, android.graphics.Rect)}. |
| 418 | * |
| 419 | * @param focusability one of {@link #FOCUS_BEFORE_DESCENDANTS}, {@link #FOCUS_AFTER_DESCENDANTS}, |
| 420 | * {@link #FOCUS_BLOCK_DESCENDANTS}. |
| 421 | */ |
| 422 | public void setDescendantFocusability(int focusability) { |
| 423 | switch (focusability) { |
| 424 | case FOCUS_BEFORE_DESCENDANTS: |
| 425 | case FOCUS_AFTER_DESCENDANTS: |
| 426 | case FOCUS_BLOCK_DESCENDANTS: |
| 427 | break; |
| 428 | default: |
| 429 | throw new IllegalArgumentException("must be one of FOCUS_BEFORE_DESCENDANTS, " |
| 430 | + "FOCUS_AFTER_DESCENDANTS, FOCUS_BLOCK_DESCENDANTS"); |
| 431 | } |
| 432 | mGroupFlags &= ~FLAG_MASK_FOCUSABILITY; |
| 433 | mGroupFlags |= (focusability & FLAG_MASK_FOCUSABILITY); |
| 434 | } |
| 435 | |
| 436 | /** |
| 437 | * {@inheritDoc} |
| 438 | */ |
| 439 | @Override |
| 440 | void handleFocusGainInternal(int direction, Rect previouslyFocusedRect) { |
| 441 | if (mFocused != null) { |
| 442 | mFocused.unFocus(); |
| 443 | mFocused = null; |
| 444 | } |
| 445 | super.handleFocusGainInternal(direction, previouslyFocusedRect); |
| 446 | } |
| 447 | |
| 448 | /** |
| 449 | * {@inheritDoc} |
| 450 | */ |
| 451 | public void requestChildFocus(View child, View focused) { |
| 452 | if (DBG) { |
| 453 | System.out.println(this + " requestChildFocus()"); |
| 454 | } |
| 455 | if (getDescendantFocusability() == FOCUS_BLOCK_DESCENDANTS) { |
| 456 | return; |
| 457 | } |
| 458 | |
| 459 | // Unfocus us, if necessary |
| 460 | super.unFocus(); |
| 461 | |
| 462 | // We had a previous notion of who had focus. Clear it. |
| 463 | if (mFocused != child) { |
| 464 | if (mFocused != null) { |
| 465 | mFocused.unFocus(); |
| 466 | } |
| 467 | |
| 468 | mFocused = child; |
| 469 | } |
| 470 | if (mParent != null) { |
| 471 | mParent.requestChildFocus(this, focused); |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | /** |
| 476 | * {@inheritDoc} |
| 477 | */ |
| 478 | public void focusableViewAvailable(View v) { |
| 479 | if (mParent != null |
| 480 | // shortcut: don't report a new focusable view if we block our descendants from |
| 481 | // getting focus |
| 482 | && (getDescendantFocusability() != FOCUS_BLOCK_DESCENDANTS) |
| 483 | // shortcut: don't report a new focusable view if we already are focused |
| 484 | // (and we don't prefer our descendants) |
| 485 | // |
| 486 | // note: knowing that mFocused is non-null is not a good enough reason |
| 487 | // to break the traversal since in that case we'd actually have to find |
| 488 | // the focused view and make sure it wasn't FOCUS_AFTER_DESCENDANTS and |
| 489 | // an ancestor of v; this will get checked for at ViewRoot |
| 490 | && !(isFocused() && getDescendantFocusability() != FOCUS_AFTER_DESCENDANTS)) { |
| 491 | mParent.focusableViewAvailable(v); |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | /** |
| 496 | * {@inheritDoc} |
| 497 | */ |
| 498 | public boolean showContextMenuForChild(View originalView) { |
| 499 | return mParent != null && mParent.showContextMenuForChild(originalView); |
| 500 | } |
| 501 | |
| 502 | /** |
Adam Powell | 6e34636 | 2010-07-23 10:18:23 -0700 | [diff] [blame] | 503 | * {@inheritDoc} |
| 504 | */ |
| 505 | public ActionMode startActionModeForChild(View originalView, ActionMode.Callback callback) { |
| 506 | return mParent != null ? mParent.startActionModeForChild(originalView, callback) : null; |
| 507 | } |
| 508 | |
| 509 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 510 | * Find the nearest view in the specified direction that wants to take |
| 511 | * focus. |
| 512 | * |
| 513 | * @param focused The view that currently has focus |
| 514 | * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and |
| 515 | * FOCUS_RIGHT, or 0 for not applicable. |
| 516 | */ |
| 517 | public View focusSearch(View focused, int direction) { |
| 518 | if (isRootNamespace()) { |
| 519 | // root namespace means we should consider ourselves the top of the |
| 520 | // tree for focus searching; otherwise we could be focus searching |
| 521 | // into other tabs. see LocalActivityManager and TabHost for more info |
| 522 | return FocusFinder.getInstance().findNextFocus(this, focused, direction); |
| 523 | } else if (mParent != null) { |
| 524 | return mParent.focusSearch(focused, direction); |
| 525 | } |
| 526 | return null; |
| 527 | } |
| 528 | |
| 529 | /** |
| 530 | * {@inheritDoc} |
| 531 | */ |
| 532 | public boolean requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) { |
| 533 | return false; |
| 534 | } |
| 535 | |
| 536 | /** |
| 537 | * {@inheritDoc} |
| 538 | */ |
| 539 | @Override |
| 540 | public boolean dispatchUnhandledMove(View focused, int direction) { |
| 541 | return mFocused != null && |
| 542 | mFocused.dispatchUnhandledMove(focused, direction); |
| 543 | } |
| 544 | |
| 545 | /** |
| 546 | * {@inheritDoc} |
| 547 | */ |
| 548 | public void clearChildFocus(View child) { |
| 549 | if (DBG) { |
| 550 | System.out.println(this + " clearChildFocus()"); |
| 551 | } |
| 552 | |
| 553 | mFocused = null; |
| 554 | if (mParent != null) { |
| 555 | mParent.clearChildFocus(this); |
| 556 | } |
| 557 | } |
| 558 | |
| 559 | /** |
| 560 | * {@inheritDoc} |
| 561 | */ |
| 562 | @Override |
| 563 | public void clearFocus() { |
| 564 | super.clearFocus(); |
| 565 | |
| 566 | // clear any child focus if it exists |
| 567 | if (mFocused != null) { |
| 568 | mFocused.clearFocus(); |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | /** |
| 573 | * {@inheritDoc} |
| 574 | */ |
| 575 | @Override |
| 576 | void unFocus() { |
| 577 | if (DBG) { |
| 578 | System.out.println(this + " unFocus()"); |
| 579 | } |
| 580 | |
| 581 | super.unFocus(); |
| 582 | if (mFocused != null) { |
| 583 | mFocused.unFocus(); |
| 584 | } |
| 585 | mFocused = null; |
| 586 | } |
| 587 | |
| 588 | /** |
| 589 | * Returns the focused child of this view, if any. The child may have focus |
| 590 | * or contain focus. |
| 591 | * |
| 592 | * @return the focused child or null. |
| 593 | */ |
| 594 | public View getFocusedChild() { |
| 595 | return mFocused; |
| 596 | } |
| 597 | |
| 598 | /** |
| 599 | * Returns true if this view has or contains focus |
| 600 | * |
| 601 | * @return true if this view has or contains focus |
| 602 | */ |
| 603 | @Override |
| 604 | public boolean hasFocus() { |
| 605 | return (mPrivateFlags & FOCUSED) != 0 || mFocused != null; |
| 606 | } |
| 607 | |
| 608 | /* |
| 609 | * (non-Javadoc) |
| 610 | * |
| 611 | * @see android.view.View#findFocus() |
| 612 | */ |
| 613 | @Override |
| 614 | public View findFocus() { |
| 615 | if (DBG) { |
| 616 | System.out.println("Find focus in " + this + ": flags=" |
| 617 | + isFocused() + ", child=" + mFocused); |
| 618 | } |
| 619 | |
| 620 | if (isFocused()) { |
| 621 | return this; |
| 622 | } |
| 623 | |
| 624 | if (mFocused != null) { |
| 625 | return mFocused.findFocus(); |
| 626 | } |
| 627 | return null; |
| 628 | } |
| 629 | |
| 630 | /** |
| 631 | * {@inheritDoc} |
| 632 | */ |
| 633 | @Override |
| 634 | public boolean hasFocusable() { |
| 635 | if ((mViewFlags & VISIBILITY_MASK) != VISIBLE) { |
| 636 | return false; |
| 637 | } |
| 638 | |
| 639 | if (isFocusable()) { |
| 640 | return true; |
| 641 | } |
| 642 | |
| 643 | final int descendantFocusability = getDescendantFocusability(); |
| 644 | if (descendantFocusability != FOCUS_BLOCK_DESCENDANTS) { |
| 645 | final int count = mChildrenCount; |
| 646 | final View[] children = mChildren; |
| 647 | |
| 648 | for (int i = 0; i < count; i++) { |
| 649 | final View child = children[i]; |
| 650 | if (child.hasFocusable()) { |
| 651 | return true; |
| 652 | } |
| 653 | } |
| 654 | } |
| 655 | |
| 656 | return false; |
| 657 | } |
| 658 | |
| 659 | /** |
| 660 | * {@inheritDoc} |
| 661 | */ |
| 662 | @Override |
| 663 | public void addFocusables(ArrayList<View> views, int direction) { |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 664 | addFocusables(views, direction, FOCUSABLES_TOUCH_MODE); |
| 665 | } |
| 666 | |
| 667 | /** |
| 668 | * {@inheritDoc} |
| 669 | */ |
| 670 | @Override |
| 671 | 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] | 672 | final int focusableCount = views.size(); |
| 673 | |
| 674 | final int descendantFocusability = getDescendantFocusability(); |
| 675 | |
| 676 | if (descendantFocusability != FOCUS_BLOCK_DESCENDANTS) { |
| 677 | final int count = mChildrenCount; |
| 678 | final View[] children = mChildren; |
| 679 | |
| 680 | for (int i = 0; i < count; i++) { |
| 681 | final View child = children[i]; |
| 682 | if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) { |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 683 | child.addFocusables(views, direction, focusableMode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 684 | } |
| 685 | } |
| 686 | } |
| 687 | |
| 688 | // we add ourselves (if focusable) in all cases except for when we are |
| 689 | // FOCUS_AFTER_DESCENDANTS and there are some descendants focusable. this is |
| 690 | // to avoid the focus search finding layouts when a more precise search |
| 691 | // among the focusable children would be more interesting. |
| 692 | if ( |
| 693 | descendantFocusability != FOCUS_AFTER_DESCENDANTS || |
| 694 | // No focusable descendants |
| 695 | (focusableCount == views.size())) { |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 696 | super.addFocusables(views, direction, focusableMode); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 697 | } |
| 698 | } |
| 699 | |
| 700 | /** |
| 701 | * {@inheritDoc} |
| 702 | */ |
| 703 | @Override |
| 704 | public void dispatchWindowFocusChanged(boolean hasFocus) { |
| 705 | super.dispatchWindowFocusChanged(hasFocus); |
| 706 | final int count = mChildrenCount; |
| 707 | final View[] children = mChildren; |
| 708 | for (int i = 0; i < count; i++) { |
| 709 | children[i].dispatchWindowFocusChanged(hasFocus); |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | /** |
| 714 | * {@inheritDoc} |
| 715 | */ |
| 716 | @Override |
| 717 | public void addTouchables(ArrayList<View> views) { |
| 718 | super.addTouchables(views); |
| 719 | |
| 720 | final int count = mChildrenCount; |
| 721 | final View[] children = mChildren; |
| 722 | |
| 723 | for (int i = 0; i < count; i++) { |
| 724 | final View child = children[i]; |
| 725 | if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) { |
| 726 | child.addTouchables(views); |
| 727 | } |
| 728 | } |
| 729 | } |
Romain Guy | 43c9cdf | 2010-01-27 13:53:55 -0800 | [diff] [blame] | 730 | |
| 731 | /** |
| 732 | * {@inheritDoc} |
| 733 | */ |
| 734 | @Override |
| 735 | public void dispatchDisplayHint(int hint) { |
| 736 | super.dispatchDisplayHint(hint); |
| 737 | final int count = mChildrenCount; |
| 738 | final View[] children = mChildren; |
| 739 | for (int i = 0; i < count; i++) { |
| 740 | children[i].dispatchDisplayHint(hint); |
| 741 | } |
| 742 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 743 | |
| 744 | /** |
| 745 | * {@inheritDoc} |
| 746 | */ |
| 747 | @Override |
Adam Powell | 326d808 | 2009-12-09 15:10:07 -0800 | [diff] [blame] | 748 | protected void dispatchVisibilityChanged(View changedView, int visibility) { |
| 749 | super.dispatchVisibilityChanged(changedView, visibility); |
| 750 | final int count = mChildrenCount; |
| 751 | final View[] children = mChildren; |
| 752 | for (int i = 0; i < count; i++) { |
| 753 | children[i].dispatchVisibilityChanged(changedView, visibility); |
| 754 | } |
| 755 | } |
| 756 | |
| 757 | /** |
| 758 | * {@inheritDoc} |
| 759 | */ |
| 760 | @Override |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 761 | public void dispatchWindowVisibilityChanged(int visibility) { |
| 762 | super.dispatchWindowVisibilityChanged(visibility); |
| 763 | final int count = mChildrenCount; |
| 764 | final View[] children = mChildren; |
| 765 | for (int i = 0; i < count; i++) { |
| 766 | children[i].dispatchWindowVisibilityChanged(visibility); |
| 767 | } |
| 768 | } |
| 769 | |
| 770 | /** |
| 771 | * {@inheritDoc} |
| 772 | */ |
Dianne Hackborn | e36d6e2 | 2010-02-17 19:46:25 -0800 | [diff] [blame] | 773 | @Override |
| 774 | public void dispatchConfigurationChanged(Configuration newConfig) { |
| 775 | super.dispatchConfigurationChanged(newConfig); |
| 776 | final int count = mChildrenCount; |
| 777 | final View[] children = mChildren; |
| 778 | for (int i = 0; i < count; i++) { |
| 779 | children[i].dispatchConfigurationChanged(newConfig); |
| 780 | } |
| 781 | } |
| 782 | |
| 783 | /** |
| 784 | * {@inheritDoc} |
| 785 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 786 | public void recomputeViewAttributes(View child) { |
| 787 | ViewParent parent = mParent; |
| 788 | if (parent != null) parent.recomputeViewAttributes(this); |
| 789 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 790 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 791 | @Override |
| 792 | void dispatchCollectViewAttributes(int visibility) { |
| 793 | visibility |= mViewFlags&VISIBILITY_MASK; |
| 794 | super.dispatchCollectViewAttributes(visibility); |
| 795 | final int count = mChildrenCount; |
| 796 | final View[] children = mChildren; |
| 797 | for (int i = 0; i < count; i++) { |
| 798 | children[i].dispatchCollectViewAttributes(visibility); |
| 799 | } |
| 800 | } |
| 801 | |
| 802 | /** |
| 803 | * {@inheritDoc} |
| 804 | */ |
| 805 | public void bringChildToFront(View child) { |
| 806 | int index = indexOfChild(child); |
| 807 | if (index >= 0) { |
| 808 | removeFromArray(index); |
| 809 | addInArray(child, mChildrenCount); |
| 810 | child.mParent = this; |
| 811 | } |
| 812 | } |
| 813 | |
| 814 | /** |
| 815 | * {@inheritDoc} |
| 816 | */ |
| 817 | @Override |
| 818 | public boolean dispatchKeyEventPreIme(KeyEvent event) { |
| 819 | if ((mPrivateFlags & (FOCUSED | HAS_BOUNDS)) == (FOCUSED | HAS_BOUNDS)) { |
| 820 | return super.dispatchKeyEventPreIme(event); |
| 821 | } else if (mFocused != null && (mFocused.mPrivateFlags & HAS_BOUNDS) == HAS_BOUNDS) { |
| 822 | return mFocused.dispatchKeyEventPreIme(event); |
| 823 | } |
| 824 | return false; |
| 825 | } |
| 826 | |
| 827 | /** |
| 828 | * {@inheritDoc} |
| 829 | */ |
| 830 | @Override |
| 831 | public boolean dispatchKeyEvent(KeyEvent event) { |
| 832 | if ((mPrivateFlags & (FOCUSED | HAS_BOUNDS)) == (FOCUSED | HAS_BOUNDS)) { |
| 833 | return super.dispatchKeyEvent(event); |
| 834 | } else if (mFocused != null && (mFocused.mPrivateFlags & HAS_BOUNDS) == HAS_BOUNDS) { |
| 835 | return mFocused.dispatchKeyEvent(event); |
| 836 | } |
| 837 | return false; |
| 838 | } |
| 839 | |
| 840 | /** |
| 841 | * {@inheritDoc} |
| 842 | */ |
| 843 | @Override |
| 844 | public boolean dispatchKeyShortcutEvent(KeyEvent event) { |
| 845 | if ((mPrivateFlags & (FOCUSED | HAS_BOUNDS)) == (FOCUSED | HAS_BOUNDS)) { |
| 846 | return super.dispatchKeyShortcutEvent(event); |
| 847 | } else if (mFocused != null && (mFocused.mPrivateFlags & HAS_BOUNDS) == HAS_BOUNDS) { |
| 848 | return mFocused.dispatchKeyShortcutEvent(event); |
| 849 | } |
| 850 | return false; |
| 851 | } |
| 852 | |
| 853 | /** |
| 854 | * {@inheritDoc} |
| 855 | */ |
| 856 | @Override |
| 857 | public boolean dispatchTrackballEvent(MotionEvent event) { |
| 858 | if ((mPrivateFlags & (FOCUSED | HAS_BOUNDS)) == (FOCUSED | HAS_BOUNDS)) { |
| 859 | return super.dispatchTrackballEvent(event); |
| 860 | } else if (mFocused != null && (mFocused.mPrivateFlags & HAS_BOUNDS) == HAS_BOUNDS) { |
| 861 | return mFocused.dispatchTrackballEvent(event); |
| 862 | } |
| 863 | return false; |
| 864 | } |
| 865 | |
| 866 | /** |
| 867 | * {@inheritDoc} |
| 868 | */ |
| 869 | @Override |
| 870 | public boolean dispatchTouchEvent(MotionEvent ev) { |
Jeff Brown | 85a3176 | 2010-09-01 17:01:00 -0700 | [diff] [blame] | 871 | if (!onFilterTouchEventForSecurity(ev)) { |
| 872 | return false; |
| 873 | } |
| 874 | |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 875 | if ((mGroupFlags & FLAG_SPLIT_MOTION_EVENTS) == FLAG_SPLIT_MOTION_EVENTS) { |
Adam Powell | 816c3be | 2010-08-23 18:00:05 -0700 | [diff] [blame] | 876 | if (mSplitMotionTargets == null) { |
| 877 | mSplitMotionTargets = new SplitMotionTargets(); |
| 878 | } |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 879 | return dispatchSplitTouchEvent(ev); |
Adam Powell | 167bc82 | 2010-09-13 18:11:08 -0700 | [diff] [blame^] | 880 | } |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 881 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 882 | final int action = ev.getAction(); |
| 883 | final float xf = ev.getX(); |
| 884 | final float yf = ev.getY(); |
| 885 | final float scrolledXFloat = xf + mScrollX; |
| 886 | final float scrolledYFloat = yf + mScrollY; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 887 | |
| 888 | boolean disallowIntercept = (mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0; |
| 889 | |
| 890 | if (action == MotionEvent.ACTION_DOWN) { |
| 891 | if (mMotionTarget != null) { |
| 892 | // this is weird, we got a pen down, but we thought it was |
| 893 | // already down! |
| 894 | // XXX: We should probably send an ACTION_UP to the current |
| 895 | // target. |
| 896 | mMotionTarget = null; |
| 897 | } |
| 898 | // If we're disallowing intercept or if we're allowing and we didn't |
| 899 | // intercept |
| 900 | if (disallowIntercept || !onInterceptTouchEvent(ev)) { |
| 901 | // reset this event's action (just to protect ourselves) |
| 902 | ev.setAction(MotionEvent.ACTION_DOWN); |
| 903 | // We know we want to dispatch the event down, find a child |
| 904 | // who can handle it, start with the front-most child. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 905 | final View[] children = mChildren; |
| 906 | final int count = mChildrenCount; |
| 907 | for (int i = count - 1; i >= 0; i--) { |
| 908 | final View child = children[i]; |
| 909 | if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE |
| 910 | || child.getAnimation() != null) { |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 911 | // Single dispatch always picks its target based on the initial down |
| 912 | // event's position - index 0 |
| 913 | if (dispatchTouchEventIfInView(child, ev, 0)) { |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 914 | mMotionTarget = child; |
| 915 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 916 | } |
| 917 | } |
| 918 | } |
| 919 | } |
| 920 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 921 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 922 | boolean isUpOrCancel = (action == MotionEvent.ACTION_UP) || |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 923 | (action == MotionEvent.ACTION_CANCEL); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 924 | |
| 925 | if (isUpOrCancel) { |
| 926 | // Note, we've already copied the previous state to our local |
| 927 | // variable, so this takes effect on the next event |
| 928 | mGroupFlags &= ~FLAG_DISALLOW_INTERCEPT; |
| 929 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 930 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 931 | // The event wasn't an ACTION_DOWN, dispatch it to our target if |
| 932 | // we have one. |
| 933 | final View target = mMotionTarget; |
| 934 | if (target == null) { |
| 935 | // We don't have a target, this means we're handling the |
| 936 | // event as a regular view. |
| 937 | ev.setLocation(xf, yf); |
Romain Guy | 8afa515 | 2010-02-26 11:56:30 -0800 | [diff] [blame] | 938 | if ((mPrivateFlags & CANCEL_NEXT_UP_EVENT) != 0) { |
| 939 | ev.setAction(MotionEvent.ACTION_CANCEL); |
| 940 | mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT; |
| 941 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 942 | return super.dispatchTouchEvent(ev); |
| 943 | } |
| 944 | |
Chet Haase | ddc8e71 | 2010-07-21 13:12:53 -0700 | [diff] [blame] | 945 | // Calculate the offset point into the target's local coordinates |
Chet Haase | fd2b002 | 2010-08-06 13:08:56 -0700 | [diff] [blame] | 946 | float xc = scrolledXFloat - (float) target.mLeft; |
| 947 | float yc = scrolledYFloat - (float) target.mTop; |
| 948 | if (!target.hasIdentityMatrix() && mAttachInfo != null) { |
Chet Haase | ddc8e71 | 2010-07-21 13:12:53 -0700 | [diff] [blame] | 949 | // non-identity matrix: transform the point into the view's coordinates |
| 950 | final float[] localXY = mAttachInfo.mTmpTransformLocation; |
Chet Haase | fd2b002 | 2010-08-06 13:08:56 -0700 | [diff] [blame] | 951 | localXY[0] = xc; |
| 952 | localXY[1] = yc; |
Chet Haase | ddc8e71 | 2010-07-21 13:12:53 -0700 | [diff] [blame] | 953 | target.getInverseMatrix().mapPoints(localXY); |
Chet Haase | fd2b002 | 2010-08-06 13:08:56 -0700 | [diff] [blame] | 954 | xc = localXY[0]; |
| 955 | yc = localXY[1]; |
Chet Haase | ddc8e71 | 2010-07-21 13:12:53 -0700 | [diff] [blame] | 956 | } |
| 957 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 958 | // if have a target, see if we're allowed to and want to intercept its |
| 959 | // events |
| 960 | if (!disallowIntercept && onInterceptTouchEvent(ev)) { |
Romain Guy | 8afa515 | 2010-02-26 11:56:30 -0800 | [diff] [blame] | 961 | mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 962 | ev.setAction(MotionEvent.ACTION_CANCEL); |
| 963 | ev.setLocation(xc, yc); |
| 964 | if (!target.dispatchTouchEvent(ev)) { |
| 965 | // target didn't handle ACTION_CANCEL. not much we can do |
| 966 | // but they should have. |
| 967 | } |
| 968 | // clear the target |
| 969 | mMotionTarget = null; |
| 970 | // Don't dispatch this event to our own view, because we already |
| 971 | // saw it when intercepting; we just want to give the following |
| 972 | // event to the normal onTouchEvent(). |
| 973 | return true; |
| 974 | } |
| 975 | |
| 976 | if (isUpOrCancel) { |
| 977 | mMotionTarget = null; |
| 978 | } |
| 979 | |
| 980 | // finally offset the event to the target's coordinate system and |
| 981 | // dispatch the event. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 982 | ev.setLocation(xc, yc); |
| 983 | |
Romain Guy | 8afa515 | 2010-02-26 11:56:30 -0800 | [diff] [blame] | 984 | if ((target.mPrivateFlags & CANCEL_NEXT_UP_EVENT) != 0) { |
| 985 | ev.setAction(MotionEvent.ACTION_CANCEL); |
| 986 | target.mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT; |
| 987 | mMotionTarget = null; |
| 988 | } |
| 989 | |
Adam Cohen | 9b07394 | 2010-08-19 16:49:52 -0700 | [diff] [blame] | 990 | if (target.dispatchTouchEvent(ev)) { |
| 991 | return true; |
| 992 | } else { |
| 993 | ev.setLocation(xf, yf); |
| 994 | } |
| 995 | return false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 996 | } |
| 997 | |
| 998 | /** |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 999 | * This method detects whether the pointer location at <code>pointerIndex</code> within |
| 1000 | * <code>ev</code> is inside the specified view. If so, the transformed event is dispatched to |
| 1001 | * <code>child</code>. |
| 1002 | * |
| 1003 | * @param child View to hit test against |
| 1004 | * @param ev MotionEvent to test |
| 1005 | * @param pointerIndex Index of the pointer within <code>ev</code> to test |
| 1006 | * @return <code>false</code> if the hit test failed, or the result of |
| 1007 | * <code>child.dispatchTouchEvent</code> |
| 1008 | */ |
| 1009 | private boolean dispatchTouchEventIfInView(View child, MotionEvent ev, int pointerIndex) { |
| 1010 | final float x = ev.getX(pointerIndex); |
| 1011 | final float y = ev.getY(pointerIndex); |
| 1012 | final float scrolledX = x + mScrollX; |
| 1013 | final float scrolledY = y + mScrollY; |
| 1014 | float localX = scrolledX - child.mLeft; |
| 1015 | float localY = scrolledY - child.mTop; |
| 1016 | if (!child.hasIdentityMatrix() && mAttachInfo != null) { |
| 1017 | // non-identity matrix: transform the point into the view's coordinates |
| 1018 | final float[] localXY = mAttachInfo.mTmpTransformLocation; |
| 1019 | localXY[0] = localX; |
| 1020 | localXY[1] = localY; |
| 1021 | child.getInverseMatrix().mapPoints(localXY); |
| 1022 | localX = localXY[0]; |
| 1023 | localY = localXY[1]; |
| 1024 | } |
| 1025 | if (localX >= 0 && localY >= 0 && localX < (child.mRight - child.mLeft) && |
| 1026 | localY < (child.mBottom - child.mTop)) { |
| 1027 | // It would be safer to clone the event here but we don't for performance. |
| 1028 | // There are many subtle interactions in touch event dispatch; change at your own risk. |
| 1029 | child.mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT; |
| 1030 | ev.offsetLocation(localX - x, localY - y); |
Adam Cohen | 9b07394 | 2010-08-19 16:49:52 -0700 | [diff] [blame] | 1031 | if (child.dispatchTouchEvent(ev)) { |
| 1032 | return true; |
| 1033 | } else { |
| 1034 | ev.offsetLocation(x - localX, y - localY); |
| 1035 | return false; |
| 1036 | } |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 1037 | } |
| 1038 | return false; |
| 1039 | } |
| 1040 | |
| 1041 | private boolean dispatchSplitTouchEvent(MotionEvent ev) { |
| 1042 | final SplitMotionTargets targets = mSplitMotionTargets; |
| 1043 | final int action = ev.getAction(); |
| 1044 | final int maskedAction = ev.getActionMasked(); |
| 1045 | float xf = ev.getX(); |
| 1046 | float yf = ev.getY(); |
| 1047 | float scrolledXFloat = xf + mScrollX; |
| 1048 | float scrolledYFloat = yf + mScrollY; |
| 1049 | |
| 1050 | boolean disallowIntercept = (mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0; |
| 1051 | |
| 1052 | if (maskedAction == MotionEvent.ACTION_DOWN || |
| 1053 | maskedAction == MotionEvent.ACTION_POINTER_DOWN) { |
| 1054 | final int actionIndex = ev.getActionIndex(); |
| 1055 | final int actionId = ev.getPointerId(actionIndex); |
| 1056 | |
| 1057 | // Clear out any current target for this ID. |
| 1058 | // XXX: We should probably send an ACTION_UP to the current |
| 1059 | // target if present. |
| 1060 | targets.removeById(actionId); |
| 1061 | |
| 1062 | // If we're disallowing intercept or if we're allowing and we didn't |
| 1063 | // intercept |
| 1064 | if (disallowIntercept || !onInterceptTouchEvent(ev)) { |
| 1065 | // reset this event's action (just to protect ourselves) |
| 1066 | ev.setAction(action); |
| 1067 | // We know we want to dispatch the event down, try to find a child |
| 1068 | // who can handle it, start with the front-most child. |
Adam Powell | 816c3be | 2010-08-23 18:00:05 -0700 | [diff] [blame] | 1069 | final long downTime = ev.getEventTime(); |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 1070 | final View[] children = mChildren; |
| 1071 | final int count = mChildrenCount; |
| 1072 | for (int i = count - 1; i >= 0; i--) { |
| 1073 | final View child = children[i]; |
| 1074 | if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE |
| 1075 | || child.getAnimation() != null) { |
Adam Powell | 816c3be | 2010-08-23 18:00:05 -0700 | [diff] [blame] | 1076 | final MotionEvent childEvent = |
| 1077 | targets.filterMotionEventForChild(ev, child, downTime); |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 1078 | if (childEvent != null) { |
| 1079 | try { |
| 1080 | final int childActionIndex = childEvent.findPointerIndex(actionId); |
| 1081 | if (dispatchTouchEventIfInView(child, childEvent, |
| 1082 | childActionIndex)) { |
Adam Powell | 816c3be | 2010-08-23 18:00:05 -0700 | [diff] [blame] | 1083 | targets.add(actionId, child, downTime); |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 1084 | |
| 1085 | return true; |
| 1086 | } |
| 1087 | } finally { |
| 1088 | childEvent.recycle(); |
| 1089 | } |
| 1090 | } |
| 1091 | } |
| 1092 | } |
| 1093 | |
| 1094 | // Didn't find a new target. Do we have a "primary" target to send to? |
Adam Powell | 816c3be | 2010-08-23 18:00:05 -0700 | [diff] [blame] | 1095 | final SplitMotionTargets.TargetInfo primaryTargetInfo = targets.getPrimaryTarget(); |
| 1096 | if (primaryTargetInfo != null) { |
| 1097 | final View primaryTarget = primaryTargetInfo.view; |
| 1098 | final MotionEvent childEvent = targets.filterMotionEventForChild(ev, |
| 1099 | primaryTarget, primaryTargetInfo.downTime); |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 1100 | if (childEvent != null) { |
| 1101 | try { |
| 1102 | // Calculate the offset point into the target's local coordinates |
| 1103 | float xc = scrolledXFloat - (float) primaryTarget.mLeft; |
| 1104 | float yc = scrolledYFloat - (float) primaryTarget.mTop; |
| 1105 | if (!primaryTarget.hasIdentityMatrix() && mAttachInfo != null) { |
| 1106 | // non-identity matrix: transform the point into the view's |
| 1107 | // coordinates |
| 1108 | final float[] localXY = mAttachInfo.mTmpTransformLocation; |
| 1109 | localXY[0] = xc; |
| 1110 | localXY[1] = yc; |
| 1111 | primaryTarget.getInverseMatrix().mapPoints(localXY); |
| 1112 | xc = localXY[0]; |
| 1113 | yc = localXY[1]; |
| 1114 | } |
| 1115 | childEvent.setLocation(xc, yc); |
| 1116 | if (primaryTarget.dispatchTouchEvent(childEvent)) { |
Adam Powell | 816c3be | 2010-08-23 18:00:05 -0700 | [diff] [blame] | 1117 | targets.add(actionId, primaryTarget, primaryTargetInfo.downTime); |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 1118 | return true; |
| 1119 | } |
| 1120 | } finally { |
| 1121 | childEvent.recycle(); |
| 1122 | } |
| 1123 | } |
| 1124 | } |
| 1125 | } |
| 1126 | } |
| 1127 | |
| 1128 | boolean isUpOrCancel = (action == MotionEvent.ACTION_UP) || |
| 1129 | (action == MotionEvent.ACTION_CANCEL); |
| 1130 | |
| 1131 | if (isUpOrCancel) { |
| 1132 | // Note, we've already copied the previous state to our local |
| 1133 | // variable, so this takes effect on the next event |
| 1134 | mGroupFlags &= ~FLAG_DISALLOW_INTERCEPT; |
| 1135 | } |
| 1136 | |
| 1137 | if (targets.isEmpty()) { |
| 1138 | // We don't have any targets, this means we're handling the |
| 1139 | // event as a regular view. |
| 1140 | ev.setLocation(xf, yf); |
| 1141 | if ((mPrivateFlags & CANCEL_NEXT_UP_EVENT) != 0) { |
| 1142 | ev.setAction(MotionEvent.ACTION_CANCEL); |
| 1143 | mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT; |
| 1144 | } |
| 1145 | return super.dispatchTouchEvent(ev); |
| 1146 | } |
| 1147 | |
| 1148 | // if we have targets, see if we're allowed to and want to intercept their |
| 1149 | // events |
| 1150 | int uniqueTargetCount = targets.getUniqueTargetCount(); |
| 1151 | if (!disallowIntercept && onInterceptTouchEvent(ev)) { |
| 1152 | mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT; |
| 1153 | |
| 1154 | for (int uniqueIndex = 0; uniqueIndex < uniqueTargetCount; uniqueIndex++) { |
Adam Powell | 816c3be | 2010-08-23 18:00:05 -0700 | [diff] [blame] | 1155 | final View target = targets.getUniqueTargetAt(uniqueIndex).view; |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 1156 | |
| 1157 | // Calculate the offset point into the target's local coordinates |
| 1158 | float xc = scrolledXFloat - (float) target.mLeft; |
| 1159 | float yc = scrolledYFloat - (float) target.mTop; |
| 1160 | if (!target.hasIdentityMatrix() && mAttachInfo != null) { |
| 1161 | // non-identity matrix: transform the point into the view's coordinates |
| 1162 | final float[] localXY = mAttachInfo.mTmpTransformLocation; |
| 1163 | localXY[0] = xc; |
| 1164 | localXY[1] = yc; |
| 1165 | target.getInverseMatrix().mapPoints(localXY); |
| 1166 | xc = localXY[0]; |
| 1167 | yc = localXY[1]; |
| 1168 | } |
| 1169 | |
| 1170 | ev.setAction(MotionEvent.ACTION_CANCEL); |
| 1171 | ev.setLocation(xc, yc); |
| 1172 | if (!target.dispatchTouchEvent(ev)) { |
| 1173 | // target didn't handle ACTION_CANCEL. not much we can do |
| 1174 | // but they should have. |
| 1175 | } |
| 1176 | } |
| 1177 | targets.clear(); |
| 1178 | // Don't dispatch this event to our own view, because we already |
| 1179 | // saw it when intercepting; we just want to give the following |
| 1180 | // event to the normal onTouchEvent(). |
| 1181 | return true; |
| 1182 | } |
| 1183 | |
| 1184 | boolean handled = false; |
| 1185 | for (int uniqueIndex = 0; uniqueIndex < uniqueTargetCount; uniqueIndex++) { |
Adam Powell | 816c3be | 2010-08-23 18:00:05 -0700 | [diff] [blame] | 1186 | final SplitMotionTargets.TargetInfo targetInfo = targets.getUniqueTargetAt(uniqueIndex); |
| 1187 | final View target = targetInfo.view; |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 1188 | |
Adam Powell | 816c3be | 2010-08-23 18:00:05 -0700 | [diff] [blame] | 1189 | final MotionEvent targetEvent = |
| 1190 | targets.filterMotionEventForChild(ev, target, targetInfo.downTime); |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 1191 | if (targetEvent == null) { |
| 1192 | continue; |
| 1193 | } |
| 1194 | |
| 1195 | try { |
| 1196 | // Calculate the offset point into the target's local coordinates |
| 1197 | xf = targetEvent.getX(); |
| 1198 | yf = targetEvent.getY(); |
| 1199 | scrolledXFloat = xf + mScrollX; |
| 1200 | scrolledYFloat = yf + mScrollY; |
| 1201 | float xc = scrolledXFloat - (float) target.mLeft; |
| 1202 | float yc = scrolledYFloat - (float) target.mTop; |
| 1203 | if (!target.hasIdentityMatrix() && mAttachInfo != null) { |
| 1204 | // non-identity matrix: transform the point into the view's coordinates |
| 1205 | final float[] localXY = mAttachInfo.mTmpTransformLocation; |
| 1206 | localXY[0] = xc; |
| 1207 | localXY[1] = yc; |
| 1208 | target.getInverseMatrix().mapPoints(localXY); |
| 1209 | xc = localXY[0]; |
| 1210 | yc = localXY[1]; |
| 1211 | } |
| 1212 | |
| 1213 | // finally offset the event to the target's coordinate system and |
| 1214 | // dispatch the event. |
| 1215 | targetEvent.setLocation(xc, yc); |
| 1216 | |
| 1217 | if ((target.mPrivateFlags & CANCEL_NEXT_UP_EVENT) != 0) { |
| 1218 | targetEvent.setAction(MotionEvent.ACTION_CANCEL); |
| 1219 | target.mPrivateFlags &= ~CANCEL_NEXT_UP_EVENT; |
| 1220 | targets.removeView(target); |
| 1221 | uniqueIndex--; |
| 1222 | uniqueTargetCount--; |
| 1223 | } |
| 1224 | |
Adam Powell | 167bc82 | 2010-09-13 18:11:08 -0700 | [diff] [blame^] | 1225 | handled |= target.dispatchTouchEvent(targetEvent); |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 1226 | } finally { |
| 1227 | targetEvent.recycle(); |
| 1228 | } |
| 1229 | } |
| 1230 | |
| 1231 | if (maskedAction == MotionEvent.ACTION_POINTER_UP) { |
| 1232 | final int removeId = ev.getPointerId(ev.getActionIndex()); |
| 1233 | targets.removeById(removeId); |
| 1234 | } |
| 1235 | |
| 1236 | if (isUpOrCancel) { |
| 1237 | targets.clear(); |
| 1238 | } |
| 1239 | |
| 1240 | return handled; |
| 1241 | } |
| 1242 | |
| 1243 | /** |
| 1244 | * Enable or disable the splitting of MotionEvents to multiple children during touch event |
| 1245 | * dispatch. This behavior is disabled by default. |
| 1246 | * |
| 1247 | * <p>When this option is enabled MotionEvents may be split and dispatched to different child |
| 1248 | * views depending on where each pointer initially went down. This allows for user interactions |
| 1249 | * such as scrolling two panes of content independently, chording of buttons, and performing |
| 1250 | * independent gestures on different pieces of content. |
| 1251 | * |
| 1252 | * @param split <code>true</code> to allow MotionEvents to be split and dispatched to multiple |
| 1253 | * child views. <code>false</code> to only allow one child view to be the target of |
| 1254 | * any MotionEvent received by this ViewGroup. |
| 1255 | */ |
| 1256 | public void setMotionEventSplittingEnabled(boolean split) { |
| 1257 | // TODO Applications really shouldn't change this setting mid-touch event, |
| 1258 | // but perhaps this should handle that case and send ACTION_CANCELs to any child views |
| 1259 | // with gestures in progress when this is changed. |
| 1260 | if (split) { |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 1261 | mGroupFlags |= FLAG_SPLIT_MOTION_EVENTS; |
| 1262 | } else { |
| 1263 | mGroupFlags &= ~FLAG_SPLIT_MOTION_EVENTS; |
| 1264 | mSplitMotionTargets = null; |
| 1265 | } |
| 1266 | } |
| 1267 | |
| 1268 | /** |
| 1269 | * @return true if MotionEvents dispatched to this ViewGroup can be split to multiple children. |
| 1270 | */ |
| 1271 | public boolean isMotionEventSplittingEnabled() { |
| 1272 | return (mGroupFlags & FLAG_SPLIT_MOTION_EVENTS) == FLAG_SPLIT_MOTION_EVENTS; |
| 1273 | } |
| 1274 | |
| 1275 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1276 | * {@inheritDoc} |
| 1277 | */ |
| 1278 | public void requestDisallowInterceptTouchEvent(boolean disallowIntercept) { |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 1279 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1280 | if (disallowIntercept == ((mGroupFlags & FLAG_DISALLOW_INTERCEPT) != 0)) { |
| 1281 | // We're already in this state, assume our ancestors are too |
| 1282 | return; |
| 1283 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 1284 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1285 | if (disallowIntercept) { |
| 1286 | mGroupFlags |= FLAG_DISALLOW_INTERCEPT; |
| 1287 | } else { |
| 1288 | mGroupFlags &= ~FLAG_DISALLOW_INTERCEPT; |
| 1289 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 1290 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1291 | // Pass it up to our parent |
| 1292 | if (mParent != null) { |
| 1293 | mParent.requestDisallowInterceptTouchEvent(disallowIntercept); |
| 1294 | } |
| 1295 | } |
| 1296 | |
| 1297 | /** |
| 1298 | * Implement this method to intercept all touch screen motion events. This |
| 1299 | * allows you to watch events as they are dispatched to your children, and |
| 1300 | * take ownership of the current gesture at any point. |
| 1301 | * |
| 1302 | * <p>Using this function takes some care, as it has a fairly complicated |
| 1303 | * interaction with {@link View#onTouchEvent(MotionEvent) |
| 1304 | * View.onTouchEvent(MotionEvent)}, and using it requires implementing |
| 1305 | * that method as well as this one in the correct way. Events will be |
| 1306 | * received in the following order: |
| 1307 | * |
| 1308 | * <ol> |
| 1309 | * <li> You will receive the down event here. |
| 1310 | * <li> The down event will be handled either by a child of this view |
| 1311 | * group, or given to your own onTouchEvent() method to handle; this means |
| 1312 | * you should implement onTouchEvent() to return true, so you will |
| 1313 | * continue to see the rest of the gesture (instead of looking for |
| 1314 | * a parent view to handle it). Also, by returning true from |
| 1315 | * onTouchEvent(), you will not receive any following |
| 1316 | * events in onInterceptTouchEvent() and all touch processing must |
| 1317 | * happen in onTouchEvent() like normal. |
| 1318 | * <li> For as long as you return false from this function, each following |
| 1319 | * event (up to and including the final up) will be delivered first here |
| 1320 | * and then to the target's onTouchEvent(). |
| 1321 | * <li> If you return true from here, you will not receive any |
| 1322 | * following events: the target view will receive the same event but |
| 1323 | * with the action {@link MotionEvent#ACTION_CANCEL}, and all further |
| 1324 | * events will be delivered to your onTouchEvent() method and no longer |
| 1325 | * appear here. |
| 1326 | * </ol> |
| 1327 | * |
| 1328 | * @param ev The motion event being dispatched down the hierarchy. |
| 1329 | * @return Return true to steal motion events from the children and have |
| 1330 | * them dispatched to this ViewGroup through onTouchEvent(). |
| 1331 | * The current target will receive an ACTION_CANCEL event, and no further |
| 1332 | * messages will be delivered here. |
| 1333 | */ |
| 1334 | public boolean onInterceptTouchEvent(MotionEvent ev) { |
| 1335 | return false; |
| 1336 | } |
| 1337 | |
| 1338 | /** |
| 1339 | * {@inheritDoc} |
| 1340 | * |
| 1341 | * Looks for a view to give focus to respecting the setting specified by |
| 1342 | * {@link #getDescendantFocusability()}. |
| 1343 | * |
| 1344 | * Uses {@link #onRequestFocusInDescendants(int, android.graphics.Rect)} to |
| 1345 | * find focus within the children of this group when appropriate. |
| 1346 | * |
| 1347 | * @see #FOCUS_BEFORE_DESCENDANTS |
| 1348 | * @see #FOCUS_AFTER_DESCENDANTS |
| 1349 | * @see #FOCUS_BLOCK_DESCENDANTS |
| 1350 | * @see #onRequestFocusInDescendants |
| 1351 | */ |
| 1352 | @Override |
| 1353 | public boolean requestFocus(int direction, Rect previouslyFocusedRect) { |
| 1354 | if (DBG) { |
| 1355 | System.out.println(this + " ViewGroup.requestFocus direction=" |
| 1356 | + direction); |
| 1357 | } |
| 1358 | int descendantFocusability = getDescendantFocusability(); |
| 1359 | |
| 1360 | switch (descendantFocusability) { |
| 1361 | case FOCUS_BLOCK_DESCENDANTS: |
| 1362 | return super.requestFocus(direction, previouslyFocusedRect); |
| 1363 | case FOCUS_BEFORE_DESCENDANTS: { |
| 1364 | final boolean took = super.requestFocus(direction, previouslyFocusedRect); |
| 1365 | return took ? took : onRequestFocusInDescendants(direction, previouslyFocusedRect); |
| 1366 | } |
| 1367 | case FOCUS_AFTER_DESCENDANTS: { |
| 1368 | final boolean took = onRequestFocusInDescendants(direction, previouslyFocusedRect); |
| 1369 | return took ? took : super.requestFocus(direction, previouslyFocusedRect); |
| 1370 | } |
| 1371 | default: |
| 1372 | throw new IllegalStateException("descendant focusability must be " |
| 1373 | + "one of FOCUS_BEFORE_DESCENDANTS, FOCUS_AFTER_DESCENDANTS, FOCUS_BLOCK_DESCENDANTS " |
| 1374 | + "but is " + descendantFocusability); |
| 1375 | } |
| 1376 | } |
| 1377 | |
| 1378 | /** |
| 1379 | * Look for a descendant to call {@link View#requestFocus} on. |
| 1380 | * Called by {@link ViewGroup#requestFocus(int, android.graphics.Rect)} |
| 1381 | * when it wants to request focus within its children. Override this to |
| 1382 | * customize how your {@link ViewGroup} requests focus within its children. |
| 1383 | * @param direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT |
| 1384 | * @param previouslyFocusedRect The rectangle (in this View's coordinate system) |
| 1385 | * to give a finer grained hint about where focus is coming from. May be null |
| 1386 | * if there is no hint. |
| 1387 | * @return Whether focus was taken. |
| 1388 | */ |
| 1389 | @SuppressWarnings({"ConstantConditions"}) |
| 1390 | protected boolean onRequestFocusInDescendants(int direction, |
| 1391 | Rect previouslyFocusedRect) { |
| 1392 | int index; |
| 1393 | int increment; |
| 1394 | int end; |
| 1395 | int count = mChildrenCount; |
| 1396 | if ((direction & FOCUS_FORWARD) != 0) { |
| 1397 | index = 0; |
| 1398 | increment = 1; |
| 1399 | end = count; |
| 1400 | } else { |
| 1401 | index = count - 1; |
| 1402 | increment = -1; |
| 1403 | end = -1; |
| 1404 | } |
| 1405 | final View[] children = mChildren; |
| 1406 | for (int i = index; i != end; i += increment) { |
| 1407 | View child = children[i]; |
| 1408 | if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) { |
| 1409 | if (child.requestFocus(direction, previouslyFocusedRect)) { |
| 1410 | return true; |
| 1411 | } |
| 1412 | } |
| 1413 | } |
| 1414 | return false; |
| 1415 | } |
Romain Guy | a440b00 | 2010-02-24 15:57:54 -0800 | [diff] [blame] | 1416 | |
| 1417 | /** |
| 1418 | * {@inheritDoc} |
Romain Guy | dcc490f | 2010-02-24 17:59:35 -0800 | [diff] [blame] | 1419 | * |
| 1420 | * @hide |
Romain Guy | a440b00 | 2010-02-24 15:57:54 -0800 | [diff] [blame] | 1421 | */ |
| 1422 | @Override |
| 1423 | public void dispatchStartTemporaryDetach() { |
| 1424 | super.dispatchStartTemporaryDetach(); |
| 1425 | final int count = mChildrenCount; |
| 1426 | final View[] children = mChildren; |
| 1427 | for (int i = 0; i < count; i++) { |
| 1428 | children[i].dispatchStartTemporaryDetach(); |
| 1429 | } |
| 1430 | } |
| 1431 | |
| 1432 | /** |
| 1433 | * {@inheritDoc} |
Romain Guy | dcc490f | 2010-02-24 17:59:35 -0800 | [diff] [blame] | 1434 | * |
| 1435 | * @hide |
Romain Guy | a440b00 | 2010-02-24 15:57:54 -0800 | [diff] [blame] | 1436 | */ |
| 1437 | @Override |
| 1438 | public void dispatchFinishTemporaryDetach() { |
| 1439 | super.dispatchFinishTemporaryDetach(); |
| 1440 | final int count = mChildrenCount; |
| 1441 | final View[] children = mChildren; |
| 1442 | for (int i = 0; i < count; i++) { |
| 1443 | children[i].dispatchFinishTemporaryDetach(); |
| 1444 | } |
| 1445 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1446 | |
| 1447 | /** |
| 1448 | * {@inheritDoc} |
| 1449 | */ |
| 1450 | @Override |
| 1451 | void dispatchAttachedToWindow(AttachInfo info, int visibility) { |
| 1452 | super.dispatchAttachedToWindow(info, visibility); |
| 1453 | visibility |= mViewFlags & VISIBILITY_MASK; |
| 1454 | final int count = mChildrenCount; |
| 1455 | final View[] children = mChildren; |
| 1456 | for (int i = 0; i < count; i++) { |
| 1457 | children[i].dispatchAttachedToWindow(info, visibility); |
| 1458 | } |
| 1459 | } |
| 1460 | |
svetoslavganov | 75986cf | 2009-05-14 22:28:01 -0700 | [diff] [blame] | 1461 | @Override |
| 1462 | public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) { |
| 1463 | boolean populated = false; |
| 1464 | for (int i = 0, count = getChildCount(); i < count; i++) { |
| 1465 | populated |= getChildAt(i).dispatchPopulateAccessibilityEvent(event); |
| 1466 | } |
| 1467 | return populated; |
| 1468 | } |
| 1469 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1470 | /** |
| 1471 | * {@inheritDoc} |
| 1472 | */ |
| 1473 | @Override |
| 1474 | void dispatchDetachedFromWindow() { |
| 1475 | // If we still have a motion target, we are still in the process of |
| 1476 | // dispatching motion events to a child; we need to get rid of that |
| 1477 | // child to avoid dispatching events to it after the window is torn |
| 1478 | // down. To make sure we keep the child in a consistent state, we |
| 1479 | // first send it an ACTION_CANCEL motion event. |
| 1480 | if (mMotionTarget != null) { |
| 1481 | final long now = SystemClock.uptimeMillis(); |
| 1482 | final MotionEvent event = MotionEvent.obtain(now, now, |
| 1483 | MotionEvent.ACTION_CANCEL, 0.0f, 0.0f, 0); |
| 1484 | mMotionTarget.dispatchTouchEvent(event); |
| 1485 | event.recycle(); |
| 1486 | mMotionTarget = null; |
| 1487 | } |
| 1488 | |
| 1489 | final int count = mChildrenCount; |
| 1490 | final View[] children = mChildren; |
| 1491 | for (int i = 0; i < count; i++) { |
| 1492 | children[i].dispatchDetachedFromWindow(); |
| 1493 | } |
| 1494 | super.dispatchDetachedFromWindow(); |
| 1495 | } |
| 1496 | |
| 1497 | /** |
| 1498 | * {@inheritDoc} |
| 1499 | */ |
| 1500 | @Override |
| 1501 | public void setPadding(int left, int top, int right, int bottom) { |
| 1502 | super.setPadding(left, top, right, bottom); |
| 1503 | |
| 1504 | if ((mPaddingLeft | mPaddingTop | mPaddingRight | mPaddingRight) != 0) { |
| 1505 | mGroupFlags |= FLAG_PADDING_NOT_NULL; |
| 1506 | } else { |
| 1507 | mGroupFlags &= ~FLAG_PADDING_NOT_NULL; |
| 1508 | } |
| 1509 | } |
| 1510 | |
| 1511 | /** |
| 1512 | * {@inheritDoc} |
| 1513 | */ |
| 1514 | @Override |
| 1515 | protected void dispatchSaveInstanceState(SparseArray<Parcelable> container) { |
| 1516 | super.dispatchSaveInstanceState(container); |
| 1517 | final int count = mChildrenCount; |
| 1518 | final View[] children = mChildren; |
| 1519 | for (int i = 0; i < count; i++) { |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1520 | View c = children[i]; |
| 1521 | if ((c.mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED) { |
| 1522 | c.dispatchSaveInstanceState(container); |
| 1523 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1524 | } |
| 1525 | } |
| 1526 | |
| 1527 | /** |
| 1528 | * Perform dispatching of a {@link #saveHierarchyState freeze()} to only this view, |
| 1529 | * not to its children. For use when overriding |
| 1530 | * {@link #dispatchSaveInstanceState dispatchFreeze()} to allow subclasses to freeze |
| 1531 | * their own state but not the state of their children. |
| 1532 | * |
| 1533 | * @param container the container |
| 1534 | */ |
| 1535 | protected void dispatchFreezeSelfOnly(SparseArray<Parcelable> container) { |
| 1536 | super.dispatchSaveInstanceState(container); |
| 1537 | } |
| 1538 | |
| 1539 | /** |
| 1540 | * {@inheritDoc} |
| 1541 | */ |
| 1542 | @Override |
| 1543 | protected void dispatchRestoreInstanceState(SparseArray<Parcelable> container) { |
| 1544 | super.dispatchRestoreInstanceState(container); |
| 1545 | final int count = mChildrenCount; |
| 1546 | final View[] children = mChildren; |
| 1547 | for (int i = 0; i < count; i++) { |
Dianne Hackborn | b4bc78b | 2010-05-12 18:59:50 -0700 | [diff] [blame] | 1548 | View c = children[i]; |
| 1549 | if ((c.mViewFlags & PARENT_SAVE_DISABLED_MASK) != PARENT_SAVE_DISABLED) { |
| 1550 | c.dispatchRestoreInstanceState(container); |
| 1551 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1552 | } |
| 1553 | } |
| 1554 | |
| 1555 | /** |
| 1556 | * Perform dispatching of a {@link #restoreHierarchyState thaw()} to only this view, |
| 1557 | * not to its children. For use when overriding |
| 1558 | * {@link #dispatchRestoreInstanceState dispatchThaw()} to allow subclasses to thaw |
| 1559 | * their own state but not the state of their children. |
| 1560 | * |
| 1561 | * @param container the container |
| 1562 | */ |
| 1563 | protected void dispatchThawSelfOnly(SparseArray<Parcelable> container) { |
| 1564 | super.dispatchRestoreInstanceState(container); |
| 1565 | } |
| 1566 | |
| 1567 | /** |
| 1568 | * Enables or disables the drawing cache for each child of this view group. |
| 1569 | * |
| 1570 | * @param enabled true to enable the cache, false to dispose of it |
| 1571 | */ |
| 1572 | protected void setChildrenDrawingCacheEnabled(boolean enabled) { |
| 1573 | if (enabled || (mPersistentDrawingCache & PERSISTENT_ALL_CACHES) != PERSISTENT_ALL_CACHES) { |
| 1574 | final View[] children = mChildren; |
| 1575 | final int count = mChildrenCount; |
| 1576 | for (int i = 0; i < count; i++) { |
| 1577 | children[i].setDrawingCacheEnabled(enabled); |
| 1578 | } |
| 1579 | } |
| 1580 | } |
| 1581 | |
| 1582 | @Override |
| 1583 | protected void onAnimationStart() { |
| 1584 | super.onAnimationStart(); |
| 1585 | |
| 1586 | // When this ViewGroup's animation starts, build the cache for the children |
| 1587 | if ((mGroupFlags & FLAG_ANIMATION_CACHE) == FLAG_ANIMATION_CACHE) { |
| 1588 | final int count = mChildrenCount; |
| 1589 | final View[] children = mChildren; |
| 1590 | |
| 1591 | for (int i = 0; i < count; i++) { |
| 1592 | final View child = children[i]; |
| 1593 | if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) { |
| 1594 | child.setDrawingCacheEnabled(true); |
Romain Guy | fbd8f69 | 2009-06-26 14:51:58 -0700 | [diff] [blame] | 1595 | child.buildDrawingCache(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1596 | } |
| 1597 | } |
| 1598 | |
| 1599 | mGroupFlags |= FLAG_CHILDREN_DRAWN_WITH_CACHE; |
| 1600 | } |
| 1601 | } |
| 1602 | |
| 1603 | @Override |
| 1604 | protected void onAnimationEnd() { |
| 1605 | super.onAnimationEnd(); |
| 1606 | |
| 1607 | // When this ViewGroup's animation ends, destroy the cache of the children |
| 1608 | if ((mGroupFlags & FLAG_ANIMATION_CACHE) == FLAG_ANIMATION_CACHE) { |
| 1609 | mGroupFlags &= ~FLAG_CHILDREN_DRAWN_WITH_CACHE; |
| 1610 | |
| 1611 | if ((mPersistentDrawingCache & PERSISTENT_ANIMATION_CACHE) == 0) { |
| 1612 | setChildrenDrawingCacheEnabled(false); |
| 1613 | } |
| 1614 | } |
| 1615 | } |
| 1616 | |
Romain Guy | 223ff5c | 2010-03-02 17:07:47 -0800 | [diff] [blame] | 1617 | @Override |
| 1618 | Bitmap createSnapshot(Bitmap.Config quality, int backgroundColor, boolean skipChildren) { |
Romain Guy | 65554f2 | 2010-03-22 18:58:21 -0700 | [diff] [blame] | 1619 | int count = mChildrenCount; |
| 1620 | int[] visibilities = null; |
| 1621 | |
Romain Guy | 223ff5c | 2010-03-02 17:07:47 -0800 | [diff] [blame] | 1622 | if (skipChildren) { |
Romain Guy | 65554f2 | 2010-03-22 18:58:21 -0700 | [diff] [blame] | 1623 | visibilities = new int[count]; |
| 1624 | for (int i = 0; i < count; i++) { |
| 1625 | View child = getChildAt(i); |
| 1626 | visibilities[i] = child.getVisibility(); |
| 1627 | if (visibilities[i] == View.VISIBLE) { |
| 1628 | child.setVisibility(INVISIBLE); |
| 1629 | } |
| 1630 | } |
Romain Guy | 223ff5c | 2010-03-02 17:07:47 -0800 | [diff] [blame] | 1631 | } |
| 1632 | |
| 1633 | Bitmap b = super.createSnapshot(quality, backgroundColor, skipChildren); |
Romain Guy | 65554f2 | 2010-03-22 18:58:21 -0700 | [diff] [blame] | 1634 | |
| 1635 | if (skipChildren) { |
| 1636 | for (int i = 0; i < count; i++) { |
| 1637 | getChildAt(i).setVisibility(visibilities[i]); |
| 1638 | } |
| 1639 | } |
Romain Guy | 223ff5c | 2010-03-02 17:07:47 -0800 | [diff] [blame] | 1640 | |
| 1641 | return b; |
| 1642 | } |
| 1643 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1644 | /** |
| 1645 | * {@inheritDoc} |
| 1646 | */ |
| 1647 | @Override |
| 1648 | protected void dispatchDraw(Canvas canvas) { |
| 1649 | final int count = mChildrenCount; |
| 1650 | final View[] children = mChildren; |
| 1651 | int flags = mGroupFlags; |
| 1652 | |
| 1653 | if ((flags & FLAG_RUN_ANIMATION) != 0 && canAnimate()) { |
| 1654 | final boolean cache = (mGroupFlags & FLAG_ANIMATION_CACHE) == FLAG_ANIMATION_CACHE; |
| 1655 | |
| 1656 | for (int i = 0; i < count; i++) { |
| 1657 | final View child = children[i]; |
| 1658 | if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE) { |
| 1659 | final LayoutParams params = child.getLayoutParams(); |
| 1660 | attachLayoutAnimationParameters(child, params, i, count); |
| 1661 | bindLayoutAnimation(child); |
| 1662 | if (cache) { |
| 1663 | child.setDrawingCacheEnabled(true); |
Romain Guy | fbd8f69 | 2009-06-26 14:51:58 -0700 | [diff] [blame] | 1664 | child.buildDrawingCache(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1665 | } |
| 1666 | } |
| 1667 | } |
| 1668 | |
| 1669 | final LayoutAnimationController controller = mLayoutAnimationController; |
| 1670 | if (controller.willOverlap()) { |
| 1671 | mGroupFlags |= FLAG_OPTIMIZE_INVALIDATE; |
| 1672 | } |
| 1673 | |
| 1674 | controller.start(); |
| 1675 | |
| 1676 | mGroupFlags &= ~FLAG_RUN_ANIMATION; |
| 1677 | mGroupFlags &= ~FLAG_ANIMATION_DONE; |
| 1678 | |
| 1679 | if (cache) { |
| 1680 | mGroupFlags |= FLAG_CHILDREN_DRAWN_WITH_CACHE; |
| 1681 | } |
| 1682 | |
| 1683 | if (mAnimationListener != null) { |
| 1684 | mAnimationListener.onAnimationStart(controller.getAnimation()); |
| 1685 | } |
| 1686 | } |
| 1687 | |
| 1688 | int saveCount = 0; |
| 1689 | final boolean clipToPadding = (flags & CLIP_TO_PADDING_MASK) == CLIP_TO_PADDING_MASK; |
| 1690 | if (clipToPadding) { |
| 1691 | saveCount = canvas.save(); |
Romain Guy | 8f2d94f | 2009-03-25 18:04:42 -0700 | [diff] [blame] | 1692 | canvas.clipRect(mScrollX + mPaddingLeft, mScrollY + mPaddingTop, |
| 1693 | mScrollX + mRight - mLeft - mPaddingRight, |
| 1694 | mScrollY + mBottom - mTop - mPaddingBottom); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1695 | |
| 1696 | } |
| 1697 | |
| 1698 | // We will draw our child's animation, let's reset the flag |
| 1699 | mPrivateFlags &= ~DRAW_ANIMATION; |
| 1700 | mGroupFlags &= ~FLAG_INVALIDATE_REQUIRED; |
| 1701 | |
| 1702 | boolean more = false; |
| 1703 | final long drawingTime = getDrawingTime(); |
| 1704 | |
| 1705 | if ((flags & FLAG_USE_CHILD_DRAWING_ORDER) == 0) { |
| 1706 | for (int i = 0; i < count; i++) { |
| 1707 | final View child = children[i]; |
| 1708 | if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null) { |
| 1709 | more |= drawChild(canvas, child, drawingTime); |
| 1710 | } |
| 1711 | } |
| 1712 | } else { |
| 1713 | for (int i = 0; i < count; i++) { |
| 1714 | final View child = children[getChildDrawingOrder(count, i)]; |
| 1715 | if ((child.mViewFlags & VISIBILITY_MASK) == VISIBLE || child.getAnimation() != null) { |
| 1716 | more |= drawChild(canvas, child, drawingTime); |
| 1717 | } |
| 1718 | } |
| 1719 | } |
| 1720 | |
| 1721 | // Draw any disappearing views that have animations |
| 1722 | if (mDisappearingChildren != null) { |
| 1723 | final ArrayList<View> disappearingChildren = mDisappearingChildren; |
| 1724 | final int disappearingCount = disappearingChildren.size() - 1; |
| 1725 | // Go backwards -- we may delete as animations finish |
| 1726 | for (int i = disappearingCount; i >= 0; i--) { |
| 1727 | final View child = disappearingChildren.get(i); |
| 1728 | more |= drawChild(canvas, child, drawingTime); |
| 1729 | } |
| 1730 | } |
| 1731 | |
| 1732 | if (clipToPadding) { |
| 1733 | canvas.restoreToCount(saveCount); |
| 1734 | } |
| 1735 | |
| 1736 | // mGroupFlags might have been updated by drawChild() |
| 1737 | flags = mGroupFlags; |
| 1738 | |
| 1739 | if ((flags & FLAG_INVALIDATE_REQUIRED) == FLAG_INVALIDATE_REQUIRED) { |
| 1740 | invalidate(); |
| 1741 | } |
| 1742 | |
| 1743 | if ((flags & FLAG_ANIMATION_DONE) == 0 && (flags & FLAG_NOTIFY_ANIMATION_LISTENER) == 0 && |
| 1744 | mLayoutAnimationController.isDone() && !more) { |
| 1745 | // We want to erase the drawing cache and notify the listener after the |
| 1746 | // next frame is drawn because one extra invalidate() is caused by |
| 1747 | // drawChild() after the animation is over |
| 1748 | mGroupFlags |= FLAG_NOTIFY_ANIMATION_LISTENER; |
| 1749 | final Runnable end = new Runnable() { |
| 1750 | public void run() { |
| 1751 | notifyAnimationListener(); |
| 1752 | } |
| 1753 | }; |
| 1754 | post(end); |
| 1755 | } |
| 1756 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 1757 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1758 | /** |
| 1759 | * Returns the index of the child to draw for this iteration. Override this |
| 1760 | * if you want to change the drawing order of children. By default, it |
| 1761 | * returns i. |
| 1762 | * <p> |
Romain Guy | 293451e | 2009-11-04 13:59:48 -0800 | [diff] [blame] | 1763 | * NOTE: In order for this method to be called, you must enable child ordering |
| 1764 | * first by calling {@link #setChildrenDrawingOrderEnabled(boolean)}. |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 1765 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1766 | * @param i The current iteration. |
| 1767 | * @return The index of the child to draw this iteration. |
Romain Guy | 293451e | 2009-11-04 13:59:48 -0800 | [diff] [blame] | 1768 | * |
| 1769 | * @see #setChildrenDrawingOrderEnabled(boolean) |
| 1770 | * @see #isChildrenDrawingOrderEnabled() |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1771 | */ |
| 1772 | protected int getChildDrawingOrder(int childCount, int i) { |
| 1773 | return i; |
| 1774 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 1775 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1776 | private void notifyAnimationListener() { |
| 1777 | mGroupFlags &= ~FLAG_NOTIFY_ANIMATION_LISTENER; |
| 1778 | mGroupFlags |= FLAG_ANIMATION_DONE; |
| 1779 | |
| 1780 | if (mAnimationListener != null) { |
| 1781 | final Runnable end = new Runnable() { |
| 1782 | public void run() { |
| 1783 | mAnimationListener.onAnimationEnd(mLayoutAnimationController.getAnimation()); |
| 1784 | } |
| 1785 | }; |
| 1786 | post(end); |
| 1787 | } |
| 1788 | |
| 1789 | if ((mGroupFlags & FLAG_ANIMATION_CACHE) == FLAG_ANIMATION_CACHE) { |
| 1790 | mGroupFlags &= ~FLAG_CHILDREN_DRAWN_WITH_CACHE; |
| 1791 | if ((mPersistentDrawingCache & PERSISTENT_ANIMATION_CACHE) == 0) { |
| 1792 | setChildrenDrawingCacheEnabled(false); |
| 1793 | } |
| 1794 | } |
| 1795 | |
| 1796 | invalidate(); |
| 1797 | } |
| 1798 | |
| 1799 | /** |
| 1800 | * Draw one child of this View Group. This method is responsible for getting |
| 1801 | * the canvas in the right state. This includes clipping, translating so |
| 1802 | * that the child's scrolled origin is at 0, 0, and applying any animation |
| 1803 | * transformations. |
| 1804 | * |
| 1805 | * @param canvas The canvas on which to draw the child |
| 1806 | * @param child Who to draw |
| 1807 | * @param drawingTime The time at which draw is occuring |
| 1808 | * @return True if an invalidate() was issued |
| 1809 | */ |
| 1810 | protected boolean drawChild(Canvas canvas, View child, long drawingTime) { |
| 1811 | boolean more = false; |
| 1812 | |
| 1813 | final int cl = child.mLeft; |
| 1814 | final int ct = child.mTop; |
| 1815 | final int cr = child.mRight; |
| 1816 | final int cb = child.mBottom; |
| 1817 | |
| 1818 | final int flags = mGroupFlags; |
| 1819 | |
| 1820 | if ((flags & FLAG_CLEAR_TRANSFORMATION) == FLAG_CLEAR_TRANSFORMATION) { |
Chet Haase | 4846032 | 2010-06-11 14:22:25 -0700 | [diff] [blame] | 1821 | mChildTransformation.clear(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1822 | mGroupFlags &= ~FLAG_CLEAR_TRANSFORMATION; |
| 1823 | } |
| 1824 | |
| 1825 | Transformation transformToApply = null; |
Chet Haase | 4846032 | 2010-06-11 14:22:25 -0700 | [diff] [blame] | 1826 | Transformation invalidationTransform; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1827 | final Animation a = child.getAnimation(); |
| 1828 | boolean concatMatrix = false; |
| 1829 | |
Chet Haase | 4846032 | 2010-06-11 14:22:25 -0700 | [diff] [blame] | 1830 | boolean scalingRequired = false; |
| 1831 | boolean caching = false; |
| 1832 | if (!canvas.isHardwareAccelerated() && |
| 1833 | (flags & FLAG_CHILDREN_DRAWN_WITH_CACHE) == FLAG_CHILDREN_DRAWN_WITH_CACHE || |
| 1834 | (flags & FLAG_ALWAYS_DRAWN_WITH_CACHE) == FLAG_ALWAYS_DRAWN_WITH_CACHE) { |
| 1835 | caching = true; |
| 1836 | if (mAttachInfo != null) scalingRequired = mAttachInfo.mScalingRequired; |
| 1837 | } |
| 1838 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1839 | if (a != null) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1840 | final boolean initialized = a.isInitialized(); |
| 1841 | if (!initialized) { |
Romain Guy | 8f2d94f | 2009-03-25 18:04:42 -0700 | [diff] [blame] | 1842 | a.initialize(cr - cl, cb - ct, getWidth(), getHeight()); |
| 1843 | a.initializeInvalidateRegion(0, 0, cr - cl, cb - ct); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1844 | child.onAnimationStart(); |
| 1845 | } |
| 1846 | |
Chet Haase | 4846032 | 2010-06-11 14:22:25 -0700 | [diff] [blame] | 1847 | more = a.getTransformation(drawingTime, mChildTransformation, |
| 1848 | scalingRequired ? mAttachInfo.mApplicationScale : 1f); |
| 1849 | if (scalingRequired && mAttachInfo.mApplicationScale != 1f) { |
| 1850 | if (mInvalidationTransformation == null) { |
| 1851 | mInvalidationTransformation = new Transformation(); |
| 1852 | } |
| 1853 | invalidationTransform = mInvalidationTransformation; |
| 1854 | a.getTransformation(drawingTime, invalidationTransform, 1f); |
| 1855 | } else { |
| 1856 | invalidationTransform = mChildTransformation; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1857 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1858 | transformToApply = mChildTransformation; |
| 1859 | |
| 1860 | concatMatrix = a.willChangeTransformationMatrix(); |
| 1861 | |
| 1862 | if (more) { |
| 1863 | if (!a.willChangeBounds()) { |
| 1864 | if ((flags & (FLAG_OPTIMIZE_INVALIDATE | FLAG_ANIMATION_DONE)) == |
| 1865 | FLAG_OPTIMIZE_INVALIDATE) { |
| 1866 | mGroupFlags |= FLAG_INVALIDATE_REQUIRED; |
| 1867 | } else if ((flags & FLAG_INVALIDATE_REQUIRED) == 0) { |
| 1868 | // The child need to draw an animation, potentially offscreen, so |
| 1869 | // make sure we do not cancel invalidate requests |
| 1870 | mPrivateFlags |= DRAW_ANIMATION; |
| 1871 | invalidate(cl, ct, cr, cb); |
| 1872 | } |
| 1873 | } else { |
Chet Haase | 4846032 | 2010-06-11 14:22:25 -0700 | [diff] [blame] | 1874 | if (mInvalidateRegion == null) { |
| 1875 | mInvalidateRegion = new RectF(); |
| 1876 | } |
| 1877 | final RectF region = mInvalidateRegion; |
| 1878 | a.getInvalidateRegion(0, 0, cr - cl, cb - ct, region, invalidationTransform); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1879 | |
| 1880 | // The child need to draw an animation, potentially offscreen, so |
| 1881 | // make sure we do not cancel invalidate requests |
| 1882 | mPrivateFlags |= DRAW_ANIMATION; |
The Android Open Source Project | 4df2423 | 2009-03-05 14:34:35 -0800 | [diff] [blame] | 1883 | |
| 1884 | final int left = cl + (int) region.left; |
| 1885 | final int top = ct + (int) region.top; |
| 1886 | invalidate(left, top, left + (int) region.width(), top + (int) region.height()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1887 | } |
| 1888 | } |
| 1889 | } else if ((flags & FLAG_SUPPORT_STATIC_TRANSFORMATIONS) == |
| 1890 | FLAG_SUPPORT_STATIC_TRANSFORMATIONS) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1891 | final boolean hasTransform = getChildStaticTransformation(child, mChildTransformation); |
| 1892 | if (hasTransform) { |
| 1893 | final int transformType = mChildTransformation.getTransformationType(); |
| 1894 | transformToApply = transformType != Transformation.TYPE_IDENTITY ? |
| 1895 | mChildTransformation : null; |
| 1896 | concatMatrix = (transformType & Transformation.TYPE_MATRIX) != 0; |
| 1897 | } |
| 1898 | } |
| 1899 | |
Chet Haase | df030d2 | 2010-07-30 17:22:38 -0700 | [diff] [blame] | 1900 | concatMatrix |= !child.hasIdentityMatrix(); |
| 1901 | |
Romain Guy | 5bcdff4 | 2009-05-14 21:27:18 -0700 | [diff] [blame] | 1902 | // Sets the flag as early as possible to allow draw() implementations |
Romain Guy | 986003d | 2009-03-25 17:42:35 -0700 | [diff] [blame] | 1903 | // to call invalidate() successfully when doing animations |
Romain Guy | 5bcdff4 | 2009-05-14 21:27:18 -0700 | [diff] [blame] | 1904 | child.mPrivateFlags |= DRAWN; |
Romain Guy | 986003d | 2009-03-25 17:42:35 -0700 | [diff] [blame] | 1905 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1906 | if (!concatMatrix && canvas.quickReject(cl, ct, cr, cb, Canvas.EdgeType.BW) && |
| 1907 | (child.mPrivateFlags & DRAW_ANIMATION) == 0) { |
| 1908 | return more; |
| 1909 | } |
| 1910 | |
| 1911 | child.computeScroll(); |
| 1912 | |
| 1913 | final int sx = child.mScrollX; |
| 1914 | final int sy = child.mScrollY; |
| 1915 | |
| 1916 | Bitmap cache = null; |
Chet Haase | 4846032 | 2010-06-11 14:22:25 -0700 | [diff] [blame] | 1917 | if (caching) { |
Romain Guy | fbd8f69 | 2009-06-26 14:51:58 -0700 | [diff] [blame] | 1918 | cache = child.getDrawingCache(true); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1919 | } |
| 1920 | |
| 1921 | final boolean hasNoCache = cache == null; |
| 1922 | |
| 1923 | final int restoreTo = canvas.save(); |
| 1924 | if (hasNoCache) { |
| 1925 | canvas.translate(cl - sx, ct - sy); |
| 1926 | } else { |
| 1927 | canvas.translate(cl, ct); |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 1928 | if (scalingRequired) { |
Romain Guy | cafdea6 | 2009-06-12 10:51:36 -0700 | [diff] [blame] | 1929 | // mAttachInfo cannot be null, otherwise scalingRequired == false |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 1930 | final float scale = 1.0f / mAttachInfo.mApplicationScale; |
| 1931 | canvas.scale(scale, scale); |
| 1932 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1933 | } |
| 1934 | |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 1935 | float alpha = child.getAlpha(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1936 | |
Romain Guy | 33e72ae | 2010-07-17 12:40:29 -0700 | [diff] [blame] | 1937 | if (transformToApply != null || alpha < 1.0f || !child.hasIdentityMatrix()) { |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 1938 | int transX = 0; |
| 1939 | int transY = 0; |
Romain Guy | 33e72ae | 2010-07-17 12:40:29 -0700 | [diff] [blame] | 1940 | |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 1941 | if (hasNoCache) { |
| 1942 | transX = -sx; |
| 1943 | transY = -sy; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1944 | } |
Romain Guy | 33e72ae | 2010-07-17 12:40:29 -0700 | [diff] [blame] | 1945 | |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 1946 | if (transformToApply != null) { |
| 1947 | if (concatMatrix) { |
| 1948 | // Undo the scroll translation, apply the transformation matrix, |
| 1949 | // then redo the scroll translate to get the correct result. |
| 1950 | canvas.translate(-transX, -transY); |
| 1951 | canvas.concat(transformToApply.getMatrix()); |
| 1952 | canvas.translate(transX, transY); |
| 1953 | mGroupFlags |= FLAG_CLEAR_TRANSFORMATION; |
| 1954 | } |
Romain Guy | 33e72ae | 2010-07-17 12:40:29 -0700 | [diff] [blame] | 1955 | |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 1956 | float transformAlpha = transformToApply.getAlpha(); |
| 1957 | if (transformAlpha < 1.0f) { |
| 1958 | alpha *= transformToApply.getAlpha(); |
| 1959 | mGroupFlags |= FLAG_CLEAR_TRANSFORMATION; |
| 1960 | } |
| 1961 | } |
Romain Guy | 33e72ae | 2010-07-17 12:40:29 -0700 | [diff] [blame] | 1962 | |
| 1963 | if (!child.hasIdentityMatrix()) { |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 1964 | canvas.translate(-transX, -transY); |
| 1965 | canvas.concat(child.getMatrix()); |
| 1966 | canvas.translate(transX, transY); |
| 1967 | } |
Romain Guy | 33e72ae | 2010-07-17 12:40:29 -0700 | [diff] [blame] | 1968 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1969 | if (alpha < 1.0f) { |
| 1970 | mGroupFlags |= FLAG_CLEAR_TRANSFORMATION; |
Romain Guy | 33e72ae | 2010-07-17 12:40:29 -0700 | [diff] [blame] | 1971 | |
| 1972 | if (hasNoCache) { |
| 1973 | final int multipliedAlpha = (int) (255 * alpha); |
| 1974 | if (!child.onSetAlpha(multipliedAlpha)) { |
| 1975 | canvas.saveLayerAlpha(sx, sy, sx + cr - cl, sy + cb - ct, multipliedAlpha, |
| 1976 | Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG); |
| 1977 | } else { |
| 1978 | child.mPrivateFlags |= ALPHA_SET; |
| 1979 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1980 | } |
| 1981 | } |
| 1982 | } else if ((child.mPrivateFlags & ALPHA_SET) == ALPHA_SET) { |
| 1983 | child.onSetAlpha(255); |
Romain Guy | 9b34d45 | 2010-09-02 11:45:04 -0700 | [diff] [blame] | 1984 | child.mPrivateFlags &= ~ALPHA_SET; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1985 | } |
| 1986 | |
| 1987 | if ((flags & FLAG_CLIP_CHILDREN) == FLAG_CLIP_CHILDREN) { |
| 1988 | if (hasNoCache) { |
Romain Guy | 8f2d94f | 2009-03-25 18:04:42 -0700 | [diff] [blame] | 1989 | canvas.clipRect(sx, sy, sx + (cr - cl), sy + (cb - ct)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1990 | } else { |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 1991 | if (!scalingRequired) { |
| 1992 | canvas.clipRect(0, 0, cr - cl, cb - ct); |
| 1993 | } else { |
| 1994 | canvas.clipRect(0, 0, cache.getWidth(), cache.getHeight()); |
| 1995 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1996 | } |
| 1997 | } |
| 1998 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1999 | if (hasNoCache) { |
| 2000 | // Fast path for layouts with no backgrounds |
| 2001 | if ((child.mPrivateFlags & SKIP_DRAW) == SKIP_DRAW) { |
| 2002 | if (ViewDebug.TRACE_HIERARCHY) { |
| 2003 | ViewDebug.trace(this, ViewDebug.HierarchyTraceType.DRAW); |
| 2004 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 2005 | child.mPrivateFlags &= ~DIRTY_MASK; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2006 | child.dispatchDraw(canvas); |
| 2007 | } else { |
| 2008 | child.draw(canvas); |
| 2009 | } |
| 2010 | } else { |
| 2011 | final Paint cachePaint = mCachePaint; |
| 2012 | if (alpha < 1.0f) { |
| 2013 | cachePaint.setAlpha((int) (alpha * 255)); |
| 2014 | mGroupFlags |= FLAG_ALPHA_LOWER_THAN_ONE; |
| 2015 | } else if ((flags & FLAG_ALPHA_LOWER_THAN_ONE) == FLAG_ALPHA_LOWER_THAN_ONE) { |
| 2016 | cachePaint.setAlpha(255); |
| 2017 | mGroupFlags &= ~FLAG_ALPHA_LOWER_THAN_ONE; |
| 2018 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2019 | canvas.drawBitmap(cache, 0.0f, 0.0f, cachePaint); |
| 2020 | } |
| 2021 | |
| 2022 | canvas.restoreToCount(restoreTo); |
| 2023 | |
| 2024 | if (a != null && !more) { |
| 2025 | child.onSetAlpha(255); |
| 2026 | finishAnimatingView(child, a); |
| 2027 | } |
| 2028 | |
| 2029 | return more; |
| 2030 | } |
| 2031 | |
| 2032 | /** |
| 2033 | * By default, children are clipped to their bounds before drawing. This |
| 2034 | * allows view groups to override this behavior for animations, etc. |
| 2035 | * |
| 2036 | * @param clipChildren true to clip children to their bounds, |
| 2037 | * false otherwise |
| 2038 | * @attr ref android.R.styleable#ViewGroup_clipChildren |
| 2039 | */ |
| 2040 | public void setClipChildren(boolean clipChildren) { |
| 2041 | setBooleanFlag(FLAG_CLIP_CHILDREN, clipChildren); |
| 2042 | } |
| 2043 | |
| 2044 | /** |
| 2045 | * By default, children are clipped to the padding of the ViewGroup. This |
| 2046 | * allows view groups to override this behavior |
| 2047 | * |
| 2048 | * @param clipToPadding true to clip children to the padding of the |
| 2049 | * group, false otherwise |
| 2050 | * @attr ref android.R.styleable#ViewGroup_clipToPadding |
| 2051 | */ |
| 2052 | public void setClipToPadding(boolean clipToPadding) { |
| 2053 | setBooleanFlag(FLAG_CLIP_TO_PADDING, clipToPadding); |
| 2054 | } |
| 2055 | |
| 2056 | /** |
| 2057 | * {@inheritDoc} |
| 2058 | */ |
| 2059 | @Override |
| 2060 | public void dispatchSetSelected(boolean selected) { |
| 2061 | final View[] children = mChildren; |
| 2062 | final int count = mChildrenCount; |
| 2063 | for (int i = 0; i < count; i++) { |
Chet Haase | df030d2 | 2010-07-30 17:22:38 -0700 | [diff] [blame] | 2064 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2065 | children[i].setSelected(selected); |
| 2066 | } |
| 2067 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 2068 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2069 | @Override |
| 2070 | protected void dispatchSetPressed(boolean pressed) { |
| 2071 | final View[] children = mChildren; |
| 2072 | final int count = mChildrenCount; |
| 2073 | for (int i = 0; i < count; i++) { |
| 2074 | children[i].setPressed(pressed); |
| 2075 | } |
| 2076 | } |
| 2077 | |
| 2078 | /** |
| 2079 | * When this property is set to true, this ViewGroup supports static transformations on |
| 2080 | * children; this causes |
| 2081 | * {@link #getChildStaticTransformation(View, android.view.animation.Transformation)} to be |
| 2082 | * invoked when a child is drawn. |
| 2083 | * |
| 2084 | * Any subclass overriding |
| 2085 | * {@link #getChildStaticTransformation(View, android.view.animation.Transformation)} should |
| 2086 | * set this property to true. |
| 2087 | * |
| 2088 | * @param enabled True to enable static transformations on children, false otherwise. |
| 2089 | * |
| 2090 | * @see #FLAG_SUPPORT_STATIC_TRANSFORMATIONS |
| 2091 | */ |
| 2092 | protected void setStaticTransformationsEnabled(boolean enabled) { |
| 2093 | setBooleanFlag(FLAG_SUPPORT_STATIC_TRANSFORMATIONS, enabled); |
| 2094 | } |
| 2095 | |
| 2096 | /** |
| 2097 | * {@inheritDoc} |
| 2098 | * |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 2099 | * @see #setStaticTransformationsEnabled(boolean) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2100 | */ |
| 2101 | protected boolean getChildStaticTransformation(View child, Transformation t) { |
| 2102 | return false; |
| 2103 | } |
| 2104 | |
| 2105 | /** |
| 2106 | * {@hide} |
| 2107 | */ |
| 2108 | @Override |
| 2109 | protected View findViewTraversal(int id) { |
| 2110 | if (id == mID) { |
| 2111 | return this; |
| 2112 | } |
| 2113 | |
| 2114 | final View[] where = mChildren; |
| 2115 | final int len = mChildrenCount; |
| 2116 | |
| 2117 | for (int i = 0; i < len; i++) { |
| 2118 | View v = where[i]; |
| 2119 | |
| 2120 | if ((v.mPrivateFlags & IS_ROOT_NAMESPACE) == 0) { |
| 2121 | v = v.findViewById(id); |
| 2122 | |
| 2123 | if (v != null) { |
| 2124 | return v; |
| 2125 | } |
| 2126 | } |
| 2127 | } |
| 2128 | |
| 2129 | return null; |
| 2130 | } |
| 2131 | |
| 2132 | /** |
| 2133 | * {@hide} |
| 2134 | */ |
| 2135 | @Override |
| 2136 | protected View findViewWithTagTraversal(Object tag) { |
| 2137 | if (tag != null && tag.equals(mTag)) { |
| 2138 | return this; |
| 2139 | } |
| 2140 | |
| 2141 | final View[] where = mChildren; |
| 2142 | final int len = mChildrenCount; |
| 2143 | |
| 2144 | for (int i = 0; i < len; i++) { |
| 2145 | View v = where[i]; |
| 2146 | |
| 2147 | if ((v.mPrivateFlags & IS_ROOT_NAMESPACE) == 0) { |
| 2148 | v = v.findViewWithTag(tag); |
| 2149 | |
| 2150 | if (v != null) { |
| 2151 | return v; |
| 2152 | } |
| 2153 | } |
| 2154 | } |
| 2155 | |
| 2156 | return null; |
| 2157 | } |
| 2158 | |
| 2159 | /** |
| 2160 | * Adds a child view. If no layout parameters are already set on the child, the |
| 2161 | * default parameters for this ViewGroup are set on the child. |
| 2162 | * |
| 2163 | * @param child the child view to add |
| 2164 | * |
| 2165 | * @see #generateDefaultLayoutParams() |
| 2166 | */ |
| 2167 | public void addView(View child) { |
| 2168 | addView(child, -1); |
| 2169 | } |
| 2170 | |
| 2171 | /** |
| 2172 | * Adds a child view. If no layout parameters are already set on the child, the |
| 2173 | * default parameters for this ViewGroup are set on the child. |
| 2174 | * |
| 2175 | * @param child the child view to add |
| 2176 | * @param index the position at which to add the child |
| 2177 | * |
| 2178 | * @see #generateDefaultLayoutParams() |
| 2179 | */ |
| 2180 | public void addView(View child, int index) { |
| 2181 | LayoutParams params = child.getLayoutParams(); |
| 2182 | if (params == null) { |
| 2183 | params = generateDefaultLayoutParams(); |
| 2184 | if (params == null) { |
| 2185 | throw new IllegalArgumentException("generateDefaultLayoutParams() cannot return null"); |
| 2186 | } |
| 2187 | } |
| 2188 | addView(child, index, params); |
| 2189 | } |
| 2190 | |
| 2191 | /** |
| 2192 | * Adds a child view with this ViewGroup's default layout parameters and the |
| 2193 | * specified width and height. |
| 2194 | * |
| 2195 | * @param child the child view to add |
| 2196 | */ |
| 2197 | public void addView(View child, int width, int height) { |
| 2198 | final LayoutParams params = generateDefaultLayoutParams(); |
| 2199 | params.width = width; |
| 2200 | params.height = height; |
| 2201 | addView(child, -1, params); |
| 2202 | } |
| 2203 | |
| 2204 | /** |
| 2205 | * Adds a child view with the specified layout parameters. |
| 2206 | * |
| 2207 | * @param child the child view to add |
| 2208 | * @param params the layout parameters to set on the child |
| 2209 | */ |
| 2210 | public void addView(View child, LayoutParams params) { |
| 2211 | addView(child, -1, params); |
| 2212 | } |
| 2213 | |
| 2214 | /** |
| 2215 | * Adds a child view with the specified layout parameters. |
| 2216 | * |
| 2217 | * @param child the child view to add |
| 2218 | * @param index the position at which to add the child |
| 2219 | * @param params the layout parameters to set on the child |
| 2220 | */ |
| 2221 | public void addView(View child, int index, LayoutParams params) { |
| 2222 | if (DBG) { |
| 2223 | System.out.println(this + " addView"); |
| 2224 | } |
| 2225 | |
| 2226 | // addViewInner() will call child.requestLayout() when setting the new LayoutParams |
| 2227 | // therefore, we call requestLayout() on ourselves before, so that the child's request |
| 2228 | // will be blocked at our level |
| 2229 | requestLayout(); |
| 2230 | invalidate(); |
| 2231 | addViewInner(child, index, params, false); |
| 2232 | } |
| 2233 | |
| 2234 | /** |
| 2235 | * {@inheritDoc} |
| 2236 | */ |
| 2237 | public void updateViewLayout(View view, ViewGroup.LayoutParams params) { |
| 2238 | if (!checkLayoutParams(params)) { |
| 2239 | throw new IllegalArgumentException("Invalid LayoutParams supplied to " + this); |
| 2240 | } |
| 2241 | if (view.mParent != this) { |
| 2242 | throw new IllegalArgumentException("Given view not a child of " + this); |
| 2243 | } |
| 2244 | view.setLayoutParams(params); |
| 2245 | } |
| 2246 | |
| 2247 | /** |
| 2248 | * {@inheritDoc} |
| 2249 | */ |
| 2250 | protected boolean checkLayoutParams(ViewGroup.LayoutParams p) { |
| 2251 | return p != null; |
| 2252 | } |
| 2253 | |
| 2254 | /** |
| 2255 | * Interface definition for a callback to be invoked when the hierarchy |
| 2256 | * within this view changed. The hierarchy changes whenever a child is added |
| 2257 | * to or removed from this view. |
| 2258 | */ |
| 2259 | public interface OnHierarchyChangeListener { |
| 2260 | /** |
| 2261 | * Called when a new child is added to a parent view. |
| 2262 | * |
| 2263 | * @param parent the view in which a child was added |
| 2264 | * @param child the new child view added in the hierarchy |
| 2265 | */ |
| 2266 | void onChildViewAdded(View parent, View child); |
| 2267 | |
| 2268 | /** |
| 2269 | * Called when a child is removed from a parent view. |
| 2270 | * |
| 2271 | * @param parent the view from which the child was removed |
| 2272 | * @param child the child removed from the hierarchy |
| 2273 | */ |
| 2274 | void onChildViewRemoved(View parent, View child); |
| 2275 | } |
| 2276 | |
| 2277 | /** |
| 2278 | * Register a callback to be invoked when a child is added to or removed |
| 2279 | * from this view. |
| 2280 | * |
| 2281 | * @param listener the callback to invoke on hierarchy change |
| 2282 | */ |
| 2283 | public void setOnHierarchyChangeListener(OnHierarchyChangeListener listener) { |
| 2284 | mOnHierarchyChangeListener = listener; |
| 2285 | } |
| 2286 | |
| 2287 | /** |
| 2288 | * Adds a view during layout. This is useful if in your onLayout() method, |
| 2289 | * you need to add more views (as does the list view for example). |
| 2290 | * |
| 2291 | * If index is negative, it means put it at the end of the list. |
| 2292 | * |
| 2293 | * @param child the view to add to the group |
| 2294 | * @param index the index at which the child must be added |
| 2295 | * @param params the layout parameters to associate with the child |
| 2296 | * @return true if the child was added, false otherwise |
| 2297 | */ |
| 2298 | protected boolean addViewInLayout(View child, int index, LayoutParams params) { |
| 2299 | return addViewInLayout(child, index, params, false); |
| 2300 | } |
| 2301 | |
| 2302 | /** |
| 2303 | * Adds a view during layout. This is useful if in your onLayout() method, |
| 2304 | * you need to add more views (as does the list view for example). |
| 2305 | * |
| 2306 | * If index is negative, it means put it at the end of the list. |
| 2307 | * |
| 2308 | * @param child the view to add to the group |
| 2309 | * @param index the index at which the child must be added |
| 2310 | * @param params the layout parameters to associate with the child |
| 2311 | * @param preventRequestLayout if true, calling this method will not trigger a |
| 2312 | * layout request on child |
| 2313 | * @return true if the child was added, false otherwise |
| 2314 | */ |
| 2315 | protected boolean addViewInLayout(View child, int index, LayoutParams params, |
| 2316 | boolean preventRequestLayout) { |
| 2317 | child.mParent = null; |
| 2318 | addViewInner(child, index, params, preventRequestLayout); |
Romain Guy | 24443ea | 2009-05-11 11:56:30 -0700 | [diff] [blame] | 2319 | child.mPrivateFlags = (child.mPrivateFlags & ~DIRTY_MASK) | DRAWN; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2320 | return true; |
| 2321 | } |
| 2322 | |
| 2323 | /** |
| 2324 | * Prevents the specified child to be laid out during the next layout pass. |
| 2325 | * |
| 2326 | * @param child the child on which to perform the cleanup |
| 2327 | */ |
| 2328 | protected void cleanupLayoutState(View child) { |
| 2329 | child.mPrivateFlags &= ~View.FORCE_LAYOUT; |
| 2330 | } |
| 2331 | |
| 2332 | private void addViewInner(View child, int index, LayoutParams params, |
| 2333 | boolean preventRequestLayout) { |
| 2334 | |
| 2335 | if (child.getParent() != null) { |
| 2336 | throw new IllegalStateException("The specified child already has a parent. " + |
| 2337 | "You must call removeView() on the child's parent first."); |
| 2338 | } |
| 2339 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 2340 | if (mTransition != null) { |
| 2341 | mTransition.childAdd(this, child); |
| 2342 | } |
| 2343 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2344 | if (!checkLayoutParams(params)) { |
| 2345 | params = generateLayoutParams(params); |
| 2346 | } |
| 2347 | |
| 2348 | if (preventRequestLayout) { |
| 2349 | child.mLayoutParams = params; |
| 2350 | } else { |
| 2351 | child.setLayoutParams(params); |
| 2352 | } |
| 2353 | |
| 2354 | if (index < 0) { |
| 2355 | index = mChildrenCount; |
| 2356 | } |
| 2357 | |
| 2358 | addInArray(child, index); |
| 2359 | |
| 2360 | // tell our children |
| 2361 | if (preventRequestLayout) { |
| 2362 | child.assignParent(this); |
| 2363 | } else { |
| 2364 | child.mParent = this; |
| 2365 | } |
| 2366 | |
| 2367 | if (child.hasFocus()) { |
| 2368 | requestChildFocus(child, child.findFocus()); |
| 2369 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 2370 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2371 | AttachInfo ai = mAttachInfo; |
| 2372 | if (ai != null) { |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 2373 | boolean lastKeepOn = ai.mKeepScreenOn; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2374 | ai.mKeepScreenOn = false; |
| 2375 | child.dispatchAttachedToWindow(mAttachInfo, (mViewFlags&VISIBILITY_MASK)); |
| 2376 | if (ai.mKeepScreenOn) { |
| 2377 | needGlobalAttributesUpdate(true); |
| 2378 | } |
| 2379 | ai.mKeepScreenOn = lastKeepOn; |
| 2380 | } |
| 2381 | |
| 2382 | if (mOnHierarchyChangeListener != null) { |
| 2383 | mOnHierarchyChangeListener.onChildViewAdded(this, child); |
| 2384 | } |
| 2385 | |
| 2386 | if ((child.mViewFlags & DUPLICATE_PARENT_STATE) == DUPLICATE_PARENT_STATE) { |
| 2387 | mGroupFlags |= FLAG_NOTIFY_CHILDREN_ON_DRAWABLE_STATE_CHANGE; |
| 2388 | } |
| 2389 | } |
| 2390 | |
| 2391 | private void addInArray(View child, int index) { |
| 2392 | View[] children = mChildren; |
| 2393 | final int count = mChildrenCount; |
| 2394 | final int size = children.length; |
| 2395 | if (index == count) { |
| 2396 | if (size == count) { |
| 2397 | mChildren = new View[size + ARRAY_CAPACITY_INCREMENT]; |
| 2398 | System.arraycopy(children, 0, mChildren, 0, size); |
| 2399 | children = mChildren; |
| 2400 | } |
| 2401 | children[mChildrenCount++] = child; |
| 2402 | } else if (index < count) { |
| 2403 | if (size == count) { |
| 2404 | mChildren = new View[size + ARRAY_CAPACITY_INCREMENT]; |
| 2405 | System.arraycopy(children, 0, mChildren, 0, index); |
| 2406 | System.arraycopy(children, index, mChildren, index + 1, count - index); |
| 2407 | children = mChildren; |
| 2408 | } else { |
| 2409 | System.arraycopy(children, index, children, index + 1, count - index); |
| 2410 | } |
| 2411 | children[index] = child; |
| 2412 | mChildrenCount++; |
| 2413 | } else { |
| 2414 | throw new IndexOutOfBoundsException("index=" + index + " count=" + count); |
| 2415 | } |
| 2416 | } |
| 2417 | |
| 2418 | // This method also sets the child's mParent to null |
| 2419 | private void removeFromArray(int index) { |
| 2420 | final View[] children = mChildren; |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 2421 | if (!(mTransitioningViews != null && mTransitioningViews.contains(children[index]))) { |
| 2422 | children[index].mParent = null; |
| 2423 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2424 | final int count = mChildrenCount; |
| 2425 | if (index == count - 1) { |
| 2426 | children[--mChildrenCount] = null; |
| 2427 | } else if (index >= 0 && index < count) { |
| 2428 | System.arraycopy(children, index + 1, children, index, count - index - 1); |
| 2429 | children[--mChildrenCount] = null; |
| 2430 | } else { |
| 2431 | throw new IndexOutOfBoundsException(); |
| 2432 | } |
| 2433 | } |
| 2434 | |
| 2435 | // This method also sets the children's mParent to null |
| 2436 | private void removeFromArray(int start, int count) { |
| 2437 | final View[] children = mChildren; |
| 2438 | final int childrenCount = mChildrenCount; |
| 2439 | |
| 2440 | start = Math.max(0, start); |
| 2441 | final int end = Math.min(childrenCount, start + count); |
| 2442 | |
| 2443 | if (start == end) { |
| 2444 | return; |
| 2445 | } |
| 2446 | |
| 2447 | if (end == childrenCount) { |
| 2448 | for (int i = start; i < end; i++) { |
| 2449 | children[i].mParent = null; |
| 2450 | children[i] = null; |
| 2451 | } |
| 2452 | } else { |
| 2453 | for (int i = start; i < end; i++) { |
| 2454 | children[i].mParent = null; |
| 2455 | } |
| 2456 | |
| 2457 | // Since we're looping above, we might as well do the copy, but is arraycopy() |
| 2458 | // faster than the extra 2 bounds checks we would do in the loop? |
| 2459 | System.arraycopy(children, end, children, start, childrenCount - end); |
| 2460 | |
| 2461 | for (int i = childrenCount - (end - start); i < childrenCount; i++) { |
| 2462 | children[i] = null; |
| 2463 | } |
| 2464 | } |
| 2465 | |
| 2466 | mChildrenCount -= (end - start); |
| 2467 | } |
| 2468 | |
| 2469 | private void bindLayoutAnimation(View child) { |
| 2470 | Animation a = mLayoutAnimationController.getAnimationForView(child); |
| 2471 | child.setAnimation(a); |
| 2472 | } |
| 2473 | |
| 2474 | /** |
| 2475 | * Subclasses should override this method to set layout animation |
| 2476 | * parameters on the supplied child. |
| 2477 | * |
| 2478 | * @param child the child to associate with animation parameters |
| 2479 | * @param params the child's layout parameters which hold the animation |
| 2480 | * parameters |
| 2481 | * @param index the index of the child in the view group |
| 2482 | * @param count the number of children in the view group |
| 2483 | */ |
| 2484 | protected void attachLayoutAnimationParameters(View child, |
| 2485 | LayoutParams params, int index, int count) { |
| 2486 | LayoutAnimationController.AnimationParameters animationParams = |
| 2487 | params.layoutAnimationParameters; |
| 2488 | if (animationParams == null) { |
| 2489 | animationParams = new LayoutAnimationController.AnimationParameters(); |
| 2490 | params.layoutAnimationParameters = animationParams; |
| 2491 | } |
| 2492 | |
| 2493 | animationParams.count = count; |
| 2494 | animationParams.index = index; |
| 2495 | } |
| 2496 | |
| 2497 | /** |
| 2498 | * {@inheritDoc} |
| 2499 | */ |
| 2500 | public void removeView(View view) { |
| 2501 | removeViewInternal(view); |
| 2502 | requestLayout(); |
| 2503 | invalidate(); |
| 2504 | } |
| 2505 | |
| 2506 | /** |
| 2507 | * Removes a view during layout. This is useful if in your onLayout() method, |
| 2508 | * you need to remove more views. |
| 2509 | * |
| 2510 | * @param view the view to remove from the group |
| 2511 | */ |
| 2512 | public void removeViewInLayout(View view) { |
| 2513 | removeViewInternal(view); |
| 2514 | } |
| 2515 | |
| 2516 | /** |
| 2517 | * Removes a range of views during layout. This is useful if in your onLayout() method, |
| 2518 | * you need to remove more views. |
| 2519 | * |
| 2520 | * @param start the index of the first view to remove from the group |
| 2521 | * @param count the number of views to remove from the group |
| 2522 | */ |
| 2523 | public void removeViewsInLayout(int start, int count) { |
| 2524 | removeViewsInternal(start, count); |
| 2525 | } |
| 2526 | |
| 2527 | /** |
| 2528 | * Removes the view at the specified position in the group. |
| 2529 | * |
| 2530 | * @param index the position in the group of the view to remove |
| 2531 | */ |
| 2532 | public void removeViewAt(int index) { |
| 2533 | removeViewInternal(index, getChildAt(index)); |
| 2534 | requestLayout(); |
| 2535 | invalidate(); |
| 2536 | } |
| 2537 | |
| 2538 | /** |
| 2539 | * Removes the specified range of views from the group. |
| 2540 | * |
| 2541 | * @param start the first position in the group of the range of views to remove |
| 2542 | * @param count the number of views to remove |
| 2543 | */ |
| 2544 | public void removeViews(int start, int count) { |
| 2545 | removeViewsInternal(start, count); |
| 2546 | requestLayout(); |
| 2547 | invalidate(); |
| 2548 | } |
| 2549 | |
| 2550 | private void removeViewInternal(View view) { |
| 2551 | final int index = indexOfChild(view); |
| 2552 | if (index >= 0) { |
| 2553 | removeViewInternal(index, view); |
| 2554 | } |
| 2555 | } |
| 2556 | |
| 2557 | private void removeViewInternal(int index, View view) { |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 2558 | |
| 2559 | if (mTransition != null) { |
| 2560 | mTransition.childRemove(this, view); |
| 2561 | } |
| 2562 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2563 | boolean clearChildFocus = false; |
| 2564 | if (view == mFocused) { |
| 2565 | view.clearFocusForRemoval(); |
| 2566 | clearChildFocus = true; |
| 2567 | } |
| 2568 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 2569 | if (view.getAnimation() != null || |
| 2570 | (mTransitioningViews != null && mTransitioningViews.contains(view))) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2571 | addDisappearingView(view); |
| 2572 | } else if (view.mAttachInfo != null) { |
| 2573 | view.dispatchDetachedFromWindow(); |
| 2574 | } |
| 2575 | |
| 2576 | if (mOnHierarchyChangeListener != null) { |
| 2577 | mOnHierarchyChangeListener.onChildViewRemoved(this, view); |
| 2578 | } |
| 2579 | |
| 2580 | needGlobalAttributesUpdate(false); |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 2581 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2582 | removeFromArray(index); |
| 2583 | |
| 2584 | if (clearChildFocus) { |
| 2585 | clearChildFocus(view); |
| 2586 | } |
| 2587 | } |
| 2588 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 2589 | /** |
| 2590 | * Sets the LayoutTransition object for this ViewGroup. If the LayoutTransition object is |
| 2591 | * not null, changes in layout which occur because of children being added to or removed from |
| 2592 | * the ViewGroup will be animated according to the animations defined in that LayoutTransition |
| 2593 | * object. By default, the transition object is null (so layout changes are not animated). |
| 2594 | * |
| 2595 | * @param transition The LayoutTransition object that will animated changes in layout. A value |
| 2596 | * of <code>null</code> means no transition will run on layout changes. |
Chet Haase | 13cc120 | 2010-09-03 15:39:20 -0700 | [diff] [blame] | 2597 | * @attr ref android.R.styleable#ViewGroup_animateLayoutChanges |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 2598 | */ |
| 2599 | public void setLayoutTransition(LayoutTransition transition) { |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 2600 | if (mTransition != null) { |
| 2601 | mTransition.removeTransitionListener(mLayoutTransitionListener); |
| 2602 | } |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 2603 | mTransition = transition; |
Chet Haase | 13cc120 | 2010-09-03 15:39:20 -0700 | [diff] [blame] | 2604 | if (mTransition != null) { |
| 2605 | mTransition.addTransitionListener(mLayoutTransitionListener); |
| 2606 | } |
| 2607 | } |
| 2608 | |
| 2609 | /** |
| 2610 | * Gets the LayoutTransition object for this ViewGroup. If the LayoutTransition object is |
| 2611 | * not null, changes in layout which occur because of children being added to or removed from |
| 2612 | * the ViewGroup will be animated according to the animations defined in that LayoutTransition |
| 2613 | * object. By default, the transition object is null (so layout changes are not animated). |
| 2614 | * |
| 2615 | * @return LayoutTranstion The LayoutTransition object that will animated changes in layout. |
| 2616 | * A value of <code>null</code> means no transition will run on layout changes. |
| 2617 | */ |
| 2618 | public LayoutTransition getLayoutTransition() { |
| 2619 | return mTransition; |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 2620 | } |
| 2621 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2622 | private void removeViewsInternal(int start, int count) { |
| 2623 | final OnHierarchyChangeListener onHierarchyChangeListener = mOnHierarchyChangeListener; |
| 2624 | final boolean notifyListener = onHierarchyChangeListener != null; |
| 2625 | final View focused = mFocused; |
| 2626 | final boolean detach = mAttachInfo != null; |
| 2627 | View clearChildFocus = null; |
| 2628 | |
| 2629 | final View[] children = mChildren; |
| 2630 | final int end = start + count; |
| 2631 | |
| 2632 | for (int i = start; i < end; i++) { |
| 2633 | final View view = children[i]; |
| 2634 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 2635 | if (mTransition != null) { |
| 2636 | mTransition.childRemove(this, view); |
| 2637 | } |
| 2638 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2639 | if (view == focused) { |
| 2640 | view.clearFocusForRemoval(); |
| 2641 | clearChildFocus = view; |
| 2642 | } |
| 2643 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 2644 | if (view.getAnimation() != null || |
| 2645 | (mTransitioningViews != null && mTransitioningViews.contains(view))) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2646 | addDisappearingView(view); |
| 2647 | } else if (detach) { |
| 2648 | view.dispatchDetachedFromWindow(); |
| 2649 | } |
| 2650 | |
| 2651 | needGlobalAttributesUpdate(false); |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 2652 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2653 | if (notifyListener) { |
| 2654 | onHierarchyChangeListener.onChildViewRemoved(this, view); |
| 2655 | } |
| 2656 | } |
| 2657 | |
| 2658 | removeFromArray(start, count); |
| 2659 | |
| 2660 | if (clearChildFocus != null) { |
| 2661 | clearChildFocus(clearChildFocus); |
| 2662 | } |
| 2663 | } |
| 2664 | |
| 2665 | /** |
| 2666 | * Call this method to remove all child views from the |
| 2667 | * ViewGroup. |
| 2668 | */ |
| 2669 | public void removeAllViews() { |
| 2670 | removeAllViewsInLayout(); |
| 2671 | requestLayout(); |
| 2672 | invalidate(); |
| 2673 | } |
| 2674 | |
| 2675 | /** |
| 2676 | * Called by a ViewGroup subclass to remove child views from itself, |
| 2677 | * when it must first know its size on screen before it can calculate how many |
| 2678 | * child views it will render. An example is a Gallery or a ListView, which |
| 2679 | * may "have" 50 children, but actually only render the number of children |
| 2680 | * that can currently fit inside the object on screen. Do not call |
| 2681 | * this method unless you are extending ViewGroup and understand the |
| 2682 | * view measuring and layout pipeline. |
| 2683 | */ |
| 2684 | public void removeAllViewsInLayout() { |
| 2685 | final int count = mChildrenCount; |
| 2686 | if (count <= 0) { |
| 2687 | return; |
| 2688 | } |
| 2689 | |
| 2690 | final View[] children = mChildren; |
| 2691 | mChildrenCount = 0; |
| 2692 | |
| 2693 | final OnHierarchyChangeListener listener = mOnHierarchyChangeListener; |
| 2694 | final boolean notify = listener != null; |
| 2695 | final View focused = mFocused; |
| 2696 | final boolean detach = mAttachInfo != null; |
| 2697 | View clearChildFocus = null; |
| 2698 | |
| 2699 | needGlobalAttributesUpdate(false); |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 2700 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2701 | for (int i = count - 1; i >= 0; i--) { |
| 2702 | final View view = children[i]; |
| 2703 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 2704 | if (mTransition != null) { |
| 2705 | mTransition.childRemove(this, view); |
| 2706 | } |
| 2707 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2708 | if (view == focused) { |
| 2709 | view.clearFocusForRemoval(); |
| 2710 | clearChildFocus = view; |
| 2711 | } |
| 2712 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 2713 | if (view.getAnimation() != null || |
| 2714 | (mTransitioningViews != null && mTransitioningViews.contains(view))) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2715 | addDisappearingView(view); |
| 2716 | } else if (detach) { |
| 2717 | view.dispatchDetachedFromWindow(); |
| 2718 | } |
| 2719 | |
| 2720 | if (notify) { |
| 2721 | listener.onChildViewRemoved(this, view); |
| 2722 | } |
| 2723 | |
| 2724 | view.mParent = null; |
| 2725 | children[i] = null; |
| 2726 | } |
| 2727 | |
| 2728 | if (clearChildFocus != null) { |
| 2729 | clearChildFocus(clearChildFocus); |
| 2730 | } |
| 2731 | } |
| 2732 | |
| 2733 | /** |
| 2734 | * Finishes the removal of a detached view. This method will dispatch the detached from |
| 2735 | * window event and notify the hierarchy change listener. |
| 2736 | * |
| 2737 | * @param child the child to be definitely removed from the view hierarchy |
| 2738 | * @param animate if true and the view has an animation, the view is placed in the |
| 2739 | * disappearing views list, otherwise, it is detached from the window |
| 2740 | * |
| 2741 | * @see #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams) |
| 2742 | * @see #detachAllViewsFromParent() |
| 2743 | * @see #detachViewFromParent(View) |
| 2744 | * @see #detachViewFromParent(int) |
| 2745 | */ |
| 2746 | protected void removeDetachedView(View child, boolean animate) { |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 2747 | if (mTransition != null) { |
| 2748 | mTransition.childRemove(this, child); |
| 2749 | } |
| 2750 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2751 | if (child == mFocused) { |
| 2752 | child.clearFocus(); |
| 2753 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 2754 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 2755 | if ((animate && child.getAnimation() != null) || |
| 2756 | (mTransitioningViews != null && mTransitioningViews.contains(child))) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2757 | addDisappearingView(child); |
| 2758 | } else if (child.mAttachInfo != null) { |
| 2759 | child.dispatchDetachedFromWindow(); |
| 2760 | } |
| 2761 | |
| 2762 | if (mOnHierarchyChangeListener != null) { |
| 2763 | mOnHierarchyChangeListener.onChildViewRemoved(this, child); |
| 2764 | } |
| 2765 | } |
| 2766 | |
| 2767 | /** |
| 2768 | * Attaches a view to this view group. Attaching a view assigns this group as the parent, |
| 2769 | * sets the layout parameters and puts the view in the list of children so it can be retrieved |
| 2770 | * by calling {@link #getChildAt(int)}. |
| 2771 | * |
| 2772 | * This method should be called only for view which were detached from their parent. |
| 2773 | * |
| 2774 | * @param child the child to attach |
| 2775 | * @param index the index at which the child should be attached |
| 2776 | * @param params the layout parameters of the child |
| 2777 | * |
| 2778 | * @see #removeDetachedView(View, boolean) |
| 2779 | * @see #detachAllViewsFromParent() |
| 2780 | * @see #detachViewFromParent(View) |
| 2781 | * @see #detachViewFromParent(int) |
| 2782 | */ |
| 2783 | protected void attachViewToParent(View child, int index, LayoutParams params) { |
| 2784 | child.mLayoutParams = params; |
| 2785 | |
| 2786 | if (index < 0) { |
| 2787 | index = mChildrenCount; |
| 2788 | } |
| 2789 | |
| 2790 | addInArray(child, index); |
| 2791 | |
| 2792 | child.mParent = this; |
Romain Guy | bc9fdc9 | 2010-01-19 13:27:48 -0800 | [diff] [blame] | 2793 | child.mPrivateFlags = (child.mPrivateFlags & ~DIRTY_MASK & ~DRAWING_CACHE_VALID) | DRAWN; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2794 | |
| 2795 | if (child.hasFocus()) { |
| 2796 | requestChildFocus(child, child.findFocus()); |
| 2797 | } |
| 2798 | } |
| 2799 | |
| 2800 | /** |
| 2801 | * Detaches a view from its parent. Detaching a view should be temporary and followed |
| 2802 | * either by a call to {@link #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)} |
| 2803 | * or a call to {@link #removeDetachedView(View, boolean)}. When a view is detached, |
| 2804 | * its parent is null and cannot be retrieved by a call to {@link #getChildAt(int)}. |
| 2805 | * |
| 2806 | * @param child the child to detach |
| 2807 | * |
| 2808 | * @see #detachViewFromParent(int) |
| 2809 | * @see #detachViewsFromParent(int, int) |
| 2810 | * @see #detachAllViewsFromParent() |
| 2811 | * @see #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams) |
| 2812 | * @see #removeDetachedView(View, boolean) |
| 2813 | */ |
| 2814 | protected void detachViewFromParent(View child) { |
| 2815 | removeFromArray(indexOfChild(child)); |
| 2816 | } |
| 2817 | |
| 2818 | /** |
| 2819 | * Detaches a view from its parent. Detaching a view should be temporary and followed |
| 2820 | * either by a call to {@link #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)} |
| 2821 | * or a call to {@link #removeDetachedView(View, boolean)}. When a view is detached, |
| 2822 | * its parent is null and cannot be retrieved by a call to {@link #getChildAt(int)}. |
| 2823 | * |
| 2824 | * @param index the index of the child to detach |
| 2825 | * |
| 2826 | * @see #detachViewFromParent(View) |
| 2827 | * @see #detachAllViewsFromParent() |
| 2828 | * @see #detachViewsFromParent(int, int) |
| 2829 | * @see #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams) |
| 2830 | * @see #removeDetachedView(View, boolean) |
| 2831 | */ |
| 2832 | protected void detachViewFromParent(int index) { |
| 2833 | removeFromArray(index); |
| 2834 | } |
| 2835 | |
| 2836 | /** |
| 2837 | * Detaches a range of view from their parent. Detaching a view should be temporary and followed |
| 2838 | * either by a call to {@link #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)} |
| 2839 | * or a call to {@link #removeDetachedView(View, boolean)}. When a view is detached, its |
| 2840 | * parent is null and cannot be retrieved by a call to {@link #getChildAt(int)}. |
| 2841 | * |
| 2842 | * @param start the first index of the childrend range to detach |
| 2843 | * @param count the number of children to detach |
| 2844 | * |
| 2845 | * @see #detachViewFromParent(View) |
| 2846 | * @see #detachViewFromParent(int) |
| 2847 | * @see #detachAllViewsFromParent() |
| 2848 | * @see #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams) |
| 2849 | * @see #removeDetachedView(View, boolean) |
| 2850 | */ |
| 2851 | protected void detachViewsFromParent(int start, int count) { |
| 2852 | removeFromArray(start, count); |
| 2853 | } |
| 2854 | |
| 2855 | /** |
| 2856 | * Detaches all views from the parent. Detaching a view should be temporary and followed |
| 2857 | * either by a call to {@link #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)} |
| 2858 | * or a call to {@link #removeDetachedView(View, boolean)}. When a view is detached, |
| 2859 | * its parent is null and cannot be retrieved by a call to {@link #getChildAt(int)}. |
| 2860 | * |
| 2861 | * @see #detachViewFromParent(View) |
| 2862 | * @see #detachViewFromParent(int) |
| 2863 | * @see #detachViewsFromParent(int, int) |
| 2864 | * @see #attachViewToParent(View, int, android.view.ViewGroup.LayoutParams) |
| 2865 | * @see #removeDetachedView(View, boolean) |
| 2866 | */ |
| 2867 | protected void detachAllViewsFromParent() { |
| 2868 | final int count = mChildrenCount; |
| 2869 | if (count <= 0) { |
| 2870 | return; |
| 2871 | } |
| 2872 | |
| 2873 | final View[] children = mChildren; |
| 2874 | mChildrenCount = 0; |
| 2875 | |
| 2876 | for (int i = count - 1; i >= 0; i--) { |
| 2877 | children[i].mParent = null; |
| 2878 | children[i] = null; |
| 2879 | } |
| 2880 | } |
| 2881 | |
| 2882 | /** |
| 2883 | * Don't call or override this method. It is used for the implementation of |
| 2884 | * the view hierarchy. |
| 2885 | */ |
| 2886 | public final void invalidateChild(View child, final Rect dirty) { |
| 2887 | if (ViewDebug.TRACE_HIERARCHY) { |
| 2888 | ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE_CHILD); |
| 2889 | } |
| 2890 | |
| 2891 | ViewParent parent = this; |
| 2892 | |
| 2893 | final AttachInfo attachInfo = mAttachInfo; |
| 2894 | if (attachInfo != null) { |
| 2895 | final int[] location = attachInfo.mInvalidateChildLocation; |
| 2896 | location[CHILD_LEFT_INDEX] = child.mLeft; |
| 2897 | location[CHILD_TOP_INDEX] = child.mTop; |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 2898 | Matrix childMatrix = child.getMatrix(); |
| 2899 | if (!childMatrix.isIdentity()) { |
Chet Haase | 9bc829c | 2010-07-15 17:09:23 -0700 | [diff] [blame] | 2900 | RectF boundingRect = attachInfo.mTmpTransformRect; |
| 2901 | boundingRect.set(dirty); |
| 2902 | childMatrix.mapRect(boundingRect); |
| 2903 | dirty.set((int) boundingRect.left, (int) boundingRect.top, |
| 2904 | (int) (boundingRect.right + 0.5f), |
| 2905 | (int) (boundingRect.bottom + 0.5f)); |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 2906 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2907 | |
| 2908 | // If the child is drawing an animation, we want to copy this flag onto |
| 2909 | // ourselves and the parent to make sure the invalidate request goes |
| 2910 | // through |
| 2911 | final boolean drawAnimation = (child.mPrivateFlags & DRAW_ANIMATION) == DRAW_ANIMATION; |
Romain Guy | 24443ea | 2009-05-11 11:56:30 -0700 | [diff] [blame] | 2912 | |
| 2913 | // Check whether the child that requests the invalidate is fully opaque |
Romain Guy | ec25df9 | 2009-05-25 04:39:37 -0700 | [diff] [blame] | 2914 | final boolean isOpaque = child.isOpaque() && !drawAnimation && |
| 2915 | child.getAnimation() != null; |
Romain Guy | 24443ea | 2009-05-11 11:56:30 -0700 | [diff] [blame] | 2916 | // Mark the child as dirty, using the appropriate flag |
| 2917 | // Make sure we do not set both flags at the same time |
| 2918 | final int opaqueFlag = isOpaque ? DIRTY_OPAQUE : DIRTY; |
| 2919 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2920 | do { |
Romain Guy | 24443ea | 2009-05-11 11:56:30 -0700 | [diff] [blame] | 2921 | View view = null; |
| 2922 | if (parent instanceof View) { |
| 2923 | view = (View) parent; |
| 2924 | } |
| 2925 | |
Romain Guy | bb93d55 | 2009-03-24 21:04:15 -0700 | [diff] [blame] | 2926 | if (drawAnimation) { |
Romain Guy | 24443ea | 2009-05-11 11:56:30 -0700 | [diff] [blame] | 2927 | if (view != null) { |
| 2928 | view.mPrivateFlags |= DRAW_ANIMATION; |
Romain Guy | bb93d55 | 2009-03-24 21:04:15 -0700 | [diff] [blame] | 2929 | } else if (parent instanceof ViewRoot) { |
| 2930 | ((ViewRoot) parent).mIsAnimating = true; |
| 2931 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2932 | } |
Romain Guy | 24443ea | 2009-05-11 11:56:30 -0700 | [diff] [blame] | 2933 | |
| 2934 | // If the parent is dirty opaque or not dirty, mark it dirty with the opaque |
| 2935 | // flag coming from the child that initiated the invalidate |
| 2936 | if (view != null && (view.mPrivateFlags & DIRTY_MASK) != DIRTY) { |
| 2937 | view.mPrivateFlags = (view.mPrivateFlags & ~DIRTY_MASK) | opaqueFlag; |
| 2938 | } |
| 2939 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2940 | parent = parent.invalidateChildInParent(location, dirty); |
Chet Haase | 9bc829c | 2010-07-15 17:09:23 -0700 | [diff] [blame] | 2941 | if (view != null) { |
| 2942 | // Account for transform on current parent |
| 2943 | Matrix m = view.getMatrix(); |
| 2944 | if (!m.isIdentity()) { |
| 2945 | RectF boundingRect = attachInfo.mTmpTransformRect; |
| 2946 | boundingRect.set(dirty); |
| 2947 | m.mapRect(boundingRect); |
| 2948 | dirty.set((int) boundingRect.left, (int) boundingRect.top, |
| 2949 | (int) (boundingRect.right + 0.5f), |
| 2950 | (int) (boundingRect.bottom + 0.5f)); |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 2951 | } |
Chet Haase | c3aa361 | 2010-06-17 08:50:37 -0700 | [diff] [blame] | 2952 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2953 | } while (parent != null); |
| 2954 | } |
| 2955 | } |
| 2956 | |
| 2957 | /** |
| 2958 | * Don't call or override this method. It is used for the implementation of |
| 2959 | * the view hierarchy. |
| 2960 | * |
| 2961 | * This implementation returns null if this ViewGroup does not have a parent, |
| 2962 | * if this ViewGroup is already fully invalidated or if the dirty rectangle |
| 2963 | * does not intersect with this ViewGroup's bounds. |
| 2964 | */ |
| 2965 | public ViewParent invalidateChildInParent(final int[] location, final Rect dirty) { |
| 2966 | if (ViewDebug.TRACE_HIERARCHY) { |
| 2967 | ViewDebug.trace(this, ViewDebug.HierarchyTraceType.INVALIDATE_CHILD_IN_PARENT); |
| 2968 | } |
| 2969 | |
| 2970 | if ((mPrivateFlags & DRAWN) == DRAWN) { |
| 2971 | if ((mGroupFlags & (FLAG_OPTIMIZE_INVALIDATE | FLAG_ANIMATION_DONE)) != |
| 2972 | FLAG_OPTIMIZE_INVALIDATE) { |
| 2973 | dirty.offset(location[CHILD_LEFT_INDEX] - mScrollX, |
| 2974 | location[CHILD_TOP_INDEX] - mScrollY); |
| 2975 | |
| 2976 | final int left = mLeft; |
| 2977 | final int top = mTop; |
| 2978 | |
| 2979 | if (dirty.intersect(0, 0, mRight - left, mBottom - top) || |
| 2980 | (mPrivateFlags & DRAW_ANIMATION) == DRAW_ANIMATION) { |
| 2981 | mPrivateFlags &= ~DRAWING_CACHE_VALID; |
| 2982 | |
| 2983 | location[CHILD_LEFT_INDEX] = left; |
| 2984 | location[CHILD_TOP_INDEX] = top; |
| 2985 | |
| 2986 | return mParent; |
| 2987 | } |
| 2988 | } else { |
| 2989 | mPrivateFlags &= ~DRAWN & ~DRAWING_CACHE_VALID; |
| 2990 | |
| 2991 | location[CHILD_LEFT_INDEX] = mLeft; |
| 2992 | location[CHILD_TOP_INDEX] = mTop; |
| 2993 | |
| 2994 | dirty.set(0, 0, mRight - location[CHILD_LEFT_INDEX], |
| 2995 | mBottom - location[CHILD_TOP_INDEX]); |
| 2996 | |
| 2997 | return mParent; |
| 2998 | } |
| 2999 | } |
| 3000 | |
| 3001 | return null; |
| 3002 | } |
| 3003 | |
| 3004 | /** |
| 3005 | * Offset a rectangle that is in a descendant's coordinate |
| 3006 | * space into our coordinate space. |
| 3007 | * @param descendant A descendant of this view |
| 3008 | * @param rect A rectangle defined in descendant's coordinate space. |
| 3009 | */ |
| 3010 | public final void offsetDescendantRectToMyCoords(View descendant, Rect rect) { |
| 3011 | offsetRectBetweenParentAndChild(descendant, rect, true, false); |
| 3012 | } |
| 3013 | |
| 3014 | /** |
| 3015 | * Offset a rectangle that is in our coordinate space into an ancestor's |
| 3016 | * coordinate space. |
| 3017 | * @param descendant A descendant of this view |
| 3018 | * @param rect A rectangle defined in descendant's coordinate space. |
| 3019 | */ |
| 3020 | public final void offsetRectIntoDescendantCoords(View descendant, Rect rect) { |
| 3021 | offsetRectBetweenParentAndChild(descendant, rect, false, false); |
| 3022 | } |
| 3023 | |
| 3024 | /** |
| 3025 | * Helper method that offsets a rect either from parent to descendant or |
| 3026 | * descendant to parent. |
| 3027 | */ |
| 3028 | void offsetRectBetweenParentAndChild(View descendant, Rect rect, |
| 3029 | boolean offsetFromChildToParent, boolean clipToBounds) { |
| 3030 | |
| 3031 | // already in the same coord system :) |
| 3032 | if (descendant == this) { |
| 3033 | return; |
| 3034 | } |
| 3035 | |
| 3036 | ViewParent theParent = descendant.mParent; |
| 3037 | |
| 3038 | // search and offset up to the parent |
| 3039 | while ((theParent != null) |
| 3040 | && (theParent instanceof View) |
| 3041 | && (theParent != this)) { |
| 3042 | |
| 3043 | if (offsetFromChildToParent) { |
| 3044 | rect.offset(descendant.mLeft - descendant.mScrollX, |
| 3045 | descendant.mTop - descendant.mScrollY); |
| 3046 | if (clipToBounds) { |
| 3047 | View p = (View) theParent; |
| 3048 | rect.intersect(0, 0, p.mRight - p.mLeft, p.mBottom - p.mTop); |
| 3049 | } |
| 3050 | } else { |
| 3051 | if (clipToBounds) { |
| 3052 | View p = (View) theParent; |
| 3053 | rect.intersect(0, 0, p.mRight - p.mLeft, p.mBottom - p.mTop); |
| 3054 | } |
| 3055 | rect.offset(descendant.mScrollX - descendant.mLeft, |
| 3056 | descendant.mScrollY - descendant.mTop); |
| 3057 | } |
| 3058 | |
| 3059 | descendant = (View) theParent; |
| 3060 | theParent = descendant.mParent; |
| 3061 | } |
| 3062 | |
| 3063 | // now that we are up to this view, need to offset one more time |
| 3064 | // to get into our coordinate space |
| 3065 | if (theParent == this) { |
| 3066 | if (offsetFromChildToParent) { |
| 3067 | rect.offset(descendant.mLeft - descendant.mScrollX, |
| 3068 | descendant.mTop - descendant.mScrollY); |
| 3069 | } else { |
| 3070 | rect.offset(descendant.mScrollX - descendant.mLeft, |
| 3071 | descendant.mScrollY - descendant.mTop); |
| 3072 | } |
| 3073 | } else { |
| 3074 | throw new IllegalArgumentException("parameter must be a descendant of this view"); |
| 3075 | } |
| 3076 | } |
| 3077 | |
| 3078 | /** |
| 3079 | * Offset the vertical location of all children of this view by the specified number of pixels. |
| 3080 | * |
| 3081 | * @param offset the number of pixels to offset |
| 3082 | * |
| 3083 | * @hide |
| 3084 | */ |
| 3085 | public void offsetChildrenTopAndBottom(int offset) { |
| 3086 | final int count = mChildrenCount; |
| 3087 | final View[] children = mChildren; |
| 3088 | |
| 3089 | for (int i = 0; i < count; i++) { |
| 3090 | final View v = children[i]; |
| 3091 | v.mTop += offset; |
| 3092 | v.mBottom += offset; |
| 3093 | } |
| 3094 | } |
| 3095 | |
| 3096 | /** |
| 3097 | * {@inheritDoc} |
| 3098 | */ |
| 3099 | public boolean getChildVisibleRect(View child, Rect r, android.graphics.Point offset) { |
| 3100 | int dx = child.mLeft - mScrollX; |
| 3101 | int dy = child.mTop - mScrollY; |
| 3102 | if (offset != null) { |
| 3103 | offset.x += dx; |
| 3104 | offset.y += dy; |
| 3105 | } |
| 3106 | r.offset(dx, dy); |
| 3107 | return r.intersect(0, 0, mRight - mLeft, mBottom - mTop) && |
| 3108 | (mParent == null || mParent.getChildVisibleRect(this, r, offset)); |
| 3109 | } |
| 3110 | |
| 3111 | /** |
| 3112 | * {@inheritDoc} |
| 3113 | */ |
| 3114 | @Override |
| 3115 | protected abstract void onLayout(boolean changed, |
| 3116 | int l, int t, int r, int b); |
| 3117 | |
| 3118 | /** |
| 3119 | * Indicates whether the view group has the ability to animate its children |
| 3120 | * after the first layout. |
| 3121 | * |
| 3122 | * @return true if the children can be animated, false otherwise |
| 3123 | */ |
| 3124 | protected boolean canAnimate() { |
| 3125 | return mLayoutAnimationController != null; |
| 3126 | } |
| 3127 | |
| 3128 | /** |
| 3129 | * Runs the layout animation. Calling this method triggers a relayout of |
| 3130 | * this view group. |
| 3131 | */ |
| 3132 | public void startLayoutAnimation() { |
| 3133 | if (mLayoutAnimationController != null) { |
| 3134 | mGroupFlags |= FLAG_RUN_ANIMATION; |
| 3135 | requestLayout(); |
| 3136 | } |
| 3137 | } |
| 3138 | |
| 3139 | /** |
| 3140 | * Schedules the layout animation to be played after the next layout pass |
| 3141 | * of this view group. This can be used to restart the layout animation |
| 3142 | * when the content of the view group changes or when the activity is |
| 3143 | * paused and resumed. |
| 3144 | */ |
| 3145 | public void scheduleLayoutAnimation() { |
| 3146 | mGroupFlags |= FLAG_RUN_ANIMATION; |
| 3147 | } |
| 3148 | |
| 3149 | /** |
| 3150 | * Sets the layout animation controller used to animate the group's |
| 3151 | * children after the first layout. |
| 3152 | * |
| 3153 | * @param controller the animation controller |
| 3154 | */ |
| 3155 | public void setLayoutAnimation(LayoutAnimationController controller) { |
| 3156 | mLayoutAnimationController = controller; |
| 3157 | if (mLayoutAnimationController != null) { |
| 3158 | mGroupFlags |= FLAG_RUN_ANIMATION; |
| 3159 | } |
| 3160 | } |
| 3161 | |
| 3162 | /** |
| 3163 | * Returns the layout animation controller used to animate the group's |
| 3164 | * children. |
| 3165 | * |
| 3166 | * @return the current animation controller |
| 3167 | */ |
| 3168 | public LayoutAnimationController getLayoutAnimation() { |
| 3169 | return mLayoutAnimationController; |
| 3170 | } |
| 3171 | |
| 3172 | /** |
| 3173 | * Indicates whether the children's drawing cache is used during a layout |
| 3174 | * animation. By default, the drawing cache is enabled but this will prevent |
| 3175 | * nested layout animations from working. To nest animations, you must disable |
| 3176 | * the cache. |
| 3177 | * |
| 3178 | * @return true if the animation cache is enabled, false otherwise |
| 3179 | * |
| 3180 | * @see #setAnimationCacheEnabled(boolean) |
| 3181 | * @see View#setDrawingCacheEnabled(boolean) |
| 3182 | */ |
| 3183 | @ViewDebug.ExportedProperty |
| 3184 | public boolean isAnimationCacheEnabled() { |
| 3185 | return (mGroupFlags & FLAG_ANIMATION_CACHE) == FLAG_ANIMATION_CACHE; |
| 3186 | } |
| 3187 | |
| 3188 | /** |
| 3189 | * Enables or disables the children's drawing cache during a layout animation. |
| 3190 | * By default, the drawing cache is enabled but this will prevent nested |
| 3191 | * layout animations from working. To nest animations, you must disable the |
| 3192 | * cache. |
| 3193 | * |
| 3194 | * @param enabled true to enable the animation cache, false otherwise |
| 3195 | * |
| 3196 | * @see #isAnimationCacheEnabled() |
| 3197 | * @see View#setDrawingCacheEnabled(boolean) |
| 3198 | */ |
| 3199 | public void setAnimationCacheEnabled(boolean enabled) { |
| 3200 | setBooleanFlag(FLAG_ANIMATION_CACHE, enabled); |
| 3201 | } |
| 3202 | |
| 3203 | /** |
| 3204 | * Indicates whether this ViewGroup will always try to draw its children using their |
| 3205 | * drawing cache. By default this property is enabled. |
| 3206 | * |
| 3207 | * @return true if the animation cache is enabled, false otherwise |
| 3208 | * |
| 3209 | * @see #setAlwaysDrawnWithCacheEnabled(boolean) |
| 3210 | * @see #setChildrenDrawnWithCacheEnabled(boolean) |
| 3211 | * @see View#setDrawingCacheEnabled(boolean) |
| 3212 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 3213 | @ViewDebug.ExportedProperty(category = "drawing") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3214 | public boolean isAlwaysDrawnWithCacheEnabled() { |
| 3215 | return (mGroupFlags & FLAG_ALWAYS_DRAWN_WITH_CACHE) == FLAG_ALWAYS_DRAWN_WITH_CACHE; |
| 3216 | } |
| 3217 | |
| 3218 | /** |
| 3219 | * Indicates whether this ViewGroup will always try to draw its children using their |
| 3220 | * drawing cache. This property can be set to true when the cache rendering is |
| 3221 | * slightly different from the children's normal rendering. Renderings can be different, |
| 3222 | * for instance, when the cache's quality is set to low. |
| 3223 | * |
| 3224 | * When this property is disabled, the ViewGroup will use the drawing cache of its |
| 3225 | * children only when asked to. It's usually the task of subclasses to tell ViewGroup |
| 3226 | * when to start using the drawing cache and when to stop using it. |
| 3227 | * |
| 3228 | * @param always true to always draw with the drawing cache, false otherwise |
| 3229 | * |
| 3230 | * @see #isAlwaysDrawnWithCacheEnabled() |
| 3231 | * @see #setChildrenDrawnWithCacheEnabled(boolean) |
| 3232 | * @see View#setDrawingCacheEnabled(boolean) |
| 3233 | * @see View#setDrawingCacheQuality(int) |
| 3234 | */ |
| 3235 | public void setAlwaysDrawnWithCacheEnabled(boolean always) { |
| 3236 | setBooleanFlag(FLAG_ALWAYS_DRAWN_WITH_CACHE, always); |
| 3237 | } |
| 3238 | |
| 3239 | /** |
| 3240 | * Indicates whether the ViewGroup is currently drawing its children using |
| 3241 | * their drawing cache. |
| 3242 | * |
| 3243 | * @return true if children should be drawn with their cache, false otherwise |
| 3244 | * |
| 3245 | * @see #setAlwaysDrawnWithCacheEnabled(boolean) |
| 3246 | * @see #setChildrenDrawnWithCacheEnabled(boolean) |
| 3247 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 3248 | @ViewDebug.ExportedProperty(category = "drawing") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3249 | protected boolean isChildrenDrawnWithCacheEnabled() { |
| 3250 | return (mGroupFlags & FLAG_CHILDREN_DRAWN_WITH_CACHE) == FLAG_CHILDREN_DRAWN_WITH_CACHE; |
| 3251 | } |
| 3252 | |
| 3253 | /** |
| 3254 | * Tells the ViewGroup to draw its children using their drawing cache. This property |
| 3255 | * is ignored when {@link #isAlwaysDrawnWithCacheEnabled()} is true. A child's drawing cache |
| 3256 | * will be used only if it has been enabled. |
| 3257 | * |
| 3258 | * Subclasses should call this method to start and stop using the drawing cache when |
| 3259 | * they perform performance sensitive operations, like scrolling or animating. |
| 3260 | * |
| 3261 | * @param enabled true if children should be drawn with their cache, false otherwise |
| 3262 | * |
| 3263 | * @see #setAlwaysDrawnWithCacheEnabled(boolean) |
| 3264 | * @see #isChildrenDrawnWithCacheEnabled() |
| 3265 | */ |
| 3266 | protected void setChildrenDrawnWithCacheEnabled(boolean enabled) { |
| 3267 | setBooleanFlag(FLAG_CHILDREN_DRAWN_WITH_CACHE, enabled); |
| 3268 | } |
| 3269 | |
Romain Guy | 293451e | 2009-11-04 13:59:48 -0800 | [diff] [blame] | 3270 | /** |
| 3271 | * Indicates whether the ViewGroup is drawing its children in the order defined by |
| 3272 | * {@link #getChildDrawingOrder(int, int)}. |
| 3273 | * |
| 3274 | * @return true if children drawing order is defined by {@link #getChildDrawingOrder(int, int)}, |
| 3275 | * false otherwise |
| 3276 | * |
| 3277 | * @see #setChildrenDrawingOrderEnabled(boolean) |
| 3278 | * @see #getChildDrawingOrder(int, int) |
| 3279 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 3280 | @ViewDebug.ExportedProperty(category = "drawing") |
Romain Guy | 293451e | 2009-11-04 13:59:48 -0800 | [diff] [blame] | 3281 | protected boolean isChildrenDrawingOrderEnabled() { |
| 3282 | return (mGroupFlags & FLAG_USE_CHILD_DRAWING_ORDER) == FLAG_USE_CHILD_DRAWING_ORDER; |
| 3283 | } |
| 3284 | |
| 3285 | /** |
| 3286 | * Tells the ViewGroup whether to draw its children in the order defined by the method |
| 3287 | * {@link #getChildDrawingOrder(int, int)}. |
| 3288 | * |
| 3289 | * @param enabled true if the order of the children when drawing is determined by |
| 3290 | * {@link #getChildDrawingOrder(int, int)}, false otherwise |
| 3291 | * |
| 3292 | * @see #isChildrenDrawingOrderEnabled() |
| 3293 | * @see #getChildDrawingOrder(int, int) |
| 3294 | */ |
| 3295 | protected void setChildrenDrawingOrderEnabled(boolean enabled) { |
| 3296 | setBooleanFlag(FLAG_USE_CHILD_DRAWING_ORDER, enabled); |
| 3297 | } |
| 3298 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3299 | private void setBooleanFlag(int flag, boolean value) { |
| 3300 | if (value) { |
| 3301 | mGroupFlags |= flag; |
| 3302 | } else { |
| 3303 | mGroupFlags &= ~flag; |
| 3304 | } |
| 3305 | } |
| 3306 | |
| 3307 | /** |
| 3308 | * Returns an integer indicating what types of drawing caches are kept in memory. |
| 3309 | * |
| 3310 | * @see #setPersistentDrawingCache(int) |
| 3311 | * @see #setAnimationCacheEnabled(boolean) |
| 3312 | * |
| 3313 | * @return one or a combination of {@link #PERSISTENT_NO_CACHE}, |
| 3314 | * {@link #PERSISTENT_ANIMATION_CACHE}, {@link #PERSISTENT_SCROLLING_CACHE} |
| 3315 | * and {@link #PERSISTENT_ALL_CACHES} |
| 3316 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 3317 | @ViewDebug.ExportedProperty(category = "drawing", mapping = { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3318 | @ViewDebug.IntToString(from = PERSISTENT_NO_CACHE, to = "NONE"), |
Romain Guy | 203688c | 2010-05-12 15:41:32 -0700 | [diff] [blame] | 3319 | @ViewDebug.IntToString(from = PERSISTENT_ANIMATION_CACHE, to = "ANIMATION"), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3320 | @ViewDebug.IntToString(from = PERSISTENT_SCROLLING_CACHE, to = "SCROLLING"), |
| 3321 | @ViewDebug.IntToString(from = PERSISTENT_ALL_CACHES, to = "ALL") |
| 3322 | }) |
| 3323 | public int getPersistentDrawingCache() { |
| 3324 | return mPersistentDrawingCache; |
| 3325 | } |
| 3326 | |
| 3327 | /** |
| 3328 | * Indicates what types of drawing caches should be kept in memory after |
| 3329 | * they have been created. |
| 3330 | * |
| 3331 | * @see #getPersistentDrawingCache() |
| 3332 | * @see #setAnimationCacheEnabled(boolean) |
| 3333 | * |
| 3334 | * @param drawingCacheToKeep one or a combination of {@link #PERSISTENT_NO_CACHE}, |
| 3335 | * {@link #PERSISTENT_ANIMATION_CACHE}, {@link #PERSISTENT_SCROLLING_CACHE} |
| 3336 | * and {@link #PERSISTENT_ALL_CACHES} |
| 3337 | */ |
| 3338 | public void setPersistentDrawingCache(int drawingCacheToKeep) { |
| 3339 | mPersistentDrawingCache = drawingCacheToKeep & PERSISTENT_ALL_CACHES; |
| 3340 | } |
| 3341 | |
| 3342 | /** |
| 3343 | * Returns a new set of layout parameters based on the supplied attributes set. |
| 3344 | * |
| 3345 | * @param attrs the attributes to build the layout parameters from |
| 3346 | * |
| 3347 | * @return an instance of {@link android.view.ViewGroup.LayoutParams} or one |
| 3348 | * of its descendants |
| 3349 | */ |
| 3350 | public LayoutParams generateLayoutParams(AttributeSet attrs) { |
| 3351 | return new LayoutParams(getContext(), attrs); |
| 3352 | } |
| 3353 | |
| 3354 | /** |
| 3355 | * Returns a safe set of layout parameters based on the supplied layout params. |
| 3356 | * When a ViewGroup is passed a View whose layout params do not pass the test of |
| 3357 | * {@link #checkLayoutParams(android.view.ViewGroup.LayoutParams)}, this method |
| 3358 | * is invoked. This method should return a new set of layout params suitable for |
| 3359 | * this ViewGroup, possibly by copying the appropriate attributes from the |
| 3360 | * specified set of layout params. |
| 3361 | * |
| 3362 | * @param p The layout parameters to convert into a suitable set of layout parameters |
| 3363 | * for this ViewGroup. |
| 3364 | * |
| 3365 | * @return an instance of {@link android.view.ViewGroup.LayoutParams} or one |
| 3366 | * of its descendants |
| 3367 | */ |
| 3368 | protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) { |
| 3369 | return p; |
| 3370 | } |
| 3371 | |
| 3372 | /** |
| 3373 | * Returns a set of default layout parameters. These parameters are requested |
| 3374 | * when the View passed to {@link #addView(View)} has no layout parameters |
| 3375 | * already set. If null is returned, an exception is thrown from addView. |
| 3376 | * |
| 3377 | * @return a set of default layout parameters or null |
| 3378 | */ |
| 3379 | protected LayoutParams generateDefaultLayoutParams() { |
| 3380 | return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); |
| 3381 | } |
| 3382 | |
| 3383 | /** |
Romain Guy | 13922e0 | 2009-05-12 17:56:14 -0700 | [diff] [blame] | 3384 | * @hide |
| 3385 | */ |
| 3386 | @Override |
| 3387 | protected boolean dispatchConsistencyCheck(int consistency) { |
| 3388 | boolean result = super.dispatchConsistencyCheck(consistency); |
| 3389 | |
| 3390 | final int count = mChildrenCount; |
| 3391 | final View[] children = mChildren; |
| 3392 | for (int i = 0; i < count; i++) { |
| 3393 | if (!children[i].dispatchConsistencyCheck(consistency)) result = false; |
| 3394 | } |
| 3395 | |
| 3396 | return result; |
| 3397 | } |
| 3398 | |
| 3399 | /** |
| 3400 | * @hide |
| 3401 | */ |
| 3402 | @Override |
| 3403 | protected boolean onConsistencyCheck(int consistency) { |
| 3404 | boolean result = super.onConsistencyCheck(consistency); |
| 3405 | |
| 3406 | final boolean checkLayout = (consistency & ViewDebug.CONSISTENCY_LAYOUT) != 0; |
| 3407 | final boolean checkDrawing = (consistency & ViewDebug.CONSISTENCY_DRAWING) != 0; |
| 3408 | |
| 3409 | if (checkLayout) { |
| 3410 | final int count = mChildrenCount; |
| 3411 | final View[] children = mChildren; |
| 3412 | for (int i = 0; i < count; i++) { |
| 3413 | if (children[i].getParent() != this) { |
| 3414 | result = false; |
| 3415 | android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG, |
| 3416 | "View " + children[i] + " has no parent/a parent that is not " + this); |
| 3417 | } |
| 3418 | } |
| 3419 | } |
| 3420 | |
| 3421 | if (checkDrawing) { |
| 3422 | // If this group is dirty, check that the parent is dirty as well |
| 3423 | if ((mPrivateFlags & DIRTY_MASK) != 0) { |
| 3424 | final ViewParent parent = getParent(); |
| 3425 | if (parent != null && !(parent instanceof ViewRoot)) { |
| 3426 | if ((((View) parent).mPrivateFlags & DIRTY_MASK) == 0) { |
| 3427 | result = false; |
| 3428 | android.util.Log.d(ViewDebug.CONSISTENCY_LOG_TAG, |
| 3429 | "ViewGroup " + this + " is dirty but its parent is not: " + this); |
| 3430 | } |
| 3431 | } |
| 3432 | } |
| 3433 | } |
| 3434 | |
| 3435 | return result; |
| 3436 | } |
| 3437 | |
| 3438 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3439 | * {@inheritDoc} |
| 3440 | */ |
| 3441 | @Override |
| 3442 | protected void debug(int depth) { |
| 3443 | super.debug(depth); |
| 3444 | String output; |
| 3445 | |
| 3446 | if (mFocused != null) { |
| 3447 | output = debugIndent(depth); |
| 3448 | output += "mFocused"; |
| 3449 | Log.d(VIEW_LOG_TAG, output); |
| 3450 | } |
| 3451 | if (mChildrenCount != 0) { |
| 3452 | output = debugIndent(depth); |
| 3453 | output += "{"; |
| 3454 | Log.d(VIEW_LOG_TAG, output); |
| 3455 | } |
| 3456 | int count = mChildrenCount; |
| 3457 | for (int i = 0; i < count; i++) { |
| 3458 | View child = mChildren[i]; |
| 3459 | child.debug(depth + 1); |
| 3460 | } |
| 3461 | |
| 3462 | if (mChildrenCount != 0) { |
| 3463 | output = debugIndent(depth); |
| 3464 | output += "}"; |
| 3465 | Log.d(VIEW_LOG_TAG, output); |
| 3466 | } |
| 3467 | } |
| 3468 | |
| 3469 | /** |
| 3470 | * Returns the position in the group of the specified child view. |
| 3471 | * |
| 3472 | * @param child the view for which to get the position |
| 3473 | * @return a positive integer representing the position of the view in the |
| 3474 | * group, or -1 if the view does not exist in the group |
| 3475 | */ |
| 3476 | public int indexOfChild(View child) { |
| 3477 | final int count = mChildrenCount; |
| 3478 | final View[] children = mChildren; |
| 3479 | for (int i = 0; i < count; i++) { |
| 3480 | if (children[i] == child) { |
| 3481 | return i; |
| 3482 | } |
| 3483 | } |
| 3484 | return -1; |
| 3485 | } |
| 3486 | |
| 3487 | /** |
| 3488 | * Returns the number of children in the group. |
| 3489 | * |
| 3490 | * @return a positive integer representing the number of children in |
| 3491 | * the group |
| 3492 | */ |
| 3493 | public int getChildCount() { |
| 3494 | return mChildrenCount; |
| 3495 | } |
| 3496 | |
| 3497 | /** |
| 3498 | * Returns the view at the specified position in the group. |
| 3499 | * |
| 3500 | * @param index the position at which to get the view from |
| 3501 | * @return the view at the specified position or null if the position |
| 3502 | * does not exist within the group |
| 3503 | */ |
| 3504 | public View getChildAt(int index) { |
| 3505 | try { |
| 3506 | return mChildren[index]; |
| 3507 | } catch (IndexOutOfBoundsException ex) { |
| 3508 | return null; |
| 3509 | } |
| 3510 | } |
| 3511 | |
| 3512 | /** |
| 3513 | * Ask all of the children of this view to measure themselves, taking into |
| 3514 | * account both the MeasureSpec requirements for this view and its padding. |
| 3515 | * We skip children that are in the GONE state The heavy lifting is done in |
| 3516 | * getChildMeasureSpec. |
| 3517 | * |
| 3518 | * @param widthMeasureSpec The width requirements for this view |
| 3519 | * @param heightMeasureSpec The height requirements for this view |
| 3520 | */ |
| 3521 | protected void measureChildren(int widthMeasureSpec, int heightMeasureSpec) { |
| 3522 | final int size = mChildrenCount; |
| 3523 | final View[] children = mChildren; |
| 3524 | for (int i = 0; i < size; ++i) { |
| 3525 | final View child = children[i]; |
| 3526 | if ((child.mViewFlags & VISIBILITY_MASK) != GONE) { |
| 3527 | measureChild(child, widthMeasureSpec, heightMeasureSpec); |
| 3528 | } |
| 3529 | } |
| 3530 | } |
| 3531 | |
| 3532 | /** |
| 3533 | * Ask one of the children of this view to measure itself, taking into |
| 3534 | * account both the MeasureSpec requirements for this view and its padding. |
| 3535 | * The heavy lifting is done in getChildMeasureSpec. |
| 3536 | * |
| 3537 | * @param child The child to measure |
| 3538 | * @param parentWidthMeasureSpec The width requirements for this view |
| 3539 | * @param parentHeightMeasureSpec The height requirements for this view |
| 3540 | */ |
| 3541 | protected void measureChild(View child, int parentWidthMeasureSpec, |
| 3542 | int parentHeightMeasureSpec) { |
| 3543 | final LayoutParams lp = child.getLayoutParams(); |
| 3544 | |
| 3545 | final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec, |
| 3546 | mPaddingLeft + mPaddingRight, lp.width); |
| 3547 | final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec, |
| 3548 | mPaddingTop + mPaddingBottom, lp.height); |
| 3549 | |
| 3550 | child.measure(childWidthMeasureSpec, childHeightMeasureSpec); |
| 3551 | } |
| 3552 | |
| 3553 | /** |
| 3554 | * Ask one of the children of this view to measure itself, taking into |
| 3555 | * account both the MeasureSpec requirements for this view and its padding |
| 3556 | * and margins. The child must have MarginLayoutParams The heavy lifting is |
| 3557 | * done in getChildMeasureSpec. |
| 3558 | * |
| 3559 | * @param child The child to measure |
| 3560 | * @param parentWidthMeasureSpec The width requirements for this view |
| 3561 | * @param widthUsed Extra space that has been used up by the parent |
| 3562 | * horizontally (possibly by other children of the parent) |
| 3563 | * @param parentHeightMeasureSpec The height requirements for this view |
| 3564 | * @param heightUsed Extra space that has been used up by the parent |
| 3565 | * vertically (possibly by other children of the parent) |
| 3566 | */ |
| 3567 | protected void measureChildWithMargins(View child, |
| 3568 | int parentWidthMeasureSpec, int widthUsed, |
| 3569 | int parentHeightMeasureSpec, int heightUsed) { |
| 3570 | final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams(); |
| 3571 | |
| 3572 | final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec, |
| 3573 | mPaddingLeft + mPaddingRight + lp.leftMargin + lp.rightMargin |
| 3574 | + widthUsed, lp.width); |
| 3575 | final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec, |
| 3576 | mPaddingTop + mPaddingBottom + lp.topMargin + lp.bottomMargin |
| 3577 | + heightUsed, lp.height); |
| 3578 | |
| 3579 | child.measure(childWidthMeasureSpec, childHeightMeasureSpec); |
| 3580 | } |
| 3581 | |
| 3582 | /** |
| 3583 | * Does the hard part of measureChildren: figuring out the MeasureSpec to |
| 3584 | * pass to a particular child. This method figures out the right MeasureSpec |
| 3585 | * for one dimension (height or width) of one child view. |
| 3586 | * |
| 3587 | * The goal is to combine information from our MeasureSpec with the |
| 3588 | * LayoutParams of the child to get the best possible results. For example, |
| 3589 | * if the this view knows its size (because its MeasureSpec has a mode of |
| 3590 | * EXACTLY), and the child has indicated in its LayoutParams that it wants |
| 3591 | * to be the same size as the parent, the parent should ask the child to |
| 3592 | * layout given an exact size. |
| 3593 | * |
| 3594 | * @param spec The requirements for this view |
| 3595 | * @param padding The padding of this view for the current dimension and |
| 3596 | * margins, if applicable |
| 3597 | * @param childDimension How big the child wants to be in the current |
| 3598 | * dimension |
| 3599 | * @return a MeasureSpec integer for the child |
| 3600 | */ |
| 3601 | public static int getChildMeasureSpec(int spec, int padding, int childDimension) { |
| 3602 | int specMode = MeasureSpec.getMode(spec); |
| 3603 | int specSize = MeasureSpec.getSize(spec); |
| 3604 | |
| 3605 | int size = Math.max(0, specSize - padding); |
| 3606 | |
| 3607 | int resultSize = 0; |
| 3608 | int resultMode = 0; |
| 3609 | |
| 3610 | switch (specMode) { |
| 3611 | // Parent has imposed an exact size on us |
| 3612 | case MeasureSpec.EXACTLY: |
| 3613 | if (childDimension >= 0) { |
| 3614 | resultSize = childDimension; |
| 3615 | resultMode = MeasureSpec.EXACTLY; |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 3616 | } else if (childDimension == LayoutParams.MATCH_PARENT) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3617 | // Child wants to be our size. So be it. |
| 3618 | resultSize = size; |
| 3619 | resultMode = MeasureSpec.EXACTLY; |
| 3620 | } else if (childDimension == LayoutParams.WRAP_CONTENT) { |
| 3621 | // Child wants to determine its own size. It can't be |
| 3622 | // bigger than us. |
| 3623 | resultSize = size; |
| 3624 | resultMode = MeasureSpec.AT_MOST; |
| 3625 | } |
| 3626 | break; |
| 3627 | |
| 3628 | // Parent has imposed a maximum size on us |
| 3629 | case MeasureSpec.AT_MOST: |
| 3630 | if (childDimension >= 0) { |
| 3631 | // Child wants a specific size... so be it |
| 3632 | resultSize = childDimension; |
| 3633 | resultMode = MeasureSpec.EXACTLY; |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 3634 | } else if (childDimension == LayoutParams.MATCH_PARENT) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3635 | // Child wants to be our size, but our size is not fixed. |
| 3636 | // Constrain child to not be bigger than us. |
| 3637 | resultSize = size; |
| 3638 | resultMode = MeasureSpec.AT_MOST; |
| 3639 | } else if (childDimension == LayoutParams.WRAP_CONTENT) { |
| 3640 | // Child wants to determine its own size. It can't be |
| 3641 | // bigger than us. |
| 3642 | resultSize = size; |
| 3643 | resultMode = MeasureSpec.AT_MOST; |
| 3644 | } |
| 3645 | break; |
| 3646 | |
| 3647 | // Parent asked to see how big we want to be |
| 3648 | case MeasureSpec.UNSPECIFIED: |
| 3649 | if (childDimension >= 0) { |
| 3650 | // Child wants a specific size... let him have it |
| 3651 | resultSize = childDimension; |
| 3652 | resultMode = MeasureSpec.EXACTLY; |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 3653 | } else if (childDimension == LayoutParams.MATCH_PARENT) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3654 | // Child wants to be our size... find out how big it should |
| 3655 | // be |
| 3656 | resultSize = 0; |
| 3657 | resultMode = MeasureSpec.UNSPECIFIED; |
| 3658 | } else if (childDimension == LayoutParams.WRAP_CONTENT) { |
| 3659 | // Child wants to determine its own size.... find out how |
| 3660 | // big it should be |
| 3661 | resultSize = 0; |
| 3662 | resultMode = MeasureSpec.UNSPECIFIED; |
| 3663 | } |
| 3664 | break; |
| 3665 | } |
| 3666 | return MeasureSpec.makeMeasureSpec(resultSize, resultMode); |
| 3667 | } |
| 3668 | |
| 3669 | |
| 3670 | /** |
| 3671 | * Removes any pending animations for views that have been removed. Call |
| 3672 | * this if you don't want animations for exiting views to stack up. |
| 3673 | */ |
| 3674 | public void clearDisappearingChildren() { |
| 3675 | if (mDisappearingChildren != null) { |
| 3676 | mDisappearingChildren.clear(); |
| 3677 | } |
| 3678 | } |
| 3679 | |
| 3680 | /** |
| 3681 | * Add a view which is removed from mChildren but still needs animation |
| 3682 | * |
| 3683 | * @param v View to add |
| 3684 | */ |
| 3685 | private void addDisappearingView(View v) { |
| 3686 | ArrayList<View> disappearingChildren = mDisappearingChildren; |
| 3687 | |
| 3688 | if (disappearingChildren == null) { |
| 3689 | disappearingChildren = mDisappearingChildren = new ArrayList<View>(); |
| 3690 | } |
| 3691 | |
| 3692 | disappearingChildren.add(v); |
| 3693 | } |
| 3694 | |
| 3695 | /** |
| 3696 | * Cleanup a view when its animation is done. This may mean removing it from |
| 3697 | * the list of disappearing views. |
| 3698 | * |
| 3699 | * @param view The view whose animation has finished |
| 3700 | * @param animation The animation, cannot be null |
| 3701 | */ |
| 3702 | private void finishAnimatingView(final View view, Animation animation) { |
| 3703 | final ArrayList<View> disappearingChildren = mDisappearingChildren; |
| 3704 | if (disappearingChildren != null) { |
| 3705 | if (disappearingChildren.contains(view)) { |
| 3706 | disappearingChildren.remove(view); |
| 3707 | |
| 3708 | if (view.mAttachInfo != null) { |
| 3709 | view.dispatchDetachedFromWindow(); |
| 3710 | } |
| 3711 | |
| 3712 | view.clearAnimation(); |
| 3713 | mGroupFlags |= FLAG_INVALIDATE_REQUIRED; |
| 3714 | } |
| 3715 | } |
| 3716 | |
| 3717 | if (animation != null && !animation.getFillAfter()) { |
| 3718 | view.clearAnimation(); |
| 3719 | } |
| 3720 | |
| 3721 | if ((view.mPrivateFlags & ANIMATION_STARTED) == ANIMATION_STARTED) { |
| 3722 | view.onAnimationEnd(); |
| 3723 | // Should be performed by onAnimationEnd() but this avoid an infinite loop, |
| 3724 | // so we'd rather be safe than sorry |
| 3725 | view.mPrivateFlags &= ~ANIMATION_STARTED; |
| 3726 | // Draw one more frame after the animation is done |
| 3727 | mGroupFlags |= FLAG_INVALIDATE_REQUIRED; |
| 3728 | } |
| 3729 | } |
| 3730 | |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 3731 | /** |
| 3732 | * This method tells the ViewGroup that the given View object, which should have this |
| 3733 | * ViewGroup as its parent, |
| 3734 | * should be kept around (re-displayed when the ViewGroup draws its children) even if it |
| 3735 | * is removed from its parent. This allows animations, such as those used by |
| 3736 | * {@link android.app.Fragment} and {@link android.animation.LayoutTransition} to animate |
| 3737 | * the removal of views. A call to this method should always be accompanied by a later call |
| 3738 | * to {@link #endViewTransition(View)}, such as after an animation on the View has finished, |
| 3739 | * so that the View finally gets removed. |
| 3740 | * |
| 3741 | * @param view The View object to be kept visible even if it gets removed from its parent. |
| 3742 | */ |
| 3743 | public void startViewTransition(View view) { |
| 3744 | if (view.mParent == this) { |
| 3745 | if (mTransitioningViews == null) { |
| 3746 | mTransitioningViews = new ArrayList<View>(); |
| 3747 | } |
| 3748 | mTransitioningViews.add(view); |
| 3749 | } |
| 3750 | } |
| 3751 | |
| 3752 | /** |
| 3753 | * This method should always be called following an earlier call to |
| 3754 | * {@link #startViewTransition(View)}. The given View is finally removed from its parent |
| 3755 | * and will no longer be displayed. Note that this method does not perform the functionality |
| 3756 | * of removing a view from its parent; it just discontinues the display of a View that |
| 3757 | * has previously been removed. |
| 3758 | * |
| 3759 | * @return view The View object that has been removed but is being kept around in the visible |
| 3760 | * hierarchy by an earlier call to {@link #startViewTransition(View)}. |
| 3761 | */ |
| 3762 | public void endViewTransition(View view) { |
| 3763 | if (mTransitioningViews != null) { |
| 3764 | mTransitioningViews.remove(view); |
| 3765 | final ArrayList<View> disappearingChildren = mDisappearingChildren; |
| 3766 | if (disappearingChildren != null && disappearingChildren.contains(view)) { |
| 3767 | disappearingChildren.remove(view); |
| 3768 | if (view.mAttachInfo != null) { |
| 3769 | view.dispatchDetachedFromWindow(); |
| 3770 | } |
| 3771 | if (view.mParent != null) { |
| 3772 | view.mParent = null; |
| 3773 | } |
| 3774 | mGroupFlags |= FLAG_INVALIDATE_REQUIRED; |
| 3775 | } |
| 3776 | } |
| 3777 | } |
| 3778 | |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 3779 | private LayoutTransition.TransitionListener mLayoutTransitionListener = |
| 3780 | new LayoutTransition.TransitionListener() { |
| 3781 | @Override |
| 3782 | public void startTransition(LayoutTransition transition, ViewGroup container, |
| 3783 | View view, int transitionType) { |
| 3784 | // We only care about disappearing items, since we need special logic to keep |
| 3785 | // those items visible after they've been 'removed' |
| 3786 | if (transitionType == LayoutTransition.DISAPPEARING) { |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 3787 | startViewTransition(view); |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 3788 | } |
| 3789 | } |
| 3790 | |
| 3791 | @Override |
| 3792 | public void endTransition(LayoutTransition transition, ViewGroup container, |
| 3793 | View view, int transitionType) { |
| 3794 | if (transitionType == LayoutTransition.DISAPPEARING && mTransitioningViews != null) { |
Chet Haase | b20db3e | 2010-09-10 13:07:30 -0700 | [diff] [blame] | 3795 | endViewTransition(view); |
Chet Haase | 21cd138 | 2010-09-01 17:42:29 -0700 | [diff] [blame] | 3796 | } |
| 3797 | } |
| 3798 | }; |
| 3799 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3800 | /** |
| 3801 | * {@inheritDoc} |
| 3802 | */ |
| 3803 | @Override |
| 3804 | public boolean gatherTransparentRegion(Region region) { |
| 3805 | // If no transparent regions requested, we are always opaque. |
| 3806 | final boolean meOpaque = (mPrivateFlags & View.REQUEST_TRANSPARENT_REGIONS) == 0; |
| 3807 | if (meOpaque && region == null) { |
| 3808 | // The caller doesn't care about the region, so stop now. |
| 3809 | return true; |
| 3810 | } |
| 3811 | super.gatherTransparentRegion(region); |
| 3812 | final View[] children = mChildren; |
| 3813 | final int count = mChildrenCount; |
| 3814 | boolean noneOfTheChildrenAreTransparent = true; |
| 3815 | for (int i = 0; i < count; i++) { |
| 3816 | final View child = children[i]; |
| 3817 | if ((child.mViewFlags & VISIBILITY_MASK) != GONE || child.getAnimation() != null) { |
| 3818 | if (!child.gatherTransparentRegion(region)) { |
| 3819 | noneOfTheChildrenAreTransparent = false; |
| 3820 | } |
| 3821 | } |
| 3822 | } |
| 3823 | return meOpaque || noneOfTheChildrenAreTransparent; |
| 3824 | } |
| 3825 | |
| 3826 | /** |
| 3827 | * {@inheritDoc} |
| 3828 | */ |
| 3829 | public void requestTransparentRegion(View child) { |
| 3830 | if (child != null) { |
| 3831 | child.mPrivateFlags |= View.REQUEST_TRANSPARENT_REGIONS; |
| 3832 | if (mParent != null) { |
| 3833 | mParent.requestTransparentRegion(this); |
| 3834 | } |
| 3835 | } |
| 3836 | } |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 3837 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3838 | |
| 3839 | @Override |
| 3840 | protected boolean fitSystemWindows(Rect insets) { |
| 3841 | boolean done = super.fitSystemWindows(insets); |
| 3842 | if (!done) { |
| 3843 | final int count = mChildrenCount; |
| 3844 | final View[] children = mChildren; |
| 3845 | for (int i = 0; i < count; i++) { |
| 3846 | done = children[i].fitSystemWindows(insets); |
| 3847 | if (done) { |
| 3848 | break; |
| 3849 | } |
| 3850 | } |
| 3851 | } |
| 3852 | return done; |
| 3853 | } |
| 3854 | |
| 3855 | /** |
| 3856 | * Returns the animation listener to which layout animation events are |
| 3857 | * sent. |
| 3858 | * |
| 3859 | * @return an {@link android.view.animation.Animation.AnimationListener} |
| 3860 | */ |
| 3861 | public Animation.AnimationListener getLayoutAnimationListener() { |
| 3862 | return mAnimationListener; |
| 3863 | } |
| 3864 | |
| 3865 | @Override |
| 3866 | protected void drawableStateChanged() { |
| 3867 | super.drawableStateChanged(); |
| 3868 | |
| 3869 | if ((mGroupFlags & FLAG_NOTIFY_CHILDREN_ON_DRAWABLE_STATE_CHANGE) != 0) { |
| 3870 | if ((mGroupFlags & FLAG_ADD_STATES_FROM_CHILDREN) != 0) { |
| 3871 | throw new IllegalStateException("addStateFromChildren cannot be enabled if a" |
| 3872 | + " child has duplicateParentState set to true"); |
| 3873 | } |
| 3874 | |
| 3875 | final View[] children = mChildren; |
| 3876 | final int count = mChildrenCount; |
| 3877 | |
| 3878 | for (int i = 0; i < count; i++) { |
| 3879 | final View child = children[i]; |
| 3880 | if ((child.mViewFlags & DUPLICATE_PARENT_STATE) != 0) { |
| 3881 | child.refreshDrawableState(); |
| 3882 | } |
| 3883 | } |
| 3884 | } |
| 3885 | } |
| 3886 | |
| 3887 | @Override |
| 3888 | protected int[] onCreateDrawableState(int extraSpace) { |
| 3889 | if ((mGroupFlags & FLAG_ADD_STATES_FROM_CHILDREN) == 0) { |
| 3890 | return super.onCreateDrawableState(extraSpace); |
| 3891 | } |
| 3892 | |
| 3893 | int need = 0; |
| 3894 | int n = getChildCount(); |
| 3895 | for (int i = 0; i < n; i++) { |
| 3896 | int[] childState = getChildAt(i).getDrawableState(); |
| 3897 | |
| 3898 | if (childState != null) { |
| 3899 | need += childState.length; |
| 3900 | } |
| 3901 | } |
| 3902 | |
| 3903 | int[] state = super.onCreateDrawableState(extraSpace + need); |
| 3904 | |
| 3905 | for (int i = 0; i < n; i++) { |
| 3906 | int[] childState = getChildAt(i).getDrawableState(); |
| 3907 | |
| 3908 | if (childState != null) { |
| 3909 | state = mergeDrawableStates(state, childState); |
| 3910 | } |
| 3911 | } |
| 3912 | |
| 3913 | return state; |
| 3914 | } |
| 3915 | |
| 3916 | /** |
| 3917 | * Sets whether this ViewGroup's drawable states also include |
| 3918 | * its children's drawable states. This is used, for example, to |
| 3919 | * make a group appear to be focused when its child EditText or button |
| 3920 | * is focused. |
| 3921 | */ |
| 3922 | public void setAddStatesFromChildren(boolean addsStates) { |
| 3923 | if (addsStates) { |
| 3924 | mGroupFlags |= FLAG_ADD_STATES_FROM_CHILDREN; |
| 3925 | } else { |
| 3926 | mGroupFlags &= ~FLAG_ADD_STATES_FROM_CHILDREN; |
| 3927 | } |
| 3928 | |
| 3929 | refreshDrawableState(); |
| 3930 | } |
| 3931 | |
| 3932 | /** |
| 3933 | * Returns whether this ViewGroup's drawable states also include |
| 3934 | * its children's drawable states. This is used, for example, to |
| 3935 | * make a group appear to be focused when its child EditText or button |
| 3936 | * is focused. |
| 3937 | */ |
| 3938 | public boolean addStatesFromChildren() { |
| 3939 | return (mGroupFlags & FLAG_ADD_STATES_FROM_CHILDREN) != 0; |
| 3940 | } |
| 3941 | |
| 3942 | /** |
| 3943 | * If {link #addStatesFromChildren} is true, refreshes this group's |
| 3944 | * drawable state (to include the states from its children). |
| 3945 | */ |
| 3946 | public void childDrawableStateChanged(View child) { |
| 3947 | if ((mGroupFlags & FLAG_ADD_STATES_FROM_CHILDREN) != 0) { |
| 3948 | refreshDrawableState(); |
| 3949 | } |
| 3950 | } |
| 3951 | |
| 3952 | /** |
| 3953 | * Specifies the animation listener to which layout animation events must |
| 3954 | * be sent. Only |
| 3955 | * {@link android.view.animation.Animation.AnimationListener#onAnimationStart(Animation)} |
| 3956 | * and |
| 3957 | * {@link android.view.animation.Animation.AnimationListener#onAnimationEnd(Animation)} |
| 3958 | * are invoked. |
| 3959 | * |
| 3960 | * @param animationListener the layout animation listener |
| 3961 | */ |
| 3962 | public void setLayoutAnimationListener(Animation.AnimationListener animationListener) { |
| 3963 | mAnimationListener = animationListener; |
| 3964 | } |
| 3965 | |
| 3966 | /** |
| 3967 | * LayoutParams are used by views to tell their parents how they want to be |
| 3968 | * laid out. See |
| 3969 | * {@link android.R.styleable#ViewGroup_Layout ViewGroup Layout Attributes} |
| 3970 | * for a list of all child view attributes that this class supports. |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 3971 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3972 | * <p> |
| 3973 | * The base LayoutParams class just describes how big the view wants to be |
| 3974 | * for both width and height. For each dimension, it can specify one of: |
| 3975 | * <ul> |
Dirk Dougherty | 75c66da | 2010-03-25 16:33:33 -0700 | [diff] [blame] | 3976 | * <li>FILL_PARENT (renamed MATCH_PARENT in API Level 8 and higher), which |
| 3977 | * 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] | 3978 | * <li> WRAP_CONTENT, which means that the view wants to be just big enough |
| 3979 | * to enclose its content (plus padding) |
Dirk Dougherty | 75c66da | 2010-03-25 16:33:33 -0700 | [diff] [blame] | 3980 | * <li> an exact number |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3981 | * </ul> |
| 3982 | * There are subclasses of LayoutParams for different subclasses of |
| 3983 | * ViewGroup. For example, AbsoluteLayout has its own subclass of |
| 3984 | * LayoutParams which adds an X and Y value. |
| 3985 | * |
| 3986 | * @attr ref android.R.styleable#ViewGroup_Layout_layout_height |
| 3987 | * @attr ref android.R.styleable#ViewGroup_Layout_layout_width |
| 3988 | */ |
| 3989 | public static class LayoutParams { |
| 3990 | /** |
Dirk Dougherty | 75c66da | 2010-03-25 16:33:33 -0700 | [diff] [blame] | 3991 | * Special value for the height or width requested by a View. |
| 3992 | * FILL_PARENT means that the view wants to be as big as its parent, |
| 3993 | * minus the parent's padding, if any. This value is deprecated |
| 3994 | * 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] | 3995 | */ |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 3996 | @SuppressWarnings({"UnusedDeclaration"}) |
| 3997 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 3998 | public static final int FILL_PARENT = -1; |
| 3999 | |
| 4000 | /** |
| 4001 | * Special value for the height or width requested by a View. |
Gilles Debunne | f5c6eff | 2010-02-09 19:08:36 -0800 | [diff] [blame] | 4002 | * 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] | 4003 | * minus the parent's padding, if any. Introduced in API Level 8. |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 4004 | */ |
| 4005 | public static final int MATCH_PARENT = -1; |
| 4006 | |
| 4007 | /** |
| 4008 | * 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] | 4009 | * WRAP_CONTENT means that the view wants to be just large enough to fit |
| 4010 | * its own internal content, taking its own padding into account. |
| 4011 | */ |
| 4012 | public static final int WRAP_CONTENT = -2; |
| 4013 | |
| 4014 | /** |
Dirk Dougherty | 75c66da | 2010-03-25 16:33:33 -0700 | [diff] [blame] | 4015 | * Information about how wide the view wants to be. Can be one of the |
| 4016 | * constants FILL_PARENT (replaced by MATCH_PARENT , |
| 4017 | * 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] | 4018 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 4019 | @ViewDebug.ExportedProperty(category = "layout", mapping = { |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 4020 | @ViewDebug.IntToString(from = MATCH_PARENT, to = "MATCH_PARENT"), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4021 | @ViewDebug.IntToString(from = WRAP_CONTENT, to = "WRAP_CONTENT") |
| 4022 | }) |
| 4023 | public int width; |
| 4024 | |
| 4025 | /** |
Dirk Dougherty | 75c66da | 2010-03-25 16:33:33 -0700 | [diff] [blame] | 4026 | * Information about how tall the view wants to be. Can be one of the |
| 4027 | * constants FILL_PARENT (replaced by MATCH_PARENT , |
| 4028 | * 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] | 4029 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 4030 | @ViewDebug.ExportedProperty(category = "layout", mapping = { |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 4031 | @ViewDebug.IntToString(from = MATCH_PARENT, to = "MATCH_PARENT"), |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4032 | @ViewDebug.IntToString(from = WRAP_CONTENT, to = "WRAP_CONTENT") |
| 4033 | }) |
| 4034 | public int height; |
| 4035 | |
| 4036 | /** |
| 4037 | * Used to animate layouts. |
| 4038 | */ |
| 4039 | public LayoutAnimationController.AnimationParameters layoutAnimationParameters; |
| 4040 | |
| 4041 | /** |
| 4042 | * Creates a new set of layout parameters. The values are extracted from |
| 4043 | * the supplied attributes set and context. The XML attributes mapped |
| 4044 | * to this set of layout parameters are: |
| 4045 | * |
| 4046 | * <ul> |
| 4047 | * <li><code>layout_width</code>: the width, either an exact value, |
Dirk Dougherty | 75c66da | 2010-03-25 16:33:33 -0700 | [diff] [blame] | 4048 | * {@link #WRAP_CONTENT}, or {@link #FILL_PARENT} (replaced by |
| 4049 | * {@link #MATCH_PARENT} in API Level 8)</li> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4050 | * <li><code>layout_height</code>: the height, either an exact value, |
Dirk Dougherty | 75c66da | 2010-03-25 16:33:33 -0700 | [diff] [blame] | 4051 | * {@link #WRAP_CONTENT}, or {@link #FILL_PARENT} (replaced by |
| 4052 | * {@link #MATCH_PARENT} in API Level 8)</li> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4053 | * </ul> |
| 4054 | * |
| 4055 | * @param c the application environment |
| 4056 | * @param attrs the set of attributes from which to extract the layout |
| 4057 | * parameters' values |
| 4058 | */ |
| 4059 | public LayoutParams(Context c, AttributeSet attrs) { |
| 4060 | TypedArray a = c.obtainStyledAttributes(attrs, R.styleable.ViewGroup_Layout); |
| 4061 | setBaseAttributes(a, |
| 4062 | R.styleable.ViewGroup_Layout_layout_width, |
| 4063 | R.styleable.ViewGroup_Layout_layout_height); |
| 4064 | a.recycle(); |
| 4065 | } |
| 4066 | |
| 4067 | /** |
| 4068 | * Creates a new set of layout parameters with the specified width |
| 4069 | * and height. |
| 4070 | * |
Dirk Dougherty | 75c66da | 2010-03-25 16:33:33 -0700 | [diff] [blame] | 4071 | * @param width the width, either {@link #WRAP_CONTENT}, |
| 4072 | * {@link #FILL_PARENT} (replaced by {@link #MATCH_PARENT} in |
| 4073 | * API Level 8), or a fixed size in pixels |
| 4074 | * @param height the height, either {@link #WRAP_CONTENT}, |
| 4075 | * {@link #FILL_PARENT} (replaced by {@link #MATCH_PARENT} in |
| 4076 | * API Level 8), or a fixed size in pixels |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4077 | */ |
| 4078 | public LayoutParams(int width, int height) { |
| 4079 | this.width = width; |
| 4080 | this.height = height; |
| 4081 | } |
| 4082 | |
| 4083 | /** |
| 4084 | * Copy constructor. Clones the width and height values of the source. |
| 4085 | * |
| 4086 | * @param source The layout params to copy from. |
| 4087 | */ |
| 4088 | public LayoutParams(LayoutParams source) { |
| 4089 | this.width = source.width; |
| 4090 | this.height = source.height; |
| 4091 | } |
| 4092 | |
| 4093 | /** |
| 4094 | * Used internally by MarginLayoutParams. |
| 4095 | * @hide |
| 4096 | */ |
| 4097 | LayoutParams() { |
| 4098 | } |
| 4099 | |
| 4100 | /** |
| 4101 | * Extracts the layout parameters from the supplied attributes. |
| 4102 | * |
| 4103 | * @param a the style attributes to extract the parameters from |
| 4104 | * @param widthAttr the identifier of the width attribute |
| 4105 | * @param heightAttr the identifier of the height attribute |
| 4106 | */ |
| 4107 | protected void setBaseAttributes(TypedArray a, int widthAttr, int heightAttr) { |
| 4108 | width = a.getLayoutDimension(widthAttr, "layout_width"); |
| 4109 | height = a.getLayoutDimension(heightAttr, "layout_height"); |
| 4110 | } |
| 4111 | |
| 4112 | /** |
| 4113 | * Returns a String representation of this set of layout parameters. |
| 4114 | * |
| 4115 | * @param output the String to prepend to the internal representation |
| 4116 | * @return a String with the following format: output + |
| 4117 | * "ViewGroup.LayoutParams={ width=WIDTH, height=HEIGHT }" |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 4118 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4119 | * @hide |
| 4120 | */ |
| 4121 | public String debug(String output) { |
| 4122 | return output + "ViewGroup.LayoutParams={ width=" |
| 4123 | + sizeToString(width) + ", height=" + sizeToString(height) + " }"; |
| 4124 | } |
| 4125 | |
| 4126 | /** |
| 4127 | * Converts the specified size to a readable String. |
| 4128 | * |
| 4129 | * @param size the size to convert |
| 4130 | * @return a String instance representing the supplied size |
Romain Guy | 8506ab4 | 2009-06-11 17:35:47 -0700 | [diff] [blame] | 4131 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4132 | * @hide |
| 4133 | */ |
| 4134 | protected static String sizeToString(int size) { |
| 4135 | if (size == WRAP_CONTENT) { |
| 4136 | return "wrap-content"; |
| 4137 | } |
Romain Guy | 980a938 | 2010-01-08 15:06:28 -0800 | [diff] [blame] | 4138 | if (size == MATCH_PARENT) { |
| 4139 | return "match-parent"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4140 | } |
| 4141 | return String.valueOf(size); |
| 4142 | } |
| 4143 | } |
| 4144 | |
| 4145 | /** |
| 4146 | * Per-child layout information for layouts that support margins. |
| 4147 | * See |
| 4148 | * {@link android.R.styleable#ViewGroup_MarginLayout ViewGroup Margin Layout Attributes} |
| 4149 | * for a list of all child view attributes that this class supports. |
| 4150 | */ |
| 4151 | public static class MarginLayoutParams extends ViewGroup.LayoutParams { |
| 4152 | /** |
| 4153 | * The left margin in pixels of the child. |
| 4154 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 4155 | @ViewDebug.ExportedProperty(category = "layout") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4156 | public int leftMargin; |
| 4157 | |
| 4158 | /** |
| 4159 | * The top margin in pixels of the child. |
| 4160 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 4161 | @ViewDebug.ExportedProperty(category = "layout") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4162 | public int topMargin; |
| 4163 | |
| 4164 | /** |
| 4165 | * The right margin in pixels of the child. |
| 4166 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 4167 | @ViewDebug.ExportedProperty(category = "layout") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4168 | public int rightMargin; |
| 4169 | |
| 4170 | /** |
| 4171 | * The bottom margin in pixels of the child. |
| 4172 | */ |
Konstantin Lopyrev | bea9516 | 2010-08-10 17:02:18 -0700 | [diff] [blame] | 4173 | @ViewDebug.ExportedProperty(category = "layout") |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4174 | public int bottomMargin; |
| 4175 | |
| 4176 | /** |
| 4177 | * Creates a new set of layout parameters. The values are extracted from |
| 4178 | * the supplied attributes set and context. |
| 4179 | * |
| 4180 | * @param c the application environment |
| 4181 | * @param attrs the set of attributes from which to extract the layout |
| 4182 | * parameters' values |
| 4183 | */ |
| 4184 | public MarginLayoutParams(Context c, AttributeSet attrs) { |
| 4185 | super(); |
| 4186 | |
| 4187 | TypedArray a = c.obtainStyledAttributes(attrs, R.styleable.ViewGroup_MarginLayout); |
| 4188 | setBaseAttributes(a, |
| 4189 | R.styleable.ViewGroup_MarginLayout_layout_width, |
| 4190 | R.styleable.ViewGroup_MarginLayout_layout_height); |
| 4191 | |
| 4192 | int margin = a.getDimensionPixelSize( |
| 4193 | com.android.internal.R.styleable.ViewGroup_MarginLayout_layout_margin, -1); |
| 4194 | if (margin >= 0) { |
| 4195 | leftMargin = margin; |
| 4196 | topMargin = margin; |
| 4197 | rightMargin= margin; |
| 4198 | bottomMargin = margin; |
| 4199 | } else { |
| 4200 | leftMargin = a.getDimensionPixelSize( |
| 4201 | R.styleable.ViewGroup_MarginLayout_layout_marginLeft, 0); |
| 4202 | topMargin = a.getDimensionPixelSize( |
| 4203 | R.styleable.ViewGroup_MarginLayout_layout_marginTop, 0); |
| 4204 | rightMargin = a.getDimensionPixelSize( |
| 4205 | R.styleable.ViewGroup_MarginLayout_layout_marginRight, 0); |
| 4206 | bottomMargin = a.getDimensionPixelSize( |
| 4207 | R.styleable.ViewGroup_MarginLayout_layout_marginBottom, 0); |
| 4208 | } |
| 4209 | |
| 4210 | a.recycle(); |
| 4211 | } |
| 4212 | |
| 4213 | /** |
| 4214 | * {@inheritDoc} |
| 4215 | */ |
| 4216 | public MarginLayoutParams(int width, int height) { |
| 4217 | super(width, height); |
| 4218 | } |
| 4219 | |
| 4220 | /** |
| 4221 | * Copy constructor. Clones the width, height and margin values of the source. |
| 4222 | * |
| 4223 | * @param source The layout params to copy from. |
| 4224 | */ |
| 4225 | public MarginLayoutParams(MarginLayoutParams source) { |
| 4226 | this.width = source.width; |
| 4227 | this.height = source.height; |
| 4228 | |
| 4229 | this.leftMargin = source.leftMargin; |
| 4230 | this.topMargin = source.topMargin; |
| 4231 | this.rightMargin = source.rightMargin; |
| 4232 | this.bottomMargin = source.bottomMargin; |
| 4233 | } |
| 4234 | |
| 4235 | /** |
| 4236 | * {@inheritDoc} |
| 4237 | */ |
| 4238 | public MarginLayoutParams(LayoutParams source) { |
| 4239 | super(source); |
| 4240 | } |
| 4241 | |
| 4242 | /** |
| 4243 | * Sets the margins, in pixels. |
| 4244 | * |
| 4245 | * @param left the left margin size |
| 4246 | * @param top the top margin size |
| 4247 | * @param right the right margin size |
| 4248 | * @param bottom the bottom margin size |
| 4249 | * |
| 4250 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginLeft |
| 4251 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginTop |
| 4252 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginRight |
| 4253 | * @attr ref android.R.styleable#ViewGroup_MarginLayout_layout_marginBottom |
| 4254 | */ |
| 4255 | public void setMargins(int left, int top, int right, int bottom) { |
| 4256 | leftMargin = left; |
| 4257 | topMargin = top; |
| 4258 | rightMargin = right; |
| 4259 | bottomMargin = bottom; |
| 4260 | } |
| 4261 | } |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 4262 | |
| 4263 | private static class SplitMotionTargets { |
| 4264 | private SparseArray<View> mTargets; |
Adam Powell | 816c3be | 2010-08-23 18:00:05 -0700 | [diff] [blame] | 4265 | private TargetInfo[] mUniqueTargets; |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 4266 | private int mUniqueTargetCount; |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 4267 | private MotionEvent.PointerCoords[] mPointerCoords; |
| 4268 | private int[] mPointerIds; |
| 4269 | |
| 4270 | private static final int INITIAL_UNIQUE_MOTION_TARGETS_SIZE = 5; |
| 4271 | private static final int INITIAL_BUCKET_SIZE = 5; |
| 4272 | |
| 4273 | public SplitMotionTargets() { |
| 4274 | mTargets = new SparseArray<View>(); |
Adam Powell | 816c3be | 2010-08-23 18:00:05 -0700 | [diff] [blame] | 4275 | mUniqueTargets = new TargetInfo[INITIAL_UNIQUE_MOTION_TARGETS_SIZE]; |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 4276 | mPointerIds = new int[INITIAL_BUCKET_SIZE]; |
| 4277 | mPointerCoords = new MotionEvent.PointerCoords[INITIAL_BUCKET_SIZE]; |
| 4278 | for (int i = 0; i < INITIAL_BUCKET_SIZE; i++) { |
| 4279 | mPointerCoords[i] = new MotionEvent.PointerCoords(); |
| 4280 | } |
| 4281 | } |
| 4282 | |
| 4283 | public void clear() { |
| 4284 | mTargets.clear(); |
Adam Powell | 816c3be | 2010-08-23 18:00:05 -0700 | [diff] [blame] | 4285 | final int count = mUniqueTargetCount; |
| 4286 | for (int i = 0; i < count; i++) { |
| 4287 | mUniqueTargets[i].recycle(); |
| 4288 | mUniqueTargets[i] = null; |
| 4289 | } |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 4290 | mUniqueTargetCount = 0; |
| 4291 | } |
| 4292 | |
Adam Powell | 816c3be | 2010-08-23 18:00:05 -0700 | [diff] [blame] | 4293 | public void add(int pointerId, View target, long downTime) { |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 4294 | mTargets.put(pointerId, target); |
| 4295 | |
| 4296 | final int uniqueCount = mUniqueTargetCount; |
| 4297 | boolean addUnique = true; |
| 4298 | for (int i = 0; i < uniqueCount; i++) { |
Adam Powell | 816c3be | 2010-08-23 18:00:05 -0700 | [diff] [blame] | 4299 | if (mUniqueTargets[i].view == target) { |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 4300 | addUnique = false; |
| 4301 | } |
| 4302 | } |
| 4303 | if (addUnique) { |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 4304 | if (mUniqueTargets.length == uniqueCount) { |
Adam Powell | 816c3be | 2010-08-23 18:00:05 -0700 | [diff] [blame] | 4305 | TargetInfo[] newTargets = |
| 4306 | new TargetInfo[uniqueCount + INITIAL_UNIQUE_MOTION_TARGETS_SIZE]; |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 4307 | System.arraycopy(mUniqueTargets, 0, newTargets, 0, uniqueCount); |
| 4308 | mUniqueTargets = newTargets; |
| 4309 | } |
Adam Powell | 816c3be | 2010-08-23 18:00:05 -0700 | [diff] [blame] | 4310 | mUniqueTargets[uniqueCount] = TargetInfo.obtain(target, downTime); |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 4311 | mUniqueTargetCount++; |
| 4312 | } |
| 4313 | } |
| 4314 | |
| 4315 | public int getIdCount() { |
| 4316 | return mTargets.size(); |
| 4317 | } |
| 4318 | |
| 4319 | public int getUniqueTargetCount() { |
| 4320 | return mUniqueTargetCount; |
| 4321 | } |
| 4322 | |
Adam Powell | 816c3be | 2010-08-23 18:00:05 -0700 | [diff] [blame] | 4323 | public TargetInfo getUniqueTargetAt(int index) { |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 4324 | return mUniqueTargets[index]; |
| 4325 | } |
| 4326 | |
| 4327 | public View get(int id) { |
| 4328 | return mTargets.get(id); |
| 4329 | } |
| 4330 | |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 4331 | public int indexOfTarget(View target) { |
| 4332 | return mTargets.indexOfValue(target); |
| 4333 | } |
| 4334 | |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 4335 | public View targetAt(int index) { |
| 4336 | return mTargets.valueAt(index); |
| 4337 | } |
| 4338 | |
Adam Powell | 816c3be | 2010-08-23 18:00:05 -0700 | [diff] [blame] | 4339 | public TargetInfo getPrimaryTarget() { |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 4340 | if (!isEmpty()) { |
Adam Powell | 816c3be | 2010-08-23 18:00:05 -0700 | [diff] [blame] | 4341 | // Find the longest-lived target |
| 4342 | long firstTime = Long.MAX_VALUE; |
| 4343 | int firstIndex = 0; |
| 4344 | final int uniqueCount = mUniqueTargetCount; |
| 4345 | for (int i = 0; i < uniqueCount; i++) { |
| 4346 | TargetInfo info = mUniqueTargets[i]; |
| 4347 | if (info.downTime < firstTime) { |
| 4348 | firstTime = info.downTime; |
| 4349 | firstIndex = i; |
| 4350 | } |
| 4351 | } |
| 4352 | return mUniqueTargets[firstIndex]; |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 4353 | } |
| 4354 | return null; |
| 4355 | } |
| 4356 | |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 4357 | public boolean isEmpty() { |
| 4358 | return mUniqueTargetCount == 0; |
| 4359 | } |
| 4360 | |
| 4361 | public void removeById(int id) { |
| 4362 | final int index = mTargets.indexOfKey(id); |
| 4363 | removeAt(index); |
| 4364 | } |
| 4365 | |
| 4366 | public void removeView(View view) { |
| 4367 | int i = 0; |
| 4368 | while (i < mTargets.size()) { |
| 4369 | if (mTargets.valueAt(i) == view) { |
| 4370 | mTargets.removeAt(i); |
| 4371 | } else { |
| 4372 | i++; |
| 4373 | } |
| 4374 | } |
| 4375 | removeUnique(view); |
| 4376 | } |
| 4377 | |
| 4378 | public void removeAt(int index) { |
| 4379 | if (index < 0 || index >= mTargets.size()) { |
| 4380 | return; |
| 4381 | } |
| 4382 | |
| 4383 | final View removeView = mTargets.valueAt(index); |
| 4384 | mTargets.removeAt(index); |
| 4385 | if (mTargets.indexOfValue(removeView) < 0) { |
| 4386 | removeUnique(removeView); |
| 4387 | } |
| 4388 | } |
| 4389 | |
| 4390 | private void removeUnique(View removeView) { |
Adam Powell | 816c3be | 2010-08-23 18:00:05 -0700 | [diff] [blame] | 4391 | TargetInfo[] unique = mUniqueTargets; |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 4392 | int uniqueCount = mUniqueTargetCount; |
| 4393 | for (int i = 0; i < uniqueCount; i++) { |
Adam Powell | 816c3be | 2010-08-23 18:00:05 -0700 | [diff] [blame] | 4394 | if (unique[i].view == removeView) { |
| 4395 | unique[i].recycle(); |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 4396 | unique[i] = unique[--uniqueCount]; |
| 4397 | unique[uniqueCount] = null; |
| 4398 | break; |
| 4399 | } |
| 4400 | } |
| 4401 | |
| 4402 | mUniqueTargetCount = uniqueCount; |
| 4403 | } |
| 4404 | |
| 4405 | /** |
| 4406 | * Return a new (obtain()ed) MotionEvent containing only data for pointers that should |
| 4407 | * be dispatched to child. Don't forget to recycle it! |
| 4408 | */ |
Adam Powell | 816c3be | 2010-08-23 18:00:05 -0700 | [diff] [blame] | 4409 | public MotionEvent filterMotionEventForChild(MotionEvent ev, View child, long downTime) { |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 4410 | int action = ev.getAction(); |
| 4411 | final int maskedAction = action & MotionEvent.ACTION_MASK; |
| 4412 | |
| 4413 | // Only send pointer up events if this child was the target. Drop it otherwise. |
| 4414 | if (maskedAction == MotionEvent.ACTION_POINTER_UP && |
| 4415 | get(ev.getPointerId(ev.getActionIndex())) != child) { |
| 4416 | return null; |
| 4417 | } |
| 4418 | |
| 4419 | int pointerCount = 0; |
| 4420 | final int idCount = getIdCount(); |
| 4421 | for (int i = 0; i < idCount; i++) { |
| 4422 | if (targetAt(i) == child) { |
| 4423 | pointerCount++; |
| 4424 | } |
| 4425 | } |
| 4426 | |
| 4427 | int actionId = -1; |
| 4428 | boolean needsNewIndex = false; // True if we should fill in the action's masked index |
| 4429 | |
| 4430 | // If we have a down event, it wasn't counted above. |
| 4431 | if (maskedAction == MotionEvent.ACTION_DOWN) { |
| 4432 | pointerCount++; |
| 4433 | actionId = ev.getPointerId(0); |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 4434 | } else if (maskedAction == MotionEvent.ACTION_POINTER_DOWN) { |
| 4435 | pointerCount++; |
| 4436 | |
| 4437 | actionId = ev.getPointerId(ev.getActionIndex()); |
| 4438 | |
| 4439 | if (indexOfTarget(child) < 0) { |
| 4440 | // The new action should be ACTION_DOWN if this child isn't currently getting |
| 4441 | // any events. |
| 4442 | action = MotionEvent.ACTION_DOWN; |
| 4443 | } else { |
| 4444 | // Fill in the index portion of the action later. |
| 4445 | needsNewIndex = true; |
| 4446 | } |
| 4447 | } else if (maskedAction == MotionEvent.ACTION_POINTER_UP) { |
| 4448 | actionId = ev.getPointerId(ev.getActionIndex()); |
| 4449 | if (pointerCount == 1) { |
| 4450 | // The new action should be ACTION_UP if there's only one pointer left for |
| 4451 | // this target. |
| 4452 | action = MotionEvent.ACTION_UP; |
| 4453 | } else { |
| 4454 | // Fill in the index portion of the action later. |
| 4455 | needsNewIndex = true; |
| 4456 | } |
| 4457 | } |
| 4458 | |
| 4459 | if (pointerCount == 0) { |
| 4460 | return null; |
| 4461 | } |
| 4462 | |
| 4463 | // Fill the buckets with pointer data! |
| 4464 | final int eventPointerCount = ev.getPointerCount(); |
| 4465 | int bucketIndex = 0; |
| 4466 | int newActionIndex = -1; |
| 4467 | for (int evp = 0; evp < eventPointerCount; evp++) { |
| 4468 | final int id = ev.getPointerId(evp); |
| 4469 | |
| 4470 | // Add this pointer to the bucket if it is new or targeted at child |
| 4471 | if (id == actionId || get(id) == child) { |
| 4472 | // Expand scratch arrays if needed |
| 4473 | if (mPointerCoords.length <= bucketIndex) { |
| 4474 | int[] pointerIds = new int[pointerCount]; |
| 4475 | MotionEvent.PointerCoords[] pointerCoords = |
| 4476 | new MotionEvent.PointerCoords[pointerCount]; |
| 4477 | for (int i = mPointerCoords.length; i < pointerCoords.length; i++) { |
| 4478 | pointerCoords[i] = new MotionEvent.PointerCoords(); |
| 4479 | } |
| 4480 | |
| 4481 | System.arraycopy(mPointerCoords, 0, |
| 4482 | pointerCoords, 0, mPointerCoords.length); |
| 4483 | System.arraycopy(mPointerIds, 0, pointerIds, 0, mPointerIds.length); |
| 4484 | |
| 4485 | mPointerCoords = pointerCoords; |
| 4486 | mPointerIds = pointerIds; |
| 4487 | } |
| 4488 | |
| 4489 | mPointerIds[bucketIndex] = id; |
| 4490 | ev.getPointerCoords(evp, mPointerCoords[bucketIndex]); |
| 4491 | |
| 4492 | if (needsNewIndex && id == actionId) { |
| 4493 | newActionIndex = bucketIndex; |
| 4494 | } |
| 4495 | |
| 4496 | bucketIndex++; |
| 4497 | } |
| 4498 | } |
| 4499 | |
| 4500 | // Encode the new action index if we have one |
| 4501 | if (newActionIndex >= 0) { |
| 4502 | action = (action & MotionEvent.ACTION_MASK) | |
| 4503 | (newActionIndex << MotionEvent.ACTION_POINTER_INDEX_SHIFT); |
| 4504 | } |
| 4505 | |
Romain Guy | 9b34d45 | 2010-09-02 11:45:04 -0700 | [diff] [blame] | 4506 | return MotionEvent.obtain(downTime, ev.getEventTime(), |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 4507 | action, pointerCount, mPointerIds, mPointerCoords, ev.getMetaState(), |
| 4508 | ev.getXPrecision(), ev.getYPrecision(), ev.getDeviceId(), ev.getEdgeFlags(), |
Jeff Brown | b380c4d | 2010-09-08 13:02:55 -0700 | [diff] [blame] | 4509 | ev.getSource(), ev.getFlags()); |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 4510 | } |
Adam Powell | 816c3be | 2010-08-23 18:00:05 -0700 | [diff] [blame] | 4511 | |
| 4512 | static class TargetInfo { |
| 4513 | public View view; |
| 4514 | public long downTime; |
| 4515 | |
| 4516 | private TargetInfo mNextRecycled; |
| 4517 | |
| 4518 | private static TargetInfo sRecycleBin; |
| 4519 | private static int sRecycledCount; |
| 4520 | |
| 4521 | private static int MAX_RECYCLED = 15; |
| 4522 | |
| 4523 | private TargetInfo() { |
| 4524 | } |
| 4525 | |
| 4526 | public static TargetInfo obtain(View v, long time) { |
| 4527 | TargetInfo info; |
| 4528 | if (sRecycleBin == null) { |
| 4529 | info = new TargetInfo(); |
| 4530 | } else { |
| 4531 | info = sRecycleBin; |
| 4532 | sRecycleBin = info.mNextRecycled; |
| 4533 | sRecycledCount--; |
| 4534 | } |
| 4535 | info.view = v; |
| 4536 | info.downTime = time; |
| 4537 | return info; |
| 4538 | } |
| 4539 | |
| 4540 | public void recycle() { |
| 4541 | if (sRecycledCount >= MAX_RECYCLED) { |
| 4542 | return; |
| 4543 | } |
| 4544 | mNextRecycled = sRecycleBin; |
| 4545 | sRecycleBin = this; |
| 4546 | sRecycledCount++; |
| 4547 | } |
| 4548 | } |
Adam Powell | 2b342f0 | 2010-08-18 18:14:13 -0700 | [diff] [blame] | 4549 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4550 | } |