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