NotificationStackScrollLayout.java revision fed1ab62223980cebbea8a9148a695159389169f
1/* 2 * Copyright (C) 2014 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 17package com.android.systemui.statusbar.stack; 18 19import android.content.Context; 20import android.content.res.Configuration; 21import android.graphics.Canvas; 22import android.graphics.Paint; 23import android.util.AttributeSet; 24import android.util.Log; 25import android.view.MotionEvent; 26import android.view.VelocityTracker; 27import android.view.View; 28import android.view.ViewConfiguration; 29import android.view.ViewGroup; 30import android.view.ViewTreeObserver; 31import android.view.animation.AnimationUtils; 32import android.widget.OverScroller; 33import com.android.systemui.ExpandHelper; 34import com.android.systemui.R; 35import com.android.systemui.SwipeHelper; 36import com.android.systemui.statusbar.ActivatableNotificationView; 37import com.android.systemui.statusbar.ExpandableNotificationRow; 38import com.android.systemui.statusbar.ExpandableView; 39import com.android.systemui.statusbar.SpeedBumpView; 40import com.android.systemui.statusbar.policy.ScrollAdapter; 41import com.android.systemui.statusbar.stack.StackScrollState.ViewState; 42 43import java.util.ArrayList; 44 45/** 46 * A layout which handles a dynamic amount of notifications and presents them in a scrollable stack. 47 */ 48public class NotificationStackScrollLayout extends ViewGroup 49 implements SwipeHelper.Callback, ExpandHelper.Callback, ScrollAdapter, 50 ExpandableView.OnHeightChangedListener { 51 52 private static final String TAG = "NotificationStackScrollLayout"; 53 private static final boolean DEBUG = false; 54 private static final float RUBBER_BAND_FACTOR_NORMAL = 0.35f; 55 private static final float RUBBER_BAND_FACTOR_AFTER_EXPAND = 0.15f; 56 private static final float RUBBER_BAND_FACTOR_ON_PANEL_EXPAND = 0.21f; 57 58 /** 59 * Sentinel value for no current active pointer. Used by {@link #mActivePointerId}. 60 */ 61 private static final int INVALID_POINTER = -1; 62 63 private ExpandHelper mExpandHelper; 64 private SwipeHelper mSwipeHelper; 65 private boolean mSwipingInProgress; 66 private int mCurrentStackHeight = Integer.MAX_VALUE; 67 private int mOwnScrollY; 68 private int mMaxLayoutHeight; 69 70 private VelocityTracker mVelocityTracker; 71 private OverScroller mScroller; 72 private int mTouchSlop; 73 private int mMinimumVelocity; 74 private int mMaximumVelocity; 75 private int mOverflingDistance; 76 private float mMaxOverScroll; 77 private boolean mIsBeingDragged; 78 private int mLastMotionY; 79 private int mDownX; 80 private int mActivePointerId; 81 82 private int mSidePaddings; 83 private Paint mDebugPaint; 84 private int mContentHeight; 85 private int mCollapsedSize; 86 private int mBottomStackSlowDownHeight; 87 private int mBottomStackPeekSize; 88 private int mPaddingBetweenElements; 89 private int mPaddingBetweenElementsDimmed; 90 private int mPaddingBetweenElementsNormal; 91 private int mTopPadding; 92 93 /** 94 * The algorithm which calculates the properties for our children 95 */ 96 private StackScrollAlgorithm mStackScrollAlgorithm; 97 98 /** 99 * The current State this Layout is in 100 */ 101 private StackScrollState mCurrentStackScrollState = new StackScrollState(this); 102 private AmbientState mAmbientState = new AmbientState(); 103 private ArrayList<View> mChildrenToAddAnimated = new ArrayList<View>(); 104 private ArrayList<View> mChildrenToRemoveAnimated = new ArrayList<View>(); 105 private ArrayList<View> mSnappedBackChildren = new ArrayList<View>(); 106 private ArrayList<View> mDragAnimPendingChildren = new ArrayList<View>(); 107 private ArrayList<View> mChildrenChangingPositions = new ArrayList<View>(); 108 private ArrayList<AnimationEvent> mAnimationEvents 109 = new ArrayList<AnimationEvent>(); 110 private ArrayList<View> mSwipedOutViews = new ArrayList<View>(); 111 private final StackStateAnimator mStateAnimator = new StackStateAnimator(this); 112 private boolean mAnimationsEnabled; 113 private boolean mChangePositionInProgress; 114 115 /** 116 * The raw amount of the overScroll on the top, which is not rubber-banded. 117 */ 118 private float mOverScrolledTopPixels; 119 120 /** 121 * The raw amount of the overScroll on the bottom, which is not rubber-banded. 122 */ 123 private float mOverScrolledBottomPixels; 124 125 private OnChildLocationsChangedListener mListener; 126 private OnOverscrollTopChangedListener mOverscrollTopChangedListener; 127 private ExpandableView.OnHeightChangedListener mOnHeightChangedListener; 128 private boolean mNeedsAnimation; 129 private boolean mTopPaddingNeedsAnimation; 130 private boolean mDimmedNeedsAnimation; 131 private boolean mActivateNeedsAnimation; 132 private boolean mIsExpanded = true; 133 private boolean mChildrenUpdateRequested; 134 private SpeedBumpView mSpeedBumpView; 135 private boolean mIsExpansionChanging; 136 private boolean mExpandingNotification; 137 private boolean mExpandedInThisMotion; 138 private boolean mScrollingEnabled; 139 140 /** 141 * Was the scroller scrolled to the top when the down motion was observed? 142 */ 143 private boolean mScrolledToTopOnFirstDown; 144 145 /** 146 * The minimal amount of over scroll which is needed in order to switch to the quick settings 147 * when over scrolling on a expanded card. 148 */ 149 private float mMinTopOverScrollToEscape; 150 private int mIntrinsicPadding; 151 private int mNotificationTopPadding; 152 private int mMinStackHeight; 153 private boolean mDontReportNextOverScroll; 154 155 /** 156 * The maximum scrollPosition which we are allowed to reach when a notification was expanded. 157 * This is needed to avoid scrolling too far after the notification was collapsed in the same 158 * motion. 159 */ 160 private int mMaxScrollAfterExpand; 161 private OnLongClickListener mLongClickListener; 162 163 /** 164 * Should in this touch motion only be scrolling allowed? It's true when the scroller was 165 * animating. 166 */ 167 private boolean mOnlyScrollingInThisMotion; 168 private boolean mTouchEnabled = true; 169 private ViewTreeObserver.OnPreDrawListener mChildrenUpdater 170 = new ViewTreeObserver.OnPreDrawListener() { 171 @Override 172 public boolean onPreDraw() { 173 updateChildren(); 174 mChildrenUpdateRequested = false; 175 getViewTreeObserver().removeOnPreDrawListener(this); 176 return true; 177 } 178 }; 179 180 public NotificationStackScrollLayout(Context context) { 181 this(context, null); 182 } 183 184 public NotificationStackScrollLayout(Context context, AttributeSet attrs) { 185 this(context, attrs, 0); 186 } 187 188 public NotificationStackScrollLayout(Context context, AttributeSet attrs, int defStyleAttr) { 189 this(context, attrs, defStyleAttr, 0); 190 } 191 192 public NotificationStackScrollLayout(Context context, AttributeSet attrs, int defStyleAttr, 193 int defStyleRes) { 194 super(context, attrs, defStyleAttr, defStyleRes); 195 initView(context); 196 if (DEBUG) { 197 setWillNotDraw(false); 198 mDebugPaint = new Paint(); 199 mDebugPaint.setColor(0xffff0000); 200 mDebugPaint.setStrokeWidth(2); 201 mDebugPaint.setStyle(Paint.Style.STROKE); 202 } 203 } 204 205 @Override 206 protected void onDraw(Canvas canvas) { 207 if (DEBUG) { 208 int y = mCollapsedSize; 209 canvas.drawLine(0, y, getWidth(), y, mDebugPaint); 210 y = (int) (getLayoutHeight() - mBottomStackPeekSize 211 - mBottomStackSlowDownHeight); 212 canvas.drawLine(0, y, getWidth(), y, mDebugPaint); 213 y = (int) (getLayoutHeight() - mBottomStackPeekSize); 214 canvas.drawLine(0, y, getWidth(), y, mDebugPaint); 215 y = (int) getLayoutHeight(); 216 canvas.drawLine(0, y, getWidth(), y, mDebugPaint); 217 y = getHeight() - getEmptyBottomMargin(); 218 canvas.drawLine(0, y, getWidth(), y, mDebugPaint); 219 } 220 } 221 222 private void initView(Context context) { 223 mScroller = new OverScroller(getContext()); 224 setFocusable(true); 225 setDescendantFocusability(FOCUS_AFTER_DESCENDANTS); 226 setClipChildren(false); 227 final ViewConfiguration configuration = ViewConfiguration.get(context); 228 mTouchSlop = configuration.getScaledTouchSlop(); 229 mMinimumVelocity = configuration.getScaledMinimumFlingVelocity(); 230 mMaximumVelocity = configuration.getScaledMaximumFlingVelocity(); 231 mOverflingDistance = configuration.getScaledOverflingDistance(); 232 float densityScale = getResources().getDisplayMetrics().density; 233 float pagingTouchSlop = ViewConfiguration.get(getContext()).getScaledPagingTouchSlop(); 234 mSwipeHelper = new SwipeHelper(SwipeHelper.X, this, densityScale, pagingTouchSlop); 235 mSwipeHelper.setLongPressListener(mLongClickListener); 236 237 mSidePaddings = context.getResources() 238 .getDimensionPixelSize(R.dimen.notification_side_padding); 239 mCollapsedSize = context.getResources() 240 .getDimensionPixelSize(R.dimen.notification_min_height); 241 mBottomStackPeekSize = context.getResources() 242 .getDimensionPixelSize(R.dimen.bottom_stack_peek_amount); 243 mStackScrollAlgorithm = new StackScrollAlgorithm(context); 244 mStackScrollAlgorithm.setDimmed(mAmbientState.isDimmed()); 245 mPaddingBetweenElementsDimmed = context.getResources() 246 .getDimensionPixelSize(R.dimen.notification_padding_dimmed); 247 mPaddingBetweenElementsNormal = context.getResources() 248 .getDimensionPixelSize(R.dimen.notification_padding); 249 updatePadding(mAmbientState.isDimmed()); 250 int minHeight = getResources().getDimensionPixelSize(R.dimen.notification_min_height); 251 int maxHeight = getResources().getDimensionPixelSize(R.dimen.notification_max_height); 252 mExpandHelper = new ExpandHelper(getContext(), this, 253 minHeight, maxHeight); 254 mExpandHelper.setEventSource(this); 255 mExpandHelper.setScrollAdapter(this); 256 mMinTopOverScrollToEscape = getResources().getDimensionPixelSize( 257 R.dimen.min_top_overscroll_to_qs); 258 mNotificationTopPadding = getResources().getDimensionPixelSize( 259 R.dimen.notifications_top_padding); 260 mMinStackHeight = getResources().getDimensionPixelSize(R.dimen.collapsed_stack_height); 261 } 262 263 private void updatePadding(boolean dimmed) { 264 mPaddingBetweenElements = dimmed 265 ? mPaddingBetweenElementsDimmed 266 : mPaddingBetweenElementsNormal; 267 mBottomStackSlowDownHeight = mStackScrollAlgorithm.getBottomStackSlowDownLength(); 268 updateContentHeight(); 269 notifyHeightChangeListener(null); 270 } 271 272 private void notifyHeightChangeListener(ExpandableView view) { 273 if (mOnHeightChangedListener != null) { 274 mOnHeightChangedListener.onHeightChanged(view); 275 } 276 } 277 278 @Override 279 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 280 super.onMeasure(widthMeasureSpec, heightMeasureSpec); 281 int mode = MeasureSpec.getMode(widthMeasureSpec); 282 int size = MeasureSpec.getSize(widthMeasureSpec); 283 int childMeasureSpec = MeasureSpec.makeMeasureSpec(size - 2 * mSidePaddings, mode); 284 measureChildren(childMeasureSpec, heightMeasureSpec); 285 } 286 287 @Override 288 protected void onLayout(boolean changed, int l, int t, int r, int b) { 289 290 // we layout all our children centered on the top 291 float centerX = getWidth() / 2.0f; 292 for (int i = 0; i < getChildCount(); i++) { 293 View child = getChildAt(i); 294 float width = child.getMeasuredWidth(); 295 float height = child.getMeasuredHeight(); 296 child.layout((int) (centerX - width / 2.0f), 297 0, 298 (int) (centerX + width / 2.0f), 299 (int) height); 300 } 301 setMaxLayoutHeight(getHeight()); 302 updateContentHeight(); 303 updateScrollPositionIfNecessary(); 304 requestChildrenUpdate(); 305 } 306 307 public void updateSpeedBumpIndex(int newIndex) { 308 int currentIndex = indexOfChild(mSpeedBumpView); 309 310 // If we are currently layouted before the new speed bump index, we have to decrease it. 311 boolean validIndex = newIndex > 0; 312 if (newIndex > getChildCount() - 1) { 313 validIndex = false; 314 newIndex = -1; 315 } 316 if (validIndex && currentIndex != newIndex) { 317 changeViewPosition(mSpeedBumpView, newIndex); 318 } 319 updateSpeedBump(validIndex); 320 mAmbientState.setSpeedBumpIndex(newIndex); 321 } 322 323 public void setChildLocationsChangedListener(OnChildLocationsChangedListener listener) { 324 mListener = listener; 325 } 326 327 /** 328 * Returns the location the given child is currently rendered at. 329 * 330 * @param child the child to get the location for 331 * @return one of {@link ViewState}'s <code>LOCATION_*</code> constants 332 */ 333 public int getChildLocation(View child) { 334 ViewState childViewState = mCurrentStackScrollState.getViewStateForView(child); 335 if (childViewState == null) { 336 return ViewState.LOCATION_UNKNOWN; 337 } 338 return childViewState.location; 339 } 340 341 private void setMaxLayoutHeight(int maxLayoutHeight) { 342 mMaxLayoutHeight = maxLayoutHeight; 343 updateAlgorithmHeightAndPadding(); 344 } 345 346 private void updateAlgorithmHeightAndPadding() { 347 mStackScrollAlgorithm.setLayoutHeight(getLayoutHeight()); 348 mStackScrollAlgorithm.setTopPadding(mTopPadding); 349 } 350 351 /** 352 * @return whether the height of the layout needs to be adapted, in order to ensure that the 353 * last child is not in the bottom stack. 354 */ 355 private boolean needsHeightAdaption() { 356 return getNotGoneChildCount() > 1; 357 } 358 359 private boolean isViewExpanded(View view) { 360 if (view != null) { 361 ExpandableView expandView = (ExpandableView) view; 362 return expandView.getActualHeight() > mCollapsedSize; 363 } 364 return false; 365 } 366 367 /** 368 * Updates the children views according to the stack scroll algorithm. Call this whenever 369 * modifications to {@link #mOwnScrollY} are performed to reflect it in the view layout. 370 */ 371 private void updateChildren() { 372 mAmbientState.setScrollY(mOwnScrollY); 373 mStackScrollAlgorithm.getStackScrollState(mAmbientState, mCurrentStackScrollState); 374 if (!isCurrentlyAnimating() && !mNeedsAnimation) { 375 applyCurrentState(); 376 } else { 377 startAnimationToState(); 378 } 379 } 380 381 private void requestChildrenUpdate() { 382 if (!mChildrenUpdateRequested) { 383 getViewTreeObserver().addOnPreDrawListener(mChildrenUpdater); 384 mChildrenUpdateRequested = true; 385 invalidate(); 386 } 387 } 388 389 private boolean isCurrentlyAnimating() { 390 return mStateAnimator.isRunning(); 391 } 392 393 private void updateScrollPositionIfNecessary() { 394 int scrollRange = getScrollRange(); 395 if (scrollRange < mOwnScrollY) { 396 mOwnScrollY = scrollRange; 397 } 398 } 399 400 public int getTopPadding() { 401 return mTopPadding; 402 } 403 404 private void setTopPadding(int topPadding, boolean animate) { 405 if (mTopPadding != topPadding) { 406 mTopPadding = topPadding; 407 updateAlgorithmHeightAndPadding(); 408 updateContentHeight(); 409 if (animate && mAnimationsEnabled && mIsExpanded) { 410 mTopPaddingNeedsAnimation = true; 411 mNeedsAnimation = true; 412 } 413 requestChildrenUpdate(); 414 notifyHeightChangeListener(null); 415 } 416 } 417 418 /** 419 * Update the height of the stack to a new height. 420 * 421 * @param height the new height of the stack 422 */ 423 public void setStackHeight(float height) { 424 setIsExpanded(height > 0.0f); 425 int newStackHeight = (int) height; 426 int itemHeight = getItemHeight(); 427 int bottomStackPeekSize = mBottomStackPeekSize; 428 int minStackHeight = itemHeight + bottomStackPeekSize; 429 int stackHeight; 430 if (newStackHeight - mTopPadding >= minStackHeight) { 431 setTranslationY(0); 432 stackHeight = newStackHeight; 433 } else { 434 435 // We did not reach the position yet where we actually start growing, 436 // so we translate the stack upwards. 437 int translationY = (newStackHeight - minStackHeight); 438 // A slight parallax effect is introduced in order for the stack to catch up with 439 // the top card. 440 float partiallyThere = (float) (newStackHeight - mTopPadding) / minStackHeight; 441 partiallyThere = Math.max(0, partiallyThere); 442 translationY += (1 - partiallyThere) * bottomStackPeekSize; 443 setTranslationY(translationY - mTopPadding); 444 stackHeight = (int) (height - (translationY - mTopPadding)); 445 } 446 if (stackHeight != mCurrentStackHeight) { 447 mCurrentStackHeight = stackHeight; 448 updateAlgorithmHeightAndPadding(); 449 requestChildrenUpdate(); 450 } 451 } 452 453 /** 454 * Get the current height of the view. This is at most the msize of the view given by a the 455 * layout but it can also be made smaller by setting {@link #mCurrentStackHeight} 456 * 457 * @return either the layout height or the externally defined height, whichever is smaller 458 */ 459 private int getLayoutHeight() { 460 return Math.min(mMaxLayoutHeight, mCurrentStackHeight); 461 } 462 463 public int getItemHeight() { 464 return mCollapsedSize; 465 } 466 467 public int getBottomStackPeekSize() { 468 return mBottomStackPeekSize; 469 } 470 471 public void setLongPressListener(View.OnLongClickListener listener) { 472 mSwipeHelper.setLongPressListener(listener); 473 mLongClickListener = listener; 474 } 475 476 public void onChildDismissed(View v) { 477 if (DEBUG) Log.v(TAG, "onChildDismissed: " + v); 478 final View veto = v.findViewById(R.id.veto); 479 if (veto != null && veto.getVisibility() != View.GONE) { 480 veto.performClick(); 481 } 482 setSwipingInProgress(false); 483 if (mDragAnimPendingChildren.contains(v)) { 484 // We start the swipe and finish it in the same frame, we don't want any animation 485 // for the drag 486 mDragAnimPendingChildren.remove(v); 487 } 488 mSwipedOutViews.add(v); 489 mAmbientState.onDragFinished(v); 490 } 491 492 @Override 493 public void onChildSnappedBack(View animView) { 494 mAmbientState.onDragFinished(animView); 495 if (!mDragAnimPendingChildren.contains(animView)) { 496 if (mAnimationsEnabled) { 497 mSnappedBackChildren.add(animView); 498 mNeedsAnimation = true; 499 } 500 requestChildrenUpdate(); 501 } else { 502 // We start the swipe and snap back in the same frame, we don't want any animation 503 mDragAnimPendingChildren.remove(animView); 504 } 505 } 506 507 @Override 508 public boolean updateSwipeProgress(View animView, boolean dismissable, float swipeProgress) { 509 return false; 510 } 511 512 public void onBeginDrag(View v) { 513 setSwipingInProgress(true); 514 mAmbientState.onBeginDrag(v); 515 if (mAnimationsEnabled) { 516 mDragAnimPendingChildren.add(v); 517 mNeedsAnimation = true; 518 } 519 requestChildrenUpdate(); 520 } 521 522 public void onDragCancelled(View v) { 523 setSwipingInProgress(false); 524 } 525 526 public View getChildAtPosition(MotionEvent ev) { 527 return getChildAtPosition(ev.getX(), ev.getY()); 528 } 529 530 public ExpandableView getChildAtRawPosition(float touchX, float touchY) { 531 int[] location = new int[2]; 532 getLocationOnScreen(location); 533 return getChildAtPosition(touchX - location[0], touchY - location[1]); 534 } 535 536 public ExpandableView getChildAtPosition(float touchX, float touchY) { 537 // find the view under the pointer, accounting for GONE views 538 final int count = getChildCount(); 539 for (int childIdx = 0; childIdx < count; childIdx++) { 540 ExpandableView slidingChild = (ExpandableView) getChildAt(childIdx); 541 if (slidingChild.getVisibility() == GONE) { 542 continue; 543 } 544 float top = slidingChild.getTranslationY(); 545 float bottom = top + slidingChild.getActualHeight(); 546 int left = slidingChild.getLeft(); 547 int right = slidingChild.getRight(); 548 549 if (touchY >= top && touchY <= bottom && touchX >= left && touchX <= right) { 550 return slidingChild; 551 } 552 } 553 return null; 554 } 555 556 public boolean canChildBeExpanded(View v) { 557 return v instanceof ExpandableNotificationRow 558 && ((ExpandableNotificationRow) v).isExpandable(); 559 } 560 561 public void setUserExpandedChild(View v, boolean userExpanded) { 562 if (v instanceof ExpandableNotificationRow) { 563 ((ExpandableNotificationRow) v).setUserExpanded(userExpanded); 564 } 565 } 566 567 public void setUserLockedChild(View v, boolean userLocked) { 568 if (v instanceof ExpandableNotificationRow) { 569 ((ExpandableNotificationRow) v).setUserLocked(userLocked); 570 } 571 removeLongPressCallback(); 572 requestDisallowInterceptTouchEvent(true); 573 } 574 575 @Override 576 public void expansionStateChanged(boolean isExpanding) { 577 mExpandingNotification = isExpanding; 578 if (!mExpandedInThisMotion) { 579 mMaxScrollAfterExpand = mOwnScrollY; 580 mExpandedInThisMotion = true; 581 } 582 } 583 584 public void setScrollingEnabled(boolean enable) { 585 mScrollingEnabled = enable; 586 } 587 588 public void setExpandingEnabled(boolean enable) { 589 mExpandHelper.setEnabled(enable); 590 } 591 592 private boolean isScrollingEnabled() { 593 return mScrollingEnabled; 594 } 595 596 public View getChildContentView(View v) { 597 return v; 598 } 599 600 public boolean canChildBeDismissed(View v) { 601 final View veto = v.findViewById(R.id.veto); 602 return (veto != null && veto.getVisibility() != View.GONE); 603 } 604 605 private void setSwipingInProgress(boolean isSwiped) { 606 mSwipingInProgress = isSwiped; 607 if(isSwiped) { 608 requestDisallowInterceptTouchEvent(true); 609 } 610 } 611 612 @Override 613 protected void onConfigurationChanged(Configuration newConfig) { 614 super.onConfigurationChanged(newConfig); 615 float densityScale = getResources().getDisplayMetrics().density; 616 mSwipeHelper.setDensityScale(densityScale); 617 float pagingTouchSlop = ViewConfiguration.get(getContext()).getScaledPagingTouchSlop(); 618 mSwipeHelper.setPagingTouchSlop(pagingTouchSlop); 619 initView(getContext()); 620 } 621 622 public void dismissRowAnimated(View child, int vel) { 623 mSwipeHelper.dismissChild(child, vel); 624 } 625 626 @Override 627 public boolean onTouchEvent(MotionEvent ev) { 628 if (!isEnabled()) { 629 return false; 630 } 631 boolean isCancelOrUp = ev.getActionMasked() == MotionEvent.ACTION_CANCEL 632 || ev.getActionMasked()== MotionEvent.ACTION_UP; 633 boolean expandWantsIt = false; 634 if (!mSwipingInProgress && !mOnlyScrollingInThisMotion) { 635 if (isCancelOrUp) { 636 mExpandHelper.onlyObserveMovements(false); 637 } 638 boolean wasExpandingBefore = mExpandingNotification; 639 expandWantsIt = mExpandHelper.onTouchEvent(ev); 640 if (mExpandedInThisMotion && !mExpandingNotification && wasExpandingBefore) { 641 dispatchDownEventToScroller(ev); 642 } 643 } 644 boolean scrollerWantsIt = false; 645 if (!mSwipingInProgress && !mExpandingNotification) { 646 scrollerWantsIt = onScrollTouch(ev); 647 } 648 boolean horizontalSwipeWantsIt = false; 649 if (!mIsBeingDragged 650 && !mExpandingNotification 651 && !mExpandedInThisMotion 652 && !mOnlyScrollingInThisMotion) { 653 horizontalSwipeWantsIt = mSwipeHelper.onTouchEvent(ev); 654 } 655 return horizontalSwipeWantsIt || scrollerWantsIt || expandWantsIt || super.onTouchEvent(ev); 656 } 657 658 private void dispatchDownEventToScroller(MotionEvent ev) { 659 MotionEvent downEvent = MotionEvent.obtain(ev); 660 downEvent.setAction(MotionEvent.ACTION_DOWN); 661 onScrollTouch(downEvent); 662 downEvent.recycle(); 663 } 664 665 private boolean onScrollTouch(MotionEvent ev) { 666 if (!isScrollingEnabled()) { 667 return false; 668 } 669 initVelocityTrackerIfNotExists(); 670 mVelocityTracker.addMovement(ev); 671 672 final int action = ev.getAction(); 673 674 switch (action & MotionEvent.ACTION_MASK) { 675 case MotionEvent.ACTION_DOWN: { 676 if (getChildCount() == 0 || !isInContentBounds(ev)) { 677 return false; 678 } 679 boolean isBeingDragged = !mScroller.isFinished(); 680 setIsBeingDragged(isBeingDragged); 681 682 /* 683 * If being flinged and user touches, stop the fling. isFinished 684 * will be false if being flinged. 685 */ 686 if (!mScroller.isFinished()) { 687 mScroller.forceFinished(true); 688 } 689 690 // Remember where the motion event started 691 mLastMotionY = (int) ev.getY(); 692 mDownX = (int) ev.getX(); 693 mActivePointerId = ev.getPointerId(0); 694 break; 695 } 696 case MotionEvent.ACTION_MOVE: 697 final int activePointerIndex = ev.findPointerIndex(mActivePointerId); 698 if (activePointerIndex == -1) { 699 Log.e(TAG, "Invalid pointerId=" + mActivePointerId + " in onTouchEvent"); 700 break; 701 } 702 703 final int y = (int) ev.getY(activePointerIndex); 704 final int x = (int) ev.getX(activePointerIndex); 705 int deltaY = mLastMotionY - y; 706 final int xDiff = Math.abs(x - mDownX); 707 final int yDiff = Math.abs(deltaY); 708 if (!mIsBeingDragged && yDiff > mTouchSlop && yDiff > xDiff) { 709 setIsBeingDragged(true); 710 if (deltaY > 0) { 711 deltaY -= mTouchSlop; 712 } else { 713 deltaY += mTouchSlop; 714 } 715 } 716 if (mIsBeingDragged) { 717 // Scroll to follow the motion event 718 mLastMotionY = y; 719 int range = getScrollRange(); 720 if (mExpandedInThisMotion) { 721 range = Math.min(range, mMaxScrollAfterExpand); 722 } 723 724 float scrollAmount; 725 if (deltaY < 0) { 726 scrollAmount = overScrollDown(deltaY); 727 } else { 728 scrollAmount = overScrollUp(deltaY, range); 729 } 730 731 // Calling overScrollBy will call onOverScrolled, which 732 // calls onScrollChanged if applicable. 733 if (scrollAmount != 0.0f) { 734 // The scrolling motion could not be compensated with the 735 // existing overScroll, we have to scroll the view 736 overScrollBy(0, (int) scrollAmount, 0, mOwnScrollY, 737 0, range, 0, getHeight() / 2, true); 738 } 739 } 740 break; 741 case MotionEvent.ACTION_UP: 742 if (mIsBeingDragged) { 743 final VelocityTracker velocityTracker = mVelocityTracker; 744 velocityTracker.computeCurrentVelocity(1000, mMaximumVelocity); 745 int initialVelocity = (int) velocityTracker.getYVelocity(mActivePointerId); 746 747 if (shouldOverScrollFling(initialVelocity)) { 748 onOverScrollFling(true, initialVelocity); 749 } else { 750 if (getChildCount() > 0) { 751 if ((Math.abs(initialVelocity) > mMinimumVelocity)) { 752 float currentOverScrollTop = getCurrentOverScrollAmount(true); 753 if (currentOverScrollTop == 0.0f || initialVelocity > 0) { 754 fling(-initialVelocity); 755 } else { 756 onOverScrollFling(false, initialVelocity); 757 } 758 } else { 759 if (mScroller.springBack(mScrollX, mOwnScrollY, 0, 0, 0, 760 getScrollRange())) { 761 postInvalidateOnAnimation(); 762 } 763 } 764 } 765 766 mActivePointerId = INVALID_POINTER; 767 endDrag(); 768 } 769 } 770 break; 771 case MotionEvent.ACTION_CANCEL: 772 if (mIsBeingDragged && getChildCount() > 0) { 773 if (mScroller.springBack(mScrollX, mOwnScrollY, 0, 0, 0, getScrollRange())) { 774 postInvalidateOnAnimation(); 775 } 776 mActivePointerId = INVALID_POINTER; 777 endDrag(); 778 } 779 break; 780 case MotionEvent.ACTION_POINTER_DOWN: { 781 final int index = ev.getActionIndex(); 782 mLastMotionY = (int) ev.getY(index); 783 mDownX = (int) ev.getX(index); 784 mActivePointerId = ev.getPointerId(index); 785 break; 786 } 787 case MotionEvent.ACTION_POINTER_UP: 788 onSecondaryPointerUp(ev); 789 mLastMotionY = (int) ev.getY(ev.findPointerIndex(mActivePointerId)); 790 mDownX = (int) ev.getX(ev.findPointerIndex(mActivePointerId)); 791 break; 792 } 793 return true; 794 } 795 796 private void onOverScrollFling(boolean open, int initialVelocity) { 797 if (mOverscrollTopChangedListener != null) { 798 mOverscrollTopChangedListener.flingTopOverscroll(initialVelocity, open); 799 } 800 mDontReportNextOverScroll = true; 801 setOverScrollAmount(0.0f, true, false); 802 } 803 804 /** 805 * Perform a scroll upwards and adapt the overscroll amounts accordingly 806 * 807 * @param deltaY The amount to scroll upwards, has to be positive. 808 * @return The amount of scrolling to be performed by the scroller, 809 * not handled by the overScroll amount. 810 */ 811 private float overScrollUp(int deltaY, int range) { 812 deltaY = Math.max(deltaY, 0); 813 float currentTopAmount = getCurrentOverScrollAmount(true); 814 float newTopAmount = currentTopAmount - deltaY; 815 if (currentTopAmount > 0) { 816 setOverScrollAmount(newTopAmount, true /* onTop */, 817 false /* animate */); 818 } 819 // Top overScroll might not grab all scrolling motion, 820 // we have to scroll as well. 821 float scrollAmount = newTopAmount < 0 ? -newTopAmount : 0.0f; 822 float newScrollY = mOwnScrollY + scrollAmount; 823 if (newScrollY > range) { 824 if (!mExpandedInThisMotion) { 825 float currentBottomPixels = getCurrentOverScrolledPixels(false); 826 // We overScroll on the top 827 setOverScrolledPixels(currentBottomPixels + newScrollY - range, 828 false /* onTop */, 829 false /* animate */); 830 } 831 mOwnScrollY = range; 832 scrollAmount = 0.0f; 833 } 834 return scrollAmount; 835 } 836 837 /** 838 * Perform a scroll downward and adapt the overscroll amounts accordingly 839 * 840 * @param deltaY The amount to scroll downwards, has to be negative. 841 * @return The amount of scrolling to be performed by the scroller, 842 * not handled by the overScroll amount. 843 */ 844 private float overScrollDown(int deltaY) { 845 deltaY = Math.min(deltaY, 0); 846 float currentBottomAmount = getCurrentOverScrollAmount(false); 847 float newBottomAmount = currentBottomAmount + deltaY; 848 if (currentBottomAmount > 0) { 849 setOverScrollAmount(newBottomAmount, false /* onTop */, 850 false /* animate */); 851 } 852 // Bottom overScroll might not grab all scrolling motion, 853 // we have to scroll as well. 854 float scrollAmount = newBottomAmount < 0 ? newBottomAmount : 0.0f; 855 float newScrollY = mOwnScrollY + scrollAmount; 856 if (newScrollY < 0) { 857 float currentTopPixels = getCurrentOverScrolledPixels(true); 858 // We overScroll on the top 859 setOverScrolledPixels(currentTopPixels - newScrollY, 860 true /* onTop */, 861 false /* animate */); 862 mOwnScrollY = 0; 863 scrollAmount = 0.0f; 864 } 865 return scrollAmount; 866 } 867 868 private void onSecondaryPointerUp(MotionEvent ev) { 869 final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >> 870 MotionEvent.ACTION_POINTER_INDEX_SHIFT; 871 final int pointerId = ev.getPointerId(pointerIndex); 872 if (pointerId == mActivePointerId) { 873 // This was our active pointer going up. Choose a new 874 // active pointer and adjust accordingly. 875 // TODO: Make this decision more intelligent. 876 final int newPointerIndex = pointerIndex == 0 ? 1 : 0; 877 mLastMotionY = (int) ev.getY(newPointerIndex); 878 mActivePointerId = ev.getPointerId(newPointerIndex); 879 if (mVelocityTracker != null) { 880 mVelocityTracker.clear(); 881 } 882 } 883 } 884 885 private void initVelocityTrackerIfNotExists() { 886 if (mVelocityTracker == null) { 887 mVelocityTracker = VelocityTracker.obtain(); 888 } 889 } 890 891 private void recycleVelocityTracker() { 892 if (mVelocityTracker != null) { 893 mVelocityTracker.recycle(); 894 mVelocityTracker = null; 895 } 896 } 897 898 private void initOrResetVelocityTracker() { 899 if (mVelocityTracker == null) { 900 mVelocityTracker = VelocityTracker.obtain(); 901 } else { 902 mVelocityTracker.clear(); 903 } 904 } 905 906 @Override 907 public void computeScroll() { 908 if (mScroller.computeScrollOffset()) { 909 // This is called at drawing time by ViewGroup. 910 int oldX = mScrollX; 911 int oldY = mOwnScrollY; 912 int x = mScroller.getCurrX(); 913 int y = mScroller.getCurrY(); 914 915 if (oldX != x || oldY != y) { 916 final int range = getScrollRange(); 917 if (y < 0 && oldY >= 0 || y > range && oldY <= range) { 918 float currVelocity = mScroller.getCurrVelocity(); 919 if (currVelocity >= mMinimumVelocity) { 920 mMaxOverScroll = Math.abs(currVelocity) / 1000 * mOverflingDistance; 921 } 922 } 923 924 overScrollBy(x - oldX, y - oldY, oldX, oldY, 0, range, 925 0, (int) (mMaxOverScroll), false); 926 onScrollChanged(mScrollX, mOwnScrollY, oldX, oldY); 927 } 928 929 // Keep on drawing until the animation has finished. 930 postInvalidateOnAnimation(); 931 } 932 } 933 934 @Override 935 protected boolean overScrollBy(int deltaX, int deltaY, 936 int scrollX, int scrollY, 937 int scrollRangeX, int scrollRangeY, 938 int maxOverScrollX, int maxOverScrollY, 939 boolean isTouchEvent) { 940 941 int newScrollY = scrollY + deltaY; 942 943 final int top = -maxOverScrollY; 944 final int bottom = maxOverScrollY + scrollRangeY; 945 946 boolean clampedY = false; 947 if (newScrollY > bottom) { 948 newScrollY = bottom; 949 clampedY = true; 950 } else if (newScrollY < top) { 951 newScrollY = top; 952 clampedY = true; 953 } 954 955 onOverScrolled(0, newScrollY, false, clampedY); 956 957 return clampedY; 958 } 959 960 /** 961 * Set the amount of overScrolled pixels which will force the view to apply a rubber-banded 962 * overscroll effect based on numPixels. By default this will also cancel animations on the 963 * same overScroll edge. 964 * 965 * @param numPixels The amount of pixels to overScroll by. These will be scaled according to 966 * the rubber-banding logic. 967 * @param onTop Should the effect be applied on top of the scroller. 968 * @param animate Should an animation be performed. 969 */ 970 public void setOverScrolledPixels(float numPixels, boolean onTop, boolean animate) { 971 setOverScrollAmount(numPixels * getRubberBandFactor(onTop), onTop, animate, true); 972 } 973 974 /** 975 * Set the effective overScroll amount which will be directly reflected in the layout. 976 * By default this will also cancel animations on the same overScroll edge. 977 * 978 * @param amount The amount to overScroll by. 979 * @param onTop Should the effect be applied on top of the scroller. 980 * @param animate Should an animation be performed. 981 */ 982 public void setOverScrollAmount(float amount, boolean onTop, boolean animate) { 983 setOverScrollAmount(amount, onTop, animate, true); 984 } 985 986 /** 987 * Set the effective overScroll amount which will be directly reflected in the layout. 988 * 989 * @param amount The amount to overScroll by. 990 * @param onTop Should the effect be applied on top of the scroller. 991 * @param animate Should an animation be performed. 992 * @param cancelAnimators Should running animations be cancelled. 993 */ 994 public void setOverScrollAmount(float amount, boolean onTop, boolean animate, 995 boolean cancelAnimators) { 996 if (cancelAnimators) { 997 mStateAnimator.cancelOverScrollAnimators(onTop); 998 } 999 setOverScrollAmountInternal(amount, onTop, animate); 1000 } 1001 1002 private void setOverScrollAmountInternal(float amount, boolean onTop, boolean animate) { 1003 amount = Math.max(0, amount); 1004 if (animate) { 1005 mStateAnimator.animateOverScrollToAmount(amount, onTop); 1006 } else { 1007 setOverScrolledPixels(amount / getRubberBandFactor(onTop), onTop); 1008 mAmbientState.setOverScrollAmount(amount, onTop); 1009 if (onTop) { 1010 notifyOverscrollTopListener(amount); 1011 } 1012 requestChildrenUpdate(); 1013 } 1014 } 1015 1016 private void notifyOverscrollTopListener(float amount) { 1017 mExpandHelper.onlyObserveMovements(amount > 1.0f); 1018 if (mDontReportNextOverScroll) { 1019 mDontReportNextOverScroll = false; 1020 return; 1021 } 1022 if (mOverscrollTopChangedListener != null) { 1023 mOverscrollTopChangedListener.onOverscrollTopChanged(amount); 1024 } 1025 } 1026 1027 public void setOverscrollTopChangedListener( 1028 OnOverscrollTopChangedListener overscrollTopChangedListener) { 1029 mOverscrollTopChangedListener = overscrollTopChangedListener; 1030 } 1031 1032 public float getCurrentOverScrollAmount(boolean top) { 1033 return mAmbientState.getOverScrollAmount(top); 1034 } 1035 1036 public float getCurrentOverScrolledPixels(boolean top) { 1037 return top? mOverScrolledTopPixels : mOverScrolledBottomPixels; 1038 } 1039 1040 private void setOverScrolledPixels(float amount, boolean onTop) { 1041 if (onTop) { 1042 mOverScrolledTopPixels = amount; 1043 } else { 1044 mOverScrolledBottomPixels = amount; 1045 } 1046 } 1047 1048 private void customScrollTo(int y) { 1049 mOwnScrollY = y; 1050 updateChildren(); 1051 } 1052 1053 @Override 1054 protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY) { 1055 // Treat animating scrolls differently; see #computeScroll() for why. 1056 if (!mScroller.isFinished()) { 1057 final int oldX = mScrollX; 1058 final int oldY = mOwnScrollY; 1059 mScrollX = scrollX; 1060 mOwnScrollY = scrollY; 1061 if (clampedY) { 1062 springBack(); 1063 } else { 1064 onScrollChanged(mScrollX, mOwnScrollY, oldX, oldY); 1065 invalidateParentIfNeeded(); 1066 updateChildren(); 1067 float overScrollTop = getCurrentOverScrollAmount(true); 1068 if (mOwnScrollY < 0) { 1069 notifyOverscrollTopListener(-mOwnScrollY); 1070 } else { 1071 notifyOverscrollTopListener(overScrollTop); 1072 } 1073 } 1074 } else { 1075 customScrollTo(scrollY); 1076 scrollTo(scrollX, mScrollY); 1077 } 1078 } 1079 1080 private void springBack() { 1081 int scrollRange = getScrollRange(); 1082 boolean overScrolledTop = mOwnScrollY <= 0; 1083 boolean overScrolledBottom = mOwnScrollY >= scrollRange; 1084 if (overScrolledTop || overScrolledBottom) { 1085 boolean onTop; 1086 float newAmount; 1087 if (overScrolledTop) { 1088 onTop = true; 1089 newAmount = -mOwnScrollY; 1090 mOwnScrollY = 0; 1091 mDontReportNextOverScroll = true; 1092 } else { 1093 onTop = false; 1094 newAmount = mOwnScrollY - scrollRange; 1095 mOwnScrollY = scrollRange; 1096 } 1097 setOverScrollAmount(newAmount, onTop, false); 1098 setOverScrollAmount(0.0f, onTop, true); 1099 mScroller.forceFinished(true); 1100 } 1101 } 1102 1103 private int getScrollRange() { 1104 int scrollRange = 0; 1105 ExpandableView firstChild = (ExpandableView) getFirstChildNotGone(); 1106 if (firstChild != null) { 1107 int contentHeight = getContentHeight(); 1108 int firstChildMaxExpandHeight = getMaxExpandHeight(firstChild); 1109 scrollRange = Math.max(0, contentHeight - mMaxLayoutHeight + mBottomStackPeekSize 1110 + mBottomStackSlowDownHeight); 1111 if (scrollRange > 0) { 1112 View lastChild = getLastChildNotGone(); 1113 // We want to at least be able collapse the first item and not ending in a weird 1114 // end state. 1115 scrollRange = Math.max(scrollRange, firstChildMaxExpandHeight - mCollapsedSize); 1116 } 1117 } 1118 return scrollRange; 1119 } 1120 1121 /** 1122 * @return the first child which has visibility unequal to GONE 1123 */ 1124 private View getFirstChildNotGone() { 1125 int childCount = getChildCount(); 1126 for (int i = 0; i < childCount; i++) { 1127 View child = getChildAt(i); 1128 if (child.getVisibility() != View.GONE) { 1129 return child; 1130 } 1131 } 1132 return null; 1133 } 1134 1135 /** 1136 * @return The first child which has visibility unequal to GONE which is currently below the 1137 * given translationY or equal to it. 1138 */ 1139 private View getFirstChildBelowTranlsationY(float translationY) { 1140 int childCount = getChildCount(); 1141 for (int i = 0; i < childCount; i++) { 1142 View child = getChildAt(i); 1143 if (child.getVisibility() != View.GONE && child.getTranslationY() >= translationY) { 1144 return child; 1145 } 1146 } 1147 return null; 1148 } 1149 1150 /** 1151 * @return the last child which has visibility unequal to GONE 1152 */ 1153 public View getLastChildNotGone() { 1154 int childCount = getChildCount(); 1155 for (int i = childCount - 1; i >= 0; i--) { 1156 View child = getChildAt(i); 1157 if (child.getVisibility() != View.GONE) { 1158 return child; 1159 } 1160 } 1161 return null; 1162 } 1163 1164 /** 1165 * @return the number of children which have visibility unequal to GONE 1166 */ 1167 public int getNotGoneChildCount() { 1168 int childCount = getChildCount(); 1169 int count = 0; 1170 for (int i = 0; i < childCount; i++) { 1171 View child = getChildAt(i); 1172 if (child.getVisibility() != View.GONE) { 1173 count++; 1174 } 1175 } 1176 return count; 1177 } 1178 1179 private int getMaxExpandHeight(View view) { 1180 if (view instanceof ExpandableNotificationRow) { 1181 ExpandableNotificationRow row = (ExpandableNotificationRow) view; 1182 return row.getIntrinsicHeight(); 1183 } 1184 return view.getHeight(); 1185 } 1186 1187 private int getContentHeight() { 1188 return mContentHeight; 1189 } 1190 1191 private void updateContentHeight() { 1192 int height = 0; 1193 for (int i = 0; i < getChildCount(); i++) { 1194 View child = getChildAt(i); 1195 if (child.getVisibility() != View.GONE) { 1196 if (height != 0) { 1197 // add the padding before this element 1198 height += mPaddingBetweenElements; 1199 } 1200 if (child instanceof ExpandableNotificationRow) { 1201 ExpandableNotificationRow row = (ExpandableNotificationRow) child; 1202 height += row.getIntrinsicHeight(); 1203 } else if (child instanceof ExpandableView) { 1204 ExpandableView expandableView = (ExpandableView) child; 1205 height += expandableView.getActualHeight(); 1206 } 1207 } 1208 } 1209 mContentHeight = height + mTopPadding; 1210 } 1211 1212 /** 1213 * Fling the scroll view 1214 * 1215 * @param velocityY The initial velocity in the Y direction. Positive 1216 * numbers mean that the finger/cursor is moving down the screen, 1217 * which means we want to scroll towards the top. 1218 */ 1219 private void fling(int velocityY) { 1220 if (getChildCount() > 0) { 1221 int scrollRange = getScrollRange(); 1222 1223 float topAmount = getCurrentOverScrollAmount(true); 1224 float bottomAmount = getCurrentOverScrollAmount(false); 1225 if (velocityY < 0 && topAmount > 0) { 1226 mOwnScrollY -= (int) topAmount; 1227 mDontReportNextOverScroll = true; 1228 setOverScrollAmount(0, true, false); 1229 mMaxOverScroll = Math.abs(velocityY) / 1000f * getRubberBandFactor(true /* onTop */) 1230 * mOverflingDistance + topAmount; 1231 } else if (velocityY > 0 && bottomAmount > 0) { 1232 mOwnScrollY += bottomAmount; 1233 setOverScrollAmount(0, false, false); 1234 mMaxOverScroll = Math.abs(velocityY) / 1000f 1235 * getRubberBandFactor(false /* onTop */) * mOverflingDistance 1236 + bottomAmount; 1237 } else { 1238 // it will be set once we reach the boundary 1239 mMaxOverScroll = 0.0f; 1240 } 1241 mScroller.fling(mScrollX, mOwnScrollY, 1, velocityY, 0, 0, 0, 1242 Math.max(0, scrollRange), 0, Integer.MAX_VALUE / 2); 1243 1244 postInvalidateOnAnimation(); 1245 } 1246 } 1247 1248 /** 1249 * @return Whether a fling performed on the top overscroll edge lead to the expanded 1250 * overScroll view (i.e QS). 1251 */ 1252 private boolean shouldOverScrollFling(int initialVelocity) { 1253 float topOverScroll = getCurrentOverScrollAmount(true); 1254 return mScrolledToTopOnFirstDown 1255 && !mExpandedInThisMotion 1256 && topOverScroll > mMinTopOverScrollToEscape 1257 && initialVelocity > 0; 1258 } 1259 1260 public void updateTopPadding(float qsHeight, int scrollY, boolean animate) { 1261 float start = qsHeight - scrollY + mNotificationTopPadding; 1262 float stackHeight = getHeight() - start; 1263 if (stackHeight <= mMinStackHeight) { 1264 float overflow = mMinStackHeight - stackHeight; 1265 stackHeight = mMinStackHeight; 1266 start = getHeight() - stackHeight; 1267 setTranslationY(overflow); 1268 } else { 1269 setTranslationY(0); 1270 } 1271 setTopPadding(clampPadding((int) start), animate); 1272 } 1273 1274 private int clampPadding(int desiredPadding) { 1275 return Math.max(desiredPadding, mIntrinsicPadding); 1276 } 1277 1278 private float getRubberBandFactor(boolean onTop) { 1279 if (!onTop) { 1280 return RUBBER_BAND_FACTOR_NORMAL; 1281 } 1282 if (mExpandedInThisMotion) { 1283 return RUBBER_BAND_FACTOR_AFTER_EXPAND; 1284 } else if (mIsExpansionChanging) { 1285 return RUBBER_BAND_FACTOR_ON_PANEL_EXPAND; 1286 } else if (mScrolledToTopOnFirstDown) { 1287 return 1.0f; 1288 } 1289 return RUBBER_BAND_FACTOR_NORMAL; 1290 } 1291 1292 private void endDrag() { 1293 setIsBeingDragged(false); 1294 1295 recycleVelocityTracker(); 1296 1297 if (getCurrentOverScrollAmount(true /* onTop */) > 0) { 1298 setOverScrollAmount(0, true /* onTop */, true /* animate */); 1299 } 1300 if (getCurrentOverScrollAmount(false /* onTop */) > 0) { 1301 setOverScrollAmount(0, false /* onTop */, true /* animate */); 1302 } 1303 } 1304 1305 @Override 1306 public boolean onInterceptTouchEvent(MotionEvent ev) { 1307 initDownStates(ev); 1308 boolean expandWantsIt = false; 1309 if (!mSwipingInProgress && !mOnlyScrollingInThisMotion) { 1310 expandWantsIt = mExpandHelper.onInterceptTouchEvent(ev); 1311 } 1312 boolean scrollWantsIt = false; 1313 if (!mSwipingInProgress && !mExpandingNotification) { 1314 scrollWantsIt = onInterceptTouchEventScroll(ev); 1315 } 1316 boolean swipeWantsIt = false; 1317 if (!mIsBeingDragged 1318 && !mExpandingNotification 1319 && !mExpandedInThisMotion 1320 && !mOnlyScrollingInThisMotion) { 1321 swipeWantsIt = mSwipeHelper.onInterceptTouchEvent(ev); 1322 } 1323 return swipeWantsIt || scrollWantsIt || expandWantsIt || super.onInterceptTouchEvent(ev); 1324 } 1325 1326 private void initDownStates(MotionEvent ev) { 1327 if (ev.getAction() == MotionEvent.ACTION_DOWN) { 1328 mExpandedInThisMotion = false; 1329 mOnlyScrollingInThisMotion = !mScroller.isFinished(); 1330 } 1331 } 1332 1333 @Override 1334 protected void onViewRemoved(View child) { 1335 super.onViewRemoved(child); 1336 mStackScrollAlgorithm.notifyChildrenChanged(this); 1337 if (mChangePositionInProgress) { 1338 // This is only a position change, don't do anything special 1339 return; 1340 } 1341 ((ExpandableView) child).setOnHeightChangedListener(null); 1342 mCurrentStackScrollState.removeViewStateForView(child); 1343 updateScrollStateForRemovedChild(child); 1344 boolean animationGenerated = generateRemoveAnimation(child); 1345 if (animationGenerated && !mSwipedOutViews.contains(child)) { 1346 // Add this view to an overlay in order to ensure that it will still be temporary 1347 // drawn when removed 1348 getOverlay().add(child); 1349 } 1350 } 1351 1352 /** 1353 * Generate a remove animation for a child view. 1354 * 1355 * @param child The view to generate the remove animation for. 1356 * @return Whether an animation was generated. 1357 */ 1358 private boolean generateRemoveAnimation(View child) { 1359 if (mIsExpanded && mAnimationsEnabled) { 1360 if (!mChildrenToAddAnimated.contains(child)) { 1361 // Generate Animations 1362 mChildrenToRemoveAnimated.add(child); 1363 mNeedsAnimation = true; 1364 return true; 1365 } else { 1366 mChildrenToAddAnimated.remove(child); 1367 return false; 1368 } 1369 } 1370 return false; 1371 } 1372 1373 /** 1374 * Updates the scroll position when a child was removed 1375 * 1376 * @param removedChild the removed child 1377 */ 1378 private void updateScrollStateForRemovedChild(View removedChild) { 1379 int startingPosition = getPositionInLinearLayout(removedChild); 1380 int childHeight = removedChild.getHeight() + mPaddingBetweenElements; 1381 int endPosition = startingPosition + childHeight; 1382 if (endPosition <= mOwnScrollY) { 1383 // This child is fully scrolled of the top, so we have to deduct its height from the 1384 // scrollPosition 1385 mOwnScrollY -= childHeight; 1386 } else if (startingPosition < mOwnScrollY) { 1387 // This child is currently being scrolled into, set the scroll position to the start of 1388 // this child 1389 mOwnScrollY = startingPosition; 1390 } 1391 } 1392 1393 private int getPositionInLinearLayout(View requestedChild) { 1394 int position = 0; 1395 for (int i = 0; i < getChildCount(); i++) { 1396 View child = getChildAt(i); 1397 if (child == requestedChild) { 1398 return position; 1399 } 1400 if (child.getVisibility() != View.GONE) { 1401 position += child.getHeight(); 1402 if (i < getChildCount()-1) { 1403 position += mPaddingBetweenElements; 1404 } 1405 } 1406 } 1407 return 0; 1408 } 1409 1410 @Override 1411 protected void onViewAdded(View child) { 1412 super.onViewAdded(child); 1413 mStackScrollAlgorithm.notifyChildrenChanged(this); 1414 ((ExpandableView) child).setOnHeightChangedListener(this); 1415 generateAddAnimation(child); 1416 } 1417 1418 public void setAnimationsEnabled(boolean animationsEnabled) { 1419 mAnimationsEnabled = animationsEnabled; 1420 } 1421 1422 public boolean isAddOrRemoveAnimationPending() { 1423 return mNeedsAnimation 1424 && (!mChildrenToAddAnimated.isEmpty() || !mChildrenToRemoveAnimated.isEmpty()); 1425 } 1426 /** 1427 * Generate an animation for an added child view. 1428 * 1429 * @param child The view to be added. 1430 */ 1431 public void generateAddAnimation(View child) { 1432 if (mIsExpanded && mAnimationsEnabled && !mChangePositionInProgress) { 1433 // Generate Animations 1434 mChildrenToAddAnimated.add(child); 1435 mNeedsAnimation = true; 1436 } 1437 } 1438 1439 /** 1440 * Change the position of child to a new location 1441 * 1442 * @param child the view to change the position for 1443 * @param newIndex the new index 1444 */ 1445 public void changeViewPosition(View child, int newIndex) { 1446 if (child != null && child.getParent() == this) { 1447 mChangePositionInProgress = true; 1448 removeView(child); 1449 addView(child, newIndex); 1450 mChangePositionInProgress = false; 1451 if (mIsExpanded && mAnimationsEnabled) { 1452 mChildrenChangingPositions.add(child); 1453 mNeedsAnimation = true; 1454 } 1455 } 1456 } 1457 1458 private void startAnimationToState() { 1459 if (mNeedsAnimation) { 1460 generateChildHierarchyEvents(); 1461 mNeedsAnimation = false; 1462 } 1463 if (!mAnimationEvents.isEmpty() || isCurrentlyAnimating()) { 1464 mStateAnimator.startAnimationForEvents(mAnimationEvents, mCurrentStackScrollState); 1465 mAnimationEvents.clear(); 1466 } else { 1467 applyCurrentState(); 1468 } 1469 } 1470 1471 private void generateChildHierarchyEvents() { 1472 generateChildRemovalEvents(); 1473 generateChildAdditionEvents(); 1474 generatePositionChangeEvents(); 1475 generateSnapBackEvents(); 1476 generateDragEvents(); 1477 generateTopPaddingEvent(); 1478 generateActivateEvent(); 1479 generateDimmedEvent(); 1480 mNeedsAnimation = false; 1481 } 1482 1483 private void generateSnapBackEvents() { 1484 for (View child : mSnappedBackChildren) { 1485 mAnimationEvents.add(new AnimationEvent(child, 1486 AnimationEvent.ANIMATION_TYPE_SNAP_BACK)); 1487 } 1488 mSnappedBackChildren.clear(); 1489 } 1490 1491 private void generateDragEvents() { 1492 for (View child : mDragAnimPendingChildren) { 1493 mAnimationEvents.add(new AnimationEvent(child, 1494 AnimationEvent.ANIMATION_TYPE_START_DRAG)); 1495 } 1496 mDragAnimPendingChildren.clear(); 1497 } 1498 1499 private void generateChildRemovalEvents() { 1500 for (View child : mChildrenToRemoveAnimated) { 1501 boolean childWasSwipedOut = mSwipedOutViews.contains(child); 1502 int animationType = childWasSwipedOut 1503 ? AnimationEvent.ANIMATION_TYPE_REMOVE_SWIPED_OUT 1504 : AnimationEvent.ANIMATION_TYPE_REMOVE; 1505 AnimationEvent event = new AnimationEvent(child, animationType); 1506 1507 // we need to know the view after this one 1508 event.viewAfterChangingView = getFirstChildBelowTranlsationY(child.getTranslationY()); 1509 mAnimationEvents.add(event); 1510 } 1511 mSwipedOutViews.clear(); 1512 mChildrenToRemoveAnimated.clear(); 1513 } 1514 1515 private void generatePositionChangeEvents() { 1516 for (View child : mChildrenChangingPositions) { 1517 mAnimationEvents.add(new AnimationEvent(child, 1518 AnimationEvent.ANIMATION_TYPE_CHANGE_POSITION)); 1519 } 1520 mChildrenChangingPositions.clear(); 1521 } 1522 1523 private void generateChildAdditionEvents() { 1524 for (View child : mChildrenToAddAnimated) { 1525 mAnimationEvents.add(new AnimationEvent(child, 1526 AnimationEvent.ANIMATION_TYPE_ADD)); 1527 } 1528 mChildrenToAddAnimated.clear(); 1529 } 1530 1531 private void generateTopPaddingEvent() { 1532 if (mTopPaddingNeedsAnimation) { 1533 mAnimationEvents.add( 1534 new AnimationEvent(null, AnimationEvent.ANIMATION_TYPE_TOP_PADDING_CHANGED)); 1535 } 1536 mTopPaddingNeedsAnimation = false; 1537 } 1538 1539 private void generateActivateEvent() { 1540 if (mActivateNeedsAnimation) { 1541 mAnimationEvents.add( 1542 new AnimationEvent(null, AnimationEvent.ANIMATION_TYPE_ACTIVATED_CHILD)); 1543 } 1544 mActivateNeedsAnimation = false; 1545 } 1546 1547 private void generateDimmedEvent() { 1548 if (mDimmedNeedsAnimation) { 1549 mAnimationEvents.add( 1550 new AnimationEvent(null, AnimationEvent.ANIMATION_TYPE_DIMMED)); 1551 } 1552 mDimmedNeedsAnimation = false; 1553 } 1554 1555 private boolean onInterceptTouchEventScroll(MotionEvent ev) { 1556 if (!isScrollingEnabled()) { 1557 return false; 1558 } 1559 /* 1560 * This method JUST determines whether we want to intercept the motion. 1561 * If we return true, onMotionEvent will be called and we do the actual 1562 * scrolling there. 1563 */ 1564 1565 /* 1566 * Shortcut the most recurring case: the user is in the dragging 1567 * state and he is moving his finger. We want to intercept this 1568 * motion. 1569 */ 1570 final int action = ev.getAction(); 1571 if ((action == MotionEvent.ACTION_MOVE) && (mIsBeingDragged)) { 1572 return true; 1573 } 1574 1575 switch (action & MotionEvent.ACTION_MASK) { 1576 case MotionEvent.ACTION_MOVE: { 1577 /* 1578 * mIsBeingDragged == false, otherwise the shortcut would have caught it. Check 1579 * whether the user has moved far enough from his original down touch. 1580 */ 1581 1582 /* 1583 * Locally do absolute value. mLastMotionY is set to the y value 1584 * of the down event. 1585 */ 1586 final int activePointerId = mActivePointerId; 1587 if (activePointerId == INVALID_POINTER) { 1588 // If we don't have a valid id, the touch down wasn't on content. 1589 break; 1590 } 1591 1592 final int pointerIndex = ev.findPointerIndex(activePointerId); 1593 if (pointerIndex == -1) { 1594 Log.e(TAG, "Invalid pointerId=" + activePointerId 1595 + " in onInterceptTouchEvent"); 1596 break; 1597 } 1598 1599 final int y = (int) ev.getY(pointerIndex); 1600 final int x = (int) ev.getX(pointerIndex); 1601 final int yDiff = Math.abs(y - mLastMotionY); 1602 final int xDiff = Math.abs(x - mDownX); 1603 if (yDiff > mTouchSlop && yDiff > xDiff) { 1604 setIsBeingDragged(true); 1605 mLastMotionY = y; 1606 mDownX = x; 1607 initVelocityTrackerIfNotExists(); 1608 mVelocityTracker.addMovement(ev); 1609 } 1610 break; 1611 } 1612 1613 case MotionEvent.ACTION_DOWN: { 1614 final int y = (int) ev.getY(); 1615 if (getChildAtPosition(ev.getX(), y) == null) { 1616 setIsBeingDragged(false); 1617 recycleVelocityTracker(); 1618 break; 1619 } 1620 1621 /* 1622 * Remember location of down touch. 1623 * ACTION_DOWN always refers to pointer index 0. 1624 */ 1625 mLastMotionY = y; 1626 mDownX = (int) ev.getX(); 1627 mActivePointerId = ev.getPointerId(0); 1628 mScrolledToTopOnFirstDown = isScrolledToTop(); 1629 1630 initOrResetVelocityTracker(); 1631 mVelocityTracker.addMovement(ev); 1632 /* 1633 * If being flinged and user touches the screen, initiate drag; 1634 * otherwise don't. mScroller.isFinished should be false when 1635 * being flinged. 1636 */ 1637 boolean isBeingDragged = !mScroller.isFinished(); 1638 setIsBeingDragged(isBeingDragged); 1639 break; 1640 } 1641 1642 case MotionEvent.ACTION_CANCEL: 1643 case MotionEvent.ACTION_UP: 1644 /* Release the drag */ 1645 setIsBeingDragged(false); 1646 mActivePointerId = INVALID_POINTER; 1647 recycleVelocityTracker(); 1648 if (mScroller.springBack(mScrollX, mOwnScrollY, 0, 0, 0, getScrollRange())) { 1649 postInvalidateOnAnimation(); 1650 } 1651 break; 1652 case MotionEvent.ACTION_POINTER_UP: 1653 onSecondaryPointerUp(ev); 1654 break; 1655 } 1656 1657 /* 1658 * The only time we want to intercept motion events is if we are in the 1659 * drag mode. 1660 */ 1661 return mIsBeingDragged; 1662 } 1663 1664 /** 1665 * @return Whether the specified motion event is actually happening over the content. 1666 */ 1667 private boolean isInContentBounds(MotionEvent event) { 1668 return event.getY() < getHeight() - getEmptyBottomMargin(); 1669 } 1670 1671 private void setIsBeingDragged(boolean isDragged) { 1672 mIsBeingDragged = isDragged; 1673 if (isDragged) { 1674 requestDisallowInterceptTouchEvent(true); 1675 removeLongPressCallback(); 1676 } 1677 } 1678 1679 @Override 1680 public void onWindowFocusChanged(boolean hasWindowFocus) { 1681 super.onWindowFocusChanged(hasWindowFocus); 1682 if (!hasWindowFocus) { 1683 removeLongPressCallback(); 1684 } 1685 } 1686 1687 public void removeLongPressCallback() { 1688 mSwipeHelper.removeLongPressCallback(); 1689 } 1690 1691 @Override 1692 public boolean isScrolledToTop() { 1693 return mOwnScrollY == 0; 1694 } 1695 1696 @Override 1697 public boolean isScrolledToBottom() { 1698 return mOwnScrollY >= getScrollRange(); 1699 } 1700 1701 @Override 1702 public View getHostView() { 1703 return this; 1704 } 1705 1706 public int getEmptyBottomMargin() { 1707 int emptyMargin = mMaxLayoutHeight - mContentHeight; 1708 if (needsHeightAdaption()) { 1709 emptyMargin = emptyMargin - mBottomStackSlowDownHeight - mBottomStackPeekSize; 1710 } else { 1711 emptyMargin = emptyMargin - mBottomStackPeekSize; 1712 } 1713 return Math.max(emptyMargin, 0); 1714 } 1715 1716 public void onExpansionStarted() { 1717 mIsExpansionChanging = true; 1718 mStackScrollAlgorithm.onExpansionStarted(mCurrentStackScrollState); 1719 } 1720 1721 public void onExpansionStopped() { 1722 mIsExpansionChanging = false; 1723 mStackScrollAlgorithm.onExpansionStopped(); 1724 } 1725 1726 private void setIsExpanded(boolean isExpanded) { 1727 mIsExpanded = isExpanded; 1728 mStackScrollAlgorithm.setIsExpanded(isExpanded); 1729 if (!isExpanded) { 1730 mOwnScrollY = 0; 1731 } 1732 } 1733 1734 @Override 1735 public void onHeightChanged(ExpandableView view) { 1736 updateContentHeight(); 1737 updateScrollPositionIfNecessary(); 1738 notifyHeightChangeListener(view); 1739 requestChildrenUpdate(); 1740 } 1741 1742 public void setOnHeightChangedListener( 1743 ExpandableView.OnHeightChangedListener mOnHeightChangedListener) { 1744 this.mOnHeightChangedListener = mOnHeightChangedListener; 1745 } 1746 1747 public void onChildAnimationFinished() { 1748 requestChildrenUpdate(); 1749 } 1750 1751 /** 1752 * See {@link AmbientState#setDimmed}. 1753 */ 1754 public void setDimmed(boolean dimmed, boolean animate) { 1755 mStackScrollAlgorithm.setDimmed(dimmed); 1756 mAmbientState.setDimmed(dimmed); 1757 updatePadding(dimmed); 1758 if (animate && mAnimationsEnabled) { 1759 mDimmedNeedsAnimation = true; 1760 mNeedsAnimation = true; 1761 } 1762 requestChildrenUpdate(); 1763 } 1764 1765 /** 1766 * See {@link AmbientState#setActivatedChild}. 1767 */ 1768 public void setActivatedChild(ActivatableNotificationView activatedChild) { 1769 mAmbientState.setActivatedChild(activatedChild); 1770 if (mAnimationsEnabled) { 1771 mActivateNeedsAnimation = true; 1772 mNeedsAnimation = true; 1773 } 1774 requestChildrenUpdate(); 1775 } 1776 1777 public ActivatableNotificationView getActivatedChild() { 1778 return mAmbientState.getActivatedChild(); 1779 } 1780 1781 private void applyCurrentState() { 1782 mCurrentStackScrollState.apply(); 1783 if (mListener != null) { 1784 mListener.onChildLocationsChanged(this); 1785 } 1786 } 1787 1788 public void setSpeedBumpView(SpeedBumpView speedBumpView) { 1789 mSpeedBumpView = speedBumpView; 1790 addView(speedBumpView); 1791 } 1792 1793 private void updateSpeedBump(boolean visible) { 1794 boolean notGoneBefore = mSpeedBumpView.getVisibility() != GONE; 1795 if (visible != notGoneBefore) { 1796 int newVisibility = visible ? VISIBLE : GONE; 1797 mSpeedBumpView.setVisibility(newVisibility); 1798 if (visible) { 1799 // Make invisible to ensure that the appear animation is played. 1800 mSpeedBumpView.setInvisible(); 1801 if (!mIsExpansionChanging) { 1802 generateAddAnimation(mSpeedBumpView); 1803 } 1804 } else { 1805 mSpeedBumpView.performVisibilityAnimation(false); 1806 generateRemoveAnimation(mSpeedBumpView); 1807 } 1808 } 1809 } 1810 1811 public void goToFullShade() { 1812 updateSpeedBump(true); 1813 } 1814 1815 public void cancelExpandHelper() { 1816 mExpandHelper.cancel(); 1817 } 1818 1819 public void setIntrinsicPadding(int intrinsicPadding) { 1820 mIntrinsicPadding = intrinsicPadding; 1821 } 1822 1823 /** 1824 * @return the y position of the first notification 1825 */ 1826 public float getNotificationsTopY() { 1827 return mTopPadding + getTranslationY(); 1828 } 1829 1830 public void setTouchEnabled(boolean touchEnabled) { 1831 mTouchEnabled = touchEnabled; 1832 } 1833 1834 @Override 1835 public boolean dispatchTouchEvent(MotionEvent ev) { 1836 if (!mTouchEnabled) { 1837 return false; 1838 } 1839 return super.dispatchTouchEvent(ev); 1840 } 1841 1842 @Override 1843 public boolean shouldDelayChildPressedState() { 1844 return true; 1845 } 1846 1847 /** 1848 * A listener that is notified when some child locations might have changed. 1849 */ 1850 public interface OnChildLocationsChangedListener { 1851 public void onChildLocationsChanged(NotificationStackScrollLayout stackScrollLayout); 1852 } 1853 1854 /** 1855 * A listener that gets notified when the overscroll at the top has changed. 1856 */ 1857 public interface OnOverscrollTopChangedListener { 1858 public void onOverscrollTopChanged(float amount); 1859 1860 /** 1861 * Notify a listener that the scroller wants to escape from the scrolling motion and 1862 * start a fling animation to the expanded or collapsed overscroll view (e.g expand the QS) 1863 * 1864 * @param velocity The velocity that the Scroller had when over flinging 1865 * @param open Should the fling open or close the overscroll view. 1866 */ 1867 public void flingTopOverscroll(float velocity, boolean open); 1868 } 1869 1870 static class AnimationEvent { 1871 1872 static AnimationFilter[] FILTERS = new AnimationFilter[] { 1873 1874 // ANIMATION_TYPE_ADD 1875 new AnimationFilter() 1876 .animateAlpha() 1877 .animateHeight() 1878 .animateTopInset() 1879 .animateY() 1880 .animateZ() 1881 .hasDelays(), 1882 1883 // ANIMATION_TYPE_REMOVE 1884 new AnimationFilter() 1885 .animateAlpha() 1886 .animateHeight() 1887 .animateTopInset() 1888 .animateY() 1889 .animateZ() 1890 .hasDelays(), 1891 1892 // ANIMATION_TYPE_REMOVE_SWIPED_OUT 1893 new AnimationFilter() 1894 .animateAlpha() 1895 .animateHeight() 1896 .animateTopInset() 1897 .animateY() 1898 .animateZ() 1899 .hasDelays(), 1900 1901 // ANIMATION_TYPE_TOP_PADDING_CHANGED 1902 new AnimationFilter() 1903 .animateAlpha() 1904 .animateHeight() 1905 .animateTopInset() 1906 .animateY() 1907 .animateDimmed() 1908 .animateScale() 1909 .animateZ(), 1910 1911 // ANIMATION_TYPE_START_DRAG 1912 new AnimationFilter() 1913 .animateAlpha(), 1914 1915 // ANIMATION_TYPE_SNAP_BACK 1916 new AnimationFilter() 1917 .animateAlpha(), 1918 1919 // ANIMATION_TYPE_ACTIVATED_CHILD 1920 new AnimationFilter() 1921 .animateScale() 1922 .animateAlpha(), 1923 1924 // ANIMATION_TYPE_DIMMED 1925 new AnimationFilter() 1926 .animateY() 1927 .animateScale() 1928 .animateDimmed(), 1929 1930 // ANIMATION_TYPE_CHANGE_POSITION 1931 new AnimationFilter() 1932 .animateAlpha() 1933 .animateHeight() 1934 .animateTopInset() 1935 .animateY() 1936 .animateZ() 1937 }; 1938 1939 static int[] LENGTHS = new int[] { 1940 1941 // ANIMATION_TYPE_ADD 1942 StackStateAnimator.ANIMATION_DURATION_APPEAR_DISAPPEAR, 1943 1944 // ANIMATION_TYPE_REMOVE 1945 StackStateAnimator.ANIMATION_DURATION_APPEAR_DISAPPEAR, 1946 1947 // ANIMATION_TYPE_REMOVE_SWIPED_OUT 1948 StackStateAnimator.ANIMATION_DURATION_STANDARD, 1949 1950 // ANIMATION_TYPE_TOP_PADDING_CHANGED 1951 StackStateAnimator.ANIMATION_DURATION_STANDARD, 1952 1953 // ANIMATION_TYPE_START_DRAG 1954 StackStateAnimator.ANIMATION_DURATION_STANDARD, 1955 1956 // ANIMATION_TYPE_SNAP_BACK 1957 StackStateAnimator.ANIMATION_DURATION_STANDARD, 1958 1959 // ANIMATION_TYPE_ACTIVATED_CHILD 1960 StackStateAnimator.ANIMATION_DURATION_DIMMED_ACTIVATED, 1961 1962 // ANIMATION_TYPE_DIMMED 1963 StackStateAnimator.ANIMATION_DURATION_DIMMED_ACTIVATED, 1964 1965 // ANIMATION_TYPE_CHANGE_POSITION 1966 StackStateAnimator.ANIMATION_DURATION_STANDARD, 1967 }; 1968 1969 static final int ANIMATION_TYPE_ADD = 0; 1970 static final int ANIMATION_TYPE_REMOVE = 1; 1971 static final int ANIMATION_TYPE_REMOVE_SWIPED_OUT = 2; 1972 static final int ANIMATION_TYPE_TOP_PADDING_CHANGED = 3; 1973 static final int ANIMATION_TYPE_START_DRAG = 4; 1974 static final int ANIMATION_TYPE_SNAP_BACK = 5; 1975 static final int ANIMATION_TYPE_ACTIVATED_CHILD = 6; 1976 static final int ANIMATION_TYPE_DIMMED = 7; 1977 static final int ANIMATION_TYPE_CHANGE_POSITION = 8; 1978 1979 final long eventStartTime; 1980 final View changingView; 1981 final int animationType; 1982 final AnimationFilter filter; 1983 final long length; 1984 View viewAfterChangingView; 1985 1986 AnimationEvent(View view, int type) { 1987 eventStartTime = AnimationUtils.currentAnimationTimeMillis(); 1988 changingView = view; 1989 animationType = type; 1990 filter = FILTERS[type]; 1991 length = LENGTHS[type]; 1992 } 1993 1994 /** 1995 * Combines the length of several animation events into a single value. 1996 * 1997 * @param events The events of the lengths to combine. 1998 * @return The combined length. This is just the maximum of all length. 1999 */ 2000 static long combineLength(ArrayList<AnimationEvent> events) { 2001 long length = 0; 2002 int size = events.size(); 2003 for (int i = 0; i < size; i++) { 2004 length = Math.max(length, events.get(i).length); 2005 } 2006 return length; 2007 } 2008 } 2009 2010} 2011