blob: 822012d8bd68c7435493c68554ba544314b3b73d [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 Cinek67b22602014-03-10 15:40:16 +010028
Jorim Jaggid4a57442014-04-10 02:45:55 +020029import java.util.ArrayList;
Selim Cinekb5605e52015-02-20 18:21:41 +010030import java.util.List;
Jorim Jaggid4a57442014-04-10 02:45:55 +020031
Selim Cinek67b22602014-03-10 15:40:16 +010032/**
33 * The Algorithm of the {@link com.android.systemui.statusbar.stack
34 * .NotificationStackScrollLayout} which can be queried for {@link com.android.systemui.statusbar
35 * .stack.StackScrollState}
36 */
37public class StackScrollAlgorithm {
38
Christoph Studer6e3eceb2014-04-01 18:40:27 +020039 private static final String LOG_TAG = "StackScrollAlgorithm";
40
Selim Cinek67b22602014-03-10 15:40:16 +010041 private static final int MAX_ITEMS_IN_BOTTOM_STACK = 3;
42 private static final int MAX_ITEMS_IN_TOP_STACK = 3;
43
Selim Cinek65b2e7c2015-10-26 14:11:31 -070044 public static final float DIMMED_SCALE = 0.98f;
Jorim Jaggid552d9d2014-05-07 19:41:13 +020045
Selim Cinek67b22602014-03-10 15:40:16 +010046 private int mPaddingBetweenElements;
47 private int mCollapsedSize;
48 private int mTopStackPeekSize;
49 private int mBottomStackPeekSize;
50 private int mZDistanceBetweenElements;
51 private int mZBasicHeight;
Selim Cinek708a6c12014-05-28 14:16:02 +020052 private int mRoundedRectCornerRadius;
Selim Cinek67b22602014-03-10 15:40:16 +010053
54 private StackIndentationFunctor mTopStackIndentationFunctor;
55 private StackIndentationFunctor mBottomStackIndentationFunctor;
56
Selim Cinek67b22602014-03-10 15:40:16 +010057 private StackScrollAlgorithmState mTempAlgorithmState = new StackScrollAlgorithmState();
Selim Cinek1685e632014-04-08 02:27:49 +020058 private boolean mIsExpansionChanging;
59 private int mFirstChildMaxHeight;
60 private boolean mIsExpanded;
Jorim Jaggibe565df2014-04-28 17:51:23 +020061 private ExpandableView mFirstChildWhileExpanding;
Selim Cinek1685e632014-04-08 02:27:49 +020062 private boolean mExpandedOnStart;
Selim Cinek343e6e22014-04-11 21:23:30 +020063 private int mTopStackTotalSize;
Selim Cinek34c0a8d2014-05-12 00:01:43 +020064 private int mPaddingBetweenElementsDimmed;
65 private int mPaddingBetweenElementsNormal;
Selim Cinek79d79c42015-05-21 16:14:45 -070066 private int mNotificationsTopPadding;
Selim Cinek34c0a8d2014-05-12 00:01:43 +020067 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;
Jorim Jaggid7c1fae2014-08-13 18:27:47 +020070 private boolean mScaleDimmed;
Selim Cinekb55386d2015-12-16 17:26:49 -080071 private ExpandableView mFirstChild;
Selim Cinek816c8e42015-11-19 12:00:45 -080072 private int mFirstChildMinHeight;
Selim Cinekaf0dc312015-12-15 17:01:44 -080073 private boolean mDimmed;
Selim Cinek67b22602014-03-10 15:40:16 +010074
75 public StackScrollAlgorithm(Context context) {
Selim Cinekaf0dc312015-12-15 17:01:44 -080076 initView(context);
Selim Cinek34c0a8d2014-05-12 00:01:43 +020077 }
78
Selim Cinekaf0dc312015-12-15 17:01:44 -080079 public void initView(Context context) {
80 initConstants(context);
81 updatePadding();
82 }
83
84 private void updatePadding() {
85 mPaddingBetweenElements = mDimmed && mScaleDimmed
Selim Cinek34c0a8d2014-05-12 00:01:43 +020086 ? mPaddingBetweenElementsDimmed
87 : mPaddingBetweenElementsNormal;
Selim Cinekad3e5af2014-07-04 12:24:11 +020088 mTopStackTotalSize = mTopStackSlowDownLength + mPaddingBetweenElements
89 + mTopStackPeekSize;
Selim Cinek34c0a8d2014-05-12 00:01:43 +020090 mTopStackIndentationFunctor = new PiecewiseLinearIndentationFunctor(
91 MAX_ITEMS_IN_TOP_STACK,
92 mTopStackPeekSize,
Selim Cinekb96924d2014-05-12 15:11:25 +020093 mTopStackTotalSize - mTopStackPeekSize,
Selim Cinek34c0a8d2014-05-12 00:01:43 +020094 0.5f);
95 mBottomStackIndentationFunctor = new PiecewiseLinearIndentationFunctor(
96 MAX_ITEMS_IN_BOTTOM_STACK,
97 mBottomStackPeekSize,
98 getBottomStackSlowDownLength(),
99 0.5f);
100 }
101
102 public int getBottomStackSlowDownLength() {
103 return mBottomStackSlowDownLength + mPaddingBetweenElements;
Selim Cinek67b22602014-03-10 15:40:16 +0100104 }
105
106 private void initConstants(Context context) {
Selim Cinek34c0a8d2014-05-12 00:01:43 +0200107 mPaddingBetweenElementsDimmed = context.getResources()
108 .getDimensionPixelSize(R.dimen.notification_padding_dimmed);
109 mPaddingBetweenElementsNormal = context.getResources()
Jorim Jaggife40f7d2014-04-28 15:20:04 +0200110 .getDimensionPixelSize(R.dimen.notification_padding);
Selim Cinek79d79c42015-05-21 16:14:45 -0700111 mNotificationsTopPadding = context.getResources()
112 .getDimensionPixelSize(R.dimen.notifications_top_padding);
Selim Cinek67b22602014-03-10 15:40:16 +0100113 mCollapsedSize = context.getResources()
Jorim Jaggife40f7d2014-04-28 15:20:04 +0200114 .getDimensionPixelSize(R.dimen.notification_min_height);
Selim Cinek67b22602014-03-10 15:40:16 +0100115 mTopStackPeekSize = context.getResources()
116 .getDimensionPixelSize(R.dimen.top_stack_peek_amount);
117 mBottomStackPeekSize = context.getResources()
118 .getDimensionPixelSize(R.dimen.bottom_stack_peek_amount);
119 mZDistanceBetweenElements = context.getResources()
120 .getDimensionPixelSize(R.dimen.z_distance_between_notifications);
121 mZBasicHeight = (MAX_ITEMS_IN_BOTTOM_STACK + 1) * mZDistanceBetweenElements;
Selim Cinek34c0a8d2014-05-12 00:01:43 +0200122 mBottomStackSlowDownLength = context.getResources()
123 .getDimensionPixelSize(R.dimen.bottom_stack_slow_down_length);
Selim Cinekad3e5af2014-07-04 12:24:11 +0200124 mTopStackSlowDownLength = context.getResources()
125 .getDimensionPixelSize(R.dimen.top_stack_slow_down_length);
Selim Cinek708a6c12014-05-28 14:16:02 +0200126 mRoundedRectCornerRadius = context.getResources().getDimensionPixelSize(
Selim Cinek697178b2014-07-02 19:40:30 +0200127 R.dimen.notification_material_rounded_rect_radius);
Selim Cinekd83771e2014-07-04 16:45:31 +0200128 mCollapseSecondCardPadding = context.getResources().getDimensionPixelSize(
129 R.dimen.notification_collapse_second_card_padding);
Jorim Jaggid7c1fae2014-08-13 18:27:47 +0200130 mScaleDimmed = context.getResources().getDisplayMetrics().densityDpi
Jorim Jaggi2cb70042015-08-24 14:52:45 -0700131 >= DisplayMetrics.DENSITY_420;
Selim Cinek67b22602014-03-10 15:40:16 +0100132 }
133
Jorim Jaggid7c1fae2014-08-13 18:27:47 +0200134 public boolean shouldScaleDimmed() {
135 return mScaleDimmed;
136 }
Selim Cinek67b22602014-03-10 15:40:16 +0100137
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200138 public void getStackScrollState(AmbientState ambientState, StackScrollState resultState) {
Selim Cinek67b22602014-03-10 15:40:16 +0100139 // The state of the local variables are saved in an algorithmState to easily subdivide it
140 // into multiple phases.
141 StackScrollAlgorithmState algorithmState = mTempAlgorithmState;
142
143 // First we reset the view states to their default values.
144 resultState.resetViewStates();
145
Selim Cinek343e6e22014-04-11 21:23:30 +0200146 algorithmState.itemsInTopStack = 0.0f;
Selim Cinek67b22602014-03-10 15:40:16 +0100147 algorithmState.partialInTop = 0.0f;
148 algorithmState.lastTopStackIndex = 0;
Selim Cinek343e6e22014-04-11 21:23:30 +0200149 algorithmState.scrolledPixelsTop = 0;
Selim Cinek67b22602014-03-10 15:40:16 +0100150 algorithmState.itemsInBottomStack = 0.0f;
Selim Cinek343e6e22014-04-11 21:23:30 +0200151 algorithmState.partialInBottom = 0.0f;
Selim Cinekb55386d2015-12-16 17:26:49 -0800152 mFirstChildMinHeight = mFirstChild == null ? 0 : mFirstChild.getMinHeight();
Selim Cinek8d9ff9c2014-05-12 15:13:04 +0200153 float bottomOverScroll = ambientState.getOverScrollAmount(false /* onTop */);
Selim Cinek1408eb52014-06-02 14:45:38 +0200154
155 int scrollY = ambientState.getScrollY();
156
157 // Due to the overScroller, the stackscroller can have negative scroll state. This is
158 // already accounted for by the top padding and doesn't need an additional adaption
159 scrollY = Math.max(0, scrollY);
Selim Cinek816c8e42015-11-19 12:00:45 -0800160 algorithmState.scrollY = (int) (scrollY + mFirstChildMinHeight + bottomOverScroll);
Selim Cinek343e6e22014-04-11 21:23:30 +0200161
Selim Cineka4baaa32015-04-20 14:27:54 -0700162 updateVisibleChildren(resultState, algorithmState);
Selim Cinek67b22602014-03-10 15:40:16 +0100163
164 // Phase 1:
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700165 findNumberOfItemsInTopStackAndUpdateState(resultState, algorithmState, ambientState);
Selim Cinek67b22602014-03-10 15:40:16 +0100166
167 // Phase 2:
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700168 updatePositionsForState(resultState, algorithmState, ambientState);
Selim Cinek67b22602014-03-10 15:40:16 +0100169
170 // Phase 3:
171 updateZValuesForState(resultState, algorithmState);
Selim Cinekeb973562014-05-02 17:07:49 +0200172
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200173 handleDraggedViews(ambientState, resultState, algorithmState);
Jorim Jaggiae441282014-08-01 02:45:18 +0200174 updateDimmedActivatedHideSensitive(ambientState, resultState, algorithmState);
Selim Cineka59ecc32015-04-07 10:51:49 -0700175 updateClipping(resultState, algorithmState, ambientState);
Selim Cinek3d2b94bf2014-07-02 22:12:47 +0200176 updateSpeedBumpState(resultState, algorithmState, ambientState.getSpeedBumpIndex());
Selim Cinekb5605e52015-02-20 18:21:41 +0100177 getNotificationChildrenStates(resultState, algorithmState);
178 }
179
180 private void getNotificationChildrenStates(StackScrollState resultState,
181 StackScrollAlgorithmState algorithmState) {
182 int childCount = algorithmState.visibleChildren.size();
183 for (int i = 0; i < childCount; i++) {
184 ExpandableView v = algorithmState.visibleChildren.get(i);
185 if (v instanceof ExpandableNotificationRow) {
186 ExpandableNotificationRow row = (ExpandableNotificationRow) v;
187 row.getChildrenStates(resultState);
188 }
189 }
Selim Cinek3d2b94bf2014-07-02 22:12:47 +0200190 }
191
192 private void updateSpeedBumpState(StackScrollState resultState,
193 StackScrollAlgorithmState algorithmState, int speedBumpIndex) {
194 int childCount = algorithmState.visibleChildren.size();
195 for (int i = 0; i < childCount; i++) {
196 View child = algorithmState.visibleChildren.get(i);
Selim Cinekb036ca42015-02-20 15:56:28 +0100197 StackViewState childViewState = resultState.getViewStateForView(child);
Selim Cinek3107cfa2014-07-22 15:24:29 +0200198
199 // The speed bump can also be gone, so equality needs to be taken when comparing
200 // indices.
201 childViewState.belowSpeedBump = speedBumpIndex != -1 && i >= speedBumpIndex;
Selim Cinek3d2b94bf2014-07-02 22:12:47 +0200202 }
Selim Cinekf54090e2014-06-17 17:24:51 -0700203 }
204
Selim Cinek708a6c12014-05-28 14:16:02 +0200205 private void updateClipping(StackScrollState resultState,
Selim Cineka59ecc32015-04-07 10:51:49 -0700206 StackScrollAlgorithmState algorithmState, AmbientState ambientState) {
Selim Cinek9c17b772015-07-07 20:37:09 -0700207 boolean dismissAllInProgress = ambientState.isDismissAllInProgress();
Selim Cinek708a6c12014-05-28 14:16:02 +0200208 float previousNotificationEnd = 0;
209 float previousNotificationStart = 0;
210 boolean previousNotificationIsSwiped = false;
211 int childCount = algorithmState.visibleChildren.size();
212 for (int i = 0; i < childCount; i++) {
213 ExpandableView child = algorithmState.visibleChildren.get(i);
Selim Cinekb036ca42015-02-20 15:56:28 +0100214 StackViewState state = resultState.getViewStateForView(child);
Jorim Jaggi2e34ec32014-07-01 03:17:05 +0200215 float newYTranslation = state.yTranslation + state.height * (1f - state.scale) / 2f;
216 float newHeight = state.height * state.scale;
Selim Cinek708a6c12014-05-28 14:16:02 +0200217 // apply clipping and shadow
218 float newNotificationEnd = newYTranslation + newHeight;
219
Selim Cinekc5baa3e2014-10-29 19:04:19 +0100220 float clipHeight;
221 if (previousNotificationIsSwiped) {
222 // When the previous notification is swiped, we don't clip the content to the
223 // bottom of it.
224 clipHeight = newHeight;
225 } else {
226 clipHeight = newNotificationEnd - previousNotificationEnd;
227 clipHeight = Math.max(0.0f, clipHeight);
228 if (clipHeight != 0.0f) {
Selim Cinek708a6c12014-05-28 14:16:02 +0200229
Selim Cinekc5baa3e2014-10-29 19:04:19 +0100230 // In the unlocked shade we have to clip a little bit higher because of the rounded
231 // corners of the notifications, but only if we are not fully overlapped by
232 // the top card.
233 float clippingCorrection = state.dimmed
234 ? 0
235 : mRoundedRectCornerRadius * state.scale;
236 clipHeight += clippingCorrection;
237 }
238 }
Selim Cinek708a6c12014-05-28 14:16:02 +0200239
240 updateChildClippingAndBackground(state, newHeight, clipHeight,
Jorim Jaggi2e34ec32014-07-01 03:17:05 +0200241 newHeight - (previousNotificationStart - newYTranslation));
Selim Cinek708a6c12014-05-28 14:16:02 +0200242
Selim Cinek9c17b772015-07-07 20:37:09 -0700243 if (dismissAllInProgress) {
244 state.clipTopAmount = Math.max(child.getMinClipTopAmount(), state.clipTopAmount);
245 }
246
Selim Cinek708a6c12014-05-28 14:16:02 +0200247 if (!child.isTransparent()) {
248 // Only update the previous values if we are not transparent,
249 // otherwise we would clip to a transparent view.
Selim Cinek9c17b772015-07-07 20:37:09 -0700250 if ((dismissAllInProgress && canChildBeDismissed(child))) {
251 previousNotificationIsSwiped = true;
252 } else {
253 previousNotificationIsSwiped = ambientState.getDraggedViews().contains(child);
254 previousNotificationEnd = newNotificationEnd;
255 previousNotificationStart = newYTranslation + state.clipTopAmount * state.scale;
256 }
Selim Cinek708a6c12014-05-28 14:16:02 +0200257 }
258 }
259 }
260
Selim Cinek9c17b772015-07-07 20:37:09 -0700261 public static boolean canChildBeDismissed(View v) {
262 final View veto = v.findViewById(R.id.veto);
263 return (veto != null && veto.getVisibility() != View.GONE);
264 }
265
Selim Cinek708a6c12014-05-28 14:16:02 +0200266 /**
267 * Updates the shadow outline and the clipping for a view.
268 *
269 * @param state the viewState to update
270 * @param realHeight the currently applied height of the view
271 * @param clipHeight the desired clip height, the rest of the view will be clipped from the top
272 * @param backgroundHeight the desired background height. The shadows of the view will be
273 * based on this height and the content will be clipped from the top
274 */
Selim Cinekb036ca42015-02-20 15:56:28 +0100275 private void updateChildClippingAndBackground(StackViewState state, float realHeight,
276 float clipHeight, float backgroundHeight) {
Selim Cinek708a6c12014-05-28 14:16:02 +0200277 if (realHeight > clipHeight) {
Jorim Jaggi2e34ec32014-07-01 03:17:05 +0200278 // Rather overlap than create a hole.
279 state.topOverLap = (int) Math.floor((realHeight - clipHeight) / state.scale);
Selim Cinek708a6c12014-05-28 14:16:02 +0200280 } else {
281 state.topOverLap = 0;
282 }
283 if (realHeight > backgroundHeight) {
Jorim Jaggi2e34ec32014-07-01 03:17:05 +0200284 // Rather overlap than create a hole.
285 state.clipTopAmount = (int) Math.floor((realHeight - backgroundHeight) / state.scale);
Selim Cinek708a6c12014-05-28 14:16:02 +0200286 } else {
287 state.clipTopAmount = 0;
288 }
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200289 }
290
291 /**
Jorim Jaggiae441282014-08-01 02:45:18 +0200292 * Updates the dimmed, activated and hiding sensitive states of the children.
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200293 */
Jorim Jaggiae441282014-08-01 02:45:18 +0200294 private void updateDimmedActivatedHideSensitive(AmbientState ambientState,
295 StackScrollState resultState, StackScrollAlgorithmState algorithmState) {
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200296 boolean dimmed = ambientState.isDimmed();
John Spurlockbf370992014-06-17 13:58:31 -0400297 boolean dark = ambientState.isDark();
Jorim Jaggiae441282014-08-01 02:45:18 +0200298 boolean hideSensitive = ambientState.isHideSensitive();
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200299 View activatedChild = ambientState.getActivatedChild();
300 int childCount = algorithmState.visibleChildren.size();
301 for (int i = 0; i < childCount; i++) {
302 View child = algorithmState.visibleChildren.get(i);
Selim Cinekb036ca42015-02-20 15:56:28 +0100303 StackViewState childViewState = resultState.getViewStateForView(child);
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200304 childViewState.dimmed = dimmed;
John Spurlockbf370992014-06-17 13:58:31 -0400305 childViewState.dark = dark;
Jorim Jaggiae441282014-08-01 02:45:18 +0200306 childViewState.hideSensitive = hideSensitive;
Selim Cinekb89de4e2014-06-10 10:47:05 +0200307 boolean isActivatedChild = activatedChild == child;
Jorim Jaggid7c1fae2014-08-13 18:27:47 +0200308 childViewState.scale = !mScaleDimmed || !dimmed || isActivatedChild
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200309 ? 1.0f
310 : DIMMED_SCALE;
Jorim Jaggi4538cee2014-09-09 15:21:38 +0200311 if (dimmed && isActivatedChild) {
312 childViewState.zTranslation += 2.0f * mZDistanceBetweenElements;
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200313 }
314 }
Selim Cinekeb973562014-05-02 17:07:49 +0200315 }
316
317 /**
318 * Handle the special state when views are being dragged
319 */
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200320 private void handleDraggedViews(AmbientState ambientState, StackScrollState resultState,
Selim Cinekeb973562014-05-02 17:07:49 +0200321 StackScrollAlgorithmState algorithmState) {
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200322 ArrayList<View> draggedViews = ambientState.getDraggedViews();
323 for (View draggedView : draggedViews) {
Selim Cinekeb973562014-05-02 17:07:49 +0200324 int childIndex = algorithmState.visibleChildren.indexOf(draggedView);
325 if (childIndex >= 0 && childIndex < algorithmState.visibleChildren.size() - 1) {
326 View nextChild = algorithmState.visibleChildren.get(childIndex + 1);
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200327 if (!draggedViews.contains(nextChild)) {
Selim Cinekeb973562014-05-02 17:07:49 +0200328 // only if the view is not dragged itself we modify its state to be fully
329 // visible
Selim Cinekb036ca42015-02-20 15:56:28 +0100330 StackViewState viewState = resultState.getViewStateForView(
Selim Cinekeb973562014-05-02 17:07:49 +0200331 nextChild);
332 // The child below the dragged one must be fully visible
Selim Cinek131c1e22015-05-11 19:04:49 -0700333 if (ambientState.isShadeExpanded()) {
Selim Cineka59ecc32015-04-07 10:51:49 -0700334 viewState.alpha = 1;
335 }
Selim Cinekeb973562014-05-02 17:07:49 +0200336 }
337
338 // Lets set the alpha to the one it currently has, as its currently being dragged
Selim Cinekb036ca42015-02-20 15:56:28 +0100339 StackViewState viewState = resultState.getViewStateForView(draggedView);
Selim Cinekeb973562014-05-02 17:07:49 +0200340 // The dragged child should keep the set alpha
341 viewState.alpha = draggedView.getAlpha();
342 }
343 }
Selim Cinek343e6e22014-04-11 21:23:30 +0200344 }
Selim Cinek67b22602014-03-10 15:40:16 +0100345
Selim Cinek343e6e22014-04-11 21:23:30 +0200346 /**
Jorim Jaggid4a57442014-04-10 02:45:55 +0200347 * Update the visible children on the state.
348 */
349 private void updateVisibleChildren(StackScrollState resultState,
Selim Cineka4baaa32015-04-20 14:27:54 -0700350 StackScrollAlgorithmState state) {
Jorim Jaggid4a57442014-04-10 02:45:55 +0200351 ViewGroup hostView = resultState.getHostView();
352 int childCount = hostView.getChildCount();
353 state.visibleChildren.clear();
354 state.visibleChildren.ensureCapacity(childCount);
Selim Cinekb036ca42015-02-20 15:56:28 +0100355 int notGoneIndex = 0;
Jorim Jaggid4a57442014-04-10 02:45:55 +0200356 for (int i = 0; i < childCount; i++) {
Jorim Jaggibe565df2014-04-28 17:51:23 +0200357 ExpandableView v = (ExpandableView) hostView.getChildAt(i);
Jorim Jaggid4a57442014-04-10 02:45:55 +0200358 if (v.getVisibility() != View.GONE) {
Selim Cineka4baaa32015-04-20 14:27:54 -0700359 notGoneIndex = updateNotGoneIndex(resultState, state, notGoneIndex, v);
Selim Cinekb5605e52015-02-20 18:21:41 +0100360 if (v instanceof ExpandableNotificationRow) {
361 ExpandableNotificationRow row = (ExpandableNotificationRow) v;
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700362
363 // handle the notgoneIndex for the children as well
Selim Cinekb5605e52015-02-20 18:21:41 +0100364 List<ExpandableNotificationRow> children =
365 row.getNotificationChildren();
Selim Cinek83bc7832015-10-22 13:26:54 -0700366 if (row.isSummaryWithChildren() && children != null) {
Selim Cinekb5605e52015-02-20 18:21:41 +0100367 for (ExpandableNotificationRow childRow : children) {
368 if (childRow.getVisibility() != View.GONE) {
369 StackViewState childState
370 = resultState.getViewStateForView(childRow);
371 childState.notGoneIndex = notGoneIndex;
372 notGoneIndex++;
373 }
374 }
375 }
376 }
Jorim Jaggid4a57442014-04-10 02:45:55 +0200377 }
378 }
379 }
380
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700381 private int updateNotGoneIndex(StackScrollState resultState,
382 StackScrollAlgorithmState state, int notGoneIndex,
383 ExpandableView v) {
384 StackViewState viewState = resultState.getViewStateForView(v);
385 viewState.notGoneIndex = notGoneIndex;
386 state.visibleChildren.add(v);
387 notGoneIndex++;
388 return notGoneIndex;
389 }
390
Jorim Jaggid4a57442014-04-10 02:45:55 +0200391 /**
Selim Cinek67b22602014-03-10 15:40:16 +0100392 * Determine the positions for the views. This is the main part of the algorithm.
393 *
Selim Cinek684a4422015-04-15 16:18:39 -0700394 * @param resultState The result state to update if a change to the properties of a child occurs
Selim Cinek67b22602014-03-10 15:40:16 +0100395 * @param algorithmState The state in which the current pass of the algorithm is currently in
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700396 * @param ambientState The current ambient state
Selim Cinek67b22602014-03-10 15:40:16 +0100397 */
398 private void updatePositionsForState(StackScrollState resultState,
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700399 StackScrollAlgorithmState algorithmState, AmbientState ambientState) {
Selim Cinek67b22602014-03-10 15:40:16 +0100400
Selim Cinek1685e632014-04-08 02:27:49 +0200401 // The starting position of the bottom stack peek
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700402 float bottomPeekStart = ambientState.getInnerHeight() - mBottomStackPeekSize;
Selim Cinek1685e632014-04-08 02:27:49 +0200403
Selim Cinek67b22602014-03-10 15:40:16 +0100404 // The position where the bottom stack starts.
Selim Cinek34c0a8d2014-05-12 00:01:43 +0200405 float bottomStackStart = bottomPeekStart - mBottomStackSlowDownLength;
Selim Cinek67b22602014-03-10 15:40:16 +0100406
407 // The y coordinate of the current child.
408 float currentYPosition = 0.0f;
409
410 // How far in is the element currently transitioning into the bottom stack.
411 float yPositionInScrollView = 0.0f;
412
Jorim Jaggid4a57442014-04-10 02:45:55 +0200413 int childCount = algorithmState.visibleChildren.size();
Selim Cinekf92a1fd2015-07-31 16:10:32 -0700414 int numberOfElementsCompletelyIn = algorithmState.partialInTop == 1.0f
415 ? algorithmState.lastTopStackIndex
416 : (int) algorithmState.itemsInTopStack;
Selim Cinek67b22602014-03-10 15:40:16 +0100417 for (int i = 0; i < childCount; i++) {
Jorim Jaggibe565df2014-04-28 17:51:23 +0200418 ExpandableView child = algorithmState.visibleChildren.get(i);
Selim Cinekb036ca42015-02-20 15:56:28 +0100419 StackViewState childViewState = resultState.getViewStateForView(child);
420 childViewState.location = StackViewState.LOCATION_UNKNOWN;
Selim Cinek31aada42015-12-18 17:51:15 -0800421 int childHeight = getMaxAllowedChildHeight(child);
Selim Cinek3e881372015-12-10 15:13:42 -0800422 int minHeight = child.getMinHeight();
Selim Cinek67b22602014-03-10 15:40:16 +0100423 float yPositionInScrollViewAfterElement = yPositionInScrollView
424 + childHeight
425 + mPaddingBetweenElements;
Selim Cinek816c8e42015-11-19 12:00:45 -0800426 float scrollOffset = yPositionInScrollView - algorithmState.scrollY +
427 mFirstChildMinHeight;
Selim Cinek343e6e22014-04-11 21:23:30 +0200428
429 if (i == algorithmState.lastTopStackIndex + 1) {
430 // Normally the position of this child is the position in the regular scrollview,
431 // but if the two stacks are very close to each other,
432 // then have have to push it even more upwards to the position of the bottom
433 // stack start.
434 currentYPosition = Math.min(scrollOffset, bottomStackStart);
435 }
436 childViewState.yTranslation = currentYPosition;
437
438 // The y position after this element
439 float nextYPosition = currentYPosition + childHeight +
440 mPaddingBetweenElements;
441
442 if (i <= algorithmState.lastTopStackIndex) {
Selim Cinek67b22602014-03-10 15:40:16 +0100443 // Case 1:
444 // We are in the top Stack
Selim Cinek343e6e22014-04-11 21:23:30 +0200445 updateStateForTopStackChild(algorithmState,
446 numberOfElementsCompletelyIn, i, childHeight, childViewState, scrollOffset);
Selim Cinek3afd00e2014-08-11 22:32:57 +0200447 clampPositionToTopStackEnd(childViewState, childHeight);
448
Selim Cinek343e6e22014-04-11 21:23:30 +0200449 // check if we are overlapping with the bottom stack
450 if (childViewState.yTranslation + childHeight + mPaddingBetweenElements
Selim Cinek3e881372015-12-10 15:13:42 -0800451 >= bottomStackStart && !mIsExpansionChanging && i != 0) {
Selim Cinek3afd00e2014-08-11 22:32:57 +0200452 // we just collapse this element slightly
453 int newSize = (int) Math.max(bottomStackStart - mPaddingBetweenElements -
Selim Cinek3e881372015-12-10 15:13:42 -0800454 childViewState.yTranslation, minHeight);
Selim Cinek3afd00e2014-08-11 22:32:57 +0200455 childViewState.height = newSize;
Selim Cinek4581cf82014-08-12 12:40:32 +0200456 updateStateForChildTransitioningInBottom(algorithmState, bottomStackStart,
Selim Cinek816c8e42015-11-19 12:00:45 -0800457 child, childViewState.yTranslation, childViewState,
Selim Cinek4581cf82014-08-12 12:40:32 +0200458 childHeight);
Selim Cinek343e6e22014-04-11 21:23:30 +0200459 }
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700460 clampPositionToBottomStackStart(childViewState, childViewState.height,
Selim Cinek3e881372015-12-10 15:13:42 -0800461 minHeight, ambientState);
Selim Cinek343e6e22014-04-11 21:23:30 +0200462 } else if (nextYPosition >= bottomStackStart) {
Selim Cinek67b22602014-03-10 15:40:16 +0100463 // Case 2:
Selim Cinek343e6e22014-04-11 21:23:30 +0200464 // We are in the bottom stack.
465 if (currentYPosition >= bottomStackStart) {
Selim Cinek67b22602014-03-10 15:40:16 +0100466 // According to the regular scroll view we are fully translated out of the
467 // bottom of the screen so we are fully in the bottom stack
Selim Cinek343e6e22014-04-11 21:23:30 +0200468 updateStateForChildFullyInBottomStack(algorithmState,
Selim Cinek3e881372015-12-10 15:13:42 -0800469 bottomStackStart, childViewState, minHeight, ambientState);
Selim Cinek67b22602014-03-10 15:40:16 +0100470 } else {
Selim Cinek67b22602014-03-10 15:40:16 +0100471 // According to the regular scroll view we are currently translating out of /
472 // into the bottom of the screen
Selim Cinek343e6e22014-04-11 21:23:30 +0200473 updateStateForChildTransitioningInBottom(algorithmState,
Selim Cinek816c8e42015-11-19 12:00:45 -0800474 bottomStackStart, child, currentYPosition,
Selim Cinek343e6e22014-04-11 21:23:30 +0200475 childViewState, childHeight);
Selim Cinek67b22602014-03-10 15:40:16 +0100476 }
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200477 } else {
Selim Cinek343e6e22014-04-11 21:23:30 +0200478 // Case 3:
479 // We are in the regular scroll area.
Selim Cinekb036ca42015-02-20 15:56:28 +0100480 childViewState.location = StackViewState.LOCATION_MAIN_AREA;
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700481 clampYTranslation(childViewState, childHeight, ambientState);
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200482 }
Selim Cinek343e6e22014-04-11 21:23:30 +0200483
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200484 // The first card is always rendered.
485 if (i == 0) {
486 childViewState.alpha = 1.0f;
Selim Cinek816c8e42015-11-19 12:00:45 -0800487 childViewState.yTranslation = Math.max(
488 mFirstChildMinHeight - algorithmState.scrollY, 0);
Selim Cinekd83771e2014-07-04 16:45:31 +0200489 if (childViewState.yTranslation + childViewState.height
490 > bottomPeekStart - mCollapseSecondCardPadding) {
Selim Cinek4fe3e472014-07-03 16:32:54 +0200491 childViewState.height = (int) Math.max(
Selim Cinekd83771e2014-07-04 16:45:31 +0200492 bottomPeekStart - mCollapseSecondCardPadding
Selim Cinek816c8e42015-11-19 12:00:45 -0800493 - childViewState.yTranslation, mFirstChildMinHeight);
Selim Cinek4fe3e472014-07-03 16:32:54 +0200494 }
Selim Cinekb036ca42015-02-20 15:56:28 +0100495 childViewState.location = StackViewState.LOCATION_FIRST_CARD;
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200496 }
Selim Cinekb036ca42015-02-20 15:56:28 +0100497 if (childViewState.location == StackViewState.LOCATION_UNKNOWN) {
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200498 Log.wtf(LOG_TAG, "Failed to assign location for child " + i);
Selim Cinek67b22602014-03-10 15:40:16 +0100499 }
Selim Cinek343e6e22014-04-11 21:23:30 +0200500 currentYPosition = childViewState.yTranslation + childHeight + mPaddingBetweenElements;
Selim Cinek67b22602014-03-10 15:40:16 +0100501 yPositionInScrollView = yPositionInScrollViewAfterElement;
Jorim Jaggi8c1a44b2014-04-29 19:04:02 +0200502
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700503 childViewState.yTranslation += ambientState.getTopPadding()
Selim Cineka59ecc32015-04-07 10:51:49 -0700504 + ambientState.getStackTranslation();
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700505 }
Selim Cineka4baaa32015-04-20 14:27:54 -0700506 updateHeadsUpStates(resultState, algorithmState, ambientState);
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700507 }
508
Selim Cineka4baaa32015-04-20 14:27:54 -0700509 private void updateHeadsUpStates(StackScrollState resultState,
510 StackScrollAlgorithmState algorithmState, AmbientState ambientState) {
511 int childCount = algorithmState.visibleChildren.size();
512 ExpandableNotificationRow topHeadsUpEntry = null;
513 for (int i = 0; i < childCount; i++) {
514 View child = algorithmState.visibleChildren.get(i);
515 if (!(child instanceof ExpandableNotificationRow)) {
516 break;
517 }
518 ExpandableNotificationRow row = (ExpandableNotificationRow) child;
519 if (!row.isHeadsUp()) {
520 break;
521 } else if (topHeadsUpEntry == null) {
522 topHeadsUpEntry = row;
523 }
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700524 StackViewState childState = resultState.getViewStateForView(row);
Selim Cinek1f3f5442015-04-10 17:54:46 -0700525 boolean isTopEntry = topHeadsUpEntry == row;
Selim Cinek131c1e22015-05-11 19:04:49 -0700526 if (mIsExpanded) {
Selim Cinek131c1e22015-05-11 19:04:49 -0700527 // Ensure that the heads up is always visible even when scrolled off from the bottom
528 float bottomPosition = ambientState.getMaxHeadsUpTranslation() - childState.height;
529 childState.yTranslation = Math.min(childState.yTranslation,
530 bottomPosition);
531 }
Selim Cinek684a4422015-04-15 16:18:39 -0700532 if (row.isPinned()) {
Selim Cinek79d79c42015-05-21 16:14:45 -0700533 childState.yTranslation = Math.max(childState.yTranslation,
534 mNotificationsTopPadding);
Selim Cinek31aada42015-12-18 17:51:15 -0800535 childState.height = Math.max(row.getIntrinsicHeight(), childState.height);
Selim Cinek1f3f5442015-04-10 17:54:46 -0700536 if (!isTopEntry) {
Selim Cinek684a4422015-04-15 16:18:39 -0700537 // Ensure that a headsUp doesn't vertically extend further than the heads-up at
538 // the top most z-position
Selim Cinek1f3f5442015-04-10 17:54:46 -0700539 StackViewState topState = resultState.getViewStateForView(topHeadsUpEntry);
Selim Cinek31aada42015-12-18 17:51:15 -0800540 childState.height = row.getIntrinsicHeight();
Selim Cineke53e6bb2015-04-13 16:14:26 -0700541 childState.yTranslation = topState.yTranslation + topState.height
542 - childState.height;
Selim Cinek1f3f5442015-04-10 17:54:46 -0700543 }
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700544 }
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700545 }
Selim Cinek67b22602014-03-10 15:40:16 +0100546 }
547
Selim Cinek1685e632014-04-08 02:27:49 +0200548 /**
Selim Cinek343e6e22014-04-11 21:23:30 +0200549 * Clamp the yTranslation both up and down to valid positions.
Selim Cinek1685e632014-04-08 02:27:49 +0200550 *
Selim Cinek1685e632014-04-08 02:27:49 +0200551 * @param childViewState the view state of the child
Selim Cinek3e881372015-12-10 15:13:42 -0800552 * @param minHeight the minimum height of this child
Selim Cinek1685e632014-04-08 02:27:49 +0200553 */
Selim Cinek3e881372015-12-10 15:13:42 -0800554 private void clampYTranslation(StackViewState childViewState, int minHeight,
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700555 AmbientState ambientState) {
Selim Cinek3e881372015-12-10 15:13:42 -0800556 clampPositionToBottomStackStart(childViewState, childViewState.height, minHeight,
557 ambientState);
558 clampPositionToTopStackEnd(childViewState, childViewState.height);
Selim Cinek343e6e22014-04-11 21:23:30 +0200559 }
560
561 /**
562 * Clamp the yTranslation of the child down such that its end is at most on the beginning of
563 * the bottom stack.
564 *
565 * @param childViewState the view state of the child
566 * @param childHeight the height of this child
Selim Cinek3e881372015-12-10 15:13:42 -0800567 * @param minHeight the minumum Height of the View
Selim Cinek343e6e22014-04-11 21:23:30 +0200568 */
Selim Cinekb036ca42015-02-20 15:56:28 +0100569 private void clampPositionToBottomStackStart(StackViewState childViewState,
Selim Cinek3e881372015-12-10 15:13:42 -0800570 int childHeight, int minHeight, AmbientState ambientState) {
571
572 int bottomStackStart = ambientState.getInnerHeight()
573 - mBottomStackPeekSize - mCollapseSecondCardPadding;
574 int childStart = bottomStackStart - childHeight;
575 if (childStart < childViewState.yTranslation) {
576 float newHeight = bottomStackStart - childViewState.yTranslation;
577 if (newHeight < minHeight) {
578 newHeight = minHeight;
579 childViewState.yTranslation = bottomStackStart - minHeight;
580 }
581 childViewState.height = (int) newHeight;
582 }
Selim Cinek343e6e22014-04-11 21:23:30 +0200583 }
584
585 /**
586 * 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 -0700587 * stack.
Selim Cinek343e6e22014-04-11 21:23:30 +0200588 *
589 * @param childViewState the view state of the child
590 * @param childHeight the height of this child
591 */
Selim Cinekb036ca42015-02-20 15:56:28 +0100592 private void clampPositionToTopStackEnd(StackViewState childViewState,
Selim Cinek343e6e22014-04-11 21:23:30 +0200593 int childHeight) {
594 childViewState.yTranslation = Math.max(childViewState.yTranslation,
Selim Cinek816c8e42015-11-19 12:00:45 -0800595 mFirstChildMinHeight - childHeight);
Selim Cinek1685e632014-04-08 02:27:49 +0200596 }
597
Selim Cinek31aada42015-12-18 17:51:15 -0800598 private int getMaxAllowedChildHeight(View child) {
599 if (child instanceof ExpandableView) {
Jorim Jaggibe565df2014-04-28 17:51:23 +0200600 ExpandableView expandableView = (ExpandableView) child;
Selim Cinek8d5727f2015-04-28 19:17:32 -0700601 return expandableView.getIntrinsicHeight();
Selim Cinek1685e632014-04-08 02:27:49 +0200602 }
Jorim Jaggibe565df2014-04-28 17:51:23 +0200603 return child == null? mCollapsedSize : child.getHeight();
Selim Cinek1685e632014-04-08 02:27:49 +0200604 }
605
Selim Cinek343e6e22014-04-11 21:23:30 +0200606 private void updateStateForChildTransitioningInBottom(StackScrollAlgorithmState algorithmState,
Selim Cinek816c8e42015-11-19 12:00:45 -0800607 float transitioningPositionStart, ExpandableView child, float currentYPosition,
Selim Cinekb036ca42015-02-20 15:56:28 +0100608 StackViewState childViewState, int childHeight) {
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200609
Selim Cinek343e6e22014-04-11 21:23:30 +0200610 // This is the transitioning element on top of bottom stack, calculate how far we are in.
611 algorithmState.partialInBottom = 1.0f - (
612 (transitioningPositionStart - currentYPosition) / (childHeight +
613 mPaddingBetweenElements));
614
615 // the offset starting at the transitionPosition of the bottom stack
616 float offset = mBottomStackIndentationFunctor.getValue(algorithmState.partialInBottom);
617 algorithmState.itemsInBottomStack += algorithmState.partialInBottom;
Selim Cinek3afd00e2014-08-11 22:32:57 +0200618 int newHeight = childHeight;
Selim Cinek3e881372015-12-10 15:13:42 -0800619 if (childHeight > child.getMinHeight()) {
Selim Cinek3afd00e2014-08-11 22:32:57 +0200620 newHeight = (int) Math.max(Math.min(transitioningPositionStart + offset -
Selim Cinek816c8e42015-11-19 12:00:45 -0800621 mPaddingBetweenElements - currentYPosition, childHeight),
622 child.getMinHeight());
Selim Cinek3afd00e2014-08-11 22:32:57 +0200623 childViewState.height = newHeight;
624 }
625 childViewState.yTranslation = transitioningPositionStart + offset - newHeight
Jorim Jaggife40f7d2014-04-28 15:20:04 +0200626 - mPaddingBetweenElements;
Selim Cinek3afd00e2014-08-11 22:32:57 +0200627
Selim Cinek343e6e22014-04-11 21:23:30 +0200628 // We want at least to be at the end of the top stack when collapsing
Selim Cinek3afd00e2014-08-11 22:32:57 +0200629 clampPositionToTopStackEnd(childViewState, newHeight);
Selim Cinekb036ca42015-02-20 15:56:28 +0100630 childViewState.location = StackViewState.LOCATION_MAIN_AREA;
Selim Cinek67b22602014-03-10 15:40:16 +0100631 }
632
Selim Cinek343e6e22014-04-11 21:23:30 +0200633 private void updateStateForChildFullyInBottomStack(StackScrollAlgorithmState algorithmState,
Selim Cinekb036ca42015-02-20 15:56:28 +0100634 float transitioningPositionStart, StackViewState childViewState,
Selim Cinek3e881372015-12-10 15:13:42 -0800635 int minHeight, AmbientState ambientState) {
Selim Cinek343e6e22014-04-11 21:23:30 +0200636 float currentYPosition;
Selim Cinek67b22602014-03-10 15:40:16 +0100637 algorithmState.itemsInBottomStack += 1.0f;
638 if (algorithmState.itemsInBottomStack < MAX_ITEMS_IN_BOTTOM_STACK) {
639 // We are visually entering the bottom stack
Selim Cinek343e6e22014-04-11 21:23:30 +0200640 currentYPosition = transitioningPositionStart
Jorim Jaggife40f7d2014-04-28 15:20:04 +0200641 + mBottomStackIndentationFunctor.getValue(algorithmState.itemsInBottomStack)
642 - mPaddingBetweenElements;
Selim Cinekb036ca42015-02-20 15:56:28 +0100643 childViewState.location = StackViewState.LOCATION_BOTTOM_STACK_PEEKING;
Selim Cinek67b22602014-03-10 15:40:16 +0100644 } else {
645 // we are fully inside the stack
646 if (algorithmState.itemsInBottomStack > MAX_ITEMS_IN_BOTTOM_STACK + 2) {
647 childViewState.alpha = 0.0f;
648 } else if (algorithmState.itemsInBottomStack
649 > MAX_ITEMS_IN_BOTTOM_STACK + 1) {
650 childViewState.alpha = 1.0f - algorithmState.partialInBottom;
651 }
Selim Cinekb036ca42015-02-20 15:56:28 +0100652 childViewState.location = StackViewState.LOCATION_BOTTOM_STACK_HIDDEN;
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700653 currentYPosition = ambientState.getInnerHeight();
Selim Cinek67b22602014-03-10 15:40:16 +0100654 }
Selim Cinek3e881372015-12-10 15:13:42 -0800655 childViewState.height = minHeight;
656 childViewState.yTranslation = currentYPosition - minHeight;
657 clampPositionToTopStackEnd(childViewState, minHeight);
Selim Cinek67b22602014-03-10 15:40:16 +0100658 }
659
Selim Cinek343e6e22014-04-11 21:23:30 +0200660 private void updateStateForTopStackChild(StackScrollAlgorithmState algorithmState,
661 int numberOfElementsCompletelyIn, int i, int childHeight,
Selim Cinekb036ca42015-02-20 15:56:28 +0100662 StackViewState childViewState, float scrollOffset) {
Selim Cinek67b22602014-03-10 15:40:16 +0100663
Selim Cinek67b22602014-03-10 15:40:16 +0100664
665 // First we calculate the index relative to the current stack window of size at most
666 // {@link #MAX_ITEMS_IN_TOP_STACK}
Selim Cinek343e6e22014-04-11 21:23:30 +0200667 int paddedIndex = i - 1
Selim Cinek67b22602014-03-10 15:40:16 +0100668 - Math.max(numberOfElementsCompletelyIn - MAX_ITEMS_IN_TOP_STACK, 0);
669 if (paddedIndex >= 0) {
Selim Cinek343e6e22014-04-11 21:23:30 +0200670
Selim Cinek67b22602014-03-10 15:40:16 +0100671 // We are currently visually entering the top stack
Selim Cinekad3e5af2014-07-04 12:24:11 +0200672 float distanceToStack = (childHeight + mPaddingBetweenElements)
673 - algorithmState.scrolledPixelsTop;
674 if (i == algorithmState.lastTopStackIndex
675 && distanceToStack > (mTopStackTotalSize + mPaddingBetweenElements)) {
Selim Cinek343e6e22014-04-11 21:23:30 +0200676
677 // Child is currently translating into stack but not yet inside slow down zone.
678 // Handle it like the regular scrollview.
679 childViewState.yTranslation = scrollOffset;
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200680 } else {
Selim Cinek343e6e22014-04-11 21:23:30 +0200681 // Apply stacking logic.
682 float numItemsBefore;
683 if (i == algorithmState.lastTopStackIndex) {
Selim Cinekad3e5af2014-07-04 12:24:11 +0200684 numItemsBefore = 1.0f
685 - (distanceToStack / (mTopStackTotalSize + mPaddingBetweenElements));
Selim Cinek343e6e22014-04-11 21:23:30 +0200686 } else {
687 numItemsBefore = algorithmState.itemsInTopStack - i;
688 }
689 // The end position of the current child
Selim Cinek816c8e42015-11-19 12:00:45 -0800690 float currentChildEndY = mFirstChildMinHeight + mTopStackTotalSize
Selim Cinekad3e5af2014-07-04 12:24:11 +0200691 - mTopStackIndentationFunctor.getValue(numItemsBefore);
Selim Cinek343e6e22014-04-11 21:23:30 +0200692 childViewState.yTranslation = currentChildEndY - childHeight;
Selim Cinek67b22602014-03-10 15:40:16 +0100693 }
Selim Cinekb036ca42015-02-20 15:56:28 +0100694 childViewState.location = StackViewState.LOCATION_TOP_STACK_PEEKING;
Selim Cinek67b22602014-03-10 15:40:16 +0100695 } else {
Selim Cinek343e6e22014-04-11 21:23:30 +0200696 if (paddedIndex == -1) {
697 childViewState.alpha = 1.0f - algorithmState.partialInTop;
698 } else {
699 // We are hidden behind the top card and faded out, so we can hide ourselves.
700 childViewState.alpha = 0.0f;
701 }
Selim Cinek816c8e42015-11-19 12:00:45 -0800702 childViewState.yTranslation = mFirstChildMinHeight - childHeight;
Selim Cinekb036ca42015-02-20 15:56:28 +0100703 childViewState.location = StackViewState.LOCATION_TOP_STACK_HIDDEN;
Selim Cinek67b22602014-03-10 15:40:16 +0100704 }
Selim Cinek343e6e22014-04-11 21:23:30 +0200705
706
Selim Cinek67b22602014-03-10 15:40:16 +0100707 }
708
709 /**
710 * Find the number of items in the top stack and update the result state if needed.
711 *
712 * @param resultState The result state to update if a height change of an child occurs
713 * @param algorithmState The state in which the current pass of the algorithm is currently in
Selim Cinek67b22602014-03-10 15:40:16 +0100714 */
715 private void findNumberOfItemsInTopStackAndUpdateState(StackScrollState resultState,
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700716 StackScrollAlgorithmState algorithmState, AmbientState ambientState) {
Selim Cinek67b22602014-03-10 15:40:16 +0100717
718 // The y Position if the element would be in a regular scrollView
719 float yPositionInScrollView = 0.0f;
Jorim Jaggid4a57442014-04-10 02:45:55 +0200720 int childCount = algorithmState.visibleChildren.size();
Selim Cinek67b22602014-03-10 15:40:16 +0100721
722 // find the number of elements in the top stack.
723 for (int i = 0; i < childCount; i++) {
Jorim Jaggibe565df2014-04-28 17:51:23 +0200724 ExpandableView child = algorithmState.visibleChildren.get(i);
Selim Cinekb036ca42015-02-20 15:56:28 +0100725 StackViewState childViewState = resultState.getViewStateForView(child);
Selim Cinek31aada42015-12-18 17:51:15 -0800726 int childHeight = getMaxAllowedChildHeight(child);
Selim Cinek67b22602014-03-10 15:40:16 +0100727 float yPositionInScrollViewAfterElement = yPositionInScrollView
728 + childHeight
729 + mPaddingBetweenElements;
730 if (yPositionInScrollView < algorithmState.scrollY) {
Selim Cinek816c8e42015-11-19 12:00:45 -0800731 if (i == 0 && algorithmState.scrollY <= mFirstChildMinHeight) {
Selim Cinek343e6e22014-04-11 21:23:30 +0200732
733 // The starting position of the bottom stack peek
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700734 int bottomPeekStart = ambientState.getInnerHeight() - mBottomStackPeekSize -
Selim Cinekd83771e2014-07-04 16:45:31 +0200735 mCollapseSecondCardPadding;
Selim Cinek343e6e22014-04-11 21:23:30 +0200736 // Collapse and expand the first child while the shade is being expanded
737 float maxHeight = mIsExpansionChanging && child == mFirstChildWhileExpanding
738 ? mFirstChildMaxHeight
739 : childHeight;
740 childViewState.height = (int) Math.max(Math.min(bottomPeekStart, maxHeight),
Selim Cinek816c8e42015-11-19 12:00:45 -0800741 mFirstChildMinHeight);
Selim Cinek343e6e22014-04-11 21:23:30 +0200742 algorithmState.itemsInTopStack = 1.0f;
743
744 } else if (yPositionInScrollViewAfterElement < algorithmState.scrollY) {
Selim Cinek67b22602014-03-10 15:40:16 +0100745 // According to the regular scroll view we are fully off screen
746 algorithmState.itemsInTopStack += 1.0f;
Selim Cinek343e6e22014-04-11 21:23:30 +0200747 if (i == 0) {
Selim Cinek816c8e42015-11-19 12:00:45 -0800748 childViewState.height = child.getMinHeight();
Selim Cinek67b22602014-03-10 15:40:16 +0100749 }
750 } else {
751 // According to the regular scroll view we are partially off screen
Selim Cinek343e6e22014-04-11 21:23:30 +0200752
Selim Cinek67b22602014-03-10 15:40:16 +0100753 // How much did we scroll into this child
Selim Cinekad3e5af2014-07-04 12:24:11 +0200754 algorithmState.scrolledPixelsTop = algorithmState.scrollY
755 - yPositionInScrollView;
Selim Cinek343e6e22014-04-11 21:23:30 +0200756 algorithmState.partialInTop = (algorithmState.scrolledPixelsTop) / (childHeight
Selim Cinek67b22602014-03-10 15:40:16 +0100757 + mPaddingBetweenElements);
758
759 // Our element can be expanded, so this can get negative
760 algorithmState.partialInTop = Math.max(0.0f, algorithmState.partialInTop);
761 algorithmState.itemsInTopStack += algorithmState.partialInTop;
Selim Cinekad3e5af2014-07-04 12:24:11 +0200762
Selim Cinek343e6e22014-04-11 21:23:30 +0200763 if (i == 0) {
Selim Cinekad3e5af2014-07-04 12:24:11 +0200764 // If it is expanded we have to collapse it to a new size
765 float newSize = yPositionInScrollViewAfterElement
766 - mPaddingBetweenElements
Selim Cinek816c8e42015-11-19 12:00:45 -0800767 - algorithmState.scrollY + mFirstChildMinHeight;
768 newSize = Math.max(mFirstChildMinHeight, newSize);
Selim Cinek4e456be2014-06-12 18:09:43 +0200769 algorithmState.itemsInTopStack = 1.0f;
Selim Cinek67b22602014-03-10 15:40:16 +0100770 childViewState.height = (int) newSize;
Selim Cinek67b22602014-03-10 15:40:16 +0100771 }
Selim Cinek343e6e22014-04-11 21:23:30 +0200772 algorithmState.lastTopStackIndex = i;
773 break;
Selim Cinek67b22602014-03-10 15:40:16 +0100774 }
775 } else {
Selim Cinek343e6e22014-04-11 21:23:30 +0200776 algorithmState.lastTopStackIndex = i - 1;
Selim Cinek67b22602014-03-10 15:40:16 +0100777 // We are already past the stack so we can end the loop
778 break;
779 }
780 yPositionInScrollView = yPositionInScrollViewAfterElement;
781 }
782 }
783
784 /**
785 * Calculate the Z positions for all children based on the number of items in both stacks and
786 * save it in the resultState
787 *
788 * @param resultState The result state to update the zTranslation values
789 * @param algorithmState The state in which the current pass of the algorithm is currently in
790 */
791 private void updateZValuesForState(StackScrollState resultState,
792 StackScrollAlgorithmState algorithmState) {
Jorim Jaggid4a57442014-04-10 02:45:55 +0200793 int childCount = algorithmState.visibleChildren.size();
Selim Cinek67b22602014-03-10 15:40:16 +0100794 for (int i = 0; i < childCount; i++) {
Jorim Jaggid4a57442014-04-10 02:45:55 +0200795 View child = algorithmState.visibleChildren.get(i);
Selim Cinekb036ca42015-02-20 15:56:28 +0100796 StackViewState childViewState = resultState.getViewStateForView(child);
Selim Cinek67b22602014-03-10 15:40:16 +0100797 if (i < algorithmState.itemsInTopStack) {
798 float stackIndex = algorithmState.itemsInTopStack - i;
Selim Cinekc5baa3e2014-10-29 19:04:19 +0100799
800 // Ensure that the topmost item is a little bit higher than the rest when fully
801 // scrolled, to avoid drawing errors when swiping it out
802 float max = MAX_ITEMS_IN_TOP_STACK + (i == 0 ? 2.5f : 2);
803 stackIndex = Math.min(stackIndex, max);
Selim Cinek4e456be2014-06-12 18:09:43 +0200804 if (i == 0 && algorithmState.itemsInTopStack < 2.0f) {
805
806 // We only have the top item and an additional item in the top stack,
807 // Interpolate the index from 0 to 2 while the second item is
808 // translating in.
809 stackIndex -= 1.0f;
Selim Cinek816c8e42015-11-19 12:00:45 -0800810 if (algorithmState.scrollY > mFirstChildMinHeight) {
Selim Cinek4e456be2014-06-12 18:09:43 +0200811
812 // Since there is a shadow treshhold, we cant just interpolate from 0 to
813 // 2 but we interpolate from 0.1f to 2.0f when scrolled in. The jump in
814 // height will not be noticable since we have padding in between.
815 stackIndex = 0.1f + stackIndex * 1.9f;
816 }
817 }
Selim Cinek67b22602014-03-10 15:40:16 +0100818 childViewState.zTranslation = mZBasicHeight
819 + stackIndex * mZDistanceBetweenElements;
820 } else if (i > (childCount - 1 - algorithmState.itemsInBottomStack)) {
821 float numItemsAbove = i - (childCount - 1 - algorithmState.itemsInBottomStack);
822 float translationZ = mZBasicHeight
823 - numItemsAbove * mZDistanceBetweenElements;
824 childViewState.zTranslation = translationZ;
825 } else {
826 childViewState.zTranslation = mZBasicHeight;
827 }
828 }
829 }
830
Selim Cinek1685e632014-04-08 02:27:49 +0200831 public void onExpansionStarted(StackScrollState currentState) {
832 mIsExpansionChanging = true;
833 mExpandedOnStart = mIsExpanded;
834 ViewGroup hostView = currentState.getHostView();
835 updateFirstChildHeightWhileExpanding(hostView);
836 }
837
838 private void updateFirstChildHeightWhileExpanding(ViewGroup hostView) {
Jorim Jaggibe565df2014-04-28 17:51:23 +0200839 mFirstChildWhileExpanding = (ExpandableView) findFirstVisibleChild(hostView);
Jorim Jaggi9f347ae2014-04-11 00:47:18 +0200840 if (mFirstChildWhileExpanding != null) {
Selim Cinek1685e632014-04-08 02:27:49 +0200841 if (mExpandedOnStart) {
842
843 // We are collapsing the shade, so the first child can get as most as high as the
Selim Cinek02af41e2014-10-14 15:46:43 +0200844 // current height or the end value of the animation.
845 mFirstChildMaxHeight = StackStateAnimator.getFinalActualHeight(
846 mFirstChildWhileExpanding);
Selim Cinek1685e632014-04-08 02:27:49 +0200847 } else {
Selim Cinek31094df2014-08-14 19:28:15 +0200848 updateFirstChildMaxSizeToMaxHeight();
Selim Cinek1685e632014-04-08 02:27:49 +0200849 }
850 } else {
Selim Cinek1685e632014-04-08 02:27:49 +0200851 mFirstChildMaxHeight = 0;
852 }
853 }
854
Selim Cinek31094df2014-08-14 19:28:15 +0200855 private void updateFirstChildMaxSizeToMaxHeight() {
856 // We are expanding the shade, expand it to its full height.
857 if (!isMaxSizeInitialized(mFirstChildWhileExpanding)) {
858
859 // This child was not layouted yet, wait for a layout pass
860 mFirstChildWhileExpanding
861 .addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
862 @Override
863 public void onLayoutChange(View v, int left, int top, int right,
864 int bottom, int oldLeft, int oldTop, int oldRight,
865 int oldBottom) {
866 if (mFirstChildWhileExpanding != null) {
867 mFirstChildMaxHeight = getMaxAllowedChildHeight(
Selim Cinek31aada42015-12-18 17:51:15 -0800868 mFirstChildWhileExpanding);
Selim Cinek31094df2014-08-14 19:28:15 +0200869 } else {
870 mFirstChildMaxHeight = 0;
871 }
872 v.removeOnLayoutChangeListener(this);
873 }
874 });
875 } else {
Selim Cinek31aada42015-12-18 17:51:15 -0800876 mFirstChildMaxHeight = getMaxAllowedChildHeight(mFirstChildWhileExpanding);
Selim Cinek31094df2014-08-14 19:28:15 +0200877 }
878 }
879
Selim Cinek7d447722014-06-10 15:51:59 +0200880 private boolean isMaxSizeInitialized(ExpandableView child) {
881 if (child instanceof ExpandableNotificationRow) {
882 ExpandableNotificationRow row = (ExpandableNotificationRow) child;
Selim Cinek31094df2014-08-14 19:28:15 +0200883 return row.isMaxExpandHeightInitialized();
Selim Cinek7d447722014-06-10 15:51:59 +0200884 }
885 return child == null || child.getWidth() != 0;
886 }
887
Jorim Jaggi9f347ae2014-04-11 00:47:18 +0200888 private View findFirstVisibleChild(ViewGroup container) {
889 int childCount = container.getChildCount();
890 for (int i = 0; i < childCount; i++) {
891 View child = container.getChildAt(i);
892 if (child.getVisibility() != View.GONE) {
893 return child;
894 }
895 }
896 return null;
897 }
898
Selim Cinek1685e632014-04-08 02:27:49 +0200899 public void onExpansionStopped() {
900 mIsExpansionChanging = false;
901 mFirstChildWhileExpanding = null;
902 }
903
904 public void setIsExpanded(boolean isExpanded) {
905 this.mIsExpanded = isExpanded;
906 }
907
Selim Cinekb55386d2015-12-16 17:26:49 -0800908 public void notifyChildrenChanged(final NotificationStackScrollLayout hostView) {
909 mFirstChild = hostView.getFirstChildNotGone();
Selim Cinek1685e632014-04-08 02:27:49 +0200910 if (mIsExpansionChanging) {
Selim Cinek02af41e2014-10-14 15:46:43 +0200911 hostView.post(new Runnable() {
912 @Override
913 public void run() {
914 updateFirstChildHeightWhileExpanding(hostView);
915 }
916 });
Selim Cinek1685e632014-04-08 02:27:49 +0200917 }
918 }
919
Selim Cinek34c0a8d2014-05-12 00:01:43 +0200920 public void setDimmed(boolean dimmed) {
Selim Cinekaf0dc312015-12-15 17:01:44 -0800921 mDimmed = dimmed;
922 updatePadding();
Selim Cinek34c0a8d2014-05-12 00:01:43 +0200923 }
924
Selim Cinek31094df2014-08-14 19:28:15 +0200925 public void onReset(ExpandableView view) {
926 if (view.equals(mFirstChildWhileExpanding)) {
927 updateFirstChildMaxSizeToMaxHeight();
928 }
929 }
930
Selim Cinek67b22602014-03-10 15:40:16 +0100931 class StackScrollAlgorithmState {
932
933 /**
934 * The scroll position of the algorithm
935 */
936 public int scrollY;
937
938 /**
939 * The quantity of items which are in the top stack.
940 */
941 public float itemsInTopStack;
942
943 /**
944 * how far in is the element currently transitioning into the top stack
945 */
946 public float partialInTop;
947
948 /**
Selim Cinek343e6e22014-04-11 21:23:30 +0200949 * The number of pixels the last child in the top stack has scrolled in to the stack
950 */
951 public float scrolledPixelsTop;
952
953 /**
Selim Cinek67b22602014-03-10 15:40:16 +0100954 * The last item index which is in the top stack.
Selim Cinek67b22602014-03-10 15:40:16 +0100955 */
956 public int lastTopStackIndex;
957
958 /**
959 * The quantity of items which are in the bottom stack.
960 */
961 public float itemsInBottomStack;
962
963 /**
964 * how far in is the element currently transitioning into the bottom stack
965 */
966 public float partialInBottom;
Jorim Jaggid4a57442014-04-10 02:45:55 +0200967
968 /**
969 * The children from the host view which are not gone.
970 */
Jorim Jaggibe565df2014-04-28 17:51:23 +0200971 public final ArrayList<ExpandableView> visibleChildren = new ArrayList<ExpandableView>();
Selim Cinek67b22602014-03-10 15:40:16 +0100972 }
973
974}