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