blob: dba027b534ae3a3c88afec4c9ea1762a16b0c9d2 [file] [log] [blame]
Selim Cinek67b22602014-03-10 15:40:16 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
15 */
16
17package com.android.systemui.statusbar.stack;
18
19import android.content.Context;
Jorim Jaggid7c1fae2014-08-13 18:27:47 +020020import android.util.DisplayMetrics;
Christoph Studer6e3eceb2014-04-01 18:40:27 +020021import android.util.Log;
Selim Cinek67b22602014-03-10 15:40:16 +010022import android.view.View;
23import android.view.ViewGroup;
Selim Cinek343e6e22014-04-11 21:23:30 +020024
Selim Cinek67b22602014-03-10 15:40:16 +010025import com.android.systemui.R;
Selim Cinek1685e632014-04-08 02:27:49 +020026import com.android.systemui.statusbar.ExpandableNotificationRow;
Jorim Jaggibe565df2014-04-28 17:51:23 +020027import com.android.systemui.statusbar.ExpandableView;
Selim Cinekb8f09cf2015-03-16 17:09:28 -070028import com.android.systemui.statusbar.policy.HeadsUpManager;
Selim Cinek67b22602014-03-10 15:40:16 +010029
Jorim Jaggid4a57442014-04-10 02:45:55 +020030import java.util.ArrayList;
Selim Cinekb5605e52015-02-20 18:21:41 +010031import java.util.List;
Jorim Jaggid4a57442014-04-10 02:45:55 +020032
Selim Cinek67b22602014-03-10 15:40:16 +010033/**
34 * The Algorithm of the {@link com.android.systemui.statusbar.stack
35 * .NotificationStackScrollLayout} which can be queried for {@link com.android.systemui.statusbar
36 * .stack.StackScrollState}
37 */
38public class StackScrollAlgorithm {
39
Christoph Studer6e3eceb2014-04-01 18:40:27 +020040 private static final String LOG_TAG = "StackScrollAlgorithm";
41
Selim Cinek67b22602014-03-10 15:40:16 +010042 private static final int MAX_ITEMS_IN_BOTTOM_STACK = 3;
43 private static final int MAX_ITEMS_IN_TOP_STACK = 3;
44
Selim Cinek65b2e7c2015-10-26 14:11:31 -070045 public static final float DIMMED_SCALE = 0.98f;
Jorim Jaggid552d9d2014-05-07 19:41:13 +020046
Selim Cinek67b22602014-03-10 15:40:16 +010047 private int mPaddingBetweenElements;
48 private int mCollapsedSize;
49 private int mTopStackPeekSize;
50 private int mBottomStackPeekSize;
51 private int mZDistanceBetweenElements;
52 private int mZBasicHeight;
Selim Cinek708a6c12014-05-28 14:16:02 +020053 private int mRoundedRectCornerRadius;
Selim Cinek67b22602014-03-10 15:40:16 +010054
55 private StackIndentationFunctor mTopStackIndentationFunctor;
56 private StackIndentationFunctor mBottomStackIndentationFunctor;
57
Selim Cinek67b22602014-03-10 15:40:16 +010058 private StackScrollAlgorithmState mTempAlgorithmState = new StackScrollAlgorithmState();
Selim Cinek1685e632014-04-08 02:27:49 +020059 private boolean mIsExpansionChanging;
60 private int mFirstChildMaxHeight;
61 private boolean mIsExpanded;
Jorim Jaggibe565df2014-04-28 17:51:23 +020062 private ExpandableView mFirstChildWhileExpanding;
Selim Cinek1685e632014-04-08 02:27:49 +020063 private boolean mExpandedOnStart;
Selim Cinek343e6e22014-04-11 21:23:30 +020064 private int mTopStackTotalSize;
Selim Cinek34c0a8d2014-05-12 00:01:43 +020065 private int mPaddingBetweenElementsDimmed;
66 private int mPaddingBetweenElementsNormal;
Selim Cinek79d79c42015-05-21 16:14:45 -070067 private int mNotificationsTopPadding;
Selim Cinek34c0a8d2014-05-12 00:01:43 +020068 private int mBottomStackSlowDownLength;
Selim Cinekad3e5af2014-07-04 12:24:11 +020069 private int mTopStackSlowDownLength;
Selim Cinekd83771e2014-07-04 16:45:31 +020070 private int mCollapseSecondCardPadding;
Selim Cinek3afd00e2014-08-11 22:32:57 +020071 private boolean mIsSmallScreen;
72 private int mMaxNotificationHeight;
Jorim Jaggid7c1fae2014-08-13 18:27:47 +020073 private boolean mScaleDimmed;
Selim Cinekd2281152015-04-10 14:37:46 -070074 private HeadsUpManager mHeadsUpManager;
Selim Cinek67b22602014-03-10 15:40:16 +010075
76 public StackScrollAlgorithm(Context context) {
77 initConstants(context);
Selim Cinek34c0a8d2014-05-12 00:01:43 +020078 updatePadding(false);
79 }
80
81 private void updatePadding(boolean dimmed) {
Jorim Jaggid7c1fae2014-08-13 18:27:47 +020082 mPaddingBetweenElements = dimmed && mScaleDimmed
Selim Cinek34c0a8d2014-05-12 00:01:43 +020083 ? mPaddingBetweenElementsDimmed
84 : mPaddingBetweenElementsNormal;
Selim Cinekad3e5af2014-07-04 12:24:11 +020085 mTopStackTotalSize = mTopStackSlowDownLength + mPaddingBetweenElements
86 + mTopStackPeekSize;
Selim Cinek34c0a8d2014-05-12 00:01:43 +020087 mTopStackIndentationFunctor = new PiecewiseLinearIndentationFunctor(
88 MAX_ITEMS_IN_TOP_STACK,
89 mTopStackPeekSize,
Selim Cinekb96924d2014-05-12 15:11:25 +020090 mTopStackTotalSize - mTopStackPeekSize,
Selim Cinek34c0a8d2014-05-12 00:01:43 +020091 0.5f);
92 mBottomStackIndentationFunctor = new PiecewiseLinearIndentationFunctor(
93 MAX_ITEMS_IN_BOTTOM_STACK,
94 mBottomStackPeekSize,
95 getBottomStackSlowDownLength(),
96 0.5f);
97 }
98
99 public int getBottomStackSlowDownLength() {
100 return mBottomStackSlowDownLength + mPaddingBetweenElements;
Selim Cinek67b22602014-03-10 15:40:16 +0100101 }
102
103 private void initConstants(Context context) {
Selim Cinek34c0a8d2014-05-12 00:01:43 +0200104 mPaddingBetweenElementsDimmed = context.getResources()
105 .getDimensionPixelSize(R.dimen.notification_padding_dimmed);
106 mPaddingBetweenElementsNormal = context.getResources()
Jorim Jaggife40f7d2014-04-28 15:20:04 +0200107 .getDimensionPixelSize(R.dimen.notification_padding);
Selim Cinek79d79c42015-05-21 16:14:45 -0700108 mNotificationsTopPadding = context.getResources()
109 .getDimensionPixelSize(R.dimen.notifications_top_padding);
Selim Cinek67b22602014-03-10 15:40:16 +0100110 mCollapsedSize = context.getResources()
Jorim Jaggife40f7d2014-04-28 15:20:04 +0200111 .getDimensionPixelSize(R.dimen.notification_min_height);
Selim Cinek3afd00e2014-08-11 22:32:57 +0200112 mMaxNotificationHeight = context.getResources()
113 .getDimensionPixelSize(R.dimen.notification_max_height);
Selim Cinek67b22602014-03-10 15:40:16 +0100114 mTopStackPeekSize = context.getResources()
115 .getDimensionPixelSize(R.dimen.top_stack_peek_amount);
116 mBottomStackPeekSize = context.getResources()
117 .getDimensionPixelSize(R.dimen.bottom_stack_peek_amount);
118 mZDistanceBetweenElements = context.getResources()
119 .getDimensionPixelSize(R.dimen.z_distance_between_notifications);
120 mZBasicHeight = (MAX_ITEMS_IN_BOTTOM_STACK + 1) * mZDistanceBetweenElements;
Selim Cinek34c0a8d2014-05-12 00:01:43 +0200121 mBottomStackSlowDownLength = context.getResources()
122 .getDimensionPixelSize(R.dimen.bottom_stack_slow_down_length);
Selim Cinekad3e5af2014-07-04 12:24:11 +0200123 mTopStackSlowDownLength = context.getResources()
124 .getDimensionPixelSize(R.dimen.top_stack_slow_down_length);
Selim Cinek708a6c12014-05-28 14:16:02 +0200125 mRoundedRectCornerRadius = context.getResources().getDimensionPixelSize(
Selim Cinek697178b2014-07-02 19:40:30 +0200126 R.dimen.notification_material_rounded_rect_radius);
Selim Cinekd83771e2014-07-04 16:45:31 +0200127 mCollapseSecondCardPadding = context.getResources().getDimensionPixelSize(
128 R.dimen.notification_collapse_second_card_padding);
Jorim Jaggid7c1fae2014-08-13 18:27:47 +0200129 mScaleDimmed = context.getResources().getDisplayMetrics().densityDpi
Jorim Jaggi2cb70042015-08-24 14:52:45 -0700130 >= DisplayMetrics.DENSITY_420;
Selim Cinek67b22602014-03-10 15:40:16 +0100131 }
132
Jorim Jaggid7c1fae2014-08-13 18:27:47 +0200133 public boolean shouldScaleDimmed() {
134 return mScaleDimmed;
135 }
Selim Cinek67b22602014-03-10 15:40:16 +0100136
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200137 public void getStackScrollState(AmbientState ambientState, StackScrollState resultState) {
Selim Cinek67b22602014-03-10 15:40:16 +0100138 // The state of the local variables are saved in an algorithmState to easily subdivide it
139 // into multiple phases.
140 StackScrollAlgorithmState algorithmState = mTempAlgorithmState;
141
142 // First we reset the view states to their default values.
143 resultState.resetViewStates();
144
Selim Cinek343e6e22014-04-11 21:23:30 +0200145 algorithmState.itemsInTopStack = 0.0f;
Selim Cinek67b22602014-03-10 15:40:16 +0100146 algorithmState.partialInTop = 0.0f;
147 algorithmState.lastTopStackIndex = 0;
Selim Cinek343e6e22014-04-11 21:23:30 +0200148 algorithmState.scrolledPixelsTop = 0;
Selim Cinek67b22602014-03-10 15:40:16 +0100149 algorithmState.itemsInBottomStack = 0.0f;
Selim Cinek343e6e22014-04-11 21:23:30 +0200150 algorithmState.partialInBottom = 0.0f;
Selim Cinek8d9ff9c2014-05-12 15:13:04 +0200151 float bottomOverScroll = ambientState.getOverScrollAmount(false /* onTop */);
Selim Cinek1408eb52014-06-02 14:45:38 +0200152
153 int scrollY = ambientState.getScrollY();
154
155 // Due to the overScroller, the stackscroller can have negative scroll state. This is
156 // already accounted for by the top padding and doesn't need an additional adaption
157 scrollY = Math.max(0, scrollY);
158 algorithmState.scrollY = (int) (scrollY + mCollapsedSize + bottomOverScroll);
Selim Cinek343e6e22014-04-11 21:23:30 +0200159
Selim Cineka4baaa32015-04-20 14:27:54 -0700160 updateVisibleChildren(resultState, algorithmState);
Selim Cinek67b22602014-03-10 15:40:16 +0100161
162 // Phase 1:
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700163 findNumberOfItemsInTopStackAndUpdateState(resultState, algorithmState, ambientState);
Selim Cinek67b22602014-03-10 15:40:16 +0100164
165 // Phase 2:
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700166 updatePositionsForState(resultState, algorithmState, ambientState);
Selim Cinek67b22602014-03-10 15:40:16 +0100167
168 // Phase 3:
169 updateZValuesForState(resultState, algorithmState);
Selim Cinekeb973562014-05-02 17:07:49 +0200170
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200171 handleDraggedViews(ambientState, resultState, algorithmState);
Jorim Jaggiae441282014-08-01 02:45:18 +0200172 updateDimmedActivatedHideSensitive(ambientState, resultState, algorithmState);
Selim Cineka59ecc32015-04-07 10:51:49 -0700173 updateClipping(resultState, algorithmState, ambientState);
Selim Cinek3d2b94bf2014-07-02 22:12:47 +0200174 updateSpeedBumpState(resultState, algorithmState, ambientState.getSpeedBumpIndex());
Selim Cinekb5605e52015-02-20 18:21:41 +0100175 getNotificationChildrenStates(resultState, algorithmState);
176 }
177
178 private void getNotificationChildrenStates(StackScrollState resultState,
179 StackScrollAlgorithmState algorithmState) {
180 int childCount = algorithmState.visibleChildren.size();
181 for (int i = 0; i < childCount; i++) {
182 ExpandableView v = algorithmState.visibleChildren.get(i);
183 if (v instanceof ExpandableNotificationRow) {
184 ExpandableNotificationRow row = (ExpandableNotificationRow) v;
185 row.getChildrenStates(resultState);
186 }
187 }
Selim Cinek3d2b94bf2014-07-02 22:12:47 +0200188 }
189
190 private void updateSpeedBumpState(StackScrollState resultState,
191 StackScrollAlgorithmState algorithmState, int speedBumpIndex) {
192 int childCount = algorithmState.visibleChildren.size();
193 for (int i = 0; i < childCount; i++) {
194 View child = algorithmState.visibleChildren.get(i);
Selim Cinekb036ca42015-02-20 15:56:28 +0100195 StackViewState childViewState = resultState.getViewStateForView(child);
Selim Cinek3107cfa2014-07-22 15:24:29 +0200196
197 // The speed bump can also be gone, so equality needs to be taken when comparing
198 // indices.
199 childViewState.belowSpeedBump = speedBumpIndex != -1 && i >= speedBumpIndex;
Selim Cinek3d2b94bf2014-07-02 22:12:47 +0200200 }
Selim Cinekf54090e2014-06-17 17:24:51 -0700201 }
202
Selim Cinek708a6c12014-05-28 14:16:02 +0200203 private void updateClipping(StackScrollState resultState,
Selim Cineka59ecc32015-04-07 10:51:49 -0700204 StackScrollAlgorithmState algorithmState, AmbientState ambientState) {
Selim Cinek9c17b772015-07-07 20:37:09 -0700205 boolean dismissAllInProgress = ambientState.isDismissAllInProgress();
Selim Cinek708a6c12014-05-28 14:16:02 +0200206 float previousNotificationEnd = 0;
207 float previousNotificationStart = 0;
208 boolean previousNotificationIsSwiped = false;
209 int childCount = algorithmState.visibleChildren.size();
210 for (int i = 0; i < childCount; i++) {
211 ExpandableView child = algorithmState.visibleChildren.get(i);
Selim Cinekb036ca42015-02-20 15:56:28 +0100212 StackViewState state = resultState.getViewStateForView(child);
Jorim Jaggi2e34ec32014-07-01 03:17:05 +0200213 float newYTranslation = state.yTranslation + state.height * (1f - state.scale) / 2f;
214 float newHeight = state.height * state.scale;
Selim Cinek708a6c12014-05-28 14:16:02 +0200215 // apply clipping and shadow
216 float newNotificationEnd = newYTranslation + newHeight;
217
Selim Cinekc5baa3e2014-10-29 19:04:19 +0100218 float clipHeight;
219 if (previousNotificationIsSwiped) {
220 // When the previous notification is swiped, we don't clip the content to the
221 // bottom of it.
222 clipHeight = newHeight;
223 } else {
224 clipHeight = newNotificationEnd - previousNotificationEnd;
225 clipHeight = Math.max(0.0f, clipHeight);
226 if (clipHeight != 0.0f) {
Selim Cinek708a6c12014-05-28 14:16:02 +0200227
Selim Cinekc5baa3e2014-10-29 19:04:19 +0100228 // In the unlocked shade we have to clip a little bit higher because of the rounded
229 // corners of the notifications, but only if we are not fully overlapped by
230 // the top card.
231 float clippingCorrection = state.dimmed
232 ? 0
233 : mRoundedRectCornerRadius * state.scale;
234 clipHeight += clippingCorrection;
235 }
236 }
Selim Cinek708a6c12014-05-28 14:16:02 +0200237
238 updateChildClippingAndBackground(state, newHeight, clipHeight,
Jorim Jaggi2e34ec32014-07-01 03:17:05 +0200239 newHeight - (previousNotificationStart - newYTranslation));
Selim Cinek708a6c12014-05-28 14:16:02 +0200240
Selim Cinek9c17b772015-07-07 20:37:09 -0700241 if (dismissAllInProgress) {
242 state.clipTopAmount = Math.max(child.getMinClipTopAmount(), state.clipTopAmount);
243 }
244
Selim Cinek708a6c12014-05-28 14:16:02 +0200245 if (!child.isTransparent()) {
246 // Only update the previous values if we are not transparent,
247 // otherwise we would clip to a transparent view.
Selim Cinek9c17b772015-07-07 20:37:09 -0700248 if ((dismissAllInProgress && canChildBeDismissed(child))) {
249 previousNotificationIsSwiped = true;
250 } else {
251 previousNotificationIsSwiped = ambientState.getDraggedViews().contains(child);
252 previousNotificationEnd = newNotificationEnd;
253 previousNotificationStart = newYTranslation + state.clipTopAmount * state.scale;
254 }
Selim Cinek708a6c12014-05-28 14:16:02 +0200255 }
256 }
257 }
258
Selim Cinek9c17b772015-07-07 20:37:09 -0700259 public static boolean canChildBeDismissed(View v) {
260 final View veto = v.findViewById(R.id.veto);
261 return (veto != null && veto.getVisibility() != View.GONE);
262 }
263
Selim Cinek708a6c12014-05-28 14:16:02 +0200264 /**
265 * Updates the shadow outline and the clipping for a view.
266 *
267 * @param state the viewState to update
268 * @param realHeight the currently applied height of the view
269 * @param clipHeight the desired clip height, the rest of the view will be clipped from the top
270 * @param backgroundHeight the desired background height. The shadows of the view will be
271 * based on this height and the content will be clipped from the top
272 */
Selim Cinekb036ca42015-02-20 15:56:28 +0100273 private void updateChildClippingAndBackground(StackViewState state, float realHeight,
274 float clipHeight, float backgroundHeight) {
Selim Cinek708a6c12014-05-28 14:16:02 +0200275 if (realHeight > clipHeight) {
Jorim Jaggi2e34ec32014-07-01 03:17:05 +0200276 // Rather overlap than create a hole.
277 state.topOverLap = (int) Math.floor((realHeight - clipHeight) / state.scale);
Selim Cinek708a6c12014-05-28 14:16:02 +0200278 } else {
279 state.topOverLap = 0;
280 }
281 if (realHeight > backgroundHeight) {
Jorim Jaggi2e34ec32014-07-01 03:17:05 +0200282 // Rather overlap than create a hole.
283 state.clipTopAmount = (int) Math.floor((realHeight - backgroundHeight) / state.scale);
Selim Cinek708a6c12014-05-28 14:16:02 +0200284 } else {
285 state.clipTopAmount = 0;
286 }
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200287 }
288
289 /**
Jorim Jaggiae441282014-08-01 02:45:18 +0200290 * Updates the dimmed, activated and hiding sensitive states of the children.
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200291 */
Jorim Jaggiae441282014-08-01 02:45:18 +0200292 private void updateDimmedActivatedHideSensitive(AmbientState ambientState,
293 StackScrollState resultState, StackScrollAlgorithmState algorithmState) {
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200294 boolean dimmed = ambientState.isDimmed();
John Spurlockbf370992014-06-17 13:58:31 -0400295 boolean dark = ambientState.isDark();
Jorim Jaggiae441282014-08-01 02:45:18 +0200296 boolean hideSensitive = ambientState.isHideSensitive();
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200297 View activatedChild = ambientState.getActivatedChild();
298 int childCount = algorithmState.visibleChildren.size();
299 for (int i = 0; i < childCount; i++) {
300 View child = algorithmState.visibleChildren.get(i);
Selim Cinekb036ca42015-02-20 15:56:28 +0100301 StackViewState childViewState = resultState.getViewStateForView(child);
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200302 childViewState.dimmed = dimmed;
John Spurlockbf370992014-06-17 13:58:31 -0400303 childViewState.dark = dark;
Jorim Jaggiae441282014-08-01 02:45:18 +0200304 childViewState.hideSensitive = hideSensitive;
Selim Cinekb89de4e2014-06-10 10:47:05 +0200305 boolean isActivatedChild = activatedChild == child;
Jorim Jaggid7c1fae2014-08-13 18:27:47 +0200306 childViewState.scale = !mScaleDimmed || !dimmed || isActivatedChild
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200307 ? 1.0f
308 : DIMMED_SCALE;
Jorim Jaggi4538cee2014-09-09 15:21:38 +0200309 if (dimmed && isActivatedChild) {
310 childViewState.zTranslation += 2.0f * mZDistanceBetweenElements;
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200311 }
312 }
Selim Cinekeb973562014-05-02 17:07:49 +0200313 }
314
315 /**
316 * Handle the special state when views are being dragged
317 */
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200318 private void handleDraggedViews(AmbientState ambientState, StackScrollState resultState,
Selim Cinekeb973562014-05-02 17:07:49 +0200319 StackScrollAlgorithmState algorithmState) {
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200320 ArrayList<View> draggedViews = ambientState.getDraggedViews();
321 for (View draggedView : draggedViews) {
Selim Cinekeb973562014-05-02 17:07:49 +0200322 int childIndex = algorithmState.visibleChildren.indexOf(draggedView);
323 if (childIndex >= 0 && childIndex < algorithmState.visibleChildren.size() - 1) {
324 View nextChild = algorithmState.visibleChildren.get(childIndex + 1);
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200325 if (!draggedViews.contains(nextChild)) {
Selim Cinekeb973562014-05-02 17:07:49 +0200326 // only if the view is not dragged itself we modify its state to be fully
327 // visible
Selim Cinekb036ca42015-02-20 15:56:28 +0100328 StackViewState viewState = resultState.getViewStateForView(
Selim Cinekeb973562014-05-02 17:07:49 +0200329 nextChild);
330 // The child below the dragged one must be fully visible
Selim Cinek131c1e22015-05-11 19:04:49 -0700331 if (ambientState.isShadeExpanded()) {
Selim Cineka59ecc32015-04-07 10:51:49 -0700332 viewState.alpha = 1;
333 }
Selim Cinekeb973562014-05-02 17:07:49 +0200334 }
335
336 // Lets set the alpha to the one it currently has, as its currently being dragged
Selim Cinekb036ca42015-02-20 15:56:28 +0100337 StackViewState viewState = resultState.getViewStateForView(draggedView);
Selim Cinekeb973562014-05-02 17:07:49 +0200338 // The dragged child should keep the set alpha
339 viewState.alpha = draggedView.getAlpha();
340 }
341 }
Selim Cinek343e6e22014-04-11 21:23:30 +0200342 }
Selim Cinek67b22602014-03-10 15:40:16 +0100343
Selim Cinek343e6e22014-04-11 21:23:30 +0200344 /**
Jorim Jaggid4a57442014-04-10 02:45:55 +0200345 * Update the visible children on the state.
346 */
347 private void updateVisibleChildren(StackScrollState resultState,
Selim Cineka4baaa32015-04-20 14:27:54 -0700348 StackScrollAlgorithmState state) {
Jorim Jaggid4a57442014-04-10 02:45:55 +0200349 ViewGroup hostView = resultState.getHostView();
350 int childCount = hostView.getChildCount();
351 state.visibleChildren.clear();
352 state.visibleChildren.ensureCapacity(childCount);
Selim Cinekb036ca42015-02-20 15:56:28 +0100353 int notGoneIndex = 0;
Jorim Jaggid4a57442014-04-10 02:45:55 +0200354 for (int i = 0; i < childCount; i++) {
Jorim Jaggibe565df2014-04-28 17:51:23 +0200355 ExpandableView v = (ExpandableView) hostView.getChildAt(i);
Jorim Jaggid4a57442014-04-10 02:45:55 +0200356 if (v.getVisibility() != View.GONE) {
Selim Cineka4baaa32015-04-20 14:27:54 -0700357 notGoneIndex = updateNotGoneIndex(resultState, state, notGoneIndex, v);
Selim Cinekb5605e52015-02-20 18:21:41 +0100358 if (v instanceof ExpandableNotificationRow) {
359 ExpandableNotificationRow row = (ExpandableNotificationRow) v;
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700360
361 // handle the notgoneIndex for the children as well
Selim Cinekb5605e52015-02-20 18:21:41 +0100362 List<ExpandableNotificationRow> children =
363 row.getNotificationChildren();
Selim Cinek83bc7832015-10-22 13:26:54 -0700364 if (row.isSummaryWithChildren() && children != null) {
Selim Cinekb5605e52015-02-20 18:21:41 +0100365 for (ExpandableNotificationRow childRow : children) {
366 if (childRow.getVisibility() != View.GONE) {
367 StackViewState childState
368 = resultState.getViewStateForView(childRow);
369 childState.notGoneIndex = notGoneIndex;
370 notGoneIndex++;
371 }
372 }
373 }
374 }
Jorim Jaggid4a57442014-04-10 02:45:55 +0200375 }
376 }
377 }
378
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700379 private int updateNotGoneIndex(StackScrollState resultState,
380 StackScrollAlgorithmState state, int notGoneIndex,
381 ExpandableView v) {
382 StackViewState viewState = resultState.getViewStateForView(v);
383 viewState.notGoneIndex = notGoneIndex;
384 state.visibleChildren.add(v);
385 notGoneIndex++;
386 return notGoneIndex;
387 }
388
Jorim Jaggid4a57442014-04-10 02:45:55 +0200389 /**
Selim Cinek67b22602014-03-10 15:40:16 +0100390 * Determine the positions for the views. This is the main part of the algorithm.
391 *
Selim Cinek684a4422015-04-15 16:18:39 -0700392 * @param resultState The result state to update if a change to the properties of a child occurs
Selim Cinek67b22602014-03-10 15:40:16 +0100393 * @param algorithmState The state in which the current pass of the algorithm is currently in
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700394 * @param ambientState The current ambient state
Selim Cinek67b22602014-03-10 15:40:16 +0100395 */
396 private void updatePositionsForState(StackScrollState resultState,
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700397 StackScrollAlgorithmState algorithmState, AmbientState ambientState) {
Selim Cinek67b22602014-03-10 15:40:16 +0100398
Selim Cinek1685e632014-04-08 02:27:49 +0200399 // The starting position of the bottom stack peek
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700400 float bottomPeekStart = ambientState.getInnerHeight() - mBottomStackPeekSize;
Selim Cinek1685e632014-04-08 02:27:49 +0200401
Selim Cinek67b22602014-03-10 15:40:16 +0100402 // The position where the bottom stack starts.
Selim Cinek34c0a8d2014-05-12 00:01:43 +0200403 float bottomStackStart = bottomPeekStart - mBottomStackSlowDownLength;
Selim Cinek67b22602014-03-10 15:40:16 +0100404
405 // The y coordinate of the current child.
406 float currentYPosition = 0.0f;
407
408 // How far in is the element currently transitioning into the bottom stack.
409 float yPositionInScrollView = 0.0f;
410
Selim Cineka59ecc32015-04-07 10:51:49 -0700411 // If we have a heads-up higher than the collapsed height we need to add the difference to
412 // the padding of all other elements, i.e push in the top stack slightly.
413 ExpandableNotificationRow topHeadsUpEntry = ambientState.getTopHeadsUpEntry();
414
Jorim Jaggid4a57442014-04-10 02:45:55 +0200415 int childCount = algorithmState.visibleChildren.size();
Selim Cinekf92a1fd2015-07-31 16:10:32 -0700416 int numberOfElementsCompletelyIn = algorithmState.partialInTop == 1.0f
417 ? algorithmState.lastTopStackIndex
418 : (int) algorithmState.itemsInTopStack;
Selim Cinek67b22602014-03-10 15:40:16 +0100419 for (int i = 0; i < childCount; i++) {
Jorim Jaggibe565df2014-04-28 17:51:23 +0200420 ExpandableView child = algorithmState.visibleChildren.get(i);
Selim Cinekb036ca42015-02-20 15:56:28 +0100421 StackViewState childViewState = resultState.getViewStateForView(child);
422 childViewState.location = StackViewState.LOCATION_UNKNOWN;
Selim Cineka59ecc32015-04-07 10:51:49 -0700423 int childHeight = getMaxAllowedChildHeight(child, ambientState);
Selim Cinek67b22602014-03-10 15:40:16 +0100424 float yPositionInScrollViewAfterElement = yPositionInScrollView
425 + childHeight
426 + mPaddingBetweenElements;
Selim Cinek343e6e22014-04-11 21:23:30 +0200427 float scrollOffset = yPositionInScrollView - algorithmState.scrollY + mCollapsedSize;
428
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 Cinek4581cf82014-08-12 12:40:32 +0200451 >= bottomStackStart && !mIsExpansionChanging && i != 0 && mIsSmallScreen) {
Selim Cinek3afd00e2014-08-11 22:32:57 +0200452 // we just collapse this element slightly
453 int newSize = (int) Math.max(bottomStackStart - mPaddingBetweenElements -
454 childViewState.yTranslation, mCollapsedSize);
455 childViewState.height = newSize;
Selim Cinek4581cf82014-08-12 12:40:32 +0200456 updateStateForChildTransitioningInBottom(algorithmState, bottomStackStart,
457 bottomPeekStart, childViewState.yTranslation, childViewState,
458 childHeight);
Selim Cinek343e6e22014-04-11 21:23:30 +0200459 }
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700460 clampPositionToBottomStackStart(childViewState, childViewState.height,
461 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 Cinekb8f09cf2015-03-16 17:09:28 -0700469 bottomStackStart, childViewState, childHeight, 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,
474 bottomStackStart, bottomPeekStart, currentYPosition,
475 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 Cinek8d9ff9c2014-05-12 15:13:04 +0200487 childViewState.yTranslation = Math.max(mCollapsedSize - algorithmState.scrollY, 0);
Selim Cinekd83771e2014-07-04 16:45:31 +0200488 if (childViewState.yTranslation + childViewState.height
489 > bottomPeekStart - mCollapseSecondCardPadding) {
Selim Cinek4fe3e472014-07-03 16:32:54 +0200490 childViewState.height = (int) Math.max(
Selim Cinekd83771e2014-07-04 16:45:31 +0200491 bottomPeekStart - mCollapseSecondCardPadding
492 - childViewState.yTranslation, mCollapsedSize);
Selim Cinek4fe3e472014-07-03 16:32:54 +0200493 }
Selim Cinekb036ca42015-02-20 15:56:28 +0100494 childViewState.location = StackViewState.LOCATION_FIRST_CARD;
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200495 }
Selim Cinekb036ca42015-02-20 15:56:28 +0100496 if (childViewState.location == StackViewState.LOCATION_UNKNOWN) {
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200497 Log.wtf(LOG_TAG, "Failed to assign location for child " + i);
Selim Cinek67b22602014-03-10 15:40:16 +0100498 }
Selim Cinek343e6e22014-04-11 21:23:30 +0200499 currentYPosition = childViewState.yTranslation + childHeight + mPaddingBetweenElements;
Selim Cinek67b22602014-03-10 15:40:16 +0100500 yPositionInScrollView = yPositionInScrollViewAfterElement;
Jorim Jaggi8c1a44b2014-04-29 19:04:02 +0200501
Selim Cineka59ecc32015-04-07 10:51:49 -0700502 if (ambientState.isShadeExpanded() && topHeadsUpEntry != null
503 && child != topHeadsUpEntry) {
504 childViewState.yTranslation += topHeadsUpEntry.getHeadsUpHeight() - mCollapsedSize;
505 }
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700506 childViewState.yTranslation += ambientState.getTopPadding()
Selim Cineka59ecc32015-04-07 10:51:49 -0700507 + ambientState.getStackTranslation();
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700508 }
Selim Cineka4baaa32015-04-20 14:27:54 -0700509 updateHeadsUpStates(resultState, algorithmState, ambientState);
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700510 }
511
Selim Cineka4baaa32015-04-20 14:27:54 -0700512 private void updateHeadsUpStates(StackScrollState resultState,
513 StackScrollAlgorithmState algorithmState, AmbientState ambientState) {
514 int childCount = algorithmState.visibleChildren.size();
515 ExpandableNotificationRow topHeadsUpEntry = null;
516 for (int i = 0; i < childCount; i++) {
517 View child = algorithmState.visibleChildren.get(i);
518 if (!(child instanceof ExpandableNotificationRow)) {
519 break;
520 }
521 ExpandableNotificationRow row = (ExpandableNotificationRow) child;
522 if (!row.isHeadsUp()) {
523 break;
524 } else if (topHeadsUpEntry == null) {
525 topHeadsUpEntry = row;
526 }
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700527 StackViewState childState = resultState.getViewStateForView(row);
Selim Cinek1f3f5442015-04-10 17:54:46 -0700528 boolean isTopEntry = topHeadsUpEntry == row;
Selim Cinek131c1e22015-05-11 19:04:49 -0700529 if (mIsExpanded) {
530 if (isTopEntry) {
531 childState.height += row.getHeadsUpHeight() - mCollapsedSize;
532 }
533 childState.height = Math.max(childState.height, row.getHeadsUpHeight());
534 // Ensure that the heads up is always visible even when scrolled off from the bottom
535 float bottomPosition = ambientState.getMaxHeadsUpTranslation() - childState.height;
536 childState.yTranslation = Math.min(childState.yTranslation,
537 bottomPosition);
538 }
Selim Cinek684a4422015-04-15 16:18:39 -0700539 if (row.isPinned()) {
Selim Cinek79d79c42015-05-21 16:14:45 -0700540 childState.yTranslation = Math.max(childState.yTranslation,
541 mNotificationsTopPadding);
Selim Cinek1f3f5442015-04-10 17:54:46 -0700542 childState.height = row.getHeadsUpHeight();
543 if (!isTopEntry) {
Selim Cinek684a4422015-04-15 16:18:39 -0700544 // Ensure that a headsUp doesn't vertically extend further than the heads-up at
545 // the top most z-position
Selim Cinek1f3f5442015-04-10 17:54:46 -0700546 StackViewState topState = resultState.getViewStateForView(topHeadsUpEntry);
547 childState.height = row.getHeadsUpHeight();
Selim Cineke53e6bb2015-04-13 16:14:26 -0700548 childState.yTranslation = topState.yTranslation + topState.height
549 - childState.height;
Selim Cinek1f3f5442015-04-10 17:54:46 -0700550 }
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700551 }
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700552 }
Selim Cinek67b22602014-03-10 15:40:16 +0100553 }
554
Selim Cinek1685e632014-04-08 02:27:49 +0200555 /**
Selim Cinek343e6e22014-04-11 21:23:30 +0200556 * Clamp the yTranslation both up and down to valid positions.
Selim Cinek1685e632014-04-08 02:27:49 +0200557 *
Selim Cinek1685e632014-04-08 02:27:49 +0200558 * @param childViewState the view state of the child
Selim Cinek343e6e22014-04-11 21:23:30 +0200559 * @param childHeight the height of this child
Selim Cinek1685e632014-04-08 02:27:49 +0200560 */
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700561 private void clampYTranslation(StackViewState childViewState, int childHeight,
562 AmbientState ambientState) {
563 clampPositionToBottomStackStart(childViewState, childHeight, ambientState);
Selim Cinek343e6e22014-04-11 21:23:30 +0200564 clampPositionToTopStackEnd(childViewState, childHeight);
565 }
566
567 /**
568 * Clamp the yTranslation of the child down such that its end is at most on the beginning of
569 * the bottom stack.
570 *
571 * @param childViewState the view state of the child
572 * @param childHeight the height of this child
573 */
Selim Cinekb036ca42015-02-20 15:56:28 +0100574 private void clampPositionToBottomStackStart(StackViewState childViewState,
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700575 int childHeight, AmbientState ambientState) {
Selim Cinek343e6e22014-04-11 21:23:30 +0200576 childViewState.yTranslation = Math.min(childViewState.yTranslation,
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700577 ambientState.getInnerHeight() - mBottomStackPeekSize - mCollapseSecondCardPadding
578 - childHeight);
Selim Cinek343e6e22014-04-11 21:23:30 +0200579 }
580
581 /**
582 * 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 -0700583 * stack.
Selim Cinek343e6e22014-04-11 21:23:30 +0200584 *
585 * @param childViewState the view state of the child
586 * @param childHeight the height of this child
587 */
Selim Cinekb036ca42015-02-20 15:56:28 +0100588 private void clampPositionToTopStackEnd(StackViewState childViewState,
Selim Cinek343e6e22014-04-11 21:23:30 +0200589 int childHeight) {
590 childViewState.yTranslation = Math.max(childViewState.yTranslation,
591 mCollapsedSize - childHeight);
Selim Cinek1685e632014-04-08 02:27:49 +0200592 }
593
Selim Cineka59ecc32015-04-07 10:51:49 -0700594 private int getMaxAllowedChildHeight(View child, AmbientState ambientState) {
Selim Cinek1685e632014-04-08 02:27:49 +0200595 if (child instanceof ExpandableNotificationRow) {
596 ExpandableNotificationRow row = (ExpandableNotificationRow) child;
Selim Cinekd2281152015-04-10 14:37:46 -0700597 if (ambientState == null && row.isHeadsUp()
598 || ambientState != null && ambientState.getTopHeadsUpEntry() == child) {
Selim Cinek8d490d42015-04-10 00:05:50 -0700599 int extraSize = row.getIntrinsicHeight() - row.getHeadsUpHeight();
600 return mCollapsedSize + extraSize;
Selim Cineka59ecc32015-04-07 10:51:49 -0700601 }
Jorim Jaggi9cbadd32014-05-01 20:18:31 +0200602 return row.getIntrinsicHeight();
Jorim Jaggibe565df2014-04-28 17:51:23 +0200603 } else if (child instanceof ExpandableView) {
604 ExpandableView expandableView = (ExpandableView) child;
Selim Cinek8d5727f2015-04-28 19:17:32 -0700605 return expandableView.getIntrinsicHeight();
Selim Cinek1685e632014-04-08 02:27:49 +0200606 }
Jorim Jaggibe565df2014-04-28 17:51:23 +0200607 return child == null? mCollapsedSize : child.getHeight();
Selim Cinek1685e632014-04-08 02:27:49 +0200608 }
609
Selim Cinek343e6e22014-04-11 21:23:30 +0200610 private void updateStateForChildTransitioningInBottom(StackScrollAlgorithmState algorithmState,
611 float transitioningPositionStart, float bottomPeakStart, float currentYPosition,
Selim Cinekb036ca42015-02-20 15:56:28 +0100612 StackViewState childViewState, int childHeight) {
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200613
Selim Cinek343e6e22014-04-11 21:23:30 +0200614 // This is the transitioning element on top of bottom stack, calculate how far we are in.
615 algorithmState.partialInBottom = 1.0f - (
616 (transitioningPositionStart - currentYPosition) / (childHeight +
617 mPaddingBetweenElements));
618
619 // the offset starting at the transitionPosition of the bottom stack
620 float offset = mBottomStackIndentationFunctor.getValue(algorithmState.partialInBottom);
621 algorithmState.itemsInBottomStack += algorithmState.partialInBottom;
Selim Cinek3afd00e2014-08-11 22:32:57 +0200622 int newHeight = childHeight;
623 if (childHeight > mCollapsedSize && mIsSmallScreen) {
624 newHeight = (int) Math.max(Math.min(transitioningPositionStart + offset -
625 mPaddingBetweenElements - currentYPosition, childHeight), mCollapsedSize);
626 childViewState.height = newHeight;
627 }
628 childViewState.yTranslation = transitioningPositionStart + offset - newHeight
Jorim Jaggife40f7d2014-04-28 15:20:04 +0200629 - mPaddingBetweenElements;
Selim Cinek3afd00e2014-08-11 22:32:57 +0200630
Selim Cinek343e6e22014-04-11 21:23:30 +0200631 // We want at least to be at the end of the top stack when collapsing
Selim Cinek3afd00e2014-08-11 22:32:57 +0200632 clampPositionToTopStackEnd(childViewState, newHeight);
Selim Cinekb036ca42015-02-20 15:56:28 +0100633 childViewState.location = StackViewState.LOCATION_MAIN_AREA;
Selim Cinek67b22602014-03-10 15:40:16 +0100634 }
635
Selim Cinek343e6e22014-04-11 21:23:30 +0200636 private void updateStateForChildFullyInBottomStack(StackScrollAlgorithmState algorithmState,
Selim Cinekb036ca42015-02-20 15:56:28 +0100637 float transitioningPositionStart, StackViewState childViewState,
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700638 int childHeight, AmbientState ambientState) {
Selim Cinek343e6e22014-04-11 21:23:30 +0200639 float currentYPosition;
Selim Cinek67b22602014-03-10 15:40:16 +0100640 algorithmState.itemsInBottomStack += 1.0f;
641 if (algorithmState.itemsInBottomStack < MAX_ITEMS_IN_BOTTOM_STACK) {
642 // We are visually entering the bottom stack
Selim Cinek343e6e22014-04-11 21:23:30 +0200643 currentYPosition = transitioningPositionStart
Jorim Jaggife40f7d2014-04-28 15:20:04 +0200644 + mBottomStackIndentationFunctor.getValue(algorithmState.itemsInBottomStack)
645 - mPaddingBetweenElements;
Selim Cinekb036ca42015-02-20 15:56:28 +0100646 childViewState.location = StackViewState.LOCATION_BOTTOM_STACK_PEEKING;
Selim Cinek67b22602014-03-10 15:40:16 +0100647 } else {
648 // we are fully inside the stack
649 if (algorithmState.itemsInBottomStack > MAX_ITEMS_IN_BOTTOM_STACK + 2) {
650 childViewState.alpha = 0.0f;
651 } else if (algorithmState.itemsInBottomStack
652 > MAX_ITEMS_IN_BOTTOM_STACK + 1) {
653 childViewState.alpha = 1.0f - algorithmState.partialInBottom;
654 }
Selim Cinekb036ca42015-02-20 15:56:28 +0100655 childViewState.location = StackViewState.LOCATION_BOTTOM_STACK_HIDDEN;
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700656 currentYPosition = ambientState.getInnerHeight();
Selim Cinek67b22602014-03-10 15:40:16 +0100657 }
Selim Cinek343e6e22014-04-11 21:23:30 +0200658 childViewState.yTranslation = currentYPosition - childHeight;
659 clampPositionToTopStackEnd(childViewState, childHeight);
Selim Cinek67b22602014-03-10 15:40:16 +0100660 }
661
Selim Cinek343e6e22014-04-11 21:23:30 +0200662 private void updateStateForTopStackChild(StackScrollAlgorithmState algorithmState,
663 int numberOfElementsCompletelyIn, int i, int childHeight,
Selim Cinekb036ca42015-02-20 15:56:28 +0100664 StackViewState childViewState, float scrollOffset) {
Selim Cinek67b22602014-03-10 15:40:16 +0100665
Selim Cinek67b22602014-03-10 15:40:16 +0100666
667 // First we calculate the index relative to the current stack window of size at most
668 // {@link #MAX_ITEMS_IN_TOP_STACK}
Selim Cinek343e6e22014-04-11 21:23:30 +0200669 int paddedIndex = i - 1
Selim Cinek67b22602014-03-10 15:40:16 +0100670 - Math.max(numberOfElementsCompletelyIn - MAX_ITEMS_IN_TOP_STACK, 0);
671 if (paddedIndex >= 0) {
Selim Cinek343e6e22014-04-11 21:23:30 +0200672
Selim Cinek67b22602014-03-10 15:40:16 +0100673 // We are currently visually entering the top stack
Selim Cinekad3e5af2014-07-04 12:24:11 +0200674 float distanceToStack = (childHeight + mPaddingBetweenElements)
675 - algorithmState.scrolledPixelsTop;
676 if (i == algorithmState.lastTopStackIndex
677 && distanceToStack > (mTopStackTotalSize + mPaddingBetweenElements)) {
Selim Cinek343e6e22014-04-11 21:23:30 +0200678
679 // Child is currently translating into stack but not yet inside slow down zone.
680 // Handle it like the regular scrollview.
681 childViewState.yTranslation = scrollOffset;
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200682 } else {
Selim Cinek343e6e22014-04-11 21:23:30 +0200683 // Apply stacking logic.
684 float numItemsBefore;
685 if (i == algorithmState.lastTopStackIndex) {
Selim Cinekad3e5af2014-07-04 12:24:11 +0200686 numItemsBefore = 1.0f
687 - (distanceToStack / (mTopStackTotalSize + mPaddingBetweenElements));
Selim Cinek343e6e22014-04-11 21:23:30 +0200688 } else {
689 numItemsBefore = algorithmState.itemsInTopStack - i;
690 }
691 // The end position of the current child
Selim Cinekad3e5af2014-07-04 12:24:11 +0200692 float currentChildEndY = mCollapsedSize + mTopStackTotalSize
693 - mTopStackIndentationFunctor.getValue(numItemsBefore);
Selim Cinek343e6e22014-04-11 21:23:30 +0200694 childViewState.yTranslation = currentChildEndY - childHeight;
Selim Cinek67b22602014-03-10 15:40:16 +0100695 }
Selim Cinekb036ca42015-02-20 15:56:28 +0100696 childViewState.location = StackViewState.LOCATION_TOP_STACK_PEEKING;
Selim Cinek67b22602014-03-10 15:40:16 +0100697 } else {
Selim Cinek343e6e22014-04-11 21:23:30 +0200698 if (paddedIndex == -1) {
699 childViewState.alpha = 1.0f - algorithmState.partialInTop;
700 } else {
701 // We are hidden behind the top card and faded out, so we can hide ourselves.
702 childViewState.alpha = 0.0f;
703 }
704 childViewState.yTranslation = mCollapsedSize - childHeight;
Selim Cinekb036ca42015-02-20 15:56:28 +0100705 childViewState.location = StackViewState.LOCATION_TOP_STACK_HIDDEN;
Selim Cinek67b22602014-03-10 15:40:16 +0100706 }
Selim Cinek343e6e22014-04-11 21:23:30 +0200707
708
Selim Cinek67b22602014-03-10 15:40:16 +0100709 }
710
711 /**
712 * Find the number of items in the top stack and update the result state if needed.
713 *
714 * @param resultState The result state to update if a height change of an child occurs
715 * @param algorithmState The state in which the current pass of the algorithm is currently in
Selim Cinek67b22602014-03-10 15:40:16 +0100716 */
717 private void findNumberOfItemsInTopStackAndUpdateState(StackScrollState resultState,
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700718 StackScrollAlgorithmState algorithmState, AmbientState ambientState) {
Selim Cinek67b22602014-03-10 15:40:16 +0100719
720 // The y Position if the element would be in a regular scrollView
721 float yPositionInScrollView = 0.0f;
Jorim Jaggid4a57442014-04-10 02:45:55 +0200722 int childCount = algorithmState.visibleChildren.size();
Selim Cinek67b22602014-03-10 15:40:16 +0100723
724 // find the number of elements in the top stack.
725 for (int i = 0; i < childCount; i++) {
Jorim Jaggibe565df2014-04-28 17:51:23 +0200726 ExpandableView child = algorithmState.visibleChildren.get(i);
Selim Cinekb036ca42015-02-20 15:56:28 +0100727 StackViewState childViewState = resultState.getViewStateForView(child);
Selim Cineka59ecc32015-04-07 10:51:49 -0700728 int childHeight = getMaxAllowedChildHeight(child, ambientState);
Selim Cinek67b22602014-03-10 15:40:16 +0100729 float yPositionInScrollViewAfterElement = yPositionInScrollView
730 + childHeight
731 + mPaddingBetweenElements;
732 if (yPositionInScrollView < algorithmState.scrollY) {
Selim Cinek8d9ff9c2014-05-12 15:13:04 +0200733 if (i == 0 && algorithmState.scrollY <= mCollapsedSize) {
Selim Cinek343e6e22014-04-11 21:23:30 +0200734
735 // The starting position of the bottom stack peek
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700736 int bottomPeekStart = ambientState.getInnerHeight() - mBottomStackPeekSize -
Selim Cinekd83771e2014-07-04 16:45:31 +0200737 mCollapseSecondCardPadding;
Selim Cinek343e6e22014-04-11 21:23:30 +0200738 // Collapse and expand the first child while the shade is being expanded
739 float maxHeight = mIsExpansionChanging && child == mFirstChildWhileExpanding
740 ? mFirstChildMaxHeight
741 : childHeight;
742 childViewState.height = (int) Math.max(Math.min(bottomPeekStart, maxHeight),
743 mCollapsedSize);
744 algorithmState.itemsInTopStack = 1.0f;
745
746 } else if (yPositionInScrollViewAfterElement < algorithmState.scrollY) {
Selim Cinek67b22602014-03-10 15:40:16 +0100747 // According to the regular scroll view we are fully off screen
748 algorithmState.itemsInTopStack += 1.0f;
Selim Cinek343e6e22014-04-11 21:23:30 +0200749 if (i == 0) {
Selim Cinek67b22602014-03-10 15:40:16 +0100750 childViewState.height = mCollapsedSize;
751 }
752 } else {
753 // According to the regular scroll view we are partially off screen
Selim Cinek343e6e22014-04-11 21:23:30 +0200754
Selim Cinek67b22602014-03-10 15:40:16 +0100755 // How much did we scroll into this child
Selim Cinekad3e5af2014-07-04 12:24:11 +0200756 algorithmState.scrolledPixelsTop = algorithmState.scrollY
757 - yPositionInScrollView;
Selim Cinek343e6e22014-04-11 21:23:30 +0200758 algorithmState.partialInTop = (algorithmState.scrolledPixelsTop) / (childHeight
Selim Cinek67b22602014-03-10 15:40:16 +0100759 + mPaddingBetweenElements);
760
761 // Our element can be expanded, so this can get negative
762 algorithmState.partialInTop = Math.max(0.0f, algorithmState.partialInTop);
763 algorithmState.itemsInTopStack += algorithmState.partialInTop;
Selim Cinekad3e5af2014-07-04 12:24:11 +0200764
Selim Cinek343e6e22014-04-11 21:23:30 +0200765 if (i == 0) {
Selim Cinekad3e5af2014-07-04 12:24:11 +0200766 // If it is expanded we have to collapse it to a new size
767 float newSize = yPositionInScrollViewAfterElement
768 - mPaddingBetweenElements
769 - algorithmState.scrollY + mCollapsedSize;
770 newSize = Math.max(mCollapsedSize, newSize);
Selim Cinek4e456be2014-06-12 18:09:43 +0200771 algorithmState.itemsInTopStack = 1.0f;
Selim Cinek67b22602014-03-10 15:40:16 +0100772 childViewState.height = (int) newSize;
Selim Cinek67b22602014-03-10 15:40:16 +0100773 }
Selim Cinek343e6e22014-04-11 21:23:30 +0200774 algorithmState.lastTopStackIndex = i;
775 break;
Selim Cinek67b22602014-03-10 15:40:16 +0100776 }
777 } else {
Selim Cinek343e6e22014-04-11 21:23:30 +0200778 algorithmState.lastTopStackIndex = i - 1;
Selim Cinek67b22602014-03-10 15:40:16 +0100779 // We are already past the stack so we can end the loop
780 break;
781 }
782 yPositionInScrollView = yPositionInScrollViewAfterElement;
783 }
784 }
785
786 /**
787 * Calculate the Z positions for all children based on the number of items in both stacks and
788 * save it in the resultState
789 *
790 * @param resultState The result state to update the zTranslation values
791 * @param algorithmState The state in which the current pass of the algorithm is currently in
792 */
793 private void updateZValuesForState(StackScrollState resultState,
794 StackScrollAlgorithmState algorithmState) {
Jorim Jaggid4a57442014-04-10 02:45:55 +0200795 int childCount = algorithmState.visibleChildren.size();
Selim Cinek67b22602014-03-10 15:40:16 +0100796 for (int i = 0; i < childCount; i++) {
Jorim Jaggid4a57442014-04-10 02:45:55 +0200797 View child = algorithmState.visibleChildren.get(i);
Selim Cinekb036ca42015-02-20 15:56:28 +0100798 StackViewState childViewState = resultState.getViewStateForView(child);
Selim Cinek67b22602014-03-10 15:40:16 +0100799 if (i < algorithmState.itemsInTopStack) {
800 float stackIndex = algorithmState.itemsInTopStack - i;
Selim Cinekc5baa3e2014-10-29 19:04:19 +0100801
802 // Ensure that the topmost item is a little bit higher than the rest when fully
803 // scrolled, to avoid drawing errors when swiping it out
804 float max = MAX_ITEMS_IN_TOP_STACK + (i == 0 ? 2.5f : 2);
805 stackIndex = Math.min(stackIndex, max);
Selim Cinek4e456be2014-06-12 18:09:43 +0200806 if (i == 0 && algorithmState.itemsInTopStack < 2.0f) {
807
808 // We only have the top item and an additional item in the top stack,
809 // Interpolate the index from 0 to 2 while the second item is
810 // translating in.
811 stackIndex -= 1.0f;
812 if (algorithmState.scrollY > mCollapsedSize) {
813
814 // Since there is a shadow treshhold, we cant just interpolate from 0 to
815 // 2 but we interpolate from 0.1f to 2.0f when scrolled in. The jump in
816 // height will not be noticable since we have padding in between.
817 stackIndex = 0.1f + stackIndex * 1.9f;
818 }
819 }
Selim Cinek67b22602014-03-10 15:40:16 +0100820 childViewState.zTranslation = mZBasicHeight
821 + stackIndex * mZDistanceBetweenElements;
822 } else if (i > (childCount - 1 - algorithmState.itemsInBottomStack)) {
823 float numItemsAbove = i - (childCount - 1 - algorithmState.itemsInBottomStack);
824 float translationZ = mZBasicHeight
825 - numItemsAbove * mZDistanceBetweenElements;
826 childViewState.zTranslation = translationZ;
827 } else {
828 childViewState.zTranslation = mZBasicHeight;
829 }
830 }
831 }
832
Selim Cinek3afd00e2014-08-11 22:32:57 +0200833 /**
834 * Update whether the device is very small, i.e. Notifications can be in both the top and the
835 * bottom stack at the same time
836 *
837 * @param panelHeight The normal height of the panel when it's open
838 */
839 public void updateIsSmallScreen(int panelHeight) {
840 mIsSmallScreen = panelHeight <
841 mCollapsedSize /* top stack */
842 + mBottomStackSlowDownLength + mBottomStackPeekSize /* bottom stack */
843 + mMaxNotificationHeight; /* max notification height */
844 }
845
Selim Cinek1685e632014-04-08 02:27:49 +0200846 public void onExpansionStarted(StackScrollState currentState) {
847 mIsExpansionChanging = true;
848 mExpandedOnStart = mIsExpanded;
849 ViewGroup hostView = currentState.getHostView();
850 updateFirstChildHeightWhileExpanding(hostView);
851 }
852
853 private void updateFirstChildHeightWhileExpanding(ViewGroup hostView) {
Jorim Jaggibe565df2014-04-28 17:51:23 +0200854 mFirstChildWhileExpanding = (ExpandableView) findFirstVisibleChild(hostView);
Jorim Jaggi9f347ae2014-04-11 00:47:18 +0200855 if (mFirstChildWhileExpanding != null) {
Selim Cinek1685e632014-04-08 02:27:49 +0200856 if (mExpandedOnStart) {
857
858 // We are collapsing the shade, so the first child can get as most as high as the
Selim Cinek02af41e2014-10-14 15:46:43 +0200859 // current height or the end value of the animation.
860 mFirstChildMaxHeight = StackStateAnimator.getFinalActualHeight(
861 mFirstChildWhileExpanding);
Selim Cinekd2281152015-04-10 14:37:46 -0700862 if (mFirstChildWhileExpanding instanceof ExpandableNotificationRow) {
863 ExpandableNotificationRow row =
864 (ExpandableNotificationRow) mFirstChildWhileExpanding;
865 if (row.isHeadsUp()) {
866 mFirstChildMaxHeight += mCollapsedSize - row.getHeadsUpHeight();
867 }
868 }
Selim Cinek1685e632014-04-08 02:27:49 +0200869 } else {
Selim Cinek31094df2014-08-14 19:28:15 +0200870 updateFirstChildMaxSizeToMaxHeight();
Selim Cinek1685e632014-04-08 02:27:49 +0200871 }
872 } else {
Selim Cinek1685e632014-04-08 02:27:49 +0200873 mFirstChildMaxHeight = 0;
874 }
875 }
876
Selim Cinek31094df2014-08-14 19:28:15 +0200877 private void updateFirstChildMaxSizeToMaxHeight() {
878 // We are expanding the shade, expand it to its full height.
879 if (!isMaxSizeInitialized(mFirstChildWhileExpanding)) {
880
881 // This child was not layouted yet, wait for a layout pass
882 mFirstChildWhileExpanding
883 .addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
884 @Override
885 public void onLayoutChange(View v, int left, int top, int right,
886 int bottom, int oldLeft, int oldTop, int oldRight,
887 int oldBottom) {
888 if (mFirstChildWhileExpanding != null) {
889 mFirstChildMaxHeight = getMaxAllowedChildHeight(
Selim Cineka59ecc32015-04-07 10:51:49 -0700890 mFirstChildWhileExpanding, null);
Selim Cinek31094df2014-08-14 19:28:15 +0200891 } else {
892 mFirstChildMaxHeight = 0;
893 }
894 v.removeOnLayoutChangeListener(this);
895 }
896 });
897 } else {
Selim Cineka59ecc32015-04-07 10:51:49 -0700898 mFirstChildMaxHeight = getMaxAllowedChildHeight(mFirstChildWhileExpanding, null);
Selim Cinek31094df2014-08-14 19:28:15 +0200899 }
900 }
901
Selim Cinek7d447722014-06-10 15:51:59 +0200902 private boolean isMaxSizeInitialized(ExpandableView child) {
903 if (child instanceof ExpandableNotificationRow) {
904 ExpandableNotificationRow row = (ExpandableNotificationRow) child;
Selim Cinek31094df2014-08-14 19:28:15 +0200905 return row.isMaxExpandHeightInitialized();
Selim Cinek7d447722014-06-10 15:51:59 +0200906 }
907 return child == null || child.getWidth() != 0;
908 }
909
Jorim Jaggi9f347ae2014-04-11 00:47:18 +0200910 private View findFirstVisibleChild(ViewGroup container) {
911 int childCount = container.getChildCount();
912 for (int i = 0; i < childCount; i++) {
913 View child = container.getChildAt(i);
914 if (child.getVisibility() != View.GONE) {
915 return child;
916 }
917 }
918 return null;
919 }
920
Selim Cinek1685e632014-04-08 02:27:49 +0200921 public void onExpansionStopped() {
922 mIsExpansionChanging = false;
923 mFirstChildWhileExpanding = null;
924 }
925
926 public void setIsExpanded(boolean isExpanded) {
927 this.mIsExpanded = isExpanded;
928 }
929
Selim Cinek02af41e2014-10-14 15:46:43 +0200930 public void notifyChildrenChanged(final ViewGroup hostView) {
Selim Cinek1685e632014-04-08 02:27:49 +0200931 if (mIsExpansionChanging) {
Selim Cinek02af41e2014-10-14 15:46:43 +0200932 hostView.post(new Runnable() {
933 @Override
934 public void run() {
935 updateFirstChildHeightWhileExpanding(hostView);
936 }
937 });
Selim Cinek1685e632014-04-08 02:27:49 +0200938 }
939 }
940
Selim Cinek34c0a8d2014-05-12 00:01:43 +0200941 public void setDimmed(boolean dimmed) {
942 updatePadding(dimmed);
943 }
944
Selim Cinek31094df2014-08-14 19:28:15 +0200945 public void onReset(ExpandableView view) {
946 if (view.equals(mFirstChildWhileExpanding)) {
947 updateFirstChildMaxSizeToMaxHeight();
948 }
949 }
950
Selim Cinekd2281152015-04-10 14:37:46 -0700951 public void setHeadsUpManager(HeadsUpManager headsUpManager) {
952 mHeadsUpManager = headsUpManager;
953 }
954
Selim Cinek67b22602014-03-10 15:40:16 +0100955 class StackScrollAlgorithmState {
956
957 /**
958 * The scroll position of the algorithm
959 */
960 public int scrollY;
961
962 /**
963 * The quantity of items which are in the top stack.
964 */
965 public float itemsInTopStack;
966
967 /**
968 * how far in is the element currently transitioning into the top stack
969 */
970 public float partialInTop;
971
972 /**
Selim Cinek343e6e22014-04-11 21:23:30 +0200973 * The number of pixels the last child in the top stack has scrolled in to the stack
974 */
975 public float scrolledPixelsTop;
976
977 /**
Selim Cinek67b22602014-03-10 15:40:16 +0100978 * The last item index which is in the top stack.
Selim Cinek67b22602014-03-10 15:40:16 +0100979 */
980 public int lastTopStackIndex;
981
982 /**
983 * The quantity of items which are in the bottom stack.
984 */
985 public float itemsInBottomStack;
986
987 /**
988 * how far in is the element currently transitioning into the bottom stack
989 */
990 public float partialInBottom;
Jorim Jaggid4a57442014-04-10 02:45:55 +0200991
992 /**
993 * The children from the host view which are not gone.
994 */
Jorim Jaggibe565df2014-04-28 17:51:23 +0200995 public final ArrayList<ExpandableView> visibleChildren = new ArrayList<ExpandableView>();
Selim Cinek67b22602014-03-10 15:40:16 +0100996 }
997
998}