blob: 2a49a4c583a22ac88fe07a15ed53266e13d95191 [file] [log] [blame]
Selim Cinek67b22602014-03-10 15:40:16 +01001/*
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
17package com.android.systemui.statusbar.stack;
18
19import android.content.Context;
Jorim Jaggid7c1fae2014-08-13 18:27:47 +020020import android.util.DisplayMetrics;
Christoph Studer6e3eceb2014-04-01 18:40:27 +020021import android.util.Log;
Selim Cinek67b22602014-03-10 15:40:16 +010022import android.view.View;
23import android.view.ViewGroup;
Selim Cinek343e6e22014-04-11 21:23:30 +020024
Selim Cinek67b22602014-03-10 15:40:16 +010025import com.android.systemui.R;
Selim Cinek1685e632014-04-08 02:27:49 +020026import com.android.systemui.statusbar.ExpandableNotificationRow;
Jorim Jaggibe565df2014-04-28 17:51:23 +020027import com.android.systemui.statusbar.ExpandableView;
Selim Cinekb8f09cf2015-03-16 17:09:28 -070028import com.android.systemui.statusbar.policy.HeadsUpManager;
Selim Cinek67b22602014-03-10 15:40:16 +010029
Jorim Jaggid4a57442014-04-10 02:45:55 +020030import java.util.ArrayList;
Selim Cinekb5605e52015-02-20 18:21:41 +010031import java.util.List;
Jorim Jaggid4a57442014-04-10 02:45:55 +020032
Selim Cinek67b22602014-03-10 15:40:16 +010033/**
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 */
38public class StackScrollAlgorithm {
39
Christoph Studer6e3eceb2014-04-01 18:40:27 +020040 private static final String LOG_TAG = "StackScrollAlgorithm";
41
Selim Cinek67b22602014-03-10 15:40:16 +010042 private static final int MAX_ITEMS_IN_BOTTOM_STACK = 3;
43 private static final int MAX_ITEMS_IN_TOP_STACK = 3;
44
Jorim Jaggi362dd6d2014-07-09 19:04:07 +020045 public static final float DIMMED_SCALE = 0.95f;
Jorim Jaggid552d9d2014-05-07 19:41:13 +020046
Selim Cinek67b22602014-03-10 15:40:16 +010047 private int mPaddingBetweenElements;
48 private int mCollapsedSize;
49 private int mTopStackPeekSize;
50 private int mBottomStackPeekSize;
51 private int mZDistanceBetweenElements;
52 private int mZBasicHeight;
Selim Cinek708a6c12014-05-28 14:16:02 +020053 private int mRoundedRectCornerRadius;
Selim Cinek67b22602014-03-10 15:40:16 +010054
55 private StackIndentationFunctor mTopStackIndentationFunctor;
56 private StackIndentationFunctor mBottomStackIndentationFunctor;
57
Selim Cinek67b22602014-03-10 15:40:16 +010058 private StackScrollAlgorithmState mTempAlgorithmState = new StackScrollAlgorithmState();
Selim Cinek1685e632014-04-08 02:27:49 +020059 private boolean mIsExpansionChanging;
60 private int mFirstChildMaxHeight;
61 private boolean mIsExpanded;
Jorim Jaggibe565df2014-04-28 17:51:23 +020062 private ExpandableView mFirstChildWhileExpanding;
Selim Cinek1685e632014-04-08 02:27:49 +020063 private boolean mExpandedOnStart;
Selim Cinek343e6e22014-04-11 21:23:30 +020064 private int mTopStackTotalSize;
Selim Cinek34c0a8d2014-05-12 00:01:43 +020065 private int mPaddingBetweenElementsDimmed;
66 private int mPaddingBetweenElementsNormal;
67 private int mBottomStackSlowDownLength;
Selim Cinekad3e5af2014-07-04 12:24:11 +020068 private int mTopStackSlowDownLength;
Selim Cinekd83771e2014-07-04 16:45:31 +020069 private int mCollapseSecondCardPadding;
Selim Cinek3afd00e2014-08-11 22:32:57 +020070 private boolean mIsSmallScreen;
71 private int mMaxNotificationHeight;
Jorim Jaggid7c1fae2014-08-13 18:27:47 +020072 private boolean mScaleDimmed;
Selim Cinekd2281152015-04-10 14:37:46 -070073 private HeadsUpManager mHeadsUpManager;
Selim Cinek67b22602014-03-10 15:40:16 +010074
75 public StackScrollAlgorithm(Context context) {
76 initConstants(context);
Selim Cinek34c0a8d2014-05-12 00:01:43 +020077 updatePadding(false);
78 }
79
80 private void updatePadding(boolean dimmed) {
Jorim Jaggid7c1fae2014-08-13 18:27:47 +020081 mPaddingBetweenElements = dimmed && mScaleDimmed
Selim Cinek34c0a8d2014-05-12 00:01:43 +020082 ? mPaddingBetweenElementsDimmed
83 : mPaddingBetweenElementsNormal;
Selim Cinekad3e5af2014-07-04 12:24:11 +020084 mTopStackTotalSize = mTopStackSlowDownLength + mPaddingBetweenElements
85 + mTopStackPeekSize;
Selim Cinek34c0a8d2014-05-12 00:01:43 +020086 mTopStackIndentationFunctor = new PiecewiseLinearIndentationFunctor(
87 MAX_ITEMS_IN_TOP_STACK,
88 mTopStackPeekSize,
Selim Cinekb96924d2014-05-12 15:11:25 +020089 mTopStackTotalSize - mTopStackPeekSize,
Selim Cinek34c0a8d2014-05-12 00:01:43 +020090 0.5f);
91 mBottomStackIndentationFunctor = new PiecewiseLinearIndentationFunctor(
92 MAX_ITEMS_IN_BOTTOM_STACK,
93 mBottomStackPeekSize,
94 getBottomStackSlowDownLength(),
95 0.5f);
96 }
97
98 public int getBottomStackSlowDownLength() {
99 return mBottomStackSlowDownLength + mPaddingBetweenElements;
Selim Cinek67b22602014-03-10 15:40:16 +0100100 }
101
102 private void initConstants(Context context) {
Selim Cinek34c0a8d2014-05-12 00:01:43 +0200103 mPaddingBetweenElementsDimmed = context.getResources()
104 .getDimensionPixelSize(R.dimen.notification_padding_dimmed);
105 mPaddingBetweenElementsNormal = context.getResources()
Jorim Jaggife40f7d2014-04-28 15:20:04 +0200106 .getDimensionPixelSize(R.dimen.notification_padding);
Selim Cinek67b22602014-03-10 15:40:16 +0100107 mCollapsedSize = context.getResources()
Jorim Jaggife40f7d2014-04-28 15:20:04 +0200108 .getDimensionPixelSize(R.dimen.notification_min_height);
Selim Cinek3afd00e2014-08-11 22:32:57 +0200109 mMaxNotificationHeight = context.getResources()
110 .getDimensionPixelSize(R.dimen.notification_max_height);
Selim Cinek67b22602014-03-10 15:40:16 +0100111 mTopStackPeekSize = context.getResources()
112 .getDimensionPixelSize(R.dimen.top_stack_peek_amount);
113 mBottomStackPeekSize = context.getResources()
114 .getDimensionPixelSize(R.dimen.bottom_stack_peek_amount);
115 mZDistanceBetweenElements = context.getResources()
116 .getDimensionPixelSize(R.dimen.z_distance_between_notifications);
117 mZBasicHeight = (MAX_ITEMS_IN_BOTTOM_STACK + 1) * mZDistanceBetweenElements;
Selim Cinek34c0a8d2014-05-12 00:01:43 +0200118 mBottomStackSlowDownLength = context.getResources()
119 .getDimensionPixelSize(R.dimen.bottom_stack_slow_down_length);
Selim Cinekad3e5af2014-07-04 12:24:11 +0200120 mTopStackSlowDownLength = context.getResources()
121 .getDimensionPixelSize(R.dimen.top_stack_slow_down_length);
Selim Cinek708a6c12014-05-28 14:16:02 +0200122 mRoundedRectCornerRadius = context.getResources().getDimensionPixelSize(
Selim Cinek697178b2014-07-02 19:40:30 +0200123 R.dimen.notification_material_rounded_rect_radius);
Selim Cinekd83771e2014-07-04 16:45:31 +0200124 mCollapseSecondCardPadding = context.getResources().getDimensionPixelSize(
125 R.dimen.notification_collapse_second_card_padding);
Jorim Jaggid7c1fae2014-08-13 18:27:47 +0200126 mScaleDimmed = context.getResources().getDisplayMetrics().densityDpi
127 >= DisplayMetrics.DENSITY_XXHIGH;
Selim Cinek67b22602014-03-10 15:40:16 +0100128 }
129
Jorim Jaggid7c1fae2014-08-13 18:27:47 +0200130 public boolean shouldScaleDimmed() {
131 return mScaleDimmed;
132 }
Selim Cinek67b22602014-03-10 15:40:16 +0100133
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200134 public void getStackScrollState(AmbientState ambientState, StackScrollState resultState) {
Selim Cinek67b22602014-03-10 15:40:16 +0100135 // The state of the local variables are saved in an algorithmState to easily subdivide it
136 // into multiple phases.
137 StackScrollAlgorithmState algorithmState = mTempAlgorithmState;
138
139 // First we reset the view states to their default values.
140 resultState.resetViewStates();
141
Selim Cinek343e6e22014-04-11 21:23:30 +0200142 algorithmState.itemsInTopStack = 0.0f;
Selim Cinek67b22602014-03-10 15:40:16 +0100143 algorithmState.partialInTop = 0.0f;
144 algorithmState.lastTopStackIndex = 0;
Selim Cinek343e6e22014-04-11 21:23:30 +0200145 algorithmState.scrolledPixelsTop = 0;
Selim Cinek67b22602014-03-10 15:40:16 +0100146 algorithmState.itemsInBottomStack = 0.0f;
Selim Cinek343e6e22014-04-11 21:23:30 +0200147 algorithmState.partialInBottom = 0.0f;
Selim Cinek8d9ff9c2014-05-12 15:13:04 +0200148 float bottomOverScroll = ambientState.getOverScrollAmount(false /* onTop */);
Selim Cinek1408eb52014-06-02 14:45:38 +0200149
150 int scrollY = ambientState.getScrollY();
151
152 // Due to the overScroller, the stackscroller can have negative scroll state. This is
153 // already accounted for by the top padding and doesn't need an additional adaption
154 scrollY = Math.max(0, scrollY);
155 algorithmState.scrollY = (int) (scrollY + mCollapsedSize + bottomOverScroll);
Selim Cinek343e6e22014-04-11 21:23:30 +0200156
Selim Cineka4baaa32015-04-20 14:27:54 -0700157 updateVisibleChildren(resultState, algorithmState);
Selim Cinek67b22602014-03-10 15:40:16 +0100158
159 // Phase 1:
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700160 findNumberOfItemsInTopStackAndUpdateState(resultState, algorithmState, ambientState);
Selim Cinek67b22602014-03-10 15:40:16 +0100161
162 // Phase 2:
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700163 updatePositionsForState(resultState, algorithmState, ambientState);
Selim Cinek67b22602014-03-10 15:40:16 +0100164
165 // Phase 3:
166 updateZValuesForState(resultState, algorithmState);
Selim Cinekeb973562014-05-02 17:07:49 +0200167
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200168 handleDraggedViews(ambientState, resultState, algorithmState);
Jorim Jaggiae441282014-08-01 02:45:18 +0200169 updateDimmedActivatedHideSensitive(ambientState, resultState, algorithmState);
Selim Cineka59ecc32015-04-07 10:51:49 -0700170 updateClipping(resultState, algorithmState, ambientState);
Selim Cinek3d2b94bf2014-07-02 22:12:47 +0200171 updateSpeedBumpState(resultState, algorithmState, ambientState.getSpeedBumpIndex());
Selim Cinekb5605e52015-02-20 18:21:41 +0100172 getNotificationChildrenStates(resultState, algorithmState);
173 }
174
175 private void getNotificationChildrenStates(StackScrollState resultState,
176 StackScrollAlgorithmState algorithmState) {
177 int childCount = algorithmState.visibleChildren.size();
178 for (int i = 0; i < childCount; i++) {
179 ExpandableView v = algorithmState.visibleChildren.get(i);
180 if (v instanceof ExpandableNotificationRow) {
181 ExpandableNotificationRow row = (ExpandableNotificationRow) v;
182 row.getChildrenStates(resultState);
183 }
184 }
Selim Cinek3d2b94bf2014-07-02 22:12:47 +0200185 }
186
187 private void updateSpeedBumpState(StackScrollState resultState,
188 StackScrollAlgorithmState algorithmState, int speedBumpIndex) {
189 int childCount = algorithmState.visibleChildren.size();
190 for (int i = 0; i < childCount; i++) {
191 View child = algorithmState.visibleChildren.get(i);
Selim Cinekb036ca42015-02-20 15:56:28 +0100192 StackViewState childViewState = resultState.getViewStateForView(child);
Selim Cinek3107cfa2014-07-22 15:24:29 +0200193
194 // The speed bump can also be gone, so equality needs to be taken when comparing
195 // indices.
196 childViewState.belowSpeedBump = speedBumpIndex != -1 && i >= speedBumpIndex;
Selim Cinek3d2b94bf2014-07-02 22:12:47 +0200197 }
Selim Cinekf54090e2014-06-17 17:24:51 -0700198 }
199
Selim Cinek708a6c12014-05-28 14:16:02 +0200200 private void updateClipping(StackScrollState resultState,
Selim Cineka59ecc32015-04-07 10:51:49 -0700201 StackScrollAlgorithmState algorithmState, AmbientState ambientState) {
Selim Cinek708a6c12014-05-28 14:16:02 +0200202 float previousNotificationEnd = 0;
203 float previousNotificationStart = 0;
204 boolean previousNotificationIsSwiped = false;
205 int childCount = algorithmState.visibleChildren.size();
206 for (int i = 0; i < childCount; i++) {
207 ExpandableView child = algorithmState.visibleChildren.get(i);
Selim Cinekb036ca42015-02-20 15:56:28 +0100208 StackViewState state = resultState.getViewStateForView(child);
Jorim Jaggi2e34ec32014-07-01 03:17:05 +0200209 float newYTranslation = state.yTranslation + state.height * (1f - state.scale) / 2f;
210 float newHeight = state.height * state.scale;
Selim Cinek708a6c12014-05-28 14:16:02 +0200211 // apply clipping and shadow
212 float newNotificationEnd = newYTranslation + newHeight;
213
Selim Cinekc5baa3e2014-10-29 19:04:19 +0100214 float clipHeight;
215 if (previousNotificationIsSwiped) {
216 // When the previous notification is swiped, we don't clip the content to the
217 // bottom of it.
218 clipHeight = newHeight;
219 } else {
220 clipHeight = newNotificationEnd - previousNotificationEnd;
221 clipHeight = Math.max(0.0f, clipHeight);
222 if (clipHeight != 0.0f) {
Selim Cinek708a6c12014-05-28 14:16:02 +0200223
Selim Cinekc5baa3e2014-10-29 19:04:19 +0100224 // In the unlocked shade we have to clip a little bit higher because of the rounded
225 // corners of the notifications, but only if we are not fully overlapped by
226 // the top card.
227 float clippingCorrection = state.dimmed
228 ? 0
229 : mRoundedRectCornerRadius * state.scale;
230 clipHeight += clippingCorrection;
231 }
232 }
Selim Cinek708a6c12014-05-28 14:16:02 +0200233
234 updateChildClippingAndBackground(state, newHeight, clipHeight,
Jorim Jaggi2e34ec32014-07-01 03:17:05 +0200235 newHeight - (previousNotificationStart - newYTranslation));
Selim Cinek708a6c12014-05-28 14:16:02 +0200236
237 if (!child.isTransparent()) {
238 // Only update the previous values if we are not transparent,
239 // otherwise we would clip to a transparent view.
Jorim Jaggi2e34ec32014-07-01 03:17:05 +0200240 previousNotificationStart = newYTranslation + state.clipTopAmount * state.scale;
Selim Cinek708a6c12014-05-28 14:16:02 +0200241 previousNotificationEnd = newNotificationEnd;
Selim Cineka59ecc32015-04-07 10:51:49 -0700242 previousNotificationIsSwiped = ambientState.getDraggedViews().contains(child);
Selim Cinek708a6c12014-05-28 14:16:02 +0200243 }
244 }
245 }
246
247 /**
248 * Updates the shadow outline and the clipping for a view.
249 *
250 * @param state the viewState to update
251 * @param realHeight the currently applied height of the view
252 * @param clipHeight the desired clip height, the rest of the view will be clipped from the top
253 * @param backgroundHeight the desired background height. The shadows of the view will be
254 * based on this height and the content will be clipped from the top
255 */
Selim Cinekb036ca42015-02-20 15:56:28 +0100256 private void updateChildClippingAndBackground(StackViewState state, float realHeight,
257 float clipHeight, float backgroundHeight) {
Selim Cinek708a6c12014-05-28 14:16:02 +0200258 if (realHeight > clipHeight) {
Jorim Jaggi2e34ec32014-07-01 03:17:05 +0200259 // Rather overlap than create a hole.
260 state.topOverLap = (int) Math.floor((realHeight - clipHeight) / state.scale);
Selim Cinek708a6c12014-05-28 14:16:02 +0200261 } else {
262 state.topOverLap = 0;
263 }
264 if (realHeight > backgroundHeight) {
Jorim Jaggi2e34ec32014-07-01 03:17:05 +0200265 // Rather overlap than create a hole.
266 state.clipTopAmount = (int) Math.floor((realHeight - backgroundHeight) / state.scale);
Selim Cinek708a6c12014-05-28 14:16:02 +0200267 } else {
268 state.clipTopAmount = 0;
269 }
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200270 }
271
272 /**
Jorim Jaggiae441282014-08-01 02:45:18 +0200273 * Updates the dimmed, activated and hiding sensitive states of the children.
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200274 */
Jorim Jaggiae441282014-08-01 02:45:18 +0200275 private void updateDimmedActivatedHideSensitive(AmbientState ambientState,
276 StackScrollState resultState, StackScrollAlgorithmState algorithmState) {
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200277 boolean dimmed = ambientState.isDimmed();
John Spurlockbf370992014-06-17 13:58:31 -0400278 boolean dark = ambientState.isDark();
Jorim Jaggiae441282014-08-01 02:45:18 +0200279 boolean hideSensitive = ambientState.isHideSensitive();
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200280 View activatedChild = ambientState.getActivatedChild();
281 int childCount = algorithmState.visibleChildren.size();
282 for (int i = 0; i < childCount; i++) {
283 View child = algorithmState.visibleChildren.get(i);
Selim Cinekb036ca42015-02-20 15:56:28 +0100284 StackViewState childViewState = resultState.getViewStateForView(child);
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200285 childViewState.dimmed = dimmed;
John Spurlockbf370992014-06-17 13:58:31 -0400286 childViewState.dark = dark;
Jorim Jaggiae441282014-08-01 02:45:18 +0200287 childViewState.hideSensitive = hideSensitive;
Selim Cinekb89de4e2014-06-10 10:47:05 +0200288 boolean isActivatedChild = activatedChild == child;
Jorim Jaggid7c1fae2014-08-13 18:27:47 +0200289 childViewState.scale = !mScaleDimmed || !dimmed || isActivatedChild
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200290 ? 1.0f
291 : DIMMED_SCALE;
Jorim Jaggi4538cee2014-09-09 15:21:38 +0200292 if (dimmed && isActivatedChild) {
293 childViewState.zTranslation += 2.0f * mZDistanceBetweenElements;
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200294 }
295 }
Selim Cinekeb973562014-05-02 17:07:49 +0200296 }
297
298 /**
299 * Handle the special state when views are being dragged
300 */
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200301 private void handleDraggedViews(AmbientState ambientState, StackScrollState resultState,
Selim Cinekeb973562014-05-02 17:07:49 +0200302 StackScrollAlgorithmState algorithmState) {
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200303 ArrayList<View> draggedViews = ambientState.getDraggedViews();
304 for (View draggedView : draggedViews) {
Selim Cinekeb973562014-05-02 17:07:49 +0200305 int childIndex = algorithmState.visibleChildren.indexOf(draggedView);
306 if (childIndex >= 0 && childIndex < algorithmState.visibleChildren.size() - 1) {
307 View nextChild = algorithmState.visibleChildren.get(childIndex + 1);
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200308 if (!draggedViews.contains(nextChild)) {
Selim Cinekeb973562014-05-02 17:07:49 +0200309 // only if the view is not dragged itself we modify its state to be fully
310 // visible
Selim Cinekb036ca42015-02-20 15:56:28 +0100311 StackViewState viewState = resultState.getViewStateForView(
Selim Cinekeb973562014-05-02 17:07:49 +0200312 nextChild);
313 // The child below the dragged one must be fully visible
Selim Cineka59ecc32015-04-07 10:51:49 -0700314 if (!isPinnedHeadsUpView(draggedView) || isPinnedHeadsUpView(nextChild)) {
315 viewState.alpha = 1;
316 }
Selim Cinekeb973562014-05-02 17:07:49 +0200317 }
318
319 // Lets set the alpha to the one it currently has, as its currently being dragged
Selim Cinekb036ca42015-02-20 15:56:28 +0100320 StackViewState viewState = resultState.getViewStateForView(draggedView);
Selim Cinekeb973562014-05-02 17:07:49 +0200321 // The dragged child should keep the set alpha
322 viewState.alpha = draggedView.getAlpha();
323 }
324 }
Selim Cinek343e6e22014-04-11 21:23:30 +0200325 }
Selim Cinek67b22602014-03-10 15:40:16 +0100326
Selim Cineka59ecc32015-04-07 10:51:49 -0700327 private boolean isPinnedHeadsUpView(View view) {
328 if (view instanceof ExpandableNotificationRow) {
329 ExpandableNotificationRow row = (ExpandableNotificationRow) view;
330 return row.isHeadsUp() && !row.isInShade();
331 }
332 return false;
333 }
334
Selim Cinek343e6e22014-04-11 21:23:30 +0200335 /**
Jorim Jaggid4a57442014-04-10 02:45:55 +0200336 * Update the visible children on the state.
337 */
338 private void updateVisibleChildren(StackScrollState resultState,
Selim Cineka4baaa32015-04-20 14:27:54 -0700339 StackScrollAlgorithmState state) {
Jorim Jaggid4a57442014-04-10 02:45:55 +0200340 ViewGroup hostView = resultState.getHostView();
341 int childCount = hostView.getChildCount();
342 state.visibleChildren.clear();
343 state.visibleChildren.ensureCapacity(childCount);
Selim Cinekb036ca42015-02-20 15:56:28 +0100344 int notGoneIndex = 0;
Jorim Jaggid4a57442014-04-10 02:45:55 +0200345 for (int i = 0; i < childCount; i++) {
Jorim Jaggibe565df2014-04-28 17:51:23 +0200346 ExpandableView v = (ExpandableView) hostView.getChildAt(i);
Jorim Jaggid4a57442014-04-10 02:45:55 +0200347 if (v.getVisibility() != View.GONE) {
Selim Cineka4baaa32015-04-20 14:27:54 -0700348 notGoneIndex = updateNotGoneIndex(resultState, state, notGoneIndex, v);
Selim Cinekb5605e52015-02-20 18:21:41 +0100349 if (v instanceof ExpandableNotificationRow) {
350 ExpandableNotificationRow row = (ExpandableNotificationRow) v;
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700351
352 // handle the notgoneIndex for the children as well
Selim Cinekb5605e52015-02-20 18:21:41 +0100353 List<ExpandableNotificationRow> children =
354 row.getNotificationChildren();
355 if (row.areChildrenExpanded() && children != null) {
356 for (ExpandableNotificationRow childRow : children) {
357 if (childRow.getVisibility() != View.GONE) {
358 StackViewState childState
359 = resultState.getViewStateForView(childRow);
360 childState.notGoneIndex = notGoneIndex;
361 notGoneIndex++;
362 }
363 }
364 }
365 }
Jorim Jaggid4a57442014-04-10 02:45:55 +0200366 }
367 }
368 }
369
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700370 private int updateNotGoneIndex(StackScrollState resultState,
371 StackScrollAlgorithmState state, int notGoneIndex,
372 ExpandableView v) {
373 StackViewState viewState = resultState.getViewStateForView(v);
374 viewState.notGoneIndex = notGoneIndex;
375 state.visibleChildren.add(v);
376 notGoneIndex++;
377 return notGoneIndex;
378 }
379
Jorim Jaggid4a57442014-04-10 02:45:55 +0200380 /**
Selim Cinek67b22602014-03-10 15:40:16 +0100381 * Determine the positions for the views. This is the main part of the algorithm.
382 *
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700383 * @param resultState The result state to update if a change to the properties of a child occurs
Selim Cinek67b22602014-03-10 15:40:16 +0100384 * @param algorithmState The state in which the current pass of the algorithm is currently in
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700385 * @param ambientState The current ambient state
Selim Cinek67b22602014-03-10 15:40:16 +0100386 */
387 private void updatePositionsForState(StackScrollState resultState,
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700388 StackScrollAlgorithmState algorithmState, AmbientState ambientState) {
Selim Cinek67b22602014-03-10 15:40:16 +0100389
Selim Cinek1685e632014-04-08 02:27:49 +0200390 // The starting position of the bottom stack peek
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700391 float bottomPeekStart = ambientState.getInnerHeight() - mBottomStackPeekSize;
Selim Cinek1685e632014-04-08 02:27:49 +0200392
Selim Cinek67b22602014-03-10 15:40:16 +0100393 // The position where the bottom stack starts.
Selim Cinek34c0a8d2014-05-12 00:01:43 +0200394 float bottomStackStart = bottomPeekStart - mBottomStackSlowDownLength;
Selim Cinek67b22602014-03-10 15:40:16 +0100395
396 // The y coordinate of the current child.
397 float currentYPosition = 0.0f;
398
399 // How far in is the element currently transitioning into the bottom stack.
400 float yPositionInScrollView = 0.0f;
401
Selim Cineka59ecc32015-04-07 10:51:49 -0700402 // If we have a heads-up higher than the collapsed height we need to add the difference to
403 // the padding of all other elements, i.e push in the top stack slightly.
404 ExpandableNotificationRow topHeadsUpEntry = ambientState.getTopHeadsUpEntry();
405
Jorim Jaggid4a57442014-04-10 02:45:55 +0200406 int childCount = algorithmState.visibleChildren.size();
Selim Cinek67b22602014-03-10 15:40:16 +0100407 int numberOfElementsCompletelyIn = (int) algorithmState.itemsInTopStack;
408 for (int i = 0; i < childCount; i++) {
Jorim Jaggibe565df2014-04-28 17:51:23 +0200409 ExpandableView child = algorithmState.visibleChildren.get(i);
Selim Cinekb036ca42015-02-20 15:56:28 +0100410 StackViewState childViewState = resultState.getViewStateForView(child);
411 childViewState.location = StackViewState.LOCATION_UNKNOWN;
Selim Cineka59ecc32015-04-07 10:51:49 -0700412 int childHeight = getMaxAllowedChildHeight(child, ambientState);
Selim Cinek67b22602014-03-10 15:40:16 +0100413 float yPositionInScrollViewAfterElement = yPositionInScrollView
414 + childHeight
415 + mPaddingBetweenElements;
Selim Cinek343e6e22014-04-11 21:23:30 +0200416 float scrollOffset = yPositionInScrollView - algorithmState.scrollY + mCollapsedSize;
417
418 if (i == algorithmState.lastTopStackIndex + 1) {
419 // Normally the position of this child is the position in the regular scrollview,
420 // but if the two stacks are very close to each other,
421 // then have have to push it even more upwards to the position of the bottom
422 // stack start.
423 currentYPosition = Math.min(scrollOffset, bottomStackStart);
424 }
425 childViewState.yTranslation = currentYPosition;
426
427 // The y position after this element
428 float nextYPosition = currentYPosition + childHeight +
429 mPaddingBetweenElements;
430
431 if (i <= algorithmState.lastTopStackIndex) {
Selim Cinek67b22602014-03-10 15:40:16 +0100432 // Case 1:
433 // We are in the top Stack
Selim Cinek343e6e22014-04-11 21:23:30 +0200434 updateStateForTopStackChild(algorithmState,
435 numberOfElementsCompletelyIn, i, childHeight, childViewState, scrollOffset);
Selim Cinek3afd00e2014-08-11 22:32:57 +0200436 clampPositionToTopStackEnd(childViewState, childHeight);
437
Selim Cinek343e6e22014-04-11 21:23:30 +0200438 // check if we are overlapping with the bottom stack
439 if (childViewState.yTranslation + childHeight + mPaddingBetweenElements
Selim Cinek4581cf82014-08-12 12:40:32 +0200440 >= bottomStackStart && !mIsExpansionChanging && i != 0 && mIsSmallScreen) {
Selim Cinek3afd00e2014-08-11 22:32:57 +0200441 // we just collapse this element slightly
442 int newSize = (int) Math.max(bottomStackStart - mPaddingBetweenElements -
443 childViewState.yTranslation, mCollapsedSize);
444 childViewState.height = newSize;
Selim Cinek4581cf82014-08-12 12:40:32 +0200445 updateStateForChildTransitioningInBottom(algorithmState, bottomStackStart,
446 bottomPeekStart, childViewState.yTranslation, childViewState,
447 childHeight);
Selim Cinek343e6e22014-04-11 21:23:30 +0200448 }
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700449 clampPositionToBottomStackStart(childViewState, childViewState.height,
450 ambientState);
Selim Cinek343e6e22014-04-11 21:23:30 +0200451 } else if (nextYPosition >= bottomStackStart) {
Selim Cinek67b22602014-03-10 15:40:16 +0100452 // Case 2:
Selim Cinek343e6e22014-04-11 21:23:30 +0200453 // We are in the bottom stack.
454 if (currentYPosition >= bottomStackStart) {
Selim Cinek67b22602014-03-10 15:40:16 +0100455 // According to the regular scroll view we are fully translated out of the
456 // bottom of the screen so we are fully in the bottom stack
Selim Cinek343e6e22014-04-11 21:23:30 +0200457 updateStateForChildFullyInBottomStack(algorithmState,
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700458 bottomStackStart, childViewState, childHeight, ambientState);
Selim Cinek67b22602014-03-10 15:40:16 +0100459 } else {
Selim Cinek67b22602014-03-10 15:40:16 +0100460 // According to the regular scroll view we are currently translating out of /
461 // into the bottom of the screen
Selim Cinek343e6e22014-04-11 21:23:30 +0200462 updateStateForChildTransitioningInBottom(algorithmState,
463 bottomStackStart, bottomPeekStart, currentYPosition,
464 childViewState, childHeight);
Selim Cinek67b22602014-03-10 15:40:16 +0100465 }
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200466 } else {
Selim Cinek343e6e22014-04-11 21:23:30 +0200467 // Case 3:
468 // We are in the regular scroll area.
Selim Cinekb036ca42015-02-20 15:56:28 +0100469 childViewState.location = StackViewState.LOCATION_MAIN_AREA;
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700470 clampYTranslation(childViewState, childHeight, ambientState);
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200471 }
Selim Cinek343e6e22014-04-11 21:23:30 +0200472
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200473 // The first card is always rendered.
474 if (i == 0) {
475 childViewState.alpha = 1.0f;
Selim Cinek8d9ff9c2014-05-12 15:13:04 +0200476 childViewState.yTranslation = Math.max(mCollapsedSize - algorithmState.scrollY, 0);
Selim Cinekd83771e2014-07-04 16:45:31 +0200477 if (childViewState.yTranslation + childViewState.height
478 > bottomPeekStart - mCollapseSecondCardPadding) {
Selim Cinek4fe3e472014-07-03 16:32:54 +0200479 childViewState.height = (int) Math.max(
Selim Cinekd83771e2014-07-04 16:45:31 +0200480 bottomPeekStart - mCollapseSecondCardPadding
481 - childViewState.yTranslation, mCollapsedSize);
Selim Cinek4fe3e472014-07-03 16:32:54 +0200482 }
Selim Cinekb036ca42015-02-20 15:56:28 +0100483 childViewState.location = StackViewState.LOCATION_FIRST_CARD;
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200484 }
Selim Cinekb036ca42015-02-20 15:56:28 +0100485 if (childViewState.location == StackViewState.LOCATION_UNKNOWN) {
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200486 Log.wtf(LOG_TAG, "Failed to assign location for child " + i);
Selim Cinek67b22602014-03-10 15:40:16 +0100487 }
Selim Cinek343e6e22014-04-11 21:23:30 +0200488 currentYPosition = childViewState.yTranslation + childHeight + mPaddingBetweenElements;
Selim Cinek67b22602014-03-10 15:40:16 +0100489 yPositionInScrollView = yPositionInScrollViewAfterElement;
Jorim Jaggi8c1a44b2014-04-29 19:04:02 +0200490
Selim Cineka59ecc32015-04-07 10:51:49 -0700491 if (ambientState.isShadeExpanded() && topHeadsUpEntry != null
492 && child != topHeadsUpEntry) {
493 childViewState.yTranslation += topHeadsUpEntry.getHeadsUpHeight() - mCollapsedSize;
494 }
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700495 childViewState.yTranslation += ambientState.getTopPadding()
Selim Cineka59ecc32015-04-07 10:51:49 -0700496 + ambientState.getStackTranslation();
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700497 }
Selim Cineka4baaa32015-04-20 14:27:54 -0700498 updateHeadsUpStates(resultState, algorithmState, ambientState);
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700499 }
500
Selim Cineka4baaa32015-04-20 14:27:54 -0700501 private void updateHeadsUpStates(StackScrollState resultState,
502 StackScrollAlgorithmState algorithmState, AmbientState ambientState) {
503 int childCount = algorithmState.visibleChildren.size();
504 ExpandableNotificationRow topHeadsUpEntry = null;
505 for (int i = 0; i < childCount; i++) {
506 View child = algorithmState.visibleChildren.get(i);
507 if (!(child instanceof ExpandableNotificationRow)) {
508 break;
509 }
510 ExpandableNotificationRow row = (ExpandableNotificationRow) child;
511 if (!row.isHeadsUp()) {
512 break;
513 } else if (topHeadsUpEntry == null) {
514 topHeadsUpEntry = row;
515 }
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700516 StackViewState childState = resultState.getViewStateForView(row);
Selim Cinek1f3f5442015-04-10 17:54:46 -0700517 boolean isTopEntry = topHeadsUpEntry == row;
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700518 if (!row.isInShade()) {
Selim Cineka59ecc32015-04-07 10:51:49 -0700519 childState.yTranslation = 0;
Selim Cinek1f3f5442015-04-10 17:54:46 -0700520 childState.height = row.getHeadsUpHeight();
521 if (!isTopEntry) {
522 // Ensure that a headsUp is never below the topmost headsUp
523 StackViewState topState = resultState.getViewStateForView(topHeadsUpEntry);
524 childState.height = row.getHeadsUpHeight();
Selim Cineke53e6bb2015-04-13 16:14:26 -0700525 childState.yTranslation = topState.yTranslation + topState.height
526 - childState.height;
Selim Cinek1f3f5442015-04-10 17:54:46 -0700527 }
528 } else if (mIsExpanded) {
529 if (isTopEntry) {
530 childState.height += row.getHeadsUpHeight() - mCollapsedSize;
531 }
532 childState.height = Math.max(childState.height, row.getHeadsUpHeight());
533 // Ensure that the heads up is always visible even when scrolled of from the bottom
534 float bottomPosition = ambientState.getMaxHeadsUpTranslation() - childState.height;
535 childState.yTranslation = Math.min(childState.yTranslation,
536 bottomPosition);
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700537 }
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700538 }
Selim Cinek67b22602014-03-10 15:40:16 +0100539 }
540
Selim Cinek1685e632014-04-08 02:27:49 +0200541 /**
Selim Cinek343e6e22014-04-11 21:23:30 +0200542 * Clamp the yTranslation both up and down to valid positions.
Selim Cinek1685e632014-04-08 02:27:49 +0200543 *
Selim Cinek1685e632014-04-08 02:27:49 +0200544 * @param childViewState the view state of the child
Selim Cinek343e6e22014-04-11 21:23:30 +0200545 * @param childHeight the height of this child
Selim Cinek1685e632014-04-08 02:27:49 +0200546 */
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700547 private void clampYTranslation(StackViewState childViewState, int childHeight,
548 AmbientState ambientState) {
549 clampPositionToBottomStackStart(childViewState, childHeight, ambientState);
Selim Cinek343e6e22014-04-11 21:23:30 +0200550 clampPositionToTopStackEnd(childViewState, childHeight);
551 }
552
553 /**
554 * Clamp the yTranslation of the child down such that its end is at most on the beginning of
555 * the bottom stack.
556 *
557 * @param childViewState the view state of the child
558 * @param childHeight the height of this child
559 */
Selim Cinekb036ca42015-02-20 15:56:28 +0100560 private void clampPositionToBottomStackStart(StackViewState childViewState,
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700561 int childHeight, AmbientState ambientState) {
Selim Cinek343e6e22014-04-11 21:23:30 +0200562 childViewState.yTranslation = Math.min(childViewState.yTranslation,
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700563 ambientState.getInnerHeight() - mBottomStackPeekSize - mCollapseSecondCardPadding
564 - childHeight);
Selim Cinek343e6e22014-04-11 21:23:30 +0200565 }
566
567 /**
568 * Clamp the yTranslation of the child up such that its end is at lest on the end of the top
Selim Cineka59ecc32015-04-07 10:51:49 -0700569 * stack.
Selim Cinek343e6e22014-04-11 21:23:30 +0200570 *
571 * @param childViewState the view state of the child
572 * @param childHeight the height of this child
573 */
Selim Cinekb036ca42015-02-20 15:56:28 +0100574 private void clampPositionToTopStackEnd(StackViewState childViewState,
Selim Cinek343e6e22014-04-11 21:23:30 +0200575 int childHeight) {
576 childViewState.yTranslation = Math.max(childViewState.yTranslation,
577 mCollapsedSize - childHeight);
Selim Cinek1685e632014-04-08 02:27:49 +0200578 }
579
Selim Cineka59ecc32015-04-07 10:51:49 -0700580 private int getMaxAllowedChildHeight(View child, AmbientState ambientState) {
Selim Cinek1685e632014-04-08 02:27:49 +0200581 if (child instanceof ExpandableNotificationRow) {
582 ExpandableNotificationRow row = (ExpandableNotificationRow) child;
Selim Cinekd2281152015-04-10 14:37:46 -0700583 if (ambientState == null && row.isHeadsUp()
584 || ambientState != null && ambientState.getTopHeadsUpEntry() == child) {
Selim Cinek8d490d42015-04-10 00:05:50 -0700585 int extraSize = row.getIntrinsicHeight() - row.getHeadsUpHeight();
586 return mCollapsedSize + extraSize;
Selim Cineka59ecc32015-04-07 10:51:49 -0700587 }
Jorim Jaggi9cbadd32014-05-01 20:18:31 +0200588 return row.getIntrinsicHeight();
Jorim Jaggibe565df2014-04-28 17:51:23 +0200589 } else if (child instanceof ExpandableView) {
590 ExpandableView expandableView = (ExpandableView) child;
591 return expandableView.getActualHeight();
Selim Cinek1685e632014-04-08 02:27:49 +0200592 }
Jorim Jaggibe565df2014-04-28 17:51:23 +0200593 return child == null? mCollapsedSize : child.getHeight();
Selim Cinek1685e632014-04-08 02:27:49 +0200594 }
595
Selim Cinek343e6e22014-04-11 21:23:30 +0200596 private void updateStateForChildTransitioningInBottom(StackScrollAlgorithmState algorithmState,
597 float transitioningPositionStart, float bottomPeakStart, float currentYPosition,
Selim Cinekb036ca42015-02-20 15:56:28 +0100598 StackViewState childViewState, int childHeight) {
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200599
Selim Cinek343e6e22014-04-11 21:23:30 +0200600 // This is the transitioning element on top of bottom stack, calculate how far we are in.
601 algorithmState.partialInBottom = 1.0f - (
602 (transitioningPositionStart - currentYPosition) / (childHeight +
603 mPaddingBetweenElements));
604
605 // the offset starting at the transitionPosition of the bottom stack
606 float offset = mBottomStackIndentationFunctor.getValue(algorithmState.partialInBottom);
607 algorithmState.itemsInBottomStack += algorithmState.partialInBottom;
Selim Cinek3afd00e2014-08-11 22:32:57 +0200608 int newHeight = childHeight;
609 if (childHeight > mCollapsedSize && mIsSmallScreen) {
610 newHeight = (int) Math.max(Math.min(transitioningPositionStart + offset -
611 mPaddingBetweenElements - currentYPosition, childHeight), mCollapsedSize);
612 childViewState.height = newHeight;
613 }
614 childViewState.yTranslation = transitioningPositionStart + offset - newHeight
Jorim Jaggife40f7d2014-04-28 15:20:04 +0200615 - mPaddingBetweenElements;
Selim Cinek3afd00e2014-08-11 22:32:57 +0200616
Selim Cinek343e6e22014-04-11 21:23:30 +0200617 // We want at least to be at the end of the top stack when collapsing
Selim Cinek3afd00e2014-08-11 22:32:57 +0200618 clampPositionToTopStackEnd(childViewState, newHeight);
Selim Cinekb036ca42015-02-20 15:56:28 +0100619 childViewState.location = StackViewState.LOCATION_MAIN_AREA;
Selim Cinek67b22602014-03-10 15:40:16 +0100620 }
621
Selim Cinek343e6e22014-04-11 21:23:30 +0200622 private void updateStateForChildFullyInBottomStack(StackScrollAlgorithmState algorithmState,
Selim Cinekb036ca42015-02-20 15:56:28 +0100623 float transitioningPositionStart, StackViewState childViewState,
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700624 int childHeight, AmbientState ambientState) {
Selim Cinek343e6e22014-04-11 21:23:30 +0200625 float currentYPosition;
Selim Cinek67b22602014-03-10 15:40:16 +0100626 algorithmState.itemsInBottomStack += 1.0f;
627 if (algorithmState.itemsInBottomStack < MAX_ITEMS_IN_BOTTOM_STACK) {
628 // We are visually entering the bottom stack
Selim Cinek343e6e22014-04-11 21:23:30 +0200629 currentYPosition = transitioningPositionStart
Jorim Jaggife40f7d2014-04-28 15:20:04 +0200630 + mBottomStackIndentationFunctor.getValue(algorithmState.itemsInBottomStack)
631 - mPaddingBetweenElements;
Selim Cinekb036ca42015-02-20 15:56:28 +0100632 childViewState.location = StackViewState.LOCATION_BOTTOM_STACK_PEEKING;
Selim Cinek67b22602014-03-10 15:40:16 +0100633 } else {
634 // we are fully inside the stack
635 if (algorithmState.itemsInBottomStack > MAX_ITEMS_IN_BOTTOM_STACK + 2) {
636 childViewState.alpha = 0.0f;
637 } else if (algorithmState.itemsInBottomStack
638 > MAX_ITEMS_IN_BOTTOM_STACK + 1) {
639 childViewState.alpha = 1.0f - algorithmState.partialInBottom;
640 }
Selim Cinekb036ca42015-02-20 15:56:28 +0100641 childViewState.location = StackViewState.LOCATION_BOTTOM_STACK_HIDDEN;
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700642 currentYPosition = ambientState.getInnerHeight();
Selim Cinek67b22602014-03-10 15:40:16 +0100643 }
Selim Cinek343e6e22014-04-11 21:23:30 +0200644 childViewState.yTranslation = currentYPosition - childHeight;
645 clampPositionToTopStackEnd(childViewState, childHeight);
Selim Cinek67b22602014-03-10 15:40:16 +0100646 }
647
Selim Cinek343e6e22014-04-11 21:23:30 +0200648 private void updateStateForTopStackChild(StackScrollAlgorithmState algorithmState,
649 int numberOfElementsCompletelyIn, int i, int childHeight,
Selim Cinekb036ca42015-02-20 15:56:28 +0100650 StackViewState childViewState, float scrollOffset) {
Selim Cinek67b22602014-03-10 15:40:16 +0100651
Selim Cinek67b22602014-03-10 15:40:16 +0100652
653 // First we calculate the index relative to the current stack window of size at most
654 // {@link #MAX_ITEMS_IN_TOP_STACK}
Selim Cinek343e6e22014-04-11 21:23:30 +0200655 int paddedIndex = i - 1
Selim Cinek67b22602014-03-10 15:40:16 +0100656 - Math.max(numberOfElementsCompletelyIn - MAX_ITEMS_IN_TOP_STACK, 0);
657 if (paddedIndex >= 0) {
Selim Cinek343e6e22014-04-11 21:23:30 +0200658
Selim Cinek67b22602014-03-10 15:40:16 +0100659 // We are currently visually entering the top stack
Selim Cinekad3e5af2014-07-04 12:24:11 +0200660 float distanceToStack = (childHeight + mPaddingBetweenElements)
661 - algorithmState.scrolledPixelsTop;
662 if (i == algorithmState.lastTopStackIndex
663 && distanceToStack > (mTopStackTotalSize + mPaddingBetweenElements)) {
Selim Cinek343e6e22014-04-11 21:23:30 +0200664
665 // Child is currently translating into stack but not yet inside slow down zone.
666 // Handle it like the regular scrollview.
667 childViewState.yTranslation = scrollOffset;
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200668 } else {
Selim Cinek343e6e22014-04-11 21:23:30 +0200669 // Apply stacking logic.
670 float numItemsBefore;
671 if (i == algorithmState.lastTopStackIndex) {
Selim Cinekad3e5af2014-07-04 12:24:11 +0200672 numItemsBefore = 1.0f
673 - (distanceToStack / (mTopStackTotalSize + mPaddingBetweenElements));
Selim Cinek343e6e22014-04-11 21:23:30 +0200674 } else {
675 numItemsBefore = algorithmState.itemsInTopStack - i;
676 }
677 // The end position of the current child
Selim Cinekad3e5af2014-07-04 12:24:11 +0200678 float currentChildEndY = mCollapsedSize + mTopStackTotalSize
679 - mTopStackIndentationFunctor.getValue(numItemsBefore);
Selim Cinek343e6e22014-04-11 21:23:30 +0200680 childViewState.yTranslation = currentChildEndY - childHeight;
Selim Cinek67b22602014-03-10 15:40:16 +0100681 }
Selim Cinekb036ca42015-02-20 15:56:28 +0100682 childViewState.location = StackViewState.LOCATION_TOP_STACK_PEEKING;
Selim Cinek67b22602014-03-10 15:40:16 +0100683 } else {
Selim Cinek343e6e22014-04-11 21:23:30 +0200684 if (paddedIndex == -1) {
685 childViewState.alpha = 1.0f - algorithmState.partialInTop;
686 } else {
687 // We are hidden behind the top card and faded out, so we can hide ourselves.
688 childViewState.alpha = 0.0f;
689 }
690 childViewState.yTranslation = mCollapsedSize - childHeight;
Selim Cinekb036ca42015-02-20 15:56:28 +0100691 childViewState.location = StackViewState.LOCATION_TOP_STACK_HIDDEN;
Selim Cinek67b22602014-03-10 15:40:16 +0100692 }
Selim Cinek343e6e22014-04-11 21:23:30 +0200693
694
Selim Cinek67b22602014-03-10 15:40:16 +0100695 }
696
697 /**
698 * Find the number of items in the top stack and update the result state if needed.
699 *
700 * @param resultState The result state to update if a height change of an child occurs
701 * @param algorithmState The state in which the current pass of the algorithm is currently in
Selim Cinek67b22602014-03-10 15:40:16 +0100702 */
703 private void findNumberOfItemsInTopStackAndUpdateState(StackScrollState resultState,
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700704 StackScrollAlgorithmState algorithmState, AmbientState ambientState) {
Selim Cinek67b22602014-03-10 15:40:16 +0100705
706 // The y Position if the element would be in a regular scrollView
707 float yPositionInScrollView = 0.0f;
Jorim Jaggid4a57442014-04-10 02:45:55 +0200708 int childCount = algorithmState.visibleChildren.size();
Selim Cinek67b22602014-03-10 15:40:16 +0100709
710 // find the number of elements in the top stack.
711 for (int i = 0; i < childCount; i++) {
Jorim Jaggibe565df2014-04-28 17:51:23 +0200712 ExpandableView child = algorithmState.visibleChildren.get(i);
Selim Cinekb036ca42015-02-20 15:56:28 +0100713 StackViewState childViewState = resultState.getViewStateForView(child);
Selim Cineka59ecc32015-04-07 10:51:49 -0700714 int childHeight = getMaxAllowedChildHeight(child, ambientState);
Selim Cinek67b22602014-03-10 15:40:16 +0100715 float yPositionInScrollViewAfterElement = yPositionInScrollView
716 + childHeight
717 + mPaddingBetweenElements;
718 if (yPositionInScrollView < algorithmState.scrollY) {
Selim Cinek8d9ff9c2014-05-12 15:13:04 +0200719 if (i == 0 && algorithmState.scrollY <= mCollapsedSize) {
Selim Cinek343e6e22014-04-11 21:23:30 +0200720
721 // The starting position of the bottom stack peek
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700722 int bottomPeekStart = ambientState.getInnerHeight() - mBottomStackPeekSize -
Selim Cinekd83771e2014-07-04 16:45:31 +0200723 mCollapseSecondCardPadding;
Selim Cinek343e6e22014-04-11 21:23:30 +0200724 // Collapse and expand the first child while the shade is being expanded
725 float maxHeight = mIsExpansionChanging && child == mFirstChildWhileExpanding
726 ? mFirstChildMaxHeight
727 : childHeight;
728 childViewState.height = (int) Math.max(Math.min(bottomPeekStart, maxHeight),
729 mCollapsedSize);
730 algorithmState.itemsInTopStack = 1.0f;
731
732 } else if (yPositionInScrollViewAfterElement < algorithmState.scrollY) {
Selim Cinek67b22602014-03-10 15:40:16 +0100733 // According to the regular scroll view we are fully off screen
734 algorithmState.itemsInTopStack += 1.0f;
Selim Cinek343e6e22014-04-11 21:23:30 +0200735 if (i == 0) {
Selim Cinek67b22602014-03-10 15:40:16 +0100736 childViewState.height = mCollapsedSize;
737 }
738 } else {
739 // According to the regular scroll view we are partially off screen
Selim Cinek343e6e22014-04-11 21:23:30 +0200740
Selim Cinek67b22602014-03-10 15:40:16 +0100741 // How much did we scroll into this child
Selim Cinekad3e5af2014-07-04 12:24:11 +0200742 algorithmState.scrolledPixelsTop = algorithmState.scrollY
743 - yPositionInScrollView;
Selim Cinek343e6e22014-04-11 21:23:30 +0200744 algorithmState.partialInTop = (algorithmState.scrolledPixelsTop) / (childHeight
Selim Cinek67b22602014-03-10 15:40:16 +0100745 + mPaddingBetweenElements);
746
747 // Our element can be expanded, so this can get negative
748 algorithmState.partialInTop = Math.max(0.0f, algorithmState.partialInTop);
749 algorithmState.itemsInTopStack += algorithmState.partialInTop;
Selim Cinekad3e5af2014-07-04 12:24:11 +0200750
Selim Cinek343e6e22014-04-11 21:23:30 +0200751 if (i == 0) {
Selim Cinekad3e5af2014-07-04 12:24:11 +0200752 // If it is expanded we have to collapse it to a new size
753 float newSize = yPositionInScrollViewAfterElement
754 - mPaddingBetweenElements
755 - algorithmState.scrollY + mCollapsedSize;
756 newSize = Math.max(mCollapsedSize, newSize);
Selim Cinek4e456be2014-06-12 18:09:43 +0200757 algorithmState.itemsInTopStack = 1.0f;
Selim Cinek67b22602014-03-10 15:40:16 +0100758 childViewState.height = (int) newSize;
Selim Cinek67b22602014-03-10 15:40:16 +0100759 }
Selim Cinek343e6e22014-04-11 21:23:30 +0200760 algorithmState.lastTopStackIndex = i;
761 break;
Selim Cinek67b22602014-03-10 15:40:16 +0100762 }
763 } else {
Selim Cinek343e6e22014-04-11 21:23:30 +0200764 algorithmState.lastTopStackIndex = i - 1;
Selim Cinek67b22602014-03-10 15:40:16 +0100765 // We are already past the stack so we can end the loop
766 break;
767 }
768 yPositionInScrollView = yPositionInScrollViewAfterElement;
769 }
770 }
771
772 /**
773 * Calculate the Z positions for all children based on the number of items in both stacks and
774 * save it in the resultState
775 *
776 * @param resultState The result state to update the zTranslation values
777 * @param algorithmState The state in which the current pass of the algorithm is currently in
778 */
779 private void updateZValuesForState(StackScrollState resultState,
780 StackScrollAlgorithmState algorithmState) {
Jorim Jaggid4a57442014-04-10 02:45:55 +0200781 int childCount = algorithmState.visibleChildren.size();
Selim Cinek67b22602014-03-10 15:40:16 +0100782 for (int i = 0; i < childCount; i++) {
Jorim Jaggid4a57442014-04-10 02:45:55 +0200783 View child = algorithmState.visibleChildren.get(i);
Selim Cinekb036ca42015-02-20 15:56:28 +0100784 StackViewState childViewState = resultState.getViewStateForView(child);
Selim Cinek67b22602014-03-10 15:40:16 +0100785 if (i < algorithmState.itemsInTopStack) {
786 float stackIndex = algorithmState.itemsInTopStack - i;
Selim Cinekc5baa3e2014-10-29 19:04:19 +0100787
788 // Ensure that the topmost item is a little bit higher than the rest when fully
789 // scrolled, to avoid drawing errors when swiping it out
790 float max = MAX_ITEMS_IN_TOP_STACK + (i == 0 ? 2.5f : 2);
791 stackIndex = Math.min(stackIndex, max);
Selim Cinek4e456be2014-06-12 18:09:43 +0200792 if (i == 0 && algorithmState.itemsInTopStack < 2.0f) {
793
794 // We only have the top item and an additional item in the top stack,
795 // Interpolate the index from 0 to 2 while the second item is
796 // translating in.
797 stackIndex -= 1.0f;
798 if (algorithmState.scrollY > mCollapsedSize) {
799
800 // Since there is a shadow treshhold, we cant just interpolate from 0 to
801 // 2 but we interpolate from 0.1f to 2.0f when scrolled in. The jump in
802 // height will not be noticable since we have padding in between.
803 stackIndex = 0.1f + stackIndex * 1.9f;
804 }
805 }
Selim Cinek67b22602014-03-10 15:40:16 +0100806 childViewState.zTranslation = mZBasicHeight
807 + stackIndex * mZDistanceBetweenElements;
808 } else if (i > (childCount - 1 - algorithmState.itemsInBottomStack)) {
809 float numItemsAbove = i - (childCount - 1 - algorithmState.itemsInBottomStack);
810 float translationZ = mZBasicHeight
811 - numItemsAbove * mZDistanceBetweenElements;
812 childViewState.zTranslation = translationZ;
813 } else {
814 childViewState.zTranslation = mZBasicHeight;
815 }
816 }
817 }
818
Selim Cinek3afd00e2014-08-11 22:32:57 +0200819 /**
820 * Update whether the device is very small, i.e. Notifications can be in both the top and the
821 * bottom stack at the same time
822 *
823 * @param panelHeight The normal height of the panel when it's open
824 */
825 public void updateIsSmallScreen(int panelHeight) {
826 mIsSmallScreen = panelHeight <
827 mCollapsedSize /* top stack */
828 + mBottomStackSlowDownLength + mBottomStackPeekSize /* bottom stack */
829 + mMaxNotificationHeight; /* max notification height */
830 }
831
Selim Cinek1685e632014-04-08 02:27:49 +0200832 public void onExpansionStarted(StackScrollState currentState) {
833 mIsExpansionChanging = true;
834 mExpandedOnStart = mIsExpanded;
835 ViewGroup hostView = currentState.getHostView();
836 updateFirstChildHeightWhileExpanding(hostView);
837 }
838
839 private void updateFirstChildHeightWhileExpanding(ViewGroup hostView) {
Jorim Jaggibe565df2014-04-28 17:51:23 +0200840 mFirstChildWhileExpanding = (ExpandableView) findFirstVisibleChild(hostView);
Jorim Jaggi9f347ae2014-04-11 00:47:18 +0200841 if (mFirstChildWhileExpanding != null) {
Selim Cinek1685e632014-04-08 02:27:49 +0200842 if (mExpandedOnStart) {
843
844 // We are collapsing the shade, so the first child can get as most as high as the
Selim Cinek02af41e2014-10-14 15:46:43 +0200845 // current height or the end value of the animation.
846 mFirstChildMaxHeight = StackStateAnimator.getFinalActualHeight(
847 mFirstChildWhileExpanding);
Selim Cinekd2281152015-04-10 14:37:46 -0700848 if (mFirstChildWhileExpanding instanceof ExpandableNotificationRow) {
849 ExpandableNotificationRow row =
850 (ExpandableNotificationRow) mFirstChildWhileExpanding;
851 if (row.isHeadsUp()) {
852 mFirstChildMaxHeight += mCollapsedSize - row.getHeadsUpHeight();
853 }
854 }
Selim Cinek1685e632014-04-08 02:27:49 +0200855 } else {
Selim Cinek31094df2014-08-14 19:28:15 +0200856 updateFirstChildMaxSizeToMaxHeight();
Selim Cinek1685e632014-04-08 02:27:49 +0200857 }
858 } else {
Selim Cinek1685e632014-04-08 02:27:49 +0200859 mFirstChildMaxHeight = 0;
860 }
861 }
862
Selim Cinek31094df2014-08-14 19:28:15 +0200863 private void updateFirstChildMaxSizeToMaxHeight() {
864 // We are expanding the shade, expand it to its full height.
865 if (!isMaxSizeInitialized(mFirstChildWhileExpanding)) {
866
867 // This child was not layouted yet, wait for a layout pass
868 mFirstChildWhileExpanding
869 .addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
870 @Override
871 public void onLayoutChange(View v, int left, int top, int right,
872 int bottom, int oldLeft, int oldTop, int oldRight,
873 int oldBottom) {
874 if (mFirstChildWhileExpanding != null) {
875 mFirstChildMaxHeight = getMaxAllowedChildHeight(
Selim Cineka59ecc32015-04-07 10:51:49 -0700876 mFirstChildWhileExpanding, null);
Selim Cinek31094df2014-08-14 19:28:15 +0200877 } else {
878 mFirstChildMaxHeight = 0;
879 }
880 v.removeOnLayoutChangeListener(this);
881 }
882 });
883 } else {
Selim Cineka59ecc32015-04-07 10:51:49 -0700884 mFirstChildMaxHeight = getMaxAllowedChildHeight(mFirstChildWhileExpanding, null);
Selim Cinek31094df2014-08-14 19:28:15 +0200885 }
886 }
887
Selim Cinek7d447722014-06-10 15:51:59 +0200888 private boolean isMaxSizeInitialized(ExpandableView child) {
889 if (child instanceof ExpandableNotificationRow) {
890 ExpandableNotificationRow row = (ExpandableNotificationRow) child;
Selim Cinek31094df2014-08-14 19:28:15 +0200891 return row.isMaxExpandHeightInitialized();
Selim Cinek7d447722014-06-10 15:51:59 +0200892 }
893 return child == null || child.getWidth() != 0;
894 }
895
Jorim Jaggi9f347ae2014-04-11 00:47:18 +0200896 private View findFirstVisibleChild(ViewGroup container) {
897 int childCount = container.getChildCount();
898 for (int i = 0; i < childCount; i++) {
899 View child = container.getChildAt(i);
900 if (child.getVisibility() != View.GONE) {
901 return child;
902 }
903 }
904 return null;
905 }
906
Selim Cinek1685e632014-04-08 02:27:49 +0200907 public void onExpansionStopped() {
908 mIsExpansionChanging = false;
909 mFirstChildWhileExpanding = null;
910 }
911
912 public void setIsExpanded(boolean isExpanded) {
913 this.mIsExpanded = isExpanded;
914 }
915
Selim Cinek02af41e2014-10-14 15:46:43 +0200916 public void notifyChildrenChanged(final ViewGroup hostView) {
Selim Cinek1685e632014-04-08 02:27:49 +0200917 if (mIsExpansionChanging) {
Selim Cinek02af41e2014-10-14 15:46:43 +0200918 hostView.post(new Runnable() {
919 @Override
920 public void run() {
921 updateFirstChildHeightWhileExpanding(hostView);
922 }
923 });
Selim Cinek1685e632014-04-08 02:27:49 +0200924 }
925 }
926
Selim Cinek34c0a8d2014-05-12 00:01:43 +0200927 public void setDimmed(boolean dimmed) {
928 updatePadding(dimmed);
929 }
930
Selim Cinek31094df2014-08-14 19:28:15 +0200931 public void onReset(ExpandableView view) {
932 if (view.equals(mFirstChildWhileExpanding)) {
933 updateFirstChildMaxSizeToMaxHeight();
934 }
935 }
936
Selim Cinekd2281152015-04-10 14:37:46 -0700937 public void setHeadsUpManager(HeadsUpManager headsUpManager) {
938 mHeadsUpManager = headsUpManager;
939 }
940
Selim Cinek67b22602014-03-10 15:40:16 +0100941 class StackScrollAlgorithmState {
942
943 /**
944 * The scroll position of the algorithm
945 */
946 public int scrollY;
947
948 /**
949 * The quantity of items which are in the top stack.
950 */
951 public float itemsInTopStack;
952
953 /**
954 * how far in is the element currently transitioning into the top stack
955 */
956 public float partialInTop;
957
958 /**
Selim Cinek343e6e22014-04-11 21:23:30 +0200959 * The number of pixels the last child in the top stack has scrolled in to the stack
960 */
961 public float scrolledPixelsTop;
962
963 /**
Selim Cinek67b22602014-03-10 15:40:16 +0100964 * The last item index which is in the top stack.
Selim Cinek67b22602014-03-10 15:40:16 +0100965 */
966 public int lastTopStackIndex;
967
968 /**
969 * The quantity of items which are in the bottom stack.
970 */
971 public float itemsInBottomStack;
972
973 /**
974 * how far in is the element currently transitioning into the bottom stack
975 */
976 public float partialInBottom;
Jorim Jaggid4a57442014-04-10 02:45:55 +0200977
978 /**
979 * The children from the host view which are not gone.
980 */
Jorim Jaggibe565df2014-04-28 17:51:23 +0200981 public final ArrayList<ExpandableView> visibleChildren = new ArrayList<ExpandableView>();
Selim Cinek67b22602014-03-10 15:40:16 +0100982 }
983
984}