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