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