StackScrollAlgorithm.java revision 33d4614dfe2b9f89f653210b8609fca4f27c7fe9
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.util.DisplayMetrics; 21import android.util.Log; 22import android.view.View; 23import android.view.ViewGroup; 24 25import com.android.systemui.R; 26import com.android.systemui.statusbar.ExpandableNotificationRow; 27import com.android.systemui.statusbar.ExpandableView; 28 29import java.util.ArrayList; 30import java.util.List; 31 32/** 33 * The Algorithm of the {@link com.android.systemui.statusbar.stack 34 * .NotificationStackScrollLayout} which can be queried for {@link com.android.systemui.statusbar 35 * .stack.StackScrollState} 36 */ 37public class StackScrollAlgorithm { 38 39 private static final String LOG_TAG = "StackScrollAlgorithm"; 40 41 private static final int MAX_ITEMS_IN_BOTTOM_STACK = 3; 42 private static final int MAX_ITEMS_IN_TOP_STACK = 3; 43 44 public static final float DIMMED_SCALE = 0.98f; 45 46 private int mPaddingBetweenElements; 47 private int mCollapsedSize; 48 private int mTopStackPeekSize; 49 private int mBottomStackPeekSize; 50 private int mZDistanceBetweenElements; 51 private int mZBasicHeight; 52 53 private StackIndentationFunctor mTopStackIndentationFunctor; 54 private StackIndentationFunctor mBottomStackIndentationFunctor; 55 56 private StackScrollAlgorithmState mTempAlgorithmState = new StackScrollAlgorithmState(); 57 private boolean mIsExpansionChanging; 58 private int mFirstChildMaxHeight; 59 private boolean mIsExpanded; 60 private ExpandableView mFirstChildWhileExpanding; 61 private boolean mExpandedOnStart; 62 private int mTopStackTotalSize; 63 private int mPaddingBetweenElementsDimmed; 64 private int mPaddingBetweenElementsNormal; 65 private int mBottomStackSlowDownLength; 66 private int mTopStackSlowDownLength; 67 private int mCollapseSecondCardPadding; 68 private boolean mScaleDimmed; 69 private ExpandableView mFirstChild; 70 private int mFirstChildMinHeight; 71 private boolean mDimmed; 72 73 public StackScrollAlgorithm(Context context) { 74 initView(context); 75 } 76 77 public void initView(Context context) { 78 initConstants(context); 79 updatePadding(); 80 } 81 82 private void updatePadding() { 83 mPaddingBetweenElements = mDimmed && mScaleDimmed 84 ? mPaddingBetweenElementsDimmed 85 : mPaddingBetweenElementsNormal; 86 mTopStackTotalSize = mTopStackSlowDownLength + mPaddingBetweenElements 87 + mTopStackPeekSize; 88 mTopStackIndentationFunctor = new PiecewiseLinearIndentationFunctor( 89 MAX_ITEMS_IN_TOP_STACK, 90 mTopStackPeekSize, 91 mTopStackTotalSize - mTopStackPeekSize, 92 0.5f); 93 mBottomStackIndentationFunctor = new PiecewiseLinearIndentationFunctor( 94 MAX_ITEMS_IN_BOTTOM_STACK, 95 mBottomStackPeekSize, 96 getBottomStackSlowDownLength(), 97 0.5f); 98 } 99 100 public int getBottomStackSlowDownLength() { 101 return mBottomStackSlowDownLength + mPaddingBetweenElements; 102 } 103 104 private void initConstants(Context context) { 105 mPaddingBetweenElementsDimmed = context.getResources() 106 .getDimensionPixelSize(R.dimen.notification_padding_dimmed); 107 mPaddingBetweenElementsNormal = context.getResources() 108 .getDimensionPixelSize(R.dimen.notification_padding); 109 mCollapsedSize = context.getResources() 110 .getDimensionPixelSize(R.dimen.notification_min_height); 111 mTopStackPeekSize = context.getResources() 112 .getDimensionPixelSize(R.dimen.top_stack_peek_amount); 113 mBottomStackPeekSize = context.getResources() 114 .getDimensionPixelSize(R.dimen.bottom_stack_peek_amount); 115 mZDistanceBetweenElements = context.getResources() 116 .getDimensionPixelSize(R.dimen.z_distance_between_notifications); 117 mZBasicHeight = (MAX_ITEMS_IN_BOTTOM_STACK + 1) * mZDistanceBetweenElements; 118 mBottomStackSlowDownLength = context.getResources() 119 .getDimensionPixelSize(R.dimen.bottom_stack_slow_down_length); 120 mTopStackSlowDownLength = context.getResources() 121 .getDimensionPixelSize(R.dimen.top_stack_slow_down_length); 122 mCollapseSecondCardPadding = context.getResources().getDimensionPixelSize( 123 R.dimen.notification_collapse_second_card_padding); 124 mScaleDimmed = context.getResources().getDisplayMetrics().densityDpi 125 >= DisplayMetrics.DENSITY_420; 126 } 127 128 public boolean shouldScaleDimmed() { 129 return mScaleDimmed; 130 } 131 132 public void getStackScrollState(AmbientState ambientState, StackScrollState resultState) { 133 // The state of the local variables are saved in an algorithmState to easily subdivide it 134 // into multiple phases. 135 StackScrollAlgorithmState algorithmState = mTempAlgorithmState; 136 137 // First we reset the view states to their default values. 138 resultState.resetViewStates(); 139 140 algorithmState.itemsInTopStack = 0.0f; 141 algorithmState.partialInTop = 0.0f; 142 algorithmState.lastTopStackIndex = 0; 143 algorithmState.scrolledPixelsTop = 0; 144 algorithmState.itemsInBottomStack = 0.0f; 145 algorithmState.partialInBottom = 0.0f; 146 mFirstChildMinHeight = mFirstChild == null ? 0 : mFirstChild.getMinHeight(); 147 float bottomOverScroll = ambientState.getOverScrollAmount(false /* onTop */); 148 149 int scrollY = ambientState.getScrollY(); 150 151 // Due to the overScroller, the stackscroller can have negative scroll state. This is 152 // already accounted for by the top padding and doesn't need an additional adaption 153 scrollY = Math.max(0, scrollY); 154 algorithmState.scrollY = (int) (scrollY + mFirstChildMinHeight + bottomOverScroll); 155 156 updateVisibleChildren(resultState, algorithmState); 157 158 // Phase 1: 159 findNumberOfItemsInTopStackAndUpdateState(resultState, algorithmState, ambientState); 160 161 // Phase 2: 162 updatePositionsForState(resultState, algorithmState, ambientState); 163 164 // Phase 3: 165 updateZValuesForState(resultState, algorithmState); 166 167 handleDraggedViews(ambientState, resultState, algorithmState); 168 updateDimmedActivatedHideSensitive(ambientState, resultState, algorithmState); 169 updateClipping(resultState, algorithmState, ambientState); 170 updateSpeedBumpState(resultState, algorithmState, ambientState.getSpeedBumpIndex()); 171 getNotificationChildrenStates(resultState, algorithmState); 172 } 173 174 private void getNotificationChildrenStates(StackScrollState resultState, 175 StackScrollAlgorithmState algorithmState) { 176 int childCount = algorithmState.visibleChildren.size(); 177 for (int i = 0; i < childCount; i++) { 178 ExpandableView v = algorithmState.visibleChildren.get(i); 179 if (v instanceof ExpandableNotificationRow) { 180 ExpandableNotificationRow row = (ExpandableNotificationRow) v; 181 row.getChildrenStates(resultState); 182 } 183 } 184 } 185 186 private void updateSpeedBumpState(StackScrollState resultState, 187 StackScrollAlgorithmState algorithmState, int speedBumpIndex) { 188 int childCount = algorithmState.visibleChildren.size(); 189 for (int i = 0; i < childCount; i++) { 190 View child = algorithmState.visibleChildren.get(i); 191 StackViewState childViewState = resultState.getViewStateForView(child); 192 193 // The speed bump can also be gone, so equality needs to be taken when comparing 194 // indices. 195 childViewState.belowSpeedBump = speedBumpIndex != -1 && i >= speedBumpIndex; 196 } 197 } 198 199 private void updateClipping(StackScrollState resultState, 200 StackScrollAlgorithmState algorithmState, AmbientState ambientState) { 201 boolean dismissAllInProgress = ambientState.isDismissAllInProgress(); 202 float previousNotificationEnd = 0; 203 float previousNotificationStart = 0; 204 boolean previousNotificationIsSwiped = false; 205 int childCount = algorithmState.visibleChildren.size(); 206 for (int i = 0; i < childCount; i++) { 207 ExpandableView child = algorithmState.visibleChildren.get(i); 208 StackViewState state = resultState.getViewStateForView(child); 209 float newYTranslation = state.yTranslation + state.height * (1f - state.scale) / 2f; 210 float newHeight = state.height * state.scale; 211 // apply clipping and shadow 212 float newNotificationEnd = newYTranslation + newHeight; 213 214 float clipHeight; 215 if (previousNotificationIsSwiped) { 216 // When the previous notification is swiped, we don't clip the content to the 217 // bottom of it. 218 clipHeight = newHeight; 219 } else { 220 clipHeight = newNotificationEnd - previousNotificationEnd; 221 clipHeight = Math.max(0.0f, clipHeight); 222 } 223 224 updateChildClippingAndBackground(state, newHeight, clipHeight, 225 newHeight - (previousNotificationStart - newYTranslation)); 226 227 if (dismissAllInProgress) { 228 state.clipTopAmount = Math.max(child.getMinClipTopAmount(), state.clipTopAmount); 229 } 230 231 if (!child.isTransparent()) { 232 // Only update the previous values if we are not transparent, 233 // otherwise we would clip to a transparent view. 234 if ((dismissAllInProgress && canChildBeDismissed(child))) { 235 previousNotificationIsSwiped = true; 236 } else { 237 previousNotificationIsSwiped = ambientState.getDraggedViews().contains(child); 238 previousNotificationEnd = newNotificationEnd; 239 previousNotificationStart = newYTranslation + state.clipTopAmount * state.scale; 240 } 241 } 242 } 243 } 244 245 public static boolean canChildBeDismissed(View v) { 246 final View veto = v.findViewById(R.id.veto); 247 return (veto != null && veto.getVisibility() != View.GONE); 248 } 249 250 /** 251 * Updates the shadow outline and the clipping for a view. 252 * 253 * @param state the viewState to update 254 * @param realHeight the currently applied height of the view 255 * @param clipHeight the desired clip height, the rest of the view will be clipped from the top 256 * @param backgroundHeight the desired background height. The shadows of the view will be 257 * based on this height and the content will be clipped from the top 258 */ 259 private void updateChildClippingAndBackground(StackViewState state, float realHeight, 260 float clipHeight, float backgroundHeight) { 261 if (realHeight > clipHeight) { 262 // Rather overlap than create a hole. 263 state.topOverLap = (int) Math.floor((realHeight - clipHeight) / state.scale); 264 } else { 265 state.topOverLap = 0; 266 } 267 if (realHeight > backgroundHeight) { 268 // Rather overlap than create a hole. 269 state.clipTopAmount = (int) Math.floor((realHeight - backgroundHeight) / state.scale); 270 } else { 271 state.clipTopAmount = 0; 272 } 273 } 274 275 /** 276 * Updates the dimmed, activated and hiding sensitive states of the children. 277 */ 278 private void updateDimmedActivatedHideSensitive(AmbientState ambientState, 279 StackScrollState resultState, StackScrollAlgorithmState algorithmState) { 280 boolean dimmed = ambientState.isDimmed(); 281 boolean dark = ambientState.isDark(); 282 boolean hideSensitive = ambientState.isHideSensitive(); 283 View activatedChild = ambientState.getActivatedChild(); 284 int childCount = algorithmState.visibleChildren.size(); 285 for (int i = 0; i < childCount; i++) { 286 View child = algorithmState.visibleChildren.get(i); 287 StackViewState childViewState = resultState.getViewStateForView(child); 288 childViewState.dimmed = dimmed; 289 childViewState.dark = dark; 290 childViewState.hideSensitive = hideSensitive; 291 boolean isActivatedChild = activatedChild == child; 292 childViewState.scale = !mScaleDimmed || !dimmed || isActivatedChild 293 ? 1.0f 294 : DIMMED_SCALE; 295 if (dimmed && isActivatedChild) { 296 childViewState.zTranslation += 2.0f * mZDistanceBetweenElements; 297 } 298 } 299 } 300 301 /** 302 * Handle the special state when views are being dragged 303 */ 304 private void handleDraggedViews(AmbientState ambientState, StackScrollState resultState, 305 StackScrollAlgorithmState algorithmState) { 306 ArrayList<View> draggedViews = ambientState.getDraggedViews(); 307 for (View draggedView : draggedViews) { 308 int childIndex = algorithmState.visibleChildren.indexOf(draggedView); 309 if (childIndex >= 0 && childIndex < algorithmState.visibleChildren.size() - 1) { 310 View nextChild = algorithmState.visibleChildren.get(childIndex + 1); 311 if (!draggedViews.contains(nextChild)) { 312 // only if the view is not dragged itself we modify its state to be fully 313 // visible 314 StackViewState viewState = resultState.getViewStateForView( 315 nextChild); 316 // The child below the dragged one must be fully visible 317 if (ambientState.isShadeExpanded()) { 318 viewState.alpha = 1; 319 } 320 } 321 322 // Lets set the alpha to the one it currently has, as its currently being dragged 323 StackViewState viewState = resultState.getViewStateForView(draggedView); 324 // The dragged child should keep the set alpha 325 viewState.alpha = draggedView.getAlpha(); 326 } 327 } 328 } 329 330 /** 331 * Update the visible children on the state. 332 */ 333 private void updateVisibleChildren(StackScrollState resultState, 334 StackScrollAlgorithmState state) { 335 ViewGroup hostView = resultState.getHostView(); 336 int childCount = hostView.getChildCount(); 337 state.visibleChildren.clear(); 338 state.visibleChildren.ensureCapacity(childCount); 339 int notGoneIndex = 0; 340 for (int i = 0; i < childCount; i++) { 341 ExpandableView v = (ExpandableView) hostView.getChildAt(i); 342 if (v.getVisibility() != View.GONE) { 343 notGoneIndex = updateNotGoneIndex(resultState, state, notGoneIndex, v); 344 if (v instanceof ExpandableNotificationRow) { 345 ExpandableNotificationRow row = (ExpandableNotificationRow) v; 346 347 // handle the notgoneIndex for the children as well 348 List<ExpandableNotificationRow> children = 349 row.getNotificationChildren(); 350 if (row.isSummaryWithChildren() && children != null) { 351 for (ExpandableNotificationRow childRow : children) { 352 if (childRow.getVisibility() != View.GONE) { 353 StackViewState childState 354 = resultState.getViewStateForView(childRow); 355 childState.notGoneIndex = notGoneIndex; 356 notGoneIndex++; 357 } 358 } 359 } 360 } 361 } 362 } 363 } 364 365 private int updateNotGoneIndex(StackScrollState resultState, 366 StackScrollAlgorithmState state, int notGoneIndex, 367 ExpandableView v) { 368 StackViewState viewState = resultState.getViewStateForView(v); 369 viewState.notGoneIndex = notGoneIndex; 370 state.visibleChildren.add(v); 371 notGoneIndex++; 372 return notGoneIndex; 373 } 374 375 /** 376 * Determine the positions for the views. This is the main part of the algorithm. 377 * 378 * @param resultState The result state to update if a change to the properties of a child occurs 379 * @param algorithmState The state in which the current pass of the algorithm is currently in 380 * @param ambientState The current ambient state 381 */ 382 private void updatePositionsForState(StackScrollState resultState, 383 StackScrollAlgorithmState algorithmState, AmbientState ambientState) { 384 385 // The starting position of the bottom stack peek 386 float bottomPeekStart = ambientState.getInnerHeight() - mBottomStackPeekSize; 387 388 // The position where the bottom stack starts. 389 float bottomStackStart = bottomPeekStart - mBottomStackSlowDownLength; 390 391 // The y coordinate of the current child. 392 float currentYPosition = 0.0f; 393 394 // How far in is the element currently transitioning into the bottom stack. 395 float yPositionInScrollView = 0.0f; 396 397 int childCount = algorithmState.visibleChildren.size(); 398 int numberOfElementsCompletelyIn = algorithmState.partialInTop == 1.0f 399 ? algorithmState.lastTopStackIndex 400 : (int) algorithmState.itemsInTopStack; 401 for (int i = 0; i < childCount; i++) { 402 ExpandableView child = algorithmState.visibleChildren.get(i); 403 StackViewState childViewState = resultState.getViewStateForView(child); 404 childViewState.location = StackViewState.LOCATION_UNKNOWN; 405 int childHeight = getMaxAllowedChildHeight(child); 406 int minHeight = child.getMinHeight(); 407 float yPositionInScrollViewAfterElement = yPositionInScrollView 408 + childHeight 409 + mPaddingBetweenElements; 410 float scrollOffset = yPositionInScrollView - algorithmState.scrollY + 411 mFirstChildMinHeight; 412 413 if (i == algorithmState.lastTopStackIndex + 1) { 414 // Normally the position of this child is the position in the regular scrollview, 415 // but if the two stacks are very close to each other, 416 // then have have to push it even more upwards to the position of the bottom 417 // stack start. 418 currentYPosition = Math.min(scrollOffset, bottomStackStart); 419 } 420 childViewState.yTranslation = currentYPosition; 421 422 // The y position after this element 423 float nextYPosition = currentYPosition + childHeight + 424 mPaddingBetweenElements; 425 426 if (i <= algorithmState.lastTopStackIndex) { 427 // Case 1: 428 // We are in the top Stack 429 updateStateForTopStackChild(algorithmState, 430 numberOfElementsCompletelyIn, i, childHeight, childViewState, scrollOffset); 431 clampPositionToTopStackEnd(childViewState, childHeight); 432 433 // check if we are overlapping with the bottom stack 434 if (childViewState.yTranslation + childHeight + mPaddingBetweenElements 435 >= bottomStackStart && !mIsExpansionChanging && i != 0) { 436 // we just collapse this element slightly 437 int newSize = (int) Math.max(bottomStackStart - mPaddingBetweenElements - 438 childViewState.yTranslation, minHeight); 439 childViewState.height = newSize; 440 updateStateForChildTransitioningInBottom(algorithmState, bottomStackStart, 441 child, childViewState.yTranslation, childViewState, 442 childHeight); 443 } 444 clampPositionToBottomStackStart(childViewState, childViewState.height, 445 minHeight, ambientState); 446 } else if (nextYPosition >= bottomStackStart) { 447 // Case 2: 448 // We are in the bottom stack. 449 if (currentYPosition >= bottomStackStart) { 450 // According to the regular scroll view we are fully translated out of the 451 // bottom of the screen so we are fully in the bottom stack 452 updateStateForChildFullyInBottomStack(algorithmState, 453 bottomStackStart, childViewState, minHeight, ambientState); 454 } else { 455 // According to the regular scroll view we are currently translating out of / 456 // into the bottom of the screen 457 updateStateForChildTransitioningInBottom(algorithmState, 458 bottomStackStart, child, currentYPosition, 459 childViewState, childHeight); 460 } 461 } else { 462 // Case 3: 463 // We are in the regular scroll area. 464 childViewState.location = StackViewState.LOCATION_MAIN_AREA; 465 clampYTranslation(childViewState, childHeight, ambientState); 466 } 467 468 // The first card is always rendered. 469 if (i == 0) { 470 childViewState.alpha = 1.0f; 471 childViewState.yTranslation = Math.max( 472 mFirstChildMinHeight - algorithmState.scrollY, 0); 473 if (childViewState.yTranslation + childViewState.height 474 > bottomPeekStart - mCollapseSecondCardPadding) { 475 childViewState.height = (int) Math.max( 476 bottomPeekStart - mCollapseSecondCardPadding 477 - childViewState.yTranslation, mFirstChildMinHeight); 478 } 479 childViewState.location = StackViewState.LOCATION_FIRST_CARD; 480 } 481 if (childViewState.location == StackViewState.LOCATION_UNKNOWN) { 482 Log.wtf(LOG_TAG, "Failed to assign location for child " + i); 483 } 484 currentYPosition = childViewState.yTranslation + childHeight + mPaddingBetweenElements; 485 yPositionInScrollView = yPositionInScrollViewAfterElement; 486 487 childViewState.yTranslation += ambientState.getTopPadding() 488 + ambientState.getStackTranslation(); 489 } 490 updateHeadsUpStates(resultState, algorithmState, ambientState); 491 } 492 493 private void updateHeadsUpStates(StackScrollState resultState, 494 StackScrollAlgorithmState algorithmState, AmbientState ambientState) { 495 int childCount = algorithmState.visibleChildren.size(); 496 ExpandableNotificationRow topHeadsUpEntry = null; 497 for (int i = 0; i < childCount; i++) { 498 View child = algorithmState.visibleChildren.get(i); 499 if (!(child instanceof ExpandableNotificationRow)) { 500 break; 501 } 502 ExpandableNotificationRow row = (ExpandableNotificationRow) child; 503 if (!row.isHeadsUp()) { 504 break; 505 } else if (topHeadsUpEntry == null) { 506 topHeadsUpEntry = row; 507 } 508 StackViewState childState = resultState.getViewStateForView(row); 509 boolean isTopEntry = topHeadsUpEntry == row; 510 if (mIsExpanded) { 511 // Ensure that the heads up is always visible even when scrolled off from the bottom 512 float bottomPosition = ambientState.getMaxHeadsUpTranslation() - childState.height; 513 childState.yTranslation = Math.min(childState.yTranslation, 514 bottomPosition); 515 } 516 if (row.isPinned()) { 517 childState.yTranslation = Math.max(childState.yTranslation, 0); 518 childState.height = Math.max(row.getIntrinsicHeight(), childState.height); 519 if (!isTopEntry) { 520 // Ensure that a headsUp doesn't vertically extend further than the heads-up at 521 // the top most z-position 522 StackViewState topState = resultState.getViewStateForView(topHeadsUpEntry); 523 childState.height = row.getIntrinsicHeight(); 524 childState.yTranslation = topState.yTranslation + topState.height 525 - childState.height; 526 } 527 } 528 } 529 } 530 531 /** 532 * Clamp the yTranslation both up and down to valid positions. 533 * 534 * @param childViewState the view state of the child 535 * @param minHeight the minimum height of this child 536 */ 537 private void clampYTranslation(StackViewState childViewState, int minHeight, 538 AmbientState ambientState) { 539 clampPositionToBottomStackStart(childViewState, childViewState.height, minHeight, 540 ambientState); 541 clampPositionToTopStackEnd(childViewState, childViewState.height); 542 } 543 544 /** 545 * Clamp the yTranslation of the child down such that its end is at most on the beginning of 546 * the bottom stack. 547 * 548 * @param childViewState the view state of the child 549 * @param childHeight the height of this child 550 * @param minHeight the minumum Height of the View 551 */ 552 private void clampPositionToBottomStackStart(StackViewState childViewState, 553 int childHeight, int minHeight, AmbientState ambientState) { 554 555 int bottomStackStart = ambientState.getInnerHeight() 556 - mBottomStackPeekSize - mCollapseSecondCardPadding; 557 int childStart = bottomStackStart - childHeight; 558 if (childStart < childViewState.yTranslation) { 559 float newHeight = bottomStackStart - childViewState.yTranslation; 560 if (newHeight < minHeight) { 561 newHeight = minHeight; 562 childViewState.yTranslation = bottomStackStart - minHeight; 563 } 564 childViewState.height = (int) newHeight; 565 } 566 } 567 568 /** 569 * Clamp the yTranslation of the child up such that its end is at lest on the end of the top 570 * stack. 571 * 572 * @param childViewState the view state of the child 573 * @param childHeight the height of this child 574 */ 575 private void clampPositionToTopStackEnd(StackViewState childViewState, 576 int childHeight) { 577 childViewState.yTranslation = Math.max(childViewState.yTranslation, 578 mFirstChildMinHeight - childHeight); 579 } 580 581 private int getMaxAllowedChildHeight(View child) { 582 if (child instanceof ExpandableView) { 583 ExpandableView expandableView = (ExpandableView) child; 584 return expandableView.getIntrinsicHeight(); 585 } 586 return child == null? mCollapsedSize : child.getHeight(); 587 } 588 589 private void updateStateForChildTransitioningInBottom(StackScrollAlgorithmState algorithmState, 590 float transitioningPositionStart, ExpandableView child, float currentYPosition, 591 StackViewState childViewState, int childHeight) { 592 593 // This is the transitioning element on top of bottom stack, calculate how far we are in. 594 algorithmState.partialInBottom = 1.0f - ( 595 (transitioningPositionStart - currentYPosition) / (childHeight + 596 mPaddingBetweenElements)); 597 598 // the offset starting at the transitionPosition of the bottom stack 599 float offset = mBottomStackIndentationFunctor.getValue(algorithmState.partialInBottom); 600 algorithmState.itemsInBottomStack += algorithmState.partialInBottom; 601 int newHeight = childHeight; 602 if (childHeight > child.getMinHeight()) { 603 newHeight = (int) Math.max(Math.min(transitioningPositionStart + offset - 604 mPaddingBetweenElements - currentYPosition, childHeight), 605 child.getMinHeight()); 606 childViewState.height = newHeight; 607 } 608 childViewState.yTranslation = transitioningPositionStart + offset - newHeight 609 - mPaddingBetweenElements; 610 611 // We want at least to be at the end of the top stack when collapsing 612 clampPositionToTopStackEnd(childViewState, newHeight); 613 childViewState.location = StackViewState.LOCATION_MAIN_AREA; 614 } 615 616 private void updateStateForChildFullyInBottomStack(StackScrollAlgorithmState algorithmState, 617 float transitioningPositionStart, StackViewState childViewState, 618 int minHeight, AmbientState ambientState) { 619 float currentYPosition; 620 algorithmState.itemsInBottomStack += 1.0f; 621 if (algorithmState.itemsInBottomStack < MAX_ITEMS_IN_BOTTOM_STACK) { 622 // We are visually entering the bottom stack 623 currentYPosition = transitioningPositionStart 624 + mBottomStackIndentationFunctor.getValue(algorithmState.itemsInBottomStack) 625 - mPaddingBetweenElements; 626 childViewState.location = StackViewState.LOCATION_BOTTOM_STACK_PEEKING; 627 } else { 628 // we are fully inside the stack 629 if (algorithmState.itemsInBottomStack > MAX_ITEMS_IN_BOTTOM_STACK + 2) { 630 childViewState.alpha = 0.0f; 631 } else if (algorithmState.itemsInBottomStack 632 > MAX_ITEMS_IN_BOTTOM_STACK + 1) { 633 childViewState.alpha = 1.0f - algorithmState.partialInBottom; 634 } 635 childViewState.location = StackViewState.LOCATION_BOTTOM_STACK_HIDDEN; 636 currentYPosition = ambientState.getInnerHeight(); 637 } 638 childViewState.height = minHeight; 639 childViewState.yTranslation = currentYPosition - minHeight; 640 clampPositionToTopStackEnd(childViewState, minHeight); 641 } 642 643 private void updateStateForTopStackChild(StackScrollAlgorithmState algorithmState, 644 int numberOfElementsCompletelyIn, int i, int childHeight, 645 StackViewState childViewState, float scrollOffset) { 646 647 648 // First we calculate the index relative to the current stack window of size at most 649 // {@link #MAX_ITEMS_IN_TOP_STACK} 650 int paddedIndex = i - 1 651 - Math.max(numberOfElementsCompletelyIn - MAX_ITEMS_IN_TOP_STACK, 0); 652 if (paddedIndex >= 0) { 653 654 // We are currently visually entering the top stack 655 float distanceToStack = (childHeight + mPaddingBetweenElements) 656 - algorithmState.scrolledPixelsTop; 657 if (i == algorithmState.lastTopStackIndex 658 && distanceToStack > (mTopStackTotalSize + mPaddingBetweenElements)) { 659 660 // Child is currently translating into stack but not yet inside slow down zone. 661 // Handle it like the regular scrollview. 662 childViewState.yTranslation = scrollOffset; 663 } else { 664 // Apply stacking logic. 665 float numItemsBefore; 666 if (i == algorithmState.lastTopStackIndex) { 667 numItemsBefore = 1.0f 668 - (distanceToStack / (mTopStackTotalSize + mPaddingBetweenElements)); 669 } else { 670 numItemsBefore = algorithmState.itemsInTopStack - i; 671 } 672 // The end position of the current child 673 float currentChildEndY = mFirstChildMinHeight + mTopStackTotalSize 674 - mTopStackIndentationFunctor.getValue(numItemsBefore); 675 childViewState.yTranslation = currentChildEndY - childHeight; 676 } 677 childViewState.location = StackViewState.LOCATION_TOP_STACK_PEEKING; 678 } else { 679 if (paddedIndex == -1) { 680 childViewState.alpha = 1.0f - algorithmState.partialInTop; 681 } else { 682 // We are hidden behind the top card and faded out, so we can hide ourselves. 683 childViewState.alpha = 0.0f; 684 } 685 childViewState.yTranslation = mFirstChildMinHeight - childHeight; 686 childViewState.location = StackViewState.LOCATION_TOP_STACK_HIDDEN; 687 } 688 689 690 } 691 692 /** 693 * Find the number of items in the top stack and update the result state if needed. 694 * 695 * @param resultState The result state to update if a height change of an child occurs 696 * @param algorithmState The state in which the current pass of the algorithm is currently in 697 */ 698 private void findNumberOfItemsInTopStackAndUpdateState(StackScrollState resultState, 699 StackScrollAlgorithmState algorithmState, AmbientState ambientState) { 700 701 // The y Position if the element would be in a regular scrollView 702 float yPositionInScrollView = 0.0f; 703 int childCount = algorithmState.visibleChildren.size(); 704 705 // find the number of elements in the top stack. 706 for (int i = 0; i < childCount; i++) { 707 ExpandableView child = algorithmState.visibleChildren.get(i); 708 StackViewState childViewState = resultState.getViewStateForView(child); 709 int childHeight = getMaxAllowedChildHeight(child); 710 float yPositionInScrollViewAfterElement = yPositionInScrollView 711 + childHeight 712 + mPaddingBetweenElements; 713 if (yPositionInScrollView < algorithmState.scrollY) { 714 if (i == 0 && algorithmState.scrollY <= mFirstChildMinHeight) { 715 716 // The starting position of the bottom stack peek 717 int bottomPeekStart = ambientState.getInnerHeight() - mBottomStackPeekSize - 718 mCollapseSecondCardPadding; 719 // Collapse and expand the first child while the shade is being expanded 720 float maxHeight = mIsExpansionChanging && child == mFirstChildWhileExpanding 721 ? mFirstChildMaxHeight 722 : childHeight; 723 childViewState.height = (int) Math.max(Math.min(bottomPeekStart, maxHeight), 724 mFirstChildMinHeight); 725 algorithmState.itemsInTopStack = 1.0f; 726 727 } else if (yPositionInScrollViewAfterElement < algorithmState.scrollY) { 728 // According to the regular scroll view we are fully off screen 729 algorithmState.itemsInTopStack += 1.0f; 730 if (i == 0) { 731 childViewState.height = child.getMinHeight(); 732 } 733 } else { 734 // According to the regular scroll view we are partially off screen 735 736 // How much did we scroll into this child 737 algorithmState.scrolledPixelsTop = algorithmState.scrollY 738 - yPositionInScrollView; 739 algorithmState.partialInTop = (algorithmState.scrolledPixelsTop) / (childHeight 740 + mPaddingBetweenElements); 741 742 // Our element can be expanded, so this can get negative 743 algorithmState.partialInTop = Math.max(0.0f, algorithmState.partialInTop); 744 algorithmState.itemsInTopStack += algorithmState.partialInTop; 745 746 if (i == 0) { 747 // If it is expanded we have to collapse it to a new size 748 float newSize = yPositionInScrollViewAfterElement 749 - mPaddingBetweenElements 750 - algorithmState.scrollY + mFirstChildMinHeight; 751 newSize = Math.max(mFirstChildMinHeight, newSize); 752 algorithmState.itemsInTopStack = 1.0f; 753 childViewState.height = (int) newSize; 754 } 755 algorithmState.lastTopStackIndex = i; 756 break; 757 } 758 } else { 759 algorithmState.lastTopStackIndex = i - 1; 760 // We are already past the stack so we can end the loop 761 break; 762 } 763 yPositionInScrollView = yPositionInScrollViewAfterElement; 764 } 765 } 766 767 /** 768 * Calculate the Z positions for all children based on the number of items in both stacks and 769 * save it in the resultState 770 * 771 * @param resultState The result state to update the zTranslation values 772 * @param algorithmState The state in which the current pass of the algorithm is currently in 773 */ 774 private void updateZValuesForState(StackScrollState resultState, 775 StackScrollAlgorithmState algorithmState) { 776 int childCount = algorithmState.visibleChildren.size(); 777 for (int i = 0; i < childCount; i++) { 778 View child = algorithmState.visibleChildren.get(i); 779 StackViewState childViewState = resultState.getViewStateForView(child); 780 if (i < algorithmState.itemsInTopStack) { 781 float stackIndex = algorithmState.itemsInTopStack - i; 782 783 // Ensure that the topmost item is a little bit higher than the rest when fully 784 // scrolled, to avoid drawing errors when swiping it out 785 float max = MAX_ITEMS_IN_TOP_STACK + (i == 0 ? 2.5f : 2); 786 stackIndex = Math.min(stackIndex, max); 787 if (i == 0 && algorithmState.itemsInTopStack < 2.0f) { 788 789 // We only have the top item and an additional item in the top stack, 790 // Interpolate the index from 0 to 2 while the second item is 791 // translating in. 792 stackIndex -= 1.0f; 793 if (algorithmState.scrollY > mFirstChildMinHeight) { 794 795 // Since there is a shadow treshhold, we cant just interpolate from 0 to 796 // 2 but we interpolate from 0.1f to 2.0f when scrolled in. The jump in 797 // height will not be noticable since we have padding in between. 798 stackIndex = 0.1f + stackIndex * 1.9f; 799 } 800 } 801 childViewState.zTranslation = mZBasicHeight 802 + stackIndex * mZDistanceBetweenElements; 803 } else if (i > (childCount - 1 - algorithmState.itemsInBottomStack)) { 804 float numItemsAbove = i - (childCount - 1 - algorithmState.itemsInBottomStack); 805 float translationZ = mZBasicHeight 806 - numItemsAbove * mZDistanceBetweenElements; 807 childViewState.zTranslation = translationZ; 808 } else { 809 childViewState.zTranslation = mZBasicHeight; 810 } 811 } 812 } 813 814 public void onExpansionStarted(StackScrollState currentState) { 815 mIsExpansionChanging = true; 816 mExpandedOnStart = mIsExpanded; 817 ViewGroup hostView = currentState.getHostView(); 818 updateFirstChildHeightWhileExpanding(hostView); 819 } 820 821 private void updateFirstChildHeightWhileExpanding(ViewGroup hostView) { 822 mFirstChildWhileExpanding = (ExpandableView) findFirstVisibleChild(hostView); 823 if (mFirstChildWhileExpanding != null) { 824 if (mExpandedOnStart) { 825 826 // We are collapsing the shade, so the first child can get as most as high as the 827 // current height or the end value of the animation. 828 mFirstChildMaxHeight = StackStateAnimator.getFinalActualHeight( 829 mFirstChildWhileExpanding); 830 } else { 831 updateFirstChildMaxSizeToMaxHeight(); 832 } 833 } else { 834 mFirstChildMaxHeight = 0; 835 } 836 } 837 838 private void updateFirstChildMaxSizeToMaxHeight() { 839 // We are expanding the shade, expand it to its full height. 840 if (!isMaxSizeInitialized(mFirstChildWhileExpanding)) { 841 842 // This child was not layouted yet, wait for a layout pass 843 mFirstChildWhileExpanding 844 .addOnLayoutChangeListener(new View.OnLayoutChangeListener() { 845 @Override 846 public void onLayoutChange(View v, int left, int top, int right, 847 int bottom, int oldLeft, int oldTop, int oldRight, 848 int oldBottom) { 849 if (mFirstChildWhileExpanding != null) { 850 mFirstChildMaxHeight = getMaxAllowedChildHeight( 851 mFirstChildWhileExpanding); 852 } else { 853 mFirstChildMaxHeight = 0; 854 } 855 v.removeOnLayoutChangeListener(this); 856 } 857 }); 858 } else { 859 mFirstChildMaxHeight = getMaxAllowedChildHeight(mFirstChildWhileExpanding); 860 } 861 } 862 863 private boolean isMaxSizeInitialized(ExpandableView child) { 864 if (child instanceof ExpandableNotificationRow) { 865 ExpandableNotificationRow row = (ExpandableNotificationRow) child; 866 return row.isMaxExpandHeightInitialized(); 867 } 868 return child == null || child.getWidth() != 0; 869 } 870 871 private View findFirstVisibleChild(ViewGroup container) { 872 int childCount = container.getChildCount(); 873 for (int i = 0; i < childCount; i++) { 874 View child = container.getChildAt(i); 875 if (child.getVisibility() != View.GONE) { 876 return child; 877 } 878 } 879 return null; 880 } 881 882 public void onExpansionStopped() { 883 mIsExpansionChanging = false; 884 mFirstChildWhileExpanding = null; 885 } 886 887 public void setIsExpanded(boolean isExpanded) { 888 this.mIsExpanded = isExpanded; 889 } 890 891 public void notifyChildrenChanged(final NotificationStackScrollLayout hostView) { 892 mFirstChild = hostView.getFirstChildNotGone(); 893 if (mIsExpansionChanging) { 894 hostView.post(new Runnable() { 895 @Override 896 public void run() { 897 updateFirstChildHeightWhileExpanding(hostView); 898 } 899 }); 900 } 901 } 902 903 public void setDimmed(boolean dimmed) { 904 mDimmed = dimmed; 905 updatePadding(); 906 } 907 908 public void onReset(ExpandableView view) { 909 if (view.equals(mFirstChildWhileExpanding)) { 910 updateFirstChildMaxSizeToMaxHeight(); 911 } 912 } 913 914 class StackScrollAlgorithmState { 915 916 /** 917 * The scroll position of the algorithm 918 */ 919 public int scrollY; 920 921 /** 922 * The quantity of items which are in the top stack. 923 */ 924 public float itemsInTopStack; 925 926 /** 927 * how far in is the element currently transitioning into the top stack 928 */ 929 public float partialInTop; 930 931 /** 932 * The number of pixels the last child in the top stack has scrolled in to the stack 933 */ 934 public float scrolledPixelsTop; 935 936 /** 937 * The last item index which is in the top stack. 938 */ 939 public int lastTopStackIndex; 940 941 /** 942 * The quantity of items which are in the bottom stack. 943 */ 944 public float itemsInBottomStack; 945 946 /** 947 * how far in is the element currently transitioning into the bottom stack 948 */ 949 public float partialInBottom; 950 951 /** 952 * The children from the host view which are not gone. 953 */ 954 public final ArrayList<ExpandableView> visibleChildren = new ArrayList<ExpandableView>(); 955 } 956 957} 958