blob: e7bf47b02782e45edcc54f5bc0f44cd9c7068495 [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
Jorim Jaggi362dd6d2014-07-09 19:04:07 +020044 public static final float DIMMED_SCALE = 0.95f;
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 Cinek343e6e22014-04-11 21:23:30 +020057 private int mLayoutHeight;
Jorim Jaggi8c1a44b2014-04-29 19:04:02 +020058
59 /** mLayoutHeight - mTopPadding */
60 private int mInnerHeight;
61 private int mTopPadding;
Selim Cinek67b22602014-03-10 15:40:16 +010062 private StackScrollAlgorithmState mTempAlgorithmState = new StackScrollAlgorithmState();
Selim Cinek1685e632014-04-08 02:27:49 +020063 private boolean mIsExpansionChanging;
64 private int mFirstChildMaxHeight;
65 private boolean mIsExpanded;
Jorim Jaggibe565df2014-04-28 17:51:23 +020066 private ExpandableView mFirstChildWhileExpanding;
Selim Cinek1685e632014-04-08 02:27:49 +020067 private boolean mExpandedOnStart;
Selim Cinek343e6e22014-04-11 21:23:30 +020068 private int mTopStackTotalSize;
Selim Cinek34c0a8d2014-05-12 00:01:43 +020069 private int mPaddingBetweenElementsDimmed;
70 private int mPaddingBetweenElementsNormal;
71 private int mBottomStackSlowDownLength;
Selim Cinekad3e5af2014-07-04 12:24:11 +020072 private int mTopStackSlowDownLength;
Selim Cinekd83771e2014-07-04 16:45:31 +020073 private int mCollapseSecondCardPadding;
Selim Cinek3afd00e2014-08-11 22:32:57 +020074 private boolean mIsSmallScreen;
75 private int mMaxNotificationHeight;
Jorim Jaggid7c1fae2014-08-13 18:27:47 +020076 private boolean mScaleDimmed;
Selim Cinek67b22602014-03-10 15:40:16 +010077
78 public StackScrollAlgorithm(Context context) {
79 initConstants(context);
Selim Cinek34c0a8d2014-05-12 00:01:43 +020080 updatePadding(false);
81 }
82
83 private void updatePadding(boolean dimmed) {
Jorim Jaggid7c1fae2014-08-13 18:27:47 +020084 mPaddingBetweenElements = dimmed && mScaleDimmed
Selim Cinek34c0a8d2014-05-12 00:01:43 +020085 ? mPaddingBetweenElementsDimmed
86 : mPaddingBetweenElementsNormal;
Selim Cinekad3e5af2014-07-04 12:24:11 +020087 mTopStackTotalSize = mTopStackSlowDownLength + mPaddingBetweenElements
88 + mTopStackPeekSize;
Selim Cinek34c0a8d2014-05-12 00:01:43 +020089 mTopStackIndentationFunctor = new PiecewiseLinearIndentationFunctor(
90 MAX_ITEMS_IN_TOP_STACK,
91 mTopStackPeekSize,
Selim Cinekb96924d2014-05-12 15:11:25 +020092 mTopStackTotalSize - mTopStackPeekSize,
Selim Cinek34c0a8d2014-05-12 00:01:43 +020093 0.5f);
94 mBottomStackIndentationFunctor = new PiecewiseLinearIndentationFunctor(
95 MAX_ITEMS_IN_BOTTOM_STACK,
96 mBottomStackPeekSize,
97 getBottomStackSlowDownLength(),
98 0.5f);
99 }
100
101 public int getBottomStackSlowDownLength() {
102 return mBottomStackSlowDownLength + mPaddingBetweenElements;
Selim Cinek67b22602014-03-10 15:40:16 +0100103 }
104
105 private void initConstants(Context context) {
Selim Cinek34c0a8d2014-05-12 00:01:43 +0200106 mPaddingBetweenElementsDimmed = context.getResources()
107 .getDimensionPixelSize(R.dimen.notification_padding_dimmed);
108 mPaddingBetweenElementsNormal = context.getResources()
Jorim Jaggife40f7d2014-04-28 15:20:04 +0200109 .getDimensionPixelSize(R.dimen.notification_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
130 >= DisplayMetrics.DENSITY_XXHIGH;
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
Jorim Jaggid4a57442014-04-10 02:45:55 +0200160 updateVisibleChildren(resultState, algorithmState);
Selim Cinek67b22602014-03-10 15:40:16 +0100161
162 // Phase 1:
163 findNumberOfItemsInTopStackAndUpdateState(resultState, algorithmState);
164
165 // Phase 2:
166 updatePositionsForState(resultState, algorithmState);
167
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 Cinek708a6c12014-05-28 14:16:02 +0200173 updateClipping(resultState, algorithmState);
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,
204 StackScrollAlgorithmState algorithmState) {
205 float previousNotificationEnd = 0;
206 float previousNotificationStart = 0;
207 boolean previousNotificationIsSwiped = false;
208 int childCount = algorithmState.visibleChildren.size();
209 for (int i = 0; i < childCount; i++) {
210 ExpandableView child = algorithmState.visibleChildren.get(i);
Selim Cinekb036ca42015-02-20 15:56:28 +0100211 StackViewState state = resultState.getViewStateForView(child);
Jorim Jaggi2e34ec32014-07-01 03:17:05 +0200212 float newYTranslation = state.yTranslation + state.height * (1f - state.scale) / 2f;
213 float newHeight = state.height * state.scale;
Selim Cinek708a6c12014-05-28 14:16:02 +0200214 // apply clipping and shadow
215 float newNotificationEnd = newYTranslation + newHeight;
216
Selim Cinekc5baa3e2014-10-29 19:04:19 +0100217 float clipHeight;
218 if (previousNotificationIsSwiped) {
219 // When the previous notification is swiped, we don't clip the content to the
220 // bottom of it.
221 clipHeight = newHeight;
222 } else {
223 clipHeight = newNotificationEnd - previousNotificationEnd;
224 clipHeight = Math.max(0.0f, clipHeight);
225 if (clipHeight != 0.0f) {
Selim Cinek708a6c12014-05-28 14:16:02 +0200226
Selim Cinekc5baa3e2014-10-29 19:04:19 +0100227 // In the unlocked shade we have to clip a little bit higher because of the rounded
228 // corners of the notifications, but only if we are not fully overlapped by
229 // the top card.
230 float clippingCorrection = state.dimmed
231 ? 0
232 : mRoundedRectCornerRadius * state.scale;
233 clipHeight += clippingCorrection;
234 }
235 }
Selim Cinek708a6c12014-05-28 14:16:02 +0200236
237 updateChildClippingAndBackground(state, newHeight, clipHeight,
Jorim Jaggi2e34ec32014-07-01 03:17:05 +0200238 newHeight - (previousNotificationStart - newYTranslation));
Selim Cinek708a6c12014-05-28 14:16:02 +0200239
240 if (!child.isTransparent()) {
241 // Only update the previous values if we are not transparent,
242 // otherwise we would clip to a transparent view.
Jorim Jaggi2e34ec32014-07-01 03:17:05 +0200243 previousNotificationStart = newYTranslation + state.clipTopAmount * state.scale;
Selim Cinek708a6c12014-05-28 14:16:02 +0200244 previousNotificationEnd = newNotificationEnd;
245 previousNotificationIsSwiped = child.getTranslationX() != 0;
246 }
247 }
248 }
249
250 /**
251 * Updates the shadow outline and the clipping for a view.
252 *
253 * @param state the viewState to update
254 * @param realHeight the currently applied height of the view
255 * @param clipHeight the desired clip height, the rest of the view will be clipped from the top
256 * @param backgroundHeight the desired background height. The shadows of the view will be
257 * based on this height and the content will be clipped from the top
258 */
Selim Cinekb036ca42015-02-20 15:56:28 +0100259 private void updateChildClippingAndBackground(StackViewState state, float realHeight,
260 float clipHeight, float backgroundHeight) {
Selim Cinek708a6c12014-05-28 14:16:02 +0200261 if (realHeight > clipHeight) {
Jorim Jaggi2e34ec32014-07-01 03:17:05 +0200262 // Rather overlap than create a hole.
263 state.topOverLap = (int) Math.floor((realHeight - clipHeight) / state.scale);
Selim Cinek708a6c12014-05-28 14:16:02 +0200264 } else {
265 state.topOverLap = 0;
266 }
267 if (realHeight > backgroundHeight) {
Jorim Jaggi2e34ec32014-07-01 03:17:05 +0200268 // Rather overlap than create a hole.
269 state.clipTopAmount = (int) Math.floor((realHeight - backgroundHeight) / state.scale);
Selim Cinek708a6c12014-05-28 14:16:02 +0200270 } else {
271 state.clipTopAmount = 0;
272 }
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200273 }
274
275 /**
Jorim Jaggiae441282014-08-01 02:45:18 +0200276 * Updates the dimmed, activated and hiding sensitive states of the children.
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200277 */
Jorim Jaggiae441282014-08-01 02:45:18 +0200278 private void updateDimmedActivatedHideSensitive(AmbientState ambientState,
279 StackScrollState resultState, StackScrollAlgorithmState algorithmState) {
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200280 boolean dimmed = ambientState.isDimmed();
John Spurlockbf370992014-06-17 13:58:31 -0400281 boolean dark = ambientState.isDark();
Jorim Jaggiae441282014-08-01 02:45:18 +0200282 boolean hideSensitive = ambientState.isHideSensitive();
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200283 View activatedChild = ambientState.getActivatedChild();
284 int childCount = algorithmState.visibleChildren.size();
285 for (int i = 0; i < childCount; i++) {
286 View child = algorithmState.visibleChildren.get(i);
Selim Cinekb036ca42015-02-20 15:56:28 +0100287 StackViewState childViewState = resultState.getViewStateForView(child);
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200288 childViewState.dimmed = dimmed;
John Spurlockbf370992014-06-17 13:58:31 -0400289 childViewState.dark = dark;
Jorim Jaggiae441282014-08-01 02:45:18 +0200290 childViewState.hideSensitive = hideSensitive;
Selim Cinekb89de4e2014-06-10 10:47:05 +0200291 boolean isActivatedChild = activatedChild == child;
Jorim Jaggid7c1fae2014-08-13 18:27:47 +0200292 childViewState.scale = !mScaleDimmed || !dimmed || isActivatedChild
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200293 ? 1.0f
294 : DIMMED_SCALE;
Jorim Jaggi4538cee2014-09-09 15:21:38 +0200295 if (dimmed && isActivatedChild) {
296 childViewState.zTranslation += 2.0f * mZDistanceBetweenElements;
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200297 }
298 }
Selim Cinekeb973562014-05-02 17:07:49 +0200299 }
300
301 /**
302 * Handle the special state when views are being dragged
303 */
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200304 private void handleDraggedViews(AmbientState ambientState, StackScrollState resultState,
Selim Cinekeb973562014-05-02 17:07:49 +0200305 StackScrollAlgorithmState algorithmState) {
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200306 ArrayList<View> draggedViews = ambientState.getDraggedViews();
307 for (View draggedView : draggedViews) {
Selim Cinekeb973562014-05-02 17:07:49 +0200308 int childIndex = algorithmState.visibleChildren.indexOf(draggedView);
309 if (childIndex >= 0 && childIndex < algorithmState.visibleChildren.size() - 1) {
310 View nextChild = algorithmState.visibleChildren.get(childIndex + 1);
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200311 if (!draggedViews.contains(nextChild)) {
Selim Cinekeb973562014-05-02 17:07:49 +0200312 // only if the view is not dragged itself we modify its state to be fully
313 // visible
Selim Cinekb036ca42015-02-20 15:56:28 +0100314 StackViewState viewState = resultState.getViewStateForView(
Selim Cinekeb973562014-05-02 17:07:49 +0200315 nextChild);
316 // The child below the dragged one must be fully visible
317 viewState.alpha = 1;
318 }
319
320 // Lets set the alpha to the one it currently has, as its currently being dragged
Selim Cinekb036ca42015-02-20 15:56:28 +0100321 StackViewState viewState = resultState.getViewStateForView(draggedView);
Selim Cinekeb973562014-05-02 17:07:49 +0200322 // The dragged child should keep the set alpha
323 viewState.alpha = draggedView.getAlpha();
324 }
325 }
Selim Cinek343e6e22014-04-11 21:23:30 +0200326 }
Selim Cinek67b22602014-03-10 15:40:16 +0100327
Selim Cinek343e6e22014-04-11 21:23:30 +0200328 /**
Jorim Jaggid4a57442014-04-10 02:45:55 +0200329 * Update the visible children on the state.
330 */
331 private void updateVisibleChildren(StackScrollState resultState,
332 StackScrollAlgorithmState state) {
333 ViewGroup hostView = resultState.getHostView();
334 int childCount = hostView.getChildCount();
335 state.visibleChildren.clear();
336 state.visibleChildren.ensureCapacity(childCount);
Selim Cinekb036ca42015-02-20 15:56:28 +0100337 int notGoneIndex = 0;
Jorim Jaggid4a57442014-04-10 02:45:55 +0200338 for (int i = 0; i < childCount; i++) {
Jorim Jaggibe565df2014-04-28 17:51:23 +0200339 ExpandableView v = (ExpandableView) hostView.getChildAt(i);
Jorim Jaggid4a57442014-04-10 02:45:55 +0200340 if (v.getVisibility() != View.GONE) {
Selim Cinekb036ca42015-02-20 15:56:28 +0100341 StackViewState viewState = resultState.getViewStateForView(v);
342 viewState.notGoneIndex = notGoneIndex;
Jorim Jaggid4a57442014-04-10 02:45:55 +0200343 state.visibleChildren.add(v);
Selim Cinekb036ca42015-02-20 15:56:28 +0100344 notGoneIndex++;
Selim Cinekb5605e52015-02-20 18:21:41 +0100345
346 // handle the notgoneIndex for the children as well
347 if (v instanceof ExpandableNotificationRow) {
348 ExpandableNotificationRow row = (ExpandableNotificationRow) v;
349 List<ExpandableNotificationRow> children =
350 row.getNotificationChildren();
351 if (row.areChildrenExpanded() && children != null) {
352 for (ExpandableNotificationRow childRow : children) {
353 if (childRow.getVisibility() != View.GONE) {
354 StackViewState childState
355 = resultState.getViewStateForView(childRow);
356 childState.notGoneIndex = notGoneIndex;
357 notGoneIndex++;
358 }
359 }
360 }
361 }
Jorim Jaggid4a57442014-04-10 02:45:55 +0200362 }
363 }
364 }
365
366 /**
Selim Cinek67b22602014-03-10 15:40:16 +0100367 * Determine the positions for the views. This is the main part of the algorithm.
368 *
369 * @param resultState The result state to update if a change to the properties of a child occurs
370 * @param algorithmState The state in which the current pass of the algorithm is currently in
Selim Cinek67b22602014-03-10 15:40:16 +0100371 */
372 private void updatePositionsForState(StackScrollState resultState,
373 StackScrollAlgorithmState algorithmState) {
Selim Cinek67b22602014-03-10 15:40:16 +0100374
Selim Cinek1685e632014-04-08 02:27:49 +0200375 // The starting position of the bottom stack peek
Jorim Jaggi8c1a44b2014-04-29 19:04:02 +0200376 float bottomPeekStart = mInnerHeight - mBottomStackPeekSize;
Selim Cinek1685e632014-04-08 02:27:49 +0200377
Selim Cinek67b22602014-03-10 15:40:16 +0100378 // The position where the bottom stack starts.
Selim Cinek34c0a8d2014-05-12 00:01:43 +0200379 float bottomStackStart = bottomPeekStart - mBottomStackSlowDownLength;
Selim Cinek67b22602014-03-10 15:40:16 +0100380
381 // The y coordinate of the current child.
382 float currentYPosition = 0.0f;
383
384 // How far in is the element currently transitioning into the bottom stack.
385 float yPositionInScrollView = 0.0f;
386
Jorim Jaggid4a57442014-04-10 02:45:55 +0200387 int childCount = algorithmState.visibleChildren.size();
Selim Cinek67b22602014-03-10 15:40:16 +0100388 int numberOfElementsCompletelyIn = (int) algorithmState.itemsInTopStack;
389 for (int i = 0; i < childCount; i++) {
Jorim Jaggibe565df2014-04-28 17:51:23 +0200390 ExpandableView child = algorithmState.visibleChildren.get(i);
Selim Cinekb036ca42015-02-20 15:56:28 +0100391 StackViewState childViewState = resultState.getViewStateForView(child);
392 childViewState.location = StackViewState.LOCATION_UNKNOWN;
Jorim Jaggibe565df2014-04-28 17:51:23 +0200393 int childHeight = getMaxAllowedChildHeight(child);
Selim Cinek67b22602014-03-10 15:40:16 +0100394 float yPositionInScrollViewAfterElement = yPositionInScrollView
395 + childHeight
396 + mPaddingBetweenElements;
Selim Cinek343e6e22014-04-11 21:23:30 +0200397 float scrollOffset = yPositionInScrollView - algorithmState.scrollY + mCollapsedSize;
398
399 if (i == algorithmState.lastTopStackIndex + 1) {
400 // Normally the position of this child is the position in the regular scrollview,
401 // but if the two stacks are very close to each other,
402 // then have have to push it even more upwards to the position of the bottom
403 // stack start.
404 currentYPosition = Math.min(scrollOffset, bottomStackStart);
405 }
406 childViewState.yTranslation = currentYPosition;
407
408 // The y position after this element
409 float nextYPosition = currentYPosition + childHeight +
410 mPaddingBetweenElements;
411
412 if (i <= algorithmState.lastTopStackIndex) {
Selim Cinek67b22602014-03-10 15:40:16 +0100413 // Case 1:
414 // We are in the top Stack
Selim Cinek343e6e22014-04-11 21:23:30 +0200415 updateStateForTopStackChild(algorithmState,
416 numberOfElementsCompletelyIn, i, childHeight, childViewState, scrollOffset);
Selim Cinek3afd00e2014-08-11 22:32:57 +0200417 clampPositionToTopStackEnd(childViewState, childHeight);
418
Selim Cinek343e6e22014-04-11 21:23:30 +0200419 // check if we are overlapping with the bottom stack
420 if (childViewState.yTranslation + childHeight + mPaddingBetweenElements
Selim Cinek4581cf82014-08-12 12:40:32 +0200421 >= bottomStackStart && !mIsExpansionChanging && i != 0 && mIsSmallScreen) {
Selim Cinek3afd00e2014-08-11 22:32:57 +0200422 // we just collapse this element slightly
423 int newSize = (int) Math.max(bottomStackStart - mPaddingBetweenElements -
424 childViewState.yTranslation, mCollapsedSize);
425 childViewState.height = newSize;
Selim Cinek4581cf82014-08-12 12:40:32 +0200426 updateStateForChildTransitioningInBottom(algorithmState, bottomStackStart,
427 bottomPeekStart, childViewState.yTranslation, childViewState,
428 childHeight);
Selim Cinek343e6e22014-04-11 21:23:30 +0200429 }
Selim Cinek3afd00e2014-08-11 22:32:57 +0200430 clampPositionToBottomStackStart(childViewState, childViewState.height);
Selim Cinek343e6e22014-04-11 21:23:30 +0200431 } else if (nextYPosition >= bottomStackStart) {
Selim Cinek67b22602014-03-10 15:40:16 +0100432 // Case 2:
Selim Cinek343e6e22014-04-11 21:23:30 +0200433 // We are in the bottom stack.
434 if (currentYPosition >= bottomStackStart) {
Selim Cinek67b22602014-03-10 15:40:16 +0100435 // According to the regular scroll view we are fully translated out of the
436 // bottom of the screen so we are fully in the bottom stack
Selim Cinek343e6e22014-04-11 21:23:30 +0200437 updateStateForChildFullyInBottomStack(algorithmState,
438 bottomStackStart, childViewState, childHeight);
Selim Cinek67b22602014-03-10 15:40:16 +0100439 } else {
Selim Cinek67b22602014-03-10 15:40:16 +0100440 // According to the regular scroll view we are currently translating out of /
441 // into the bottom of the screen
Selim Cinek343e6e22014-04-11 21:23:30 +0200442 updateStateForChildTransitioningInBottom(algorithmState,
443 bottomStackStart, bottomPeekStart, currentYPosition,
444 childViewState, childHeight);
Selim Cinek67b22602014-03-10 15:40:16 +0100445 }
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200446 } else {
Selim Cinek343e6e22014-04-11 21:23:30 +0200447 // Case 3:
448 // We are in the regular scroll area.
Selim Cinekb036ca42015-02-20 15:56:28 +0100449 childViewState.location = StackViewState.LOCATION_MAIN_AREA;
Selim Cinek343e6e22014-04-11 21:23:30 +0200450 clampYTranslation(childViewState, childHeight);
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200451 }
Selim Cinek343e6e22014-04-11 21:23:30 +0200452
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200453 // The first card is always rendered.
454 if (i == 0) {
455 childViewState.alpha = 1.0f;
Selim Cinek8d9ff9c2014-05-12 15:13:04 +0200456 childViewState.yTranslation = Math.max(mCollapsedSize - algorithmState.scrollY, 0);
Selim Cinekd83771e2014-07-04 16:45:31 +0200457 if (childViewState.yTranslation + childViewState.height
458 > bottomPeekStart - mCollapseSecondCardPadding) {
Selim Cinek4fe3e472014-07-03 16:32:54 +0200459 childViewState.height = (int) Math.max(
Selim Cinekd83771e2014-07-04 16:45:31 +0200460 bottomPeekStart - mCollapseSecondCardPadding
461 - childViewState.yTranslation, mCollapsedSize);
Selim Cinek4fe3e472014-07-03 16:32:54 +0200462 }
Selim Cinekb036ca42015-02-20 15:56:28 +0100463 childViewState.location = StackViewState.LOCATION_FIRST_CARD;
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200464 }
Selim Cinekb036ca42015-02-20 15:56:28 +0100465 if (childViewState.location == StackViewState.LOCATION_UNKNOWN) {
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200466 Log.wtf(LOG_TAG, "Failed to assign location for child " + i);
Selim Cinek67b22602014-03-10 15:40:16 +0100467 }
Selim Cinek343e6e22014-04-11 21:23:30 +0200468 currentYPosition = childViewState.yTranslation + childHeight + mPaddingBetweenElements;
Selim Cinek67b22602014-03-10 15:40:16 +0100469 yPositionInScrollView = yPositionInScrollViewAfterElement;
Jorim Jaggi8c1a44b2014-04-29 19:04:02 +0200470
471 childViewState.yTranslation += mTopPadding;
Selim Cinek67b22602014-03-10 15:40:16 +0100472 }
473 }
474
Selim Cinek1685e632014-04-08 02:27:49 +0200475 /**
Selim Cinek343e6e22014-04-11 21:23:30 +0200476 * Clamp the yTranslation both up and down to valid positions.
Selim Cinek1685e632014-04-08 02:27:49 +0200477 *
Selim Cinek1685e632014-04-08 02:27:49 +0200478 * @param childViewState the view state of the child
Selim Cinek343e6e22014-04-11 21:23:30 +0200479 * @param childHeight the height of this child
Selim Cinek1685e632014-04-08 02:27:49 +0200480 */
Selim Cinekb036ca42015-02-20 15:56:28 +0100481 private void clampYTranslation(StackViewState childViewState, int childHeight) {
Selim Cinek343e6e22014-04-11 21:23:30 +0200482 clampPositionToBottomStackStart(childViewState, childHeight);
483 clampPositionToTopStackEnd(childViewState, childHeight);
484 }
485
486 /**
487 * Clamp the yTranslation of the child down such that its end is at most on the beginning of
488 * the bottom stack.
489 *
490 * @param childViewState the view state of the child
491 * @param childHeight the height of this child
492 */
Selim Cinekb036ca42015-02-20 15:56:28 +0100493 private void clampPositionToBottomStackStart(StackViewState childViewState,
Selim Cinek343e6e22014-04-11 21:23:30 +0200494 int childHeight) {
495 childViewState.yTranslation = Math.min(childViewState.yTranslation,
Selim Cinekd83771e2014-07-04 16:45:31 +0200496 mInnerHeight - mBottomStackPeekSize - mCollapseSecondCardPadding - childHeight);
Selim Cinek343e6e22014-04-11 21:23:30 +0200497 }
498
499 /**
500 * Clamp the yTranslation of the child up such that its end is at lest on the end of the top
Jorim Jaggibe565df2014-04-28 17:51:23 +0200501 * stack.get
Selim Cinek343e6e22014-04-11 21:23:30 +0200502 *
503 * @param childViewState the view state of the child
504 * @param childHeight the height of this child
505 */
Selim Cinekb036ca42015-02-20 15:56:28 +0100506 private void clampPositionToTopStackEnd(StackViewState childViewState,
Selim Cinek343e6e22014-04-11 21:23:30 +0200507 int childHeight) {
508 childViewState.yTranslation = Math.max(childViewState.yTranslation,
509 mCollapsedSize - childHeight);
Selim Cinek1685e632014-04-08 02:27:49 +0200510 }
511
512 private int getMaxAllowedChildHeight(View child) {
513 if (child instanceof ExpandableNotificationRow) {
514 ExpandableNotificationRow row = (ExpandableNotificationRow) child;
Jorim Jaggi9cbadd32014-05-01 20:18:31 +0200515 return row.getIntrinsicHeight();
Jorim Jaggibe565df2014-04-28 17:51:23 +0200516 } else if (child instanceof ExpandableView) {
517 ExpandableView expandableView = (ExpandableView) child;
518 return expandableView.getActualHeight();
Selim Cinek1685e632014-04-08 02:27:49 +0200519 }
Jorim Jaggibe565df2014-04-28 17:51:23 +0200520 return child == null? mCollapsedSize : child.getHeight();
Selim Cinek1685e632014-04-08 02:27:49 +0200521 }
522
Selim Cinek343e6e22014-04-11 21:23:30 +0200523 private void updateStateForChildTransitioningInBottom(StackScrollAlgorithmState algorithmState,
524 float transitioningPositionStart, float bottomPeakStart, float currentYPosition,
Selim Cinekb036ca42015-02-20 15:56:28 +0100525 StackViewState childViewState, int childHeight) {
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200526
Selim Cinek343e6e22014-04-11 21:23:30 +0200527 // This is the transitioning element on top of bottom stack, calculate how far we are in.
528 algorithmState.partialInBottom = 1.0f - (
529 (transitioningPositionStart - currentYPosition) / (childHeight +
530 mPaddingBetweenElements));
531
532 // the offset starting at the transitionPosition of the bottom stack
533 float offset = mBottomStackIndentationFunctor.getValue(algorithmState.partialInBottom);
534 algorithmState.itemsInBottomStack += algorithmState.partialInBottom;
Selim Cinek3afd00e2014-08-11 22:32:57 +0200535 int newHeight = childHeight;
536 if (childHeight > mCollapsedSize && mIsSmallScreen) {
537 newHeight = (int) Math.max(Math.min(transitioningPositionStart + offset -
538 mPaddingBetweenElements - currentYPosition, childHeight), mCollapsedSize);
539 childViewState.height = newHeight;
540 }
541 childViewState.yTranslation = transitioningPositionStart + offset - newHeight
Jorim Jaggife40f7d2014-04-28 15:20:04 +0200542 - mPaddingBetweenElements;
Selim Cinek3afd00e2014-08-11 22:32:57 +0200543
Selim Cinek343e6e22014-04-11 21:23:30 +0200544 // We want at least to be at the end of the top stack when collapsing
Selim Cinek3afd00e2014-08-11 22:32:57 +0200545 clampPositionToTopStackEnd(childViewState, newHeight);
Selim Cinekb036ca42015-02-20 15:56:28 +0100546 childViewState.location = StackViewState.LOCATION_MAIN_AREA;
Selim Cinek67b22602014-03-10 15:40:16 +0100547 }
548
Selim Cinek343e6e22014-04-11 21:23:30 +0200549 private void updateStateForChildFullyInBottomStack(StackScrollAlgorithmState algorithmState,
Selim Cinekb036ca42015-02-20 15:56:28 +0100550 float transitioningPositionStart, StackViewState childViewState,
Selim Cinek67b22602014-03-10 15:40:16 +0100551 int childHeight) {
552
Selim Cinek343e6e22014-04-11 21:23:30 +0200553 float currentYPosition;
Selim Cinek67b22602014-03-10 15:40:16 +0100554 algorithmState.itemsInBottomStack += 1.0f;
555 if (algorithmState.itemsInBottomStack < MAX_ITEMS_IN_BOTTOM_STACK) {
556 // We are visually entering the bottom stack
Selim Cinek343e6e22014-04-11 21:23:30 +0200557 currentYPosition = transitioningPositionStart
Jorim Jaggife40f7d2014-04-28 15:20:04 +0200558 + mBottomStackIndentationFunctor.getValue(algorithmState.itemsInBottomStack)
559 - mPaddingBetweenElements;
Selim Cinekb036ca42015-02-20 15:56:28 +0100560 childViewState.location = StackViewState.LOCATION_BOTTOM_STACK_PEEKING;
Selim Cinek67b22602014-03-10 15:40:16 +0100561 } else {
562 // we are fully inside the stack
563 if (algorithmState.itemsInBottomStack > MAX_ITEMS_IN_BOTTOM_STACK + 2) {
564 childViewState.alpha = 0.0f;
565 } else if (algorithmState.itemsInBottomStack
566 > MAX_ITEMS_IN_BOTTOM_STACK + 1) {
567 childViewState.alpha = 1.0f - algorithmState.partialInBottom;
568 }
Selim Cinekb036ca42015-02-20 15:56:28 +0100569 childViewState.location = StackViewState.LOCATION_BOTTOM_STACK_HIDDEN;
Jorim Jaggi8c1a44b2014-04-29 19:04:02 +0200570 currentYPosition = mInnerHeight;
Selim Cinek67b22602014-03-10 15:40:16 +0100571 }
Selim Cinek343e6e22014-04-11 21:23:30 +0200572 childViewState.yTranslation = currentYPosition - childHeight;
573 clampPositionToTopStackEnd(childViewState, childHeight);
Selim Cinek67b22602014-03-10 15:40:16 +0100574 }
575
Selim Cinek343e6e22014-04-11 21:23:30 +0200576 private void updateStateForTopStackChild(StackScrollAlgorithmState algorithmState,
577 int numberOfElementsCompletelyIn, int i, int childHeight,
Selim Cinekb036ca42015-02-20 15:56:28 +0100578 StackViewState childViewState, float scrollOffset) {
Selim Cinek67b22602014-03-10 15:40:16 +0100579
Selim Cinek67b22602014-03-10 15:40:16 +0100580
581 // First we calculate the index relative to the current stack window of size at most
582 // {@link #MAX_ITEMS_IN_TOP_STACK}
Selim Cinek343e6e22014-04-11 21:23:30 +0200583 int paddedIndex = i - 1
Selim Cinek67b22602014-03-10 15:40:16 +0100584 - Math.max(numberOfElementsCompletelyIn - MAX_ITEMS_IN_TOP_STACK, 0);
585 if (paddedIndex >= 0) {
Selim Cinek343e6e22014-04-11 21:23:30 +0200586
Selim Cinek67b22602014-03-10 15:40:16 +0100587 // We are currently visually entering the top stack
Selim Cinekad3e5af2014-07-04 12:24:11 +0200588 float distanceToStack = (childHeight + mPaddingBetweenElements)
589 - algorithmState.scrolledPixelsTop;
590 if (i == algorithmState.lastTopStackIndex
591 && distanceToStack > (mTopStackTotalSize + mPaddingBetweenElements)) {
Selim Cinek343e6e22014-04-11 21:23:30 +0200592
593 // Child is currently translating into stack but not yet inside slow down zone.
594 // Handle it like the regular scrollview.
595 childViewState.yTranslation = scrollOffset;
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200596 } else {
Selim Cinek343e6e22014-04-11 21:23:30 +0200597 // Apply stacking logic.
598 float numItemsBefore;
599 if (i == algorithmState.lastTopStackIndex) {
Selim Cinekad3e5af2014-07-04 12:24:11 +0200600 numItemsBefore = 1.0f
601 - (distanceToStack / (mTopStackTotalSize + mPaddingBetweenElements));
Selim Cinek343e6e22014-04-11 21:23:30 +0200602 } else {
603 numItemsBefore = algorithmState.itemsInTopStack - i;
604 }
605 // The end position of the current child
Selim Cinekad3e5af2014-07-04 12:24:11 +0200606 float currentChildEndY = mCollapsedSize + mTopStackTotalSize
607 - mTopStackIndentationFunctor.getValue(numItemsBefore);
Selim Cinek343e6e22014-04-11 21:23:30 +0200608 childViewState.yTranslation = currentChildEndY - childHeight;
Selim Cinek67b22602014-03-10 15:40:16 +0100609 }
Selim Cinekb036ca42015-02-20 15:56:28 +0100610 childViewState.location = StackViewState.LOCATION_TOP_STACK_PEEKING;
Selim Cinek67b22602014-03-10 15:40:16 +0100611 } else {
Selim Cinek343e6e22014-04-11 21:23:30 +0200612 if (paddedIndex == -1) {
613 childViewState.alpha = 1.0f - algorithmState.partialInTop;
614 } else {
615 // We are hidden behind the top card and faded out, so we can hide ourselves.
616 childViewState.alpha = 0.0f;
617 }
618 childViewState.yTranslation = mCollapsedSize - childHeight;
Selim Cinekb036ca42015-02-20 15:56:28 +0100619 childViewState.location = StackViewState.LOCATION_TOP_STACK_HIDDEN;
Selim Cinek67b22602014-03-10 15:40:16 +0100620 }
Selim Cinek343e6e22014-04-11 21:23:30 +0200621
622
Selim Cinek67b22602014-03-10 15:40:16 +0100623 }
624
625 /**
626 * Find the number of items in the top stack and update the result state if needed.
627 *
628 * @param resultState The result state to update if a height change of an child occurs
629 * @param algorithmState The state in which the current pass of the algorithm is currently in
Selim Cinek67b22602014-03-10 15:40:16 +0100630 */
631 private void findNumberOfItemsInTopStackAndUpdateState(StackScrollState resultState,
632 StackScrollAlgorithmState algorithmState) {
633
634 // The y Position if the element would be in a regular scrollView
635 float yPositionInScrollView = 0.0f;
Jorim Jaggid4a57442014-04-10 02:45:55 +0200636 int childCount = algorithmState.visibleChildren.size();
Selim Cinek67b22602014-03-10 15:40:16 +0100637
638 // find the number of elements in the top stack.
639 for (int i = 0; i < childCount; i++) {
Jorim Jaggibe565df2014-04-28 17:51:23 +0200640 ExpandableView child = algorithmState.visibleChildren.get(i);
Selim Cinekb036ca42015-02-20 15:56:28 +0100641 StackViewState childViewState = resultState.getViewStateForView(child);
Jorim Jaggibe565df2014-04-28 17:51:23 +0200642 int childHeight = getMaxAllowedChildHeight(child);
Selim Cinek67b22602014-03-10 15:40:16 +0100643 float yPositionInScrollViewAfterElement = yPositionInScrollView
644 + childHeight
645 + mPaddingBetweenElements;
646 if (yPositionInScrollView < algorithmState.scrollY) {
Selim Cinek8d9ff9c2014-05-12 15:13:04 +0200647 if (i == 0 && algorithmState.scrollY <= mCollapsedSize) {
Selim Cinek343e6e22014-04-11 21:23:30 +0200648
649 // The starting position of the bottom stack peek
Selim Cinekd83771e2014-07-04 16:45:31 +0200650 int bottomPeekStart = mInnerHeight - mBottomStackPeekSize -
651 mCollapseSecondCardPadding;
Selim Cinek343e6e22014-04-11 21:23:30 +0200652 // Collapse and expand the first child while the shade is being expanded
653 float maxHeight = mIsExpansionChanging && child == mFirstChildWhileExpanding
654 ? mFirstChildMaxHeight
655 : childHeight;
656 childViewState.height = (int) Math.max(Math.min(bottomPeekStart, maxHeight),
657 mCollapsedSize);
658 algorithmState.itemsInTopStack = 1.0f;
659
660 } else if (yPositionInScrollViewAfterElement < algorithmState.scrollY) {
Selim Cinek67b22602014-03-10 15:40:16 +0100661 // According to the regular scroll view we are fully off screen
662 algorithmState.itemsInTopStack += 1.0f;
Selim Cinek343e6e22014-04-11 21:23:30 +0200663 if (i == 0) {
Selim Cinek67b22602014-03-10 15:40:16 +0100664 childViewState.height = mCollapsedSize;
665 }
666 } else {
667 // According to the regular scroll view we are partially off screen
Selim Cinek343e6e22014-04-11 21:23:30 +0200668
Selim Cinek67b22602014-03-10 15:40:16 +0100669 // How much did we scroll into this child
Selim Cinekad3e5af2014-07-04 12:24:11 +0200670 algorithmState.scrolledPixelsTop = algorithmState.scrollY
671 - yPositionInScrollView;
Selim Cinek343e6e22014-04-11 21:23:30 +0200672 algorithmState.partialInTop = (algorithmState.scrolledPixelsTop) / (childHeight
Selim Cinek67b22602014-03-10 15:40:16 +0100673 + mPaddingBetweenElements);
674
675 // Our element can be expanded, so this can get negative
676 algorithmState.partialInTop = Math.max(0.0f, algorithmState.partialInTop);
677 algorithmState.itemsInTopStack += algorithmState.partialInTop;
Selim Cinekad3e5af2014-07-04 12:24:11 +0200678
Selim Cinek343e6e22014-04-11 21:23:30 +0200679 if (i == 0) {
Selim Cinekad3e5af2014-07-04 12:24:11 +0200680 // If it is expanded we have to collapse it to a new size
681 float newSize = yPositionInScrollViewAfterElement
682 - mPaddingBetweenElements
683 - algorithmState.scrollY + mCollapsedSize;
684 newSize = Math.max(mCollapsedSize, newSize);
Selim Cinek4e456be2014-06-12 18:09:43 +0200685 algorithmState.itemsInTopStack = 1.0f;
Selim Cinek67b22602014-03-10 15:40:16 +0100686 childViewState.height = (int) newSize;
Selim Cinek67b22602014-03-10 15:40:16 +0100687 }
Selim Cinek343e6e22014-04-11 21:23:30 +0200688 algorithmState.lastTopStackIndex = i;
689 break;
Selim Cinek67b22602014-03-10 15:40:16 +0100690 }
691 } else {
Selim Cinek343e6e22014-04-11 21:23:30 +0200692 algorithmState.lastTopStackIndex = i - 1;
Selim Cinek67b22602014-03-10 15:40:16 +0100693 // We are already past the stack so we can end the loop
694 break;
695 }
696 yPositionInScrollView = yPositionInScrollViewAfterElement;
697 }
698 }
699
700 /**
701 * Calculate the Z positions for all children based on the number of items in both stacks and
702 * save it in the resultState
703 *
704 * @param resultState The result state to update the zTranslation values
705 * @param algorithmState The state in which the current pass of the algorithm is currently in
706 */
707 private void updateZValuesForState(StackScrollState resultState,
708 StackScrollAlgorithmState algorithmState) {
Jorim Jaggid4a57442014-04-10 02:45:55 +0200709 int childCount = algorithmState.visibleChildren.size();
Selim Cinek67b22602014-03-10 15:40:16 +0100710 for (int i = 0; i < childCount; i++) {
Jorim Jaggid4a57442014-04-10 02:45:55 +0200711 View child = algorithmState.visibleChildren.get(i);
Selim Cinekb036ca42015-02-20 15:56:28 +0100712 StackViewState childViewState = resultState.getViewStateForView(child);
Selim Cinek67b22602014-03-10 15:40:16 +0100713 if (i < algorithmState.itemsInTopStack) {
714 float stackIndex = algorithmState.itemsInTopStack - i;
Selim Cinekc5baa3e2014-10-29 19:04:19 +0100715
716 // Ensure that the topmost item is a little bit higher than the rest when fully
717 // scrolled, to avoid drawing errors when swiping it out
718 float max = MAX_ITEMS_IN_TOP_STACK + (i == 0 ? 2.5f : 2);
719 stackIndex = Math.min(stackIndex, max);
Selim Cinek4e456be2014-06-12 18:09:43 +0200720 if (i == 0 && algorithmState.itemsInTopStack < 2.0f) {
721
722 // We only have the top item and an additional item in the top stack,
723 // Interpolate the index from 0 to 2 while the second item is
724 // translating in.
725 stackIndex -= 1.0f;
726 if (algorithmState.scrollY > mCollapsedSize) {
727
728 // Since there is a shadow treshhold, we cant just interpolate from 0 to
729 // 2 but we interpolate from 0.1f to 2.0f when scrolled in. The jump in
730 // height will not be noticable since we have padding in between.
731 stackIndex = 0.1f + stackIndex * 1.9f;
732 }
733 }
Selim Cinek67b22602014-03-10 15:40:16 +0100734 childViewState.zTranslation = mZBasicHeight
735 + stackIndex * mZDistanceBetweenElements;
736 } else if (i > (childCount - 1 - algorithmState.itemsInBottomStack)) {
737 float numItemsAbove = i - (childCount - 1 - algorithmState.itemsInBottomStack);
738 float translationZ = mZBasicHeight
739 - numItemsAbove * mZDistanceBetweenElements;
740 childViewState.zTranslation = translationZ;
741 } else {
742 childViewState.zTranslation = mZBasicHeight;
743 }
744 }
745 }
746
Selim Cinek343e6e22014-04-11 21:23:30 +0200747 public void setLayoutHeight(int layoutHeight) {
Selim Cinek67b22602014-03-10 15:40:16 +0100748 this.mLayoutHeight = layoutHeight;
Jorim Jaggi8c1a44b2014-04-29 19:04:02 +0200749 updateInnerHeight();
750 }
751
752 public void setTopPadding(int topPadding) {
753 mTopPadding = topPadding;
754 updateInnerHeight();
755 }
756
757 private void updateInnerHeight() {
758 mInnerHeight = mLayoutHeight - mTopPadding;
Selim Cinek67b22602014-03-10 15:40:16 +0100759 }
760
Selim Cinek3afd00e2014-08-11 22:32:57 +0200761
762 /**
763 * Update whether the device is very small, i.e. Notifications can be in both the top and the
764 * bottom stack at the same time
765 *
766 * @param panelHeight The normal height of the panel when it's open
767 */
768 public void updateIsSmallScreen(int panelHeight) {
769 mIsSmallScreen = panelHeight <
770 mCollapsedSize /* top stack */
771 + mBottomStackSlowDownLength + mBottomStackPeekSize /* bottom stack */
772 + mMaxNotificationHeight; /* max notification height */
773 }
774
Selim Cinek1685e632014-04-08 02:27:49 +0200775 public void onExpansionStarted(StackScrollState currentState) {
776 mIsExpansionChanging = true;
777 mExpandedOnStart = mIsExpanded;
778 ViewGroup hostView = currentState.getHostView();
779 updateFirstChildHeightWhileExpanding(hostView);
780 }
781
782 private void updateFirstChildHeightWhileExpanding(ViewGroup hostView) {
Jorim Jaggibe565df2014-04-28 17:51:23 +0200783 mFirstChildWhileExpanding = (ExpandableView) findFirstVisibleChild(hostView);
Jorim Jaggi9f347ae2014-04-11 00:47:18 +0200784 if (mFirstChildWhileExpanding != null) {
Selim Cinek1685e632014-04-08 02:27:49 +0200785 if (mExpandedOnStart) {
786
787 // We are collapsing the shade, so the first child can get as most as high as the
Selim Cinek02af41e2014-10-14 15:46:43 +0200788 // current height or the end value of the animation.
789 mFirstChildMaxHeight = StackStateAnimator.getFinalActualHeight(
790 mFirstChildWhileExpanding);
Selim Cinek1685e632014-04-08 02:27:49 +0200791 } else {
Selim Cinek31094df2014-08-14 19:28:15 +0200792 updateFirstChildMaxSizeToMaxHeight();
Selim Cinek1685e632014-04-08 02:27:49 +0200793 }
794 } else {
Selim Cinek1685e632014-04-08 02:27:49 +0200795 mFirstChildMaxHeight = 0;
796 }
797 }
798
Selim Cinek31094df2014-08-14 19:28:15 +0200799 private void updateFirstChildMaxSizeToMaxHeight() {
800 // We are expanding the shade, expand it to its full height.
801 if (!isMaxSizeInitialized(mFirstChildWhileExpanding)) {
802
803 // This child was not layouted yet, wait for a layout pass
804 mFirstChildWhileExpanding
805 .addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
806 @Override
807 public void onLayoutChange(View v, int left, int top, int right,
808 int bottom, int oldLeft, int oldTop, int oldRight,
809 int oldBottom) {
810 if (mFirstChildWhileExpanding != null) {
811 mFirstChildMaxHeight = getMaxAllowedChildHeight(
812 mFirstChildWhileExpanding);
813 } else {
814 mFirstChildMaxHeight = 0;
815 }
816 v.removeOnLayoutChangeListener(this);
817 }
818 });
819 } else {
820 mFirstChildMaxHeight = getMaxAllowedChildHeight(mFirstChildWhileExpanding);
821 }
822 }
823
Selim Cinek7d447722014-06-10 15:51:59 +0200824 private boolean isMaxSizeInitialized(ExpandableView child) {
825 if (child instanceof ExpandableNotificationRow) {
826 ExpandableNotificationRow row = (ExpandableNotificationRow) child;
Selim Cinek31094df2014-08-14 19:28:15 +0200827 return row.isMaxExpandHeightInitialized();
Selim Cinek7d447722014-06-10 15:51:59 +0200828 }
829 return child == null || child.getWidth() != 0;
830 }
831
Jorim Jaggi9f347ae2014-04-11 00:47:18 +0200832 private View findFirstVisibleChild(ViewGroup container) {
833 int childCount = container.getChildCount();
834 for (int i = 0; i < childCount; i++) {
835 View child = container.getChildAt(i);
836 if (child.getVisibility() != View.GONE) {
837 return child;
838 }
839 }
840 return null;
841 }
842
Selim Cinek1685e632014-04-08 02:27:49 +0200843 public void onExpansionStopped() {
844 mIsExpansionChanging = false;
845 mFirstChildWhileExpanding = null;
846 }
847
848 public void setIsExpanded(boolean isExpanded) {
849 this.mIsExpanded = isExpanded;
850 }
851
Selim Cinek02af41e2014-10-14 15:46:43 +0200852 public void notifyChildrenChanged(final ViewGroup hostView) {
Selim Cinek1685e632014-04-08 02:27:49 +0200853 if (mIsExpansionChanging) {
Selim Cinek02af41e2014-10-14 15:46:43 +0200854 hostView.post(new Runnable() {
855 @Override
856 public void run() {
857 updateFirstChildHeightWhileExpanding(hostView);
858 }
859 });
Selim Cinek1685e632014-04-08 02:27:49 +0200860 }
861 }
862
Selim Cinek34c0a8d2014-05-12 00:01:43 +0200863 public void setDimmed(boolean dimmed) {
864 updatePadding(dimmed);
865 }
866
Selim Cinek31094df2014-08-14 19:28:15 +0200867 public void onReset(ExpandableView view) {
868 if (view.equals(mFirstChildWhileExpanding)) {
869 updateFirstChildMaxSizeToMaxHeight();
870 }
871 }
872
Selim Cinek67b22602014-03-10 15:40:16 +0100873 class StackScrollAlgorithmState {
874
875 /**
876 * The scroll position of the algorithm
877 */
878 public int scrollY;
879
880 /**
881 * The quantity of items which are in the top stack.
882 */
883 public float itemsInTopStack;
884
885 /**
886 * how far in is the element currently transitioning into the top stack
887 */
888 public float partialInTop;
889
890 /**
Selim Cinek343e6e22014-04-11 21:23:30 +0200891 * The number of pixels the last child in the top stack has scrolled in to the stack
892 */
893 public float scrolledPixelsTop;
894
895 /**
Selim Cinek67b22602014-03-10 15:40:16 +0100896 * The last item index which is in the top stack.
Selim Cinek67b22602014-03-10 15:40:16 +0100897 */
898 public int lastTopStackIndex;
899
900 /**
901 * The quantity of items which are in the bottom stack.
902 */
903 public float itemsInBottomStack;
904
905 /**
906 * how far in is the element currently transitioning into the bottom stack
907 */
908 public float partialInBottom;
Jorim Jaggid4a57442014-04-10 02:45:55 +0200909
910 /**
911 * The children from the host view which are not gone.
912 */
Jorim Jaggibe565df2014-04-28 17:51:23 +0200913 public final ArrayList<ExpandableView> visibleChildren = new ArrayList<ExpandableView>();
Selim Cinek67b22602014-03-10 15:40:16 +0100914 }
915
916}