blob: 5496963fba191ef0fc0dbbc667070c438719985c [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
Selim Cineka59ecc32015-04-07 10:51:49 -0700413 // If we have a heads-up higher than the collapsed height we need to add the difference to
414 // the padding of all other elements, i.e push in the top stack slightly.
415 ExpandableNotificationRow topHeadsUpEntry = ambientState.getTopHeadsUpEntry();
416
Jorim Jaggid4a57442014-04-10 02:45:55 +0200417 int childCount = algorithmState.visibleChildren.size();
Selim Cinekf92a1fd2015-07-31 16:10:32 -0700418 int numberOfElementsCompletelyIn = algorithmState.partialInTop == 1.0f
419 ? algorithmState.lastTopStackIndex
420 : (int) algorithmState.itemsInTopStack;
Selim Cinek67b22602014-03-10 15:40:16 +0100421 for (int i = 0; i < childCount; i++) {
Jorim Jaggibe565df2014-04-28 17:51:23 +0200422 ExpandableView child = algorithmState.visibleChildren.get(i);
Selim Cinekb036ca42015-02-20 15:56:28 +0100423 StackViewState childViewState = resultState.getViewStateForView(child);
424 childViewState.location = StackViewState.LOCATION_UNKNOWN;
Selim Cineka59ecc32015-04-07 10:51:49 -0700425 int childHeight = getMaxAllowedChildHeight(child, ambientState);
Selim Cinek3e881372015-12-10 15:13:42 -0800426 int minHeight = child.getMinHeight();
Selim Cinek67b22602014-03-10 15:40:16 +0100427 float yPositionInScrollViewAfterElement = yPositionInScrollView
428 + childHeight
429 + mPaddingBetweenElements;
Selim Cinek816c8e42015-11-19 12:00:45 -0800430 float scrollOffset = yPositionInScrollView - algorithmState.scrollY +
431 mFirstChildMinHeight;
Selim Cinek343e6e22014-04-11 21:23:30 +0200432
433 if (i == algorithmState.lastTopStackIndex + 1) {
434 // Normally the position of this child is the position in the regular scrollview,
435 // but if the two stacks are very close to each other,
436 // then have have to push it even more upwards to the position of the bottom
437 // stack start.
438 currentYPosition = Math.min(scrollOffset, bottomStackStart);
439 }
440 childViewState.yTranslation = currentYPosition;
441
442 // The y position after this element
443 float nextYPosition = currentYPosition + childHeight +
444 mPaddingBetweenElements;
445
446 if (i <= algorithmState.lastTopStackIndex) {
Selim Cinek67b22602014-03-10 15:40:16 +0100447 // Case 1:
448 // We are in the top Stack
Selim Cinek343e6e22014-04-11 21:23:30 +0200449 updateStateForTopStackChild(algorithmState,
450 numberOfElementsCompletelyIn, i, childHeight, childViewState, scrollOffset);
Selim Cinek3afd00e2014-08-11 22:32:57 +0200451 clampPositionToTopStackEnd(childViewState, childHeight);
452
Selim Cinek343e6e22014-04-11 21:23:30 +0200453 // check if we are overlapping with the bottom stack
454 if (childViewState.yTranslation + childHeight + mPaddingBetweenElements
Selim Cinek3e881372015-12-10 15:13:42 -0800455 >= bottomStackStart && !mIsExpansionChanging && i != 0) {
Selim Cinek3afd00e2014-08-11 22:32:57 +0200456 // we just collapse this element slightly
457 int newSize = (int) Math.max(bottomStackStart - mPaddingBetweenElements -
Selim Cinek3e881372015-12-10 15:13:42 -0800458 childViewState.yTranslation, minHeight);
Selim Cinek3afd00e2014-08-11 22:32:57 +0200459 childViewState.height = newSize;
Selim Cinek4581cf82014-08-12 12:40:32 +0200460 updateStateForChildTransitioningInBottom(algorithmState, bottomStackStart,
Selim Cinek816c8e42015-11-19 12:00:45 -0800461 child, childViewState.yTranslation, childViewState,
Selim Cinek4581cf82014-08-12 12:40:32 +0200462 childHeight);
Selim Cinek343e6e22014-04-11 21:23:30 +0200463 }
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700464 clampPositionToBottomStackStart(childViewState, childViewState.height,
Selim Cinek3e881372015-12-10 15:13:42 -0800465 minHeight, ambientState);
Selim Cinek343e6e22014-04-11 21:23:30 +0200466 } else if (nextYPosition >= bottomStackStart) {
Selim Cinek67b22602014-03-10 15:40:16 +0100467 // Case 2:
Selim Cinek343e6e22014-04-11 21:23:30 +0200468 // We are in the bottom stack.
469 if (currentYPosition >= bottomStackStart) {
Selim Cinek67b22602014-03-10 15:40:16 +0100470 // According to the regular scroll view we are fully translated out of the
471 // bottom of the screen so we are fully in the bottom stack
Selim Cinek343e6e22014-04-11 21:23:30 +0200472 updateStateForChildFullyInBottomStack(algorithmState,
Selim Cinek3e881372015-12-10 15:13:42 -0800473 bottomStackStart, childViewState, minHeight, ambientState);
Selim Cinek67b22602014-03-10 15:40:16 +0100474 } else {
Selim Cinek67b22602014-03-10 15:40:16 +0100475 // According to the regular scroll view we are currently translating out of /
476 // into the bottom of the screen
Selim Cinek343e6e22014-04-11 21:23:30 +0200477 updateStateForChildTransitioningInBottom(algorithmState,
Selim Cinek816c8e42015-11-19 12:00:45 -0800478 bottomStackStart, child, currentYPosition,
Selim Cinek343e6e22014-04-11 21:23:30 +0200479 childViewState, childHeight);
Selim Cinek67b22602014-03-10 15:40:16 +0100480 }
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200481 } else {
Selim Cinek343e6e22014-04-11 21:23:30 +0200482 // Case 3:
483 // We are in the regular scroll area.
Selim Cinekb036ca42015-02-20 15:56:28 +0100484 childViewState.location = StackViewState.LOCATION_MAIN_AREA;
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700485 clampYTranslation(childViewState, childHeight, ambientState);
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200486 }
Selim Cinek343e6e22014-04-11 21:23:30 +0200487
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200488 // The first card is always rendered.
489 if (i == 0) {
490 childViewState.alpha = 1.0f;
Selim Cinek816c8e42015-11-19 12:00:45 -0800491 childViewState.yTranslation = Math.max(
492 mFirstChildMinHeight - algorithmState.scrollY, 0);
Selim Cinekd83771e2014-07-04 16:45:31 +0200493 if (childViewState.yTranslation + childViewState.height
494 > bottomPeekStart - mCollapseSecondCardPadding) {
Selim Cinek4fe3e472014-07-03 16:32:54 +0200495 childViewState.height = (int) Math.max(
Selim Cinekd83771e2014-07-04 16:45:31 +0200496 bottomPeekStart - mCollapseSecondCardPadding
Selim Cinek816c8e42015-11-19 12:00:45 -0800497 - childViewState.yTranslation, mFirstChildMinHeight);
Selim Cinek4fe3e472014-07-03 16:32:54 +0200498 }
Selim Cinekb036ca42015-02-20 15:56:28 +0100499 childViewState.location = StackViewState.LOCATION_FIRST_CARD;
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200500 }
Selim Cinekb036ca42015-02-20 15:56:28 +0100501 if (childViewState.location == StackViewState.LOCATION_UNKNOWN) {
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200502 Log.wtf(LOG_TAG, "Failed to assign location for child " + i);
Selim Cinek67b22602014-03-10 15:40:16 +0100503 }
Selim Cinek343e6e22014-04-11 21:23:30 +0200504 currentYPosition = childViewState.yTranslation + childHeight + mPaddingBetweenElements;
Selim Cinek67b22602014-03-10 15:40:16 +0100505 yPositionInScrollView = yPositionInScrollViewAfterElement;
Jorim Jaggi8c1a44b2014-04-29 19:04:02 +0200506
Selim Cineka59ecc32015-04-07 10:51:49 -0700507 if (ambientState.isShadeExpanded() && topHeadsUpEntry != null
508 && child != topHeadsUpEntry) {
Selim Cinek816c8e42015-11-19 12:00:45 -0800509 childViewState.yTranslation += topHeadsUpEntry.getHeadsUpHeight() -
510 mFirstChildMinHeight;
Selim Cineka59ecc32015-04-07 10:51:49 -0700511 }
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700512 childViewState.yTranslation += ambientState.getTopPadding()
Selim Cineka59ecc32015-04-07 10:51:49 -0700513 + ambientState.getStackTranslation();
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700514 }
Selim Cineka4baaa32015-04-20 14:27:54 -0700515 updateHeadsUpStates(resultState, algorithmState, ambientState);
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700516 }
517
Selim Cineka4baaa32015-04-20 14:27:54 -0700518 private void updateHeadsUpStates(StackScrollState resultState,
519 StackScrollAlgorithmState algorithmState, AmbientState ambientState) {
520 int childCount = algorithmState.visibleChildren.size();
521 ExpandableNotificationRow topHeadsUpEntry = null;
522 for (int i = 0; i < childCount; i++) {
523 View child = algorithmState.visibleChildren.get(i);
524 if (!(child instanceof ExpandableNotificationRow)) {
525 break;
526 }
527 ExpandableNotificationRow row = (ExpandableNotificationRow) child;
528 if (!row.isHeadsUp()) {
529 break;
530 } else if (topHeadsUpEntry == null) {
531 topHeadsUpEntry = row;
532 }
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700533 StackViewState childState = resultState.getViewStateForView(row);
Selim Cinek1f3f5442015-04-10 17:54:46 -0700534 boolean isTopEntry = topHeadsUpEntry == row;
Selim Cinek131c1e22015-05-11 19:04:49 -0700535 if (mIsExpanded) {
536 if (isTopEntry) {
Selim Cinek816c8e42015-11-19 12:00:45 -0800537 childState.height += row.getHeadsUpHeight() - mFirstChildMinHeight;
Selim Cinek131c1e22015-05-11 19:04:49 -0700538 }
539 childState.height = Math.max(childState.height, row.getHeadsUpHeight());
540 // Ensure that the heads up is always visible even when scrolled off from the bottom
541 float bottomPosition = ambientState.getMaxHeadsUpTranslation() - childState.height;
542 childState.yTranslation = Math.min(childState.yTranslation,
543 bottomPosition);
544 }
Selim Cinek684a4422015-04-15 16:18:39 -0700545 if (row.isPinned()) {
Selim Cinek79d79c42015-05-21 16:14:45 -0700546 childState.yTranslation = Math.max(childState.yTranslation,
547 mNotificationsTopPadding);
Selim Cinek496126a2015-12-11 17:02:17 -0800548 childState.height = Math.max(row.getHeadsUpHeight(), childState.height);
Selim Cinek1f3f5442015-04-10 17:54:46 -0700549 if (!isTopEntry) {
Selim Cinek684a4422015-04-15 16:18:39 -0700550 // Ensure that a headsUp doesn't vertically extend further than the heads-up at
551 // the top most z-position
Selim Cinek1f3f5442015-04-10 17:54:46 -0700552 StackViewState topState = resultState.getViewStateForView(topHeadsUpEntry);
553 childState.height = row.getHeadsUpHeight();
Selim Cineke53e6bb2015-04-13 16:14:26 -0700554 childState.yTranslation = topState.yTranslation + topState.height
555 - childState.height;
Selim Cinek1f3f5442015-04-10 17:54:46 -0700556 }
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700557 }
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700558 }
Selim Cinek67b22602014-03-10 15:40:16 +0100559 }
560
Selim Cinek1685e632014-04-08 02:27:49 +0200561 /**
Selim Cinek343e6e22014-04-11 21:23:30 +0200562 * Clamp the yTranslation both up and down to valid positions.
Selim Cinek1685e632014-04-08 02:27:49 +0200563 *
Selim Cinek1685e632014-04-08 02:27:49 +0200564 * @param childViewState the view state of the child
Selim Cinek3e881372015-12-10 15:13:42 -0800565 * @param minHeight the minimum height of this child
Selim Cinek1685e632014-04-08 02:27:49 +0200566 */
Selim Cinek3e881372015-12-10 15:13:42 -0800567 private void clampYTranslation(StackViewState childViewState, int minHeight,
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700568 AmbientState ambientState) {
Selim Cinek3e881372015-12-10 15:13:42 -0800569 clampPositionToBottomStackStart(childViewState, childViewState.height, minHeight,
570 ambientState);
571 clampPositionToTopStackEnd(childViewState, childViewState.height);
Selim Cinek343e6e22014-04-11 21:23:30 +0200572 }
573
574 /**
575 * Clamp the yTranslation of the child down such that its end is at most on the beginning of
576 * the bottom stack.
577 *
578 * @param childViewState the view state of the child
579 * @param childHeight the height of this child
Selim Cinek3e881372015-12-10 15:13:42 -0800580 * @param minHeight the minumum Height of the View
Selim Cinek343e6e22014-04-11 21:23:30 +0200581 */
Selim Cinekb036ca42015-02-20 15:56:28 +0100582 private void clampPositionToBottomStackStart(StackViewState childViewState,
Selim Cinek3e881372015-12-10 15:13:42 -0800583 int childHeight, int minHeight, AmbientState ambientState) {
584
585 int bottomStackStart = ambientState.getInnerHeight()
586 - mBottomStackPeekSize - mCollapseSecondCardPadding;
587 int childStart = bottomStackStart - childHeight;
588 if (childStart < childViewState.yTranslation) {
589 float newHeight = bottomStackStart - childViewState.yTranslation;
590 if (newHeight < minHeight) {
591 newHeight = minHeight;
592 childViewState.yTranslation = bottomStackStart - minHeight;
593 }
594 childViewState.height = (int) newHeight;
595 }
Selim Cinek343e6e22014-04-11 21:23:30 +0200596 }
597
598 /**
599 * 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 -0700600 * stack.
Selim Cinek343e6e22014-04-11 21:23:30 +0200601 *
602 * @param childViewState the view state of the child
603 * @param childHeight the height of this child
604 */
Selim Cinekb036ca42015-02-20 15:56:28 +0100605 private void clampPositionToTopStackEnd(StackViewState childViewState,
Selim Cinek343e6e22014-04-11 21:23:30 +0200606 int childHeight) {
607 childViewState.yTranslation = Math.max(childViewState.yTranslation,
Selim Cinek816c8e42015-11-19 12:00:45 -0800608 mFirstChildMinHeight - childHeight);
Selim Cinek1685e632014-04-08 02:27:49 +0200609 }
610
Selim Cineka59ecc32015-04-07 10:51:49 -0700611 private int getMaxAllowedChildHeight(View child, AmbientState ambientState) {
Selim Cinek1685e632014-04-08 02:27:49 +0200612 if (child instanceof ExpandableNotificationRow) {
613 ExpandableNotificationRow row = (ExpandableNotificationRow) child;
Selim Cinekd2281152015-04-10 14:37:46 -0700614 if (ambientState == null && row.isHeadsUp()
615 || ambientState != null && ambientState.getTopHeadsUpEntry() == child) {
Selim Cinek8d490d42015-04-10 00:05:50 -0700616 int extraSize = row.getIntrinsicHeight() - row.getHeadsUpHeight();
Selim Cinek816c8e42015-11-19 12:00:45 -0800617 return mFirstChildMinHeight + extraSize;
Selim Cineka59ecc32015-04-07 10:51:49 -0700618 }
Jorim Jaggi9cbadd32014-05-01 20:18:31 +0200619 return row.getIntrinsicHeight();
Jorim Jaggibe565df2014-04-28 17:51:23 +0200620 } else if (child instanceof ExpandableView) {
621 ExpandableView expandableView = (ExpandableView) child;
Selim Cinek8d5727f2015-04-28 19:17:32 -0700622 return expandableView.getIntrinsicHeight();
Selim Cinek1685e632014-04-08 02:27:49 +0200623 }
Jorim Jaggibe565df2014-04-28 17:51:23 +0200624 return child == null? mCollapsedSize : child.getHeight();
Selim Cinek1685e632014-04-08 02:27:49 +0200625 }
626
Selim Cinek343e6e22014-04-11 21:23:30 +0200627 private void updateStateForChildTransitioningInBottom(StackScrollAlgorithmState algorithmState,
Selim Cinek816c8e42015-11-19 12:00:45 -0800628 float transitioningPositionStart, ExpandableView child, float currentYPosition,
Selim Cinekb036ca42015-02-20 15:56:28 +0100629 StackViewState childViewState, int childHeight) {
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200630
Selim Cinek343e6e22014-04-11 21:23:30 +0200631 // This is the transitioning element on top of bottom stack, calculate how far we are in.
632 algorithmState.partialInBottom = 1.0f - (
633 (transitioningPositionStart - currentYPosition) / (childHeight +
634 mPaddingBetweenElements));
635
636 // the offset starting at the transitionPosition of the bottom stack
637 float offset = mBottomStackIndentationFunctor.getValue(algorithmState.partialInBottom);
638 algorithmState.itemsInBottomStack += algorithmState.partialInBottom;
Selim Cinek3afd00e2014-08-11 22:32:57 +0200639 int newHeight = childHeight;
Selim Cinek3e881372015-12-10 15:13:42 -0800640 if (childHeight > child.getMinHeight()) {
Selim Cinek3afd00e2014-08-11 22:32:57 +0200641 newHeight = (int) Math.max(Math.min(transitioningPositionStart + offset -
Selim Cinek816c8e42015-11-19 12:00:45 -0800642 mPaddingBetweenElements - currentYPosition, childHeight),
643 child.getMinHeight());
Selim Cinek3afd00e2014-08-11 22:32:57 +0200644 childViewState.height = newHeight;
645 }
646 childViewState.yTranslation = transitioningPositionStart + offset - newHeight
Jorim Jaggife40f7d2014-04-28 15:20:04 +0200647 - mPaddingBetweenElements;
Selim Cinek3afd00e2014-08-11 22:32:57 +0200648
Selim Cinek343e6e22014-04-11 21:23:30 +0200649 // We want at least to be at the end of the top stack when collapsing
Selim Cinek3afd00e2014-08-11 22:32:57 +0200650 clampPositionToTopStackEnd(childViewState, newHeight);
Selim Cinekb036ca42015-02-20 15:56:28 +0100651 childViewState.location = StackViewState.LOCATION_MAIN_AREA;
Selim Cinek67b22602014-03-10 15:40:16 +0100652 }
653
Selim Cinek343e6e22014-04-11 21:23:30 +0200654 private void updateStateForChildFullyInBottomStack(StackScrollAlgorithmState algorithmState,
Selim Cinekb036ca42015-02-20 15:56:28 +0100655 float transitioningPositionStart, StackViewState childViewState,
Selim Cinek3e881372015-12-10 15:13:42 -0800656 int minHeight, AmbientState ambientState) {
Selim Cinek343e6e22014-04-11 21:23:30 +0200657 float currentYPosition;
Selim Cinek67b22602014-03-10 15:40:16 +0100658 algorithmState.itemsInBottomStack += 1.0f;
659 if (algorithmState.itemsInBottomStack < MAX_ITEMS_IN_BOTTOM_STACK) {
660 // We are visually entering the bottom stack
Selim Cinek343e6e22014-04-11 21:23:30 +0200661 currentYPosition = transitioningPositionStart
Jorim Jaggife40f7d2014-04-28 15:20:04 +0200662 + mBottomStackIndentationFunctor.getValue(algorithmState.itemsInBottomStack)
663 - mPaddingBetweenElements;
Selim Cinekb036ca42015-02-20 15:56:28 +0100664 childViewState.location = StackViewState.LOCATION_BOTTOM_STACK_PEEKING;
Selim Cinek67b22602014-03-10 15:40:16 +0100665 } else {
666 // we are fully inside the stack
667 if (algorithmState.itemsInBottomStack > MAX_ITEMS_IN_BOTTOM_STACK + 2) {
668 childViewState.alpha = 0.0f;
669 } else if (algorithmState.itemsInBottomStack
670 > MAX_ITEMS_IN_BOTTOM_STACK + 1) {
671 childViewState.alpha = 1.0f - algorithmState.partialInBottom;
672 }
Selim Cinekb036ca42015-02-20 15:56:28 +0100673 childViewState.location = StackViewState.LOCATION_BOTTOM_STACK_HIDDEN;
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700674 currentYPosition = ambientState.getInnerHeight();
Selim Cinek67b22602014-03-10 15:40:16 +0100675 }
Selim Cinek3e881372015-12-10 15:13:42 -0800676 childViewState.height = minHeight;
677 childViewState.yTranslation = currentYPosition - minHeight;
678 clampPositionToTopStackEnd(childViewState, minHeight);
Selim Cinek67b22602014-03-10 15:40:16 +0100679 }
680
Selim Cinek343e6e22014-04-11 21:23:30 +0200681 private void updateStateForTopStackChild(StackScrollAlgorithmState algorithmState,
682 int numberOfElementsCompletelyIn, int i, int childHeight,
Selim Cinekb036ca42015-02-20 15:56:28 +0100683 StackViewState childViewState, float scrollOffset) {
Selim Cinek67b22602014-03-10 15:40:16 +0100684
Selim Cinek67b22602014-03-10 15:40:16 +0100685
686 // First we calculate the index relative to the current stack window of size at most
687 // {@link #MAX_ITEMS_IN_TOP_STACK}
Selim Cinek343e6e22014-04-11 21:23:30 +0200688 int paddedIndex = i - 1
Selim Cinek67b22602014-03-10 15:40:16 +0100689 - Math.max(numberOfElementsCompletelyIn - MAX_ITEMS_IN_TOP_STACK, 0);
690 if (paddedIndex >= 0) {
Selim Cinek343e6e22014-04-11 21:23:30 +0200691
Selim Cinek67b22602014-03-10 15:40:16 +0100692 // We are currently visually entering the top stack
Selim Cinekad3e5af2014-07-04 12:24:11 +0200693 float distanceToStack = (childHeight + mPaddingBetweenElements)
694 - algorithmState.scrolledPixelsTop;
695 if (i == algorithmState.lastTopStackIndex
696 && distanceToStack > (mTopStackTotalSize + mPaddingBetweenElements)) {
Selim Cinek343e6e22014-04-11 21:23:30 +0200697
698 // Child is currently translating into stack but not yet inside slow down zone.
699 // Handle it like the regular scrollview.
700 childViewState.yTranslation = scrollOffset;
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200701 } else {
Selim Cinek343e6e22014-04-11 21:23:30 +0200702 // Apply stacking logic.
703 float numItemsBefore;
704 if (i == algorithmState.lastTopStackIndex) {
Selim Cinekad3e5af2014-07-04 12:24:11 +0200705 numItemsBefore = 1.0f
706 - (distanceToStack / (mTopStackTotalSize + mPaddingBetweenElements));
Selim Cinek343e6e22014-04-11 21:23:30 +0200707 } else {
708 numItemsBefore = algorithmState.itemsInTopStack - i;
709 }
710 // The end position of the current child
Selim Cinek816c8e42015-11-19 12:00:45 -0800711 float currentChildEndY = mFirstChildMinHeight + mTopStackTotalSize
Selim Cinekad3e5af2014-07-04 12:24:11 +0200712 - mTopStackIndentationFunctor.getValue(numItemsBefore);
Selim Cinek343e6e22014-04-11 21:23:30 +0200713 childViewState.yTranslation = currentChildEndY - childHeight;
Selim Cinek67b22602014-03-10 15:40:16 +0100714 }
Selim Cinekb036ca42015-02-20 15:56:28 +0100715 childViewState.location = StackViewState.LOCATION_TOP_STACK_PEEKING;
Selim Cinek67b22602014-03-10 15:40:16 +0100716 } else {
Selim Cinek343e6e22014-04-11 21:23:30 +0200717 if (paddedIndex == -1) {
718 childViewState.alpha = 1.0f - algorithmState.partialInTop;
719 } else {
720 // We are hidden behind the top card and faded out, so we can hide ourselves.
721 childViewState.alpha = 0.0f;
722 }
Selim Cinek816c8e42015-11-19 12:00:45 -0800723 childViewState.yTranslation = mFirstChildMinHeight - childHeight;
Selim Cinekb036ca42015-02-20 15:56:28 +0100724 childViewState.location = StackViewState.LOCATION_TOP_STACK_HIDDEN;
Selim Cinek67b22602014-03-10 15:40:16 +0100725 }
Selim Cinek343e6e22014-04-11 21:23:30 +0200726
727
Selim Cinek67b22602014-03-10 15:40:16 +0100728 }
729
730 /**
731 * Find the number of items in the top stack and update the result state if needed.
732 *
733 * @param resultState The result state to update if a height change of an child occurs
734 * @param algorithmState The state in which the current pass of the algorithm is currently in
Selim Cinek67b22602014-03-10 15:40:16 +0100735 */
736 private void findNumberOfItemsInTopStackAndUpdateState(StackScrollState resultState,
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700737 StackScrollAlgorithmState algorithmState, AmbientState ambientState) {
Selim Cinek67b22602014-03-10 15:40:16 +0100738
739 // The y Position if the element would be in a regular scrollView
740 float yPositionInScrollView = 0.0f;
Jorim Jaggid4a57442014-04-10 02:45:55 +0200741 int childCount = algorithmState.visibleChildren.size();
Selim Cinek67b22602014-03-10 15:40:16 +0100742
743 // find the number of elements in the top stack.
744 for (int i = 0; i < childCount; i++) {
Jorim Jaggibe565df2014-04-28 17:51:23 +0200745 ExpandableView child = algorithmState.visibleChildren.get(i);
Selim Cinekb036ca42015-02-20 15:56:28 +0100746 StackViewState childViewState = resultState.getViewStateForView(child);
Selim Cineka59ecc32015-04-07 10:51:49 -0700747 int childHeight = getMaxAllowedChildHeight(child, ambientState);
Selim Cinek67b22602014-03-10 15:40:16 +0100748 float yPositionInScrollViewAfterElement = yPositionInScrollView
749 + childHeight
750 + mPaddingBetweenElements;
751 if (yPositionInScrollView < algorithmState.scrollY) {
Selim Cinek816c8e42015-11-19 12:00:45 -0800752 if (i == 0 && algorithmState.scrollY <= mFirstChildMinHeight) {
Selim Cinek343e6e22014-04-11 21:23:30 +0200753
754 // The starting position of the bottom stack peek
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700755 int bottomPeekStart = ambientState.getInnerHeight() - mBottomStackPeekSize -
Selim Cinekd83771e2014-07-04 16:45:31 +0200756 mCollapseSecondCardPadding;
Selim Cinek343e6e22014-04-11 21:23:30 +0200757 // Collapse and expand the first child while the shade is being expanded
758 float maxHeight = mIsExpansionChanging && child == mFirstChildWhileExpanding
759 ? mFirstChildMaxHeight
760 : childHeight;
761 childViewState.height = (int) Math.max(Math.min(bottomPeekStart, maxHeight),
Selim Cinek816c8e42015-11-19 12:00:45 -0800762 mFirstChildMinHeight);
Selim Cinek343e6e22014-04-11 21:23:30 +0200763 algorithmState.itemsInTopStack = 1.0f;
764
765 } else if (yPositionInScrollViewAfterElement < algorithmState.scrollY) {
Selim Cinek67b22602014-03-10 15:40:16 +0100766 // According to the regular scroll view we are fully off screen
767 algorithmState.itemsInTopStack += 1.0f;
Selim Cinek343e6e22014-04-11 21:23:30 +0200768 if (i == 0) {
Selim Cinek816c8e42015-11-19 12:00:45 -0800769 childViewState.height = child.getMinHeight();
Selim Cinek67b22602014-03-10 15:40:16 +0100770 }
771 } else {
772 // According to the regular scroll view we are partially off screen
Selim Cinek343e6e22014-04-11 21:23:30 +0200773
Selim Cinek67b22602014-03-10 15:40:16 +0100774 // How much did we scroll into this child
Selim Cinekad3e5af2014-07-04 12:24:11 +0200775 algorithmState.scrolledPixelsTop = algorithmState.scrollY
776 - yPositionInScrollView;
Selim Cinek343e6e22014-04-11 21:23:30 +0200777 algorithmState.partialInTop = (algorithmState.scrolledPixelsTop) / (childHeight
Selim Cinek67b22602014-03-10 15:40:16 +0100778 + mPaddingBetweenElements);
779
780 // Our element can be expanded, so this can get negative
781 algorithmState.partialInTop = Math.max(0.0f, algorithmState.partialInTop);
782 algorithmState.itemsInTopStack += algorithmState.partialInTop;
Selim Cinekad3e5af2014-07-04 12:24:11 +0200783
Selim Cinek343e6e22014-04-11 21:23:30 +0200784 if (i == 0) {
Selim Cinekad3e5af2014-07-04 12:24:11 +0200785 // If it is expanded we have to collapse it to a new size
786 float newSize = yPositionInScrollViewAfterElement
787 - mPaddingBetweenElements
Selim Cinek816c8e42015-11-19 12:00:45 -0800788 - algorithmState.scrollY + mFirstChildMinHeight;
789 newSize = Math.max(mFirstChildMinHeight, newSize);
Selim Cinek4e456be2014-06-12 18:09:43 +0200790 algorithmState.itemsInTopStack = 1.0f;
Selim Cinek67b22602014-03-10 15:40:16 +0100791 childViewState.height = (int) newSize;
Selim Cinek67b22602014-03-10 15:40:16 +0100792 }
Selim Cinek343e6e22014-04-11 21:23:30 +0200793 algorithmState.lastTopStackIndex = i;
794 break;
Selim Cinek67b22602014-03-10 15:40:16 +0100795 }
796 } else {
Selim Cinek343e6e22014-04-11 21:23:30 +0200797 algorithmState.lastTopStackIndex = i - 1;
Selim Cinek67b22602014-03-10 15:40:16 +0100798 // We are already past the stack so we can end the loop
799 break;
800 }
801 yPositionInScrollView = yPositionInScrollViewAfterElement;
802 }
803 }
804
805 /**
806 * Calculate the Z positions for all children based on the number of items in both stacks and
807 * save it in the resultState
808 *
809 * @param resultState The result state to update the zTranslation values
810 * @param algorithmState The state in which the current pass of the algorithm is currently in
811 */
812 private void updateZValuesForState(StackScrollState resultState,
813 StackScrollAlgorithmState algorithmState) {
Jorim Jaggid4a57442014-04-10 02:45:55 +0200814 int childCount = algorithmState.visibleChildren.size();
Selim Cinek67b22602014-03-10 15:40:16 +0100815 for (int i = 0; i < childCount; i++) {
Jorim Jaggid4a57442014-04-10 02:45:55 +0200816 View child = algorithmState.visibleChildren.get(i);
Selim Cinekb036ca42015-02-20 15:56:28 +0100817 StackViewState childViewState = resultState.getViewStateForView(child);
Selim Cinek67b22602014-03-10 15:40:16 +0100818 if (i < algorithmState.itemsInTopStack) {
819 float stackIndex = algorithmState.itemsInTopStack - i;
Selim Cinekc5baa3e2014-10-29 19:04:19 +0100820
821 // Ensure that the topmost item is a little bit higher than the rest when fully
822 // scrolled, to avoid drawing errors when swiping it out
823 float max = MAX_ITEMS_IN_TOP_STACK + (i == 0 ? 2.5f : 2);
824 stackIndex = Math.min(stackIndex, max);
Selim Cinek4e456be2014-06-12 18:09:43 +0200825 if (i == 0 && algorithmState.itemsInTopStack < 2.0f) {
826
827 // We only have the top item and an additional item in the top stack,
828 // Interpolate the index from 0 to 2 while the second item is
829 // translating in.
830 stackIndex -= 1.0f;
Selim Cinek816c8e42015-11-19 12:00:45 -0800831 if (algorithmState.scrollY > mFirstChildMinHeight) {
Selim Cinek4e456be2014-06-12 18:09:43 +0200832
833 // Since there is a shadow treshhold, we cant just interpolate from 0 to
834 // 2 but we interpolate from 0.1f to 2.0f when scrolled in. The jump in
835 // height will not be noticable since we have padding in between.
836 stackIndex = 0.1f + stackIndex * 1.9f;
837 }
838 }
Selim Cinek67b22602014-03-10 15:40:16 +0100839 childViewState.zTranslation = mZBasicHeight
840 + stackIndex * mZDistanceBetweenElements;
841 } else if (i > (childCount - 1 - algorithmState.itemsInBottomStack)) {
842 float numItemsAbove = i - (childCount - 1 - algorithmState.itemsInBottomStack);
843 float translationZ = mZBasicHeight
844 - numItemsAbove * mZDistanceBetweenElements;
845 childViewState.zTranslation = translationZ;
846 } else {
847 childViewState.zTranslation = mZBasicHeight;
848 }
849 }
850 }
851
Selim Cinek1685e632014-04-08 02:27:49 +0200852 public void onExpansionStarted(StackScrollState currentState) {
853 mIsExpansionChanging = true;
854 mExpandedOnStart = mIsExpanded;
855 ViewGroup hostView = currentState.getHostView();
856 updateFirstChildHeightWhileExpanding(hostView);
857 }
858
859 private void updateFirstChildHeightWhileExpanding(ViewGroup hostView) {
Jorim Jaggibe565df2014-04-28 17:51:23 +0200860 mFirstChildWhileExpanding = (ExpandableView) findFirstVisibleChild(hostView);
Jorim Jaggi9f347ae2014-04-11 00:47:18 +0200861 if (mFirstChildWhileExpanding != null) {
Selim Cinek1685e632014-04-08 02:27:49 +0200862 if (mExpandedOnStart) {
863
864 // We are collapsing the shade, so the first child can get as most as high as the
Selim Cinek02af41e2014-10-14 15:46:43 +0200865 // current height or the end value of the animation.
866 mFirstChildMaxHeight = StackStateAnimator.getFinalActualHeight(
867 mFirstChildWhileExpanding);
Selim Cinekd2281152015-04-10 14:37:46 -0700868 if (mFirstChildWhileExpanding instanceof ExpandableNotificationRow) {
869 ExpandableNotificationRow row =
870 (ExpandableNotificationRow) mFirstChildWhileExpanding;
871 if (row.isHeadsUp()) {
Selim Cinek816c8e42015-11-19 12:00:45 -0800872 mFirstChildMaxHeight += mFirstChildMinHeight - row.getHeadsUpHeight();
Selim Cinekd2281152015-04-10 14:37:46 -0700873 }
874 }
Selim Cinek1685e632014-04-08 02:27:49 +0200875 } else {
Selim Cinek31094df2014-08-14 19:28:15 +0200876 updateFirstChildMaxSizeToMaxHeight();
Selim Cinek1685e632014-04-08 02:27:49 +0200877 }
878 } else {
Selim Cinek1685e632014-04-08 02:27:49 +0200879 mFirstChildMaxHeight = 0;
880 }
881 }
882
Selim Cinek31094df2014-08-14 19:28:15 +0200883 private void updateFirstChildMaxSizeToMaxHeight() {
884 // We are expanding the shade, expand it to its full height.
885 if (!isMaxSizeInitialized(mFirstChildWhileExpanding)) {
886
887 // This child was not layouted yet, wait for a layout pass
888 mFirstChildWhileExpanding
889 .addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
890 @Override
891 public void onLayoutChange(View v, int left, int top, int right,
892 int bottom, int oldLeft, int oldTop, int oldRight,
893 int oldBottom) {
894 if (mFirstChildWhileExpanding != null) {
895 mFirstChildMaxHeight = getMaxAllowedChildHeight(
Selim Cineka59ecc32015-04-07 10:51:49 -0700896 mFirstChildWhileExpanding, null);
Selim Cinek31094df2014-08-14 19:28:15 +0200897 } else {
898 mFirstChildMaxHeight = 0;
899 }
900 v.removeOnLayoutChangeListener(this);
901 }
902 });
903 } else {
Selim Cineka59ecc32015-04-07 10:51:49 -0700904 mFirstChildMaxHeight = getMaxAllowedChildHeight(mFirstChildWhileExpanding, null);
Selim Cinek31094df2014-08-14 19:28:15 +0200905 }
906 }
907
Selim Cinek7d447722014-06-10 15:51:59 +0200908 private boolean isMaxSizeInitialized(ExpandableView child) {
909 if (child instanceof ExpandableNotificationRow) {
910 ExpandableNotificationRow row = (ExpandableNotificationRow) child;
Selim Cinek31094df2014-08-14 19:28:15 +0200911 return row.isMaxExpandHeightInitialized();
Selim Cinek7d447722014-06-10 15:51:59 +0200912 }
913 return child == null || child.getWidth() != 0;
914 }
915
Jorim Jaggi9f347ae2014-04-11 00:47:18 +0200916 private View findFirstVisibleChild(ViewGroup container) {
917 int childCount = container.getChildCount();
918 for (int i = 0; i < childCount; i++) {
919 View child = container.getChildAt(i);
920 if (child.getVisibility() != View.GONE) {
921 return child;
922 }
923 }
924 return null;
925 }
926
Selim Cinek1685e632014-04-08 02:27:49 +0200927 public void onExpansionStopped() {
928 mIsExpansionChanging = false;
929 mFirstChildWhileExpanding = null;
930 }
931
932 public void setIsExpanded(boolean isExpanded) {
933 this.mIsExpanded = isExpanded;
934 }
935
Selim Cinekb55386d2015-12-16 17:26:49 -0800936 public void notifyChildrenChanged(final NotificationStackScrollLayout hostView) {
937 mFirstChild = hostView.getFirstChildNotGone();
Selim Cinek1685e632014-04-08 02:27:49 +0200938 if (mIsExpansionChanging) {
Selim Cinek02af41e2014-10-14 15:46:43 +0200939 hostView.post(new Runnable() {
940 @Override
941 public void run() {
942 updateFirstChildHeightWhileExpanding(hostView);
943 }
944 });
Selim Cinek1685e632014-04-08 02:27:49 +0200945 }
946 }
947
Selim Cinek34c0a8d2014-05-12 00:01:43 +0200948 public void setDimmed(boolean dimmed) {
Selim Cinekaf0dc312015-12-15 17:01:44 -0800949 mDimmed = dimmed;
950 updatePadding();
Selim Cinek34c0a8d2014-05-12 00:01:43 +0200951 }
952
Selim Cinek31094df2014-08-14 19:28:15 +0200953 public void onReset(ExpandableView view) {
954 if (view.equals(mFirstChildWhileExpanding)) {
955 updateFirstChildMaxSizeToMaxHeight();
956 }
957 }
958
Selim Cinek67b22602014-03-10 15:40:16 +0100959 class StackScrollAlgorithmState {
960
961 /**
962 * The scroll position of the algorithm
963 */
964 public int scrollY;
965
966 /**
967 * The quantity of items which are in the top stack.
968 */
969 public float itemsInTopStack;
970
971 /**
972 * how far in is the element currently transitioning into the top stack
973 */
974 public float partialInTop;
975
976 /**
Selim Cinek343e6e22014-04-11 21:23:30 +0200977 * The number of pixels the last child in the top stack has scrolled in to the stack
978 */
979 public float scrolledPixelsTop;
980
981 /**
Selim Cinek67b22602014-03-10 15:40:16 +0100982 * The last item index which is in the top stack.
Selim Cinek67b22602014-03-10 15:40:16 +0100983 */
984 public int lastTopStackIndex;
985
986 /**
987 * The quantity of items which are in the bottom stack.
988 */
989 public float itemsInBottomStack;
990
991 /**
992 * how far in is the element currently transitioning into the bottom stack
993 */
994 public float partialInBottom;
Jorim Jaggid4a57442014-04-10 02:45:55 +0200995
996 /**
997 * The children from the host view which are not gone.
998 */
Jorim Jaggibe565df2014-04-28 17:51:23 +0200999 public final ArrayList<ExpandableView> visibleChildren = new ArrayList<ExpandableView>();
Selim Cinek67b22602014-03-10 15:40:16 +01001000 }
1001
1002}