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