blob: 42edbf3203768538732b66e646e6d0d413ca2131 [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;
Christoph Studer6e3eceb2014-04-01 18:40:27 +020020import android.util.Log;
Selim Cinek67b22602014-03-10 15:40:16 +010021import android.view.View;
22import android.view.ViewGroup;
Selim Cinek343e6e22014-04-11 21:23:30 +020023
Selim Cinek67b22602014-03-10 15:40:16 +010024import com.android.systemui.R;
Selim Cinek1685e632014-04-08 02:27:49 +020025import com.android.systemui.statusbar.ExpandableNotificationRow;
Jorim Jaggibe565df2014-04-28 17:51:23 +020026import com.android.systemui.statusbar.ExpandableView;
Selim Cinek67b22602014-03-10 15:40:16 +010027
Jorim Jaggid4a57442014-04-10 02:45:55 +020028import java.util.ArrayList;
29
Selim Cinek67b22602014-03-10 15:40:16 +010030/**
31 * The Algorithm of the {@link com.android.systemui.statusbar.stack
32 * .NotificationStackScrollLayout} which can be queried for {@link com.android.systemui.statusbar
33 * .stack.StackScrollState}
34 */
35public class StackScrollAlgorithm {
36
Christoph Studer6e3eceb2014-04-01 18:40:27 +020037 private static final String LOG_TAG = "StackScrollAlgorithm";
38
Selim Cinek67b22602014-03-10 15:40:16 +010039 private static final int MAX_ITEMS_IN_BOTTOM_STACK = 3;
40 private static final int MAX_ITEMS_IN_TOP_STACK = 3;
41
42 private int mPaddingBetweenElements;
43 private int mCollapsedSize;
44 private int mTopStackPeekSize;
45 private int mBottomStackPeekSize;
46 private int mZDistanceBetweenElements;
47 private int mZBasicHeight;
48
49 private StackIndentationFunctor mTopStackIndentationFunctor;
50 private StackIndentationFunctor mBottomStackIndentationFunctor;
51
Selim Cinek343e6e22014-04-11 21:23:30 +020052 private int mLayoutHeight;
Jorim Jaggi8c1a44b2014-04-29 19:04:02 +020053
54 /** mLayoutHeight - mTopPadding */
55 private int mInnerHeight;
56 private int mTopPadding;
Selim Cinek67b22602014-03-10 15:40:16 +010057 private StackScrollAlgorithmState mTempAlgorithmState = new StackScrollAlgorithmState();
Selim Cinek1685e632014-04-08 02:27:49 +020058 private boolean mIsExpansionChanging;
59 private int mFirstChildMaxHeight;
60 private boolean mIsExpanded;
Jorim Jaggibe565df2014-04-28 17:51:23 +020061 private ExpandableView mFirstChildWhileExpanding;
Selim Cinek1685e632014-04-08 02:27:49 +020062 private boolean mExpandedOnStart;
Selim Cinek343e6e22014-04-11 21:23:30 +020063 private int mTopStackTotalSize;
Selim Cinek67b22602014-03-10 15:40:16 +010064
65 public StackScrollAlgorithm(Context context) {
66 initConstants(context);
67 }
68
69 private void initConstants(Context context) {
Jorim Jaggife40f7d2014-04-28 15:20:04 +020070 mPaddingBetweenElements = context.getResources()
71 .getDimensionPixelSize(R.dimen.notification_padding);
Selim Cinek67b22602014-03-10 15:40:16 +010072 mCollapsedSize = context.getResources()
Jorim Jaggife40f7d2014-04-28 15:20:04 +020073 .getDimensionPixelSize(R.dimen.notification_min_height);
Selim Cinek67b22602014-03-10 15:40:16 +010074 mTopStackPeekSize = context.getResources()
75 .getDimensionPixelSize(R.dimen.top_stack_peek_amount);
76 mBottomStackPeekSize = context.getResources()
77 .getDimensionPixelSize(R.dimen.bottom_stack_peek_amount);
78 mZDistanceBetweenElements = context.getResources()
79 .getDimensionPixelSize(R.dimen.z_distance_between_notifications);
80 mZBasicHeight = (MAX_ITEMS_IN_BOTTOM_STACK + 1) * mZDistanceBetweenElements;
Selim Cinek343e6e22014-04-11 21:23:30 +020081 mTopStackTotalSize = mCollapsedSize + mPaddingBetweenElements;
Selim Cinek67b22602014-03-10 15:40:16 +010082 mTopStackIndentationFunctor = new PiecewiseLinearIndentationFunctor(
83 MAX_ITEMS_IN_TOP_STACK,
84 mTopStackPeekSize,
Selim Cinek343e6e22014-04-11 21:23:30 +020085 mTopStackTotalSize,
Selim Cinek67b22602014-03-10 15:40:16 +010086 0.5f);
87 mBottomStackIndentationFunctor = new PiecewiseLinearIndentationFunctor(
88 MAX_ITEMS_IN_BOTTOM_STACK,
89 mBottomStackPeekSize,
Jorim Jaggibe565df2014-04-28 17:51:23 +020090 mCollapsedSize + mBottomStackPeekSize + mPaddingBetweenElements,
Selim Cinek67b22602014-03-10 15:40:16 +010091 0.5f);
92 }
93
94
95 public void getStackScrollState(StackScrollState resultState) {
96 // The state of the local variables are saved in an algorithmState to easily subdivide it
97 // into multiple phases.
98 StackScrollAlgorithmState algorithmState = mTempAlgorithmState;
99
100 // First we reset the view states to their default values.
101 resultState.resetViewStates();
102
Selim Cinek343e6e22014-04-11 21:23:30 +0200103 algorithmState.itemsInTopStack = 0.0f;
Selim Cinek67b22602014-03-10 15:40:16 +0100104 algorithmState.partialInTop = 0.0f;
105 algorithmState.lastTopStackIndex = 0;
Selim Cinek343e6e22014-04-11 21:23:30 +0200106 algorithmState.scrolledPixelsTop = 0;
Selim Cinek67b22602014-03-10 15:40:16 +0100107 algorithmState.itemsInBottomStack = 0.0f;
Selim Cinek343e6e22014-04-11 21:23:30 +0200108 algorithmState.partialInBottom = 0.0f;
Jorim Jaggibe565df2014-04-28 17:51:23 +0200109 algorithmState.scrollY = resultState.getScrollY() + mCollapsedSize;
Selim Cinek343e6e22014-04-11 21:23:30 +0200110
Jorim Jaggid4a57442014-04-10 02:45:55 +0200111 updateVisibleChildren(resultState, algorithmState);
Selim Cinek67b22602014-03-10 15:40:16 +0100112
113 // Phase 1:
114 findNumberOfItemsInTopStackAndUpdateState(resultState, algorithmState);
115
116 // Phase 2:
117 updatePositionsForState(resultState, algorithmState);
118
119 // Phase 3:
120 updateZValuesForState(resultState, algorithmState);
Selim Cinek343e6e22014-04-11 21:23:30 +0200121 }
Selim Cinek67b22602014-03-10 15:40:16 +0100122
Selim Cinek343e6e22014-04-11 21:23:30 +0200123 /**
Jorim Jaggid4a57442014-04-10 02:45:55 +0200124 * Update the visible children on the state.
125 */
126 private void updateVisibleChildren(StackScrollState resultState,
127 StackScrollAlgorithmState state) {
128 ViewGroup hostView = resultState.getHostView();
129 int childCount = hostView.getChildCount();
130 state.visibleChildren.clear();
131 state.visibleChildren.ensureCapacity(childCount);
132 for (int i = 0; i < childCount; i++) {
Jorim Jaggibe565df2014-04-28 17:51:23 +0200133 ExpandableView v = (ExpandableView) hostView.getChildAt(i);
Jorim Jaggid4a57442014-04-10 02:45:55 +0200134 if (v.getVisibility() != View.GONE) {
135 state.visibleChildren.add(v);
136 }
137 }
138 }
139
140 /**
Selim Cinek67b22602014-03-10 15:40:16 +0100141 * Determine the positions for the views. This is the main part of the algorithm.
142 *
143 * @param resultState The result state to update if a change to the properties of a child occurs
144 * @param algorithmState The state in which the current pass of the algorithm is currently in
145 * and which will be updated
146 */
147 private void updatePositionsForState(StackScrollState resultState,
148 StackScrollAlgorithmState algorithmState) {
Selim Cinek67b22602014-03-10 15:40:16 +0100149
Selim Cinek1685e632014-04-08 02:27:49 +0200150 // The starting position of the bottom stack peek
Jorim Jaggi8c1a44b2014-04-29 19:04:02 +0200151 float bottomPeekStart = mInnerHeight - mBottomStackPeekSize;
Selim Cinek1685e632014-04-08 02:27:49 +0200152
Selim Cinek67b22602014-03-10 15:40:16 +0100153 // The position where the bottom stack starts.
Selim Cinek343e6e22014-04-11 21:23:30 +0200154 float bottomStackStart = bottomPeekStart - mCollapsedSize;
Selim Cinek67b22602014-03-10 15:40:16 +0100155
156 // The y coordinate of the current child.
157 float currentYPosition = 0.0f;
158
159 // How far in is the element currently transitioning into the bottom stack.
160 float yPositionInScrollView = 0.0f;
161
Jorim Jaggid4a57442014-04-10 02:45:55 +0200162 int childCount = algorithmState.visibleChildren.size();
Selim Cinek67b22602014-03-10 15:40:16 +0100163 int numberOfElementsCompletelyIn = (int) algorithmState.itemsInTopStack;
164 for (int i = 0; i < childCount; i++) {
Jorim Jaggibe565df2014-04-28 17:51:23 +0200165 ExpandableView child = algorithmState.visibleChildren.get(i);
Selim Cinek67b22602014-03-10 15:40:16 +0100166 StackScrollState.ViewState childViewState = resultState.getViewStateForView(child);
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200167 childViewState.location = StackScrollState.ViewState.LOCATION_UNKNOWN;
Jorim Jaggibe565df2014-04-28 17:51:23 +0200168 int childHeight = getMaxAllowedChildHeight(child);
Selim Cinek67b22602014-03-10 15:40:16 +0100169 float yPositionInScrollViewAfterElement = yPositionInScrollView
170 + childHeight
171 + mPaddingBetweenElements;
Selim Cinek343e6e22014-04-11 21:23:30 +0200172 float scrollOffset = yPositionInScrollView - algorithmState.scrollY + mCollapsedSize;
173
174 if (i == algorithmState.lastTopStackIndex + 1) {
175 // Normally the position of this child is the position in the regular scrollview,
176 // but if the two stacks are very close to each other,
177 // then have have to push it even more upwards to the position of the bottom
178 // stack start.
179 currentYPosition = Math.min(scrollOffset, bottomStackStart);
180 }
181 childViewState.yTranslation = currentYPosition;
182
183 // The y position after this element
184 float nextYPosition = currentYPosition + childHeight +
185 mPaddingBetweenElements;
186
187 if (i <= algorithmState.lastTopStackIndex) {
Selim Cinek67b22602014-03-10 15:40:16 +0100188 // Case 1:
189 // We are in the top Stack
Selim Cinek343e6e22014-04-11 21:23:30 +0200190 updateStateForTopStackChild(algorithmState,
191 numberOfElementsCompletelyIn, i, childHeight, childViewState, scrollOffset);
192 clampYTranslation(childViewState, childHeight);
193 // check if we are overlapping with the bottom stack
194 if (childViewState.yTranslation + childHeight + mPaddingBetweenElements
Selim Cinek4a1ac842014-05-01 15:51:58 +0200195 >= bottomStackStart && !mIsExpansionChanging && i != 0) {
Selim Cinek343e6e22014-04-11 21:23:30 +0200196 // TODO: handle overlapping sizes with end stack better
197 // we just collapse this element
198 childViewState.height = mCollapsedSize;
199 }
200 } else if (nextYPosition >= bottomStackStart) {
Selim Cinek67b22602014-03-10 15:40:16 +0100201 // Case 2:
Selim Cinek343e6e22014-04-11 21:23:30 +0200202 // We are in the bottom stack.
203 if (currentYPosition >= bottomStackStart) {
Selim Cinek67b22602014-03-10 15:40:16 +0100204 // According to the regular scroll view we are fully translated out of the
205 // bottom of the screen so we are fully in the bottom stack
Selim Cinek343e6e22014-04-11 21:23:30 +0200206 updateStateForChildFullyInBottomStack(algorithmState,
207 bottomStackStart, childViewState, childHeight);
Selim Cinek67b22602014-03-10 15:40:16 +0100208 } else {
Selim Cinek67b22602014-03-10 15:40:16 +0100209 // According to the regular scroll view we are currently translating out of /
210 // into the bottom of the screen
Selim Cinek343e6e22014-04-11 21:23:30 +0200211 updateStateForChildTransitioningInBottom(algorithmState,
212 bottomStackStart, bottomPeekStart, currentYPosition,
213 childViewState, childHeight);
Selim Cinek67b22602014-03-10 15:40:16 +0100214 }
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200215 } else {
Selim Cinek343e6e22014-04-11 21:23:30 +0200216 // Case 3:
217 // We are in the regular scroll area.
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200218 childViewState.location = StackScrollState.ViewState.LOCATION_MAIN_AREA;
Selim Cinek343e6e22014-04-11 21:23:30 +0200219 clampYTranslation(childViewState, childHeight);
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200220 }
Selim Cinek343e6e22014-04-11 21:23:30 +0200221
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200222 // The first card is always rendered.
223 if (i == 0) {
224 childViewState.alpha = 1.0f;
Selim Cinek343e6e22014-04-11 21:23:30 +0200225 childViewState.yTranslation = 0;
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200226 childViewState.location = StackScrollState.ViewState.LOCATION_FIRST_CARD;
227 }
228 if (childViewState.location == StackScrollState.ViewState.LOCATION_UNKNOWN) {
229 Log.wtf(LOG_TAG, "Failed to assign location for child " + i);
Selim Cinek67b22602014-03-10 15:40:16 +0100230 }
Selim Cinek343e6e22014-04-11 21:23:30 +0200231 currentYPosition = childViewState.yTranslation + childHeight + mPaddingBetweenElements;
Selim Cinek67b22602014-03-10 15:40:16 +0100232 yPositionInScrollView = yPositionInScrollViewAfterElement;
Jorim Jaggi8c1a44b2014-04-29 19:04:02 +0200233
234 childViewState.yTranslation += mTopPadding;
Selim Cinek67b22602014-03-10 15:40:16 +0100235 }
236 }
237
Selim Cinek1685e632014-04-08 02:27:49 +0200238 /**
Selim Cinek343e6e22014-04-11 21:23:30 +0200239 * Clamp the yTranslation both up and down to valid positions.
Selim Cinek1685e632014-04-08 02:27:49 +0200240 *
Selim Cinek1685e632014-04-08 02:27:49 +0200241 * @param childViewState the view state of the child
Selim Cinek343e6e22014-04-11 21:23:30 +0200242 * @param childHeight the height of this child
Selim Cinek1685e632014-04-08 02:27:49 +0200243 */
Selim Cinek343e6e22014-04-11 21:23:30 +0200244 private void clampYTranslation(StackScrollState.ViewState childViewState, int childHeight) {
245 clampPositionToBottomStackStart(childViewState, childHeight);
246 clampPositionToTopStackEnd(childViewState, childHeight);
247 }
248
249 /**
250 * Clamp the yTranslation of the child down such that its end is at most on the beginning of
251 * the bottom stack.
252 *
253 * @param childViewState the view state of the child
254 * @param childHeight the height of this child
255 */
256 private void clampPositionToBottomStackStart(StackScrollState.ViewState childViewState,
257 int childHeight) {
258 childViewState.yTranslation = Math.min(childViewState.yTranslation,
Jorim Jaggi8c1a44b2014-04-29 19:04:02 +0200259 mInnerHeight - mBottomStackPeekSize - childHeight);
Selim Cinek343e6e22014-04-11 21:23:30 +0200260 }
261
262 /**
263 * 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 +0200264 * stack.get
Selim Cinek343e6e22014-04-11 21:23:30 +0200265 *
266 * @param childViewState the view state of the child
267 * @param childHeight the height of this child
268 */
269 private void clampPositionToTopStackEnd(StackScrollState.ViewState childViewState,
270 int childHeight) {
271 childViewState.yTranslation = Math.max(childViewState.yTranslation,
272 mCollapsedSize - childHeight);
Selim Cinek1685e632014-04-08 02:27:49 +0200273 }
274
275 private int getMaxAllowedChildHeight(View child) {
276 if (child instanceof ExpandableNotificationRow) {
277 ExpandableNotificationRow row = (ExpandableNotificationRow) child;
278 return row.getMaximumAllowedExpandHeight();
Jorim Jaggibe565df2014-04-28 17:51:23 +0200279 } else if (child instanceof ExpandableView) {
280 ExpandableView expandableView = (ExpandableView) child;
281 return expandableView.getActualHeight();
Selim Cinek1685e632014-04-08 02:27:49 +0200282 }
Jorim Jaggibe565df2014-04-28 17:51:23 +0200283 return child == null? mCollapsedSize : child.getHeight();
Selim Cinek1685e632014-04-08 02:27:49 +0200284 }
285
Selim Cinek343e6e22014-04-11 21:23:30 +0200286 private void updateStateForChildTransitioningInBottom(StackScrollAlgorithmState algorithmState,
287 float transitioningPositionStart, float bottomPeakStart, float currentYPosition,
288 StackScrollState.ViewState childViewState, int childHeight) {
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200289
Selim Cinek343e6e22014-04-11 21:23:30 +0200290 // This is the transitioning element on top of bottom stack, calculate how far we are in.
291 algorithmState.partialInBottom = 1.0f - (
292 (transitioningPositionStart - currentYPosition) / (childHeight +
293 mPaddingBetweenElements));
294
295 // the offset starting at the transitionPosition of the bottom stack
296 float offset = mBottomStackIndentationFunctor.getValue(algorithmState.partialInBottom);
297 algorithmState.itemsInBottomStack += algorithmState.partialInBottom;
Jorim Jaggife40f7d2014-04-28 15:20:04 +0200298 childViewState.yTranslation = transitioningPositionStart + offset - childHeight
299 - mPaddingBetweenElements;
Selim Cinek343e6e22014-04-11 21:23:30 +0200300
301 // We want at least to be at the end of the top stack when collapsing
302 clampPositionToTopStackEnd(childViewState, childHeight);
303 childViewState.location = StackScrollState.ViewState.LOCATION_MAIN_AREA;
Selim Cinek67b22602014-03-10 15:40:16 +0100304 }
305
Selim Cinek343e6e22014-04-11 21:23:30 +0200306 private void updateStateForChildFullyInBottomStack(StackScrollAlgorithmState algorithmState,
Selim Cinek67b22602014-03-10 15:40:16 +0100307 float transitioningPositionStart, StackScrollState.ViewState childViewState,
308 int childHeight) {
309
Selim Cinek343e6e22014-04-11 21:23:30 +0200310 float currentYPosition;
Selim Cinek67b22602014-03-10 15:40:16 +0100311 algorithmState.itemsInBottomStack += 1.0f;
312 if (algorithmState.itemsInBottomStack < MAX_ITEMS_IN_BOTTOM_STACK) {
313 // We are visually entering the bottom stack
Selim Cinek343e6e22014-04-11 21:23:30 +0200314 currentYPosition = transitioningPositionStart
Jorim Jaggife40f7d2014-04-28 15:20:04 +0200315 + mBottomStackIndentationFunctor.getValue(algorithmState.itemsInBottomStack)
316 - mPaddingBetweenElements;
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200317 childViewState.location = StackScrollState.ViewState.LOCATION_BOTTOM_STACK_PEEKING;
Selim Cinek67b22602014-03-10 15:40:16 +0100318 } else {
319 // we are fully inside the stack
320 if (algorithmState.itemsInBottomStack > MAX_ITEMS_IN_BOTTOM_STACK + 2) {
321 childViewState.alpha = 0.0f;
322 } else if (algorithmState.itemsInBottomStack
323 > MAX_ITEMS_IN_BOTTOM_STACK + 1) {
324 childViewState.alpha = 1.0f - algorithmState.partialInBottom;
325 }
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200326 childViewState.location = StackScrollState.ViewState.LOCATION_BOTTOM_STACK_HIDDEN;
Jorim Jaggi8c1a44b2014-04-29 19:04:02 +0200327 currentYPosition = mInnerHeight;
Selim Cinek67b22602014-03-10 15:40:16 +0100328 }
Selim Cinek343e6e22014-04-11 21:23:30 +0200329 childViewState.yTranslation = currentYPosition - childHeight;
330 clampPositionToTopStackEnd(childViewState, childHeight);
Selim Cinek67b22602014-03-10 15:40:16 +0100331 }
332
Selim Cinek343e6e22014-04-11 21:23:30 +0200333 private void updateStateForTopStackChild(StackScrollAlgorithmState algorithmState,
334 int numberOfElementsCompletelyIn, int i, int childHeight,
335 StackScrollState.ViewState childViewState, float scrollOffset) {
Selim Cinek67b22602014-03-10 15:40:16 +0100336
Selim Cinek67b22602014-03-10 15:40:16 +0100337
338 // First we calculate the index relative to the current stack window of size at most
339 // {@link #MAX_ITEMS_IN_TOP_STACK}
Selim Cinek343e6e22014-04-11 21:23:30 +0200340 int paddedIndex = i - 1
Selim Cinek67b22602014-03-10 15:40:16 +0100341 - Math.max(numberOfElementsCompletelyIn - MAX_ITEMS_IN_TOP_STACK, 0);
342 if (paddedIndex >= 0) {
Selim Cinek343e6e22014-04-11 21:23:30 +0200343
Selim Cinek67b22602014-03-10 15:40:16 +0100344 // We are currently visually entering the top stack
Selim Cinek343e6e22014-04-11 21:23:30 +0200345 float distanceToStack = childHeight - algorithmState.scrolledPixelsTop;
346 if (i == algorithmState.lastTopStackIndex && distanceToStack > mTopStackTotalSize) {
347
348 // Child is currently translating into stack but not yet inside slow down zone.
349 // Handle it like the regular scrollview.
350 childViewState.yTranslation = scrollOffset;
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200351 } else {
Selim Cinek343e6e22014-04-11 21:23:30 +0200352 // Apply stacking logic.
353 float numItemsBefore;
354 if (i == algorithmState.lastTopStackIndex) {
355 numItemsBefore = 1.0f - (distanceToStack / mTopStackTotalSize);
356 } else {
357 numItemsBefore = algorithmState.itemsInTopStack - i;
358 }
359 // The end position of the current child
360 float currentChildEndY = mCollapsedSize + mTopStackTotalSize -
361 mTopStackIndentationFunctor.getValue(numItemsBefore);
362 childViewState.yTranslation = currentChildEndY - childHeight;
Selim Cinek67b22602014-03-10 15:40:16 +0100363 }
Selim Cinek343e6e22014-04-11 21:23:30 +0200364 childViewState.location = StackScrollState.ViewState.LOCATION_TOP_STACK_PEEKING;
Selim Cinek67b22602014-03-10 15:40:16 +0100365 } else {
Selim Cinek343e6e22014-04-11 21:23:30 +0200366 if (paddedIndex == -1) {
367 childViewState.alpha = 1.0f - algorithmState.partialInTop;
368 } else {
369 // We are hidden behind the top card and faded out, so we can hide ourselves.
370 childViewState.alpha = 0.0f;
371 }
372 childViewState.yTranslation = mCollapsedSize - childHeight;
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200373 childViewState.location = StackScrollState.ViewState.LOCATION_TOP_STACK_HIDDEN;
Selim Cinek67b22602014-03-10 15:40:16 +0100374 }
Selim Cinek343e6e22014-04-11 21:23:30 +0200375
376
Selim Cinek67b22602014-03-10 15:40:16 +0100377 }
378
379 /**
380 * Find the number of items in the top stack and update the result state if needed.
381 *
382 * @param resultState The result state to update if a height change of an child occurs
383 * @param algorithmState The state in which the current pass of the algorithm is currently in
384 * and which will be updated
385 */
386 private void findNumberOfItemsInTopStackAndUpdateState(StackScrollState resultState,
387 StackScrollAlgorithmState algorithmState) {
388
389 // The y Position if the element would be in a regular scrollView
390 float yPositionInScrollView = 0.0f;
Jorim Jaggid4a57442014-04-10 02:45:55 +0200391 int childCount = algorithmState.visibleChildren.size();
Selim Cinek67b22602014-03-10 15:40:16 +0100392
393 // find the number of elements in the top stack.
394 for (int i = 0; i < childCount; i++) {
Jorim Jaggibe565df2014-04-28 17:51:23 +0200395 ExpandableView child = algorithmState.visibleChildren.get(i);
Selim Cinek67b22602014-03-10 15:40:16 +0100396 StackScrollState.ViewState childViewState = resultState.getViewStateForView(child);
Jorim Jaggibe565df2014-04-28 17:51:23 +0200397 int childHeight = getMaxAllowedChildHeight(child);
Selim Cinek67b22602014-03-10 15:40:16 +0100398 float yPositionInScrollViewAfterElement = yPositionInScrollView
399 + childHeight
400 + mPaddingBetweenElements;
401 if (yPositionInScrollView < algorithmState.scrollY) {
Selim Cinek343e6e22014-04-11 21:23:30 +0200402 if (i == 0 && algorithmState.scrollY == mCollapsedSize) {
403
404 // The starting position of the bottom stack peek
Jorim Jaggi8c1a44b2014-04-29 19:04:02 +0200405 int bottomPeekStart = mInnerHeight - mBottomStackPeekSize;
Selim Cinek343e6e22014-04-11 21:23:30 +0200406 // Collapse and expand the first child while the shade is being expanded
407 float maxHeight = mIsExpansionChanging && child == mFirstChildWhileExpanding
408 ? mFirstChildMaxHeight
409 : childHeight;
410 childViewState.height = (int) Math.max(Math.min(bottomPeekStart, maxHeight),
411 mCollapsedSize);
412 algorithmState.itemsInTopStack = 1.0f;
413
414 } else if (yPositionInScrollViewAfterElement < algorithmState.scrollY) {
Selim Cinek67b22602014-03-10 15:40:16 +0100415 // According to the regular scroll view we are fully off screen
416 algorithmState.itemsInTopStack += 1.0f;
Selim Cinek343e6e22014-04-11 21:23:30 +0200417 if (i == 0) {
Selim Cinek67b22602014-03-10 15:40:16 +0100418 childViewState.height = mCollapsedSize;
419 }
420 } else {
421 // According to the regular scroll view we are partially off screen
422 // If it is expanded we have to collapse it to a new size
423 float newSize = yPositionInScrollViewAfterElement
424 - mPaddingBetweenElements
425 - algorithmState.scrollY;
426
Selim Cinek343e6e22014-04-11 21:23:30 +0200427 if (i == 0) {
428 newSize += mCollapsedSize;
429 }
430
Selim Cinek67b22602014-03-10 15:40:16 +0100431 // How much did we scroll into this child
Selim Cinek343e6e22014-04-11 21:23:30 +0200432 algorithmState.scrolledPixelsTop = childHeight - newSize;
433 algorithmState.partialInTop = (algorithmState.scrolledPixelsTop) / (childHeight
Selim Cinek67b22602014-03-10 15:40:16 +0100434 + mPaddingBetweenElements);
435
436 // Our element can be expanded, so this can get negative
437 algorithmState.partialInTop = Math.max(0.0f, algorithmState.partialInTop);
438 algorithmState.itemsInTopStack += algorithmState.partialInTop;
Selim Cinek67b22602014-03-10 15:40:16 +0100439 newSize = Math.max(mCollapsedSize, newSize);
Selim Cinek343e6e22014-04-11 21:23:30 +0200440 if (i == 0) {
Selim Cinek67b22602014-03-10 15:40:16 +0100441 childViewState.height = (int) newSize;
Selim Cinek67b22602014-03-10 15:40:16 +0100442 }
Selim Cinek343e6e22014-04-11 21:23:30 +0200443 algorithmState.lastTopStackIndex = i;
444 break;
Selim Cinek67b22602014-03-10 15:40:16 +0100445 }
446 } else {
Selim Cinek343e6e22014-04-11 21:23:30 +0200447 algorithmState.lastTopStackIndex = i - 1;
Selim Cinek67b22602014-03-10 15:40:16 +0100448 // We are already past the stack so we can end the loop
449 break;
450 }
451 yPositionInScrollView = yPositionInScrollViewAfterElement;
452 }
453 }
454
455 /**
456 * Calculate the Z positions for all children based on the number of items in both stacks and
457 * save it in the resultState
458 *
459 * @param resultState The result state to update the zTranslation values
460 * @param algorithmState The state in which the current pass of the algorithm is currently in
461 */
462 private void updateZValuesForState(StackScrollState resultState,
463 StackScrollAlgorithmState algorithmState) {
Jorim Jaggid4a57442014-04-10 02:45:55 +0200464 int childCount = algorithmState.visibleChildren.size();
Selim Cinek67b22602014-03-10 15:40:16 +0100465 for (int i = 0; i < childCount; i++) {
Jorim Jaggid4a57442014-04-10 02:45:55 +0200466 View child = algorithmState.visibleChildren.get(i);
Selim Cinek67b22602014-03-10 15:40:16 +0100467 StackScrollState.ViewState childViewState = resultState.getViewStateForView(child);
468 if (i < algorithmState.itemsInTopStack) {
469 float stackIndex = algorithmState.itemsInTopStack - i;
470 stackIndex = Math.min(stackIndex, MAX_ITEMS_IN_TOP_STACK + 2);
471 childViewState.zTranslation = mZBasicHeight
472 + stackIndex * mZDistanceBetweenElements;
473 } else if (i > (childCount - 1 - algorithmState.itemsInBottomStack)) {
474 float numItemsAbove = i - (childCount - 1 - algorithmState.itemsInBottomStack);
475 float translationZ = mZBasicHeight
476 - numItemsAbove * mZDistanceBetweenElements;
477 childViewState.zTranslation = translationZ;
478 } else {
479 childViewState.zTranslation = mZBasicHeight;
480 }
481 }
482 }
483
Selim Cinek343e6e22014-04-11 21:23:30 +0200484 public void setLayoutHeight(int layoutHeight) {
Selim Cinek67b22602014-03-10 15:40:16 +0100485 this.mLayoutHeight = layoutHeight;
Jorim Jaggi8c1a44b2014-04-29 19:04:02 +0200486 updateInnerHeight();
487 }
488
489 public void setTopPadding(int topPadding) {
490 mTopPadding = topPadding;
491 updateInnerHeight();
492 }
493
494 private void updateInnerHeight() {
495 mInnerHeight = mLayoutHeight - mTopPadding;
Selim Cinek67b22602014-03-10 15:40:16 +0100496 }
497
Selim Cinek1685e632014-04-08 02:27:49 +0200498 public void onExpansionStarted(StackScrollState currentState) {
499 mIsExpansionChanging = true;
500 mExpandedOnStart = mIsExpanded;
501 ViewGroup hostView = currentState.getHostView();
502 updateFirstChildHeightWhileExpanding(hostView);
503 }
504
505 private void updateFirstChildHeightWhileExpanding(ViewGroup hostView) {
Jorim Jaggibe565df2014-04-28 17:51:23 +0200506 mFirstChildWhileExpanding = (ExpandableView) findFirstVisibleChild(hostView);
Jorim Jaggi9f347ae2014-04-11 00:47:18 +0200507 if (mFirstChildWhileExpanding != null) {
Selim Cinek1685e632014-04-08 02:27:49 +0200508 if (mExpandedOnStart) {
509
510 // We are collapsing the shade, so the first child can get as most as high as the
511 // current height.
Jorim Jaggibe565df2014-04-28 17:51:23 +0200512 mFirstChildMaxHeight = mFirstChildWhileExpanding.getActualHeight();
Selim Cinek1685e632014-04-08 02:27:49 +0200513 } else {
514
515 // We are expanding the shade, expand it to its full height.
Selim Cinekb6e0e122014-04-23 17:24:37 +0200516 if (mFirstChildWhileExpanding.getWidth() == 0) {
517
518 // This child was not layouted yet, wait for a layout pass
519 mFirstChildWhileExpanding
520 .addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
521 @Override
522 public void onLayoutChange(View v, int left, int top, int right,
523 int bottom, int oldLeft, int oldTop, int oldRight,
524 int oldBottom) {
Selim Cinek2ba5f1f2014-04-28 20:23:30 +0200525 if (mFirstChildWhileExpanding != null) {
526 mFirstChildMaxHeight = getMaxAllowedChildHeight(
527 mFirstChildWhileExpanding);
528 } else {
529 mFirstChildMaxHeight = 0;
530 }
531 v.removeOnLayoutChangeListener(this);
Selim Cinekb6e0e122014-04-23 17:24:37 +0200532 }
533 });
534 } else {
535 mFirstChildMaxHeight = getMaxAllowedChildHeight(mFirstChildWhileExpanding);
536 }
Selim Cinek1685e632014-04-08 02:27:49 +0200537 }
538 } else {
Selim Cinek1685e632014-04-08 02:27:49 +0200539 mFirstChildMaxHeight = 0;
540 }
541 }
542
Jorim Jaggi9f347ae2014-04-11 00:47:18 +0200543 private View findFirstVisibleChild(ViewGroup container) {
544 int childCount = container.getChildCount();
545 for (int i = 0; i < childCount; i++) {
546 View child = container.getChildAt(i);
547 if (child.getVisibility() != View.GONE) {
548 return child;
549 }
550 }
551 return null;
552 }
553
Selim Cinek1685e632014-04-08 02:27:49 +0200554 public void onExpansionStopped() {
555 mIsExpansionChanging = false;
556 mFirstChildWhileExpanding = null;
557 }
558
559 public void setIsExpanded(boolean isExpanded) {
560 this.mIsExpanded = isExpanded;
561 }
562
563 public void notifyChildrenChanged(ViewGroup hostView) {
564 if (mIsExpansionChanging) {
565 updateFirstChildHeightWhileExpanding(hostView);
566 }
567 }
568
Selim Cinek67b22602014-03-10 15:40:16 +0100569 class StackScrollAlgorithmState {
570
571 /**
572 * The scroll position of the algorithm
573 */
574 public int scrollY;
575
576 /**
577 * The quantity of items which are in the top stack.
578 */
579 public float itemsInTopStack;
580
581 /**
582 * how far in is the element currently transitioning into the top stack
583 */
584 public float partialInTop;
585
586 /**
Selim Cinek343e6e22014-04-11 21:23:30 +0200587 * The number of pixels the last child in the top stack has scrolled in to the stack
588 */
589 public float scrolledPixelsTop;
590
591 /**
Selim Cinek67b22602014-03-10 15:40:16 +0100592 * The last item index which is in the top stack.
Selim Cinek67b22602014-03-10 15:40:16 +0100593 */
594 public int lastTopStackIndex;
595
596 /**
597 * The quantity of items which are in the bottom stack.
598 */
599 public float itemsInBottomStack;
600
601 /**
602 * how far in is the element currently transitioning into the bottom stack
603 */
604 public float partialInBottom;
Jorim Jaggid4a57442014-04-10 02:45:55 +0200605
606 /**
607 * The children from the host view which are not gone.
608 */
Jorim Jaggibe565df2014-04-28 17:51:23 +0200609 public final ArrayList<ExpandableView> visibleChildren = new ArrayList<ExpandableView>();
Selim Cinek67b22602014-03-10 15:40:16 +0100610 }
611
612}