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