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