blob: 5e4d496ee00fb394b06d1973c78645afb62d22eb [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
Jorim Jaggid552d9d2014-05-07 19:41:13 +020042 /** When a child is activated, the other cards' alpha fade to this value. */
43 private static final float ACTIVATED_INVERSE_ALPHA = 0.9f;
44 private static final float DIMMED_SCALE = 0.95f;
45
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;
52
53 private StackIndentationFunctor mTopStackIndentationFunctor;
54 private StackIndentationFunctor mBottomStackIndentationFunctor;
55
Selim Cinek343e6e22014-04-11 21:23:30 +020056 private int mLayoutHeight;
Jorim Jaggi8c1a44b2014-04-29 19:04:02 +020057
58 /** mLayoutHeight - mTopPadding */
59 private int mInnerHeight;
60 private int mTopPadding;
Selim Cinek67b22602014-03-10 15:40:16 +010061 private StackScrollAlgorithmState mTempAlgorithmState = new StackScrollAlgorithmState();
Selim Cinek1685e632014-04-08 02:27:49 +020062 private boolean mIsExpansionChanging;
63 private int mFirstChildMaxHeight;
64 private boolean mIsExpanded;
Jorim Jaggibe565df2014-04-28 17:51:23 +020065 private ExpandableView mFirstChildWhileExpanding;
Selim Cinek1685e632014-04-08 02:27:49 +020066 private boolean mExpandedOnStart;
Selim Cinek343e6e22014-04-11 21:23:30 +020067 private int mTopStackTotalSize;
Selim Cinek67b22602014-03-10 15:40:16 +010068
69 public StackScrollAlgorithm(Context context) {
70 initConstants(context);
71 }
72
73 private void initConstants(Context context) {
Jorim Jaggife40f7d2014-04-28 15:20:04 +020074 mPaddingBetweenElements = context.getResources()
75 .getDimensionPixelSize(R.dimen.notification_padding);
Selim Cinek67b22602014-03-10 15:40:16 +010076 mCollapsedSize = context.getResources()
Jorim Jaggife40f7d2014-04-28 15:20:04 +020077 .getDimensionPixelSize(R.dimen.notification_min_height);
Selim Cinek67b22602014-03-10 15:40:16 +010078 mTopStackPeekSize = context.getResources()
79 .getDimensionPixelSize(R.dimen.top_stack_peek_amount);
80 mBottomStackPeekSize = context.getResources()
81 .getDimensionPixelSize(R.dimen.bottom_stack_peek_amount);
82 mZDistanceBetweenElements = context.getResources()
83 .getDimensionPixelSize(R.dimen.z_distance_between_notifications);
84 mZBasicHeight = (MAX_ITEMS_IN_BOTTOM_STACK + 1) * mZDistanceBetweenElements;
Selim Cinek343e6e22014-04-11 21:23:30 +020085 mTopStackTotalSize = mCollapsedSize + mPaddingBetweenElements;
Selim Cinek67b22602014-03-10 15:40:16 +010086 mTopStackIndentationFunctor = new PiecewiseLinearIndentationFunctor(
87 MAX_ITEMS_IN_TOP_STACK,
88 mTopStackPeekSize,
Selim Cinek343e6e22014-04-11 21:23:30 +020089 mTopStackTotalSize,
Selim Cinek67b22602014-03-10 15:40:16 +010090 0.5f);
91 mBottomStackIndentationFunctor = new PiecewiseLinearIndentationFunctor(
92 MAX_ITEMS_IN_BOTTOM_STACK,
93 mBottomStackPeekSize,
Jorim Jaggibe565df2014-04-28 17:51:23 +020094 mCollapsedSize + mBottomStackPeekSize + mPaddingBetweenElements,
Selim Cinek67b22602014-03-10 15:40:16 +010095 0.5f);
96 }
97
98
Jorim Jaggid552d9d2014-05-07 19:41:13 +020099 public void getStackScrollState(AmbientState ambientState, StackScrollState resultState) {
Selim Cinek67b22602014-03-10 15:40:16 +0100100 // The state of the local variables are saved in an algorithmState to easily subdivide it
101 // into multiple phases.
102 StackScrollAlgorithmState algorithmState = mTempAlgorithmState;
103
104 // First we reset the view states to their default values.
105 resultState.resetViewStates();
106
Selim Cinek343e6e22014-04-11 21:23:30 +0200107 algorithmState.itemsInTopStack = 0.0f;
Selim Cinek67b22602014-03-10 15:40:16 +0100108 algorithmState.partialInTop = 0.0f;
109 algorithmState.lastTopStackIndex = 0;
Selim Cinek343e6e22014-04-11 21:23:30 +0200110 algorithmState.scrolledPixelsTop = 0;
Selim Cinek67b22602014-03-10 15:40:16 +0100111 algorithmState.itemsInBottomStack = 0.0f;
Selim Cinek343e6e22014-04-11 21:23:30 +0200112 algorithmState.partialInBottom = 0.0f;
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200113 algorithmState.scrollY = ambientState.getScrollY() + mCollapsedSize;
Selim Cinek343e6e22014-04-11 21:23:30 +0200114
Jorim Jaggid4a57442014-04-10 02:45:55 +0200115 updateVisibleChildren(resultState, algorithmState);
Selim Cinek67b22602014-03-10 15:40:16 +0100116
117 // Phase 1:
118 findNumberOfItemsInTopStackAndUpdateState(resultState, algorithmState);
119
120 // Phase 2:
121 updatePositionsForState(resultState, algorithmState);
122
123 // Phase 3:
124 updateZValuesForState(resultState, algorithmState);
Selim Cinekeb973562014-05-02 17:07:49 +0200125
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200126 handleDraggedViews(ambientState, resultState, algorithmState);
127 updateDimmedActivated(ambientState, resultState, algorithmState);
128 }
129
130 /**
131 * Updates the dimmed and activated states of the children.
132 */
133 private void updateDimmedActivated(AmbientState ambientState, StackScrollState resultState,
134 StackScrollAlgorithmState algorithmState) {
135 boolean dimmed = ambientState.isDimmed();
136 View activatedChild = ambientState.getActivatedChild();
137 int childCount = algorithmState.visibleChildren.size();
138 for (int i = 0; i < childCount; i++) {
139 View child = algorithmState.visibleChildren.get(i);
140 StackScrollState.ViewState childViewState = resultState.getViewStateForView(child);
141 childViewState.dimmed = dimmed;
142 childViewState.scale = !dimmed || activatedChild == child
143 ? 1.0f
144 : DIMMED_SCALE;
145 if (dimmed && activatedChild != null && child != activatedChild) {
146 childViewState.alpha *= ACTIVATED_INVERSE_ALPHA;
147 }
148 }
Selim Cinekeb973562014-05-02 17:07:49 +0200149 }
150
151 /**
152 * Handle the special state when views are being dragged
153 */
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200154 private void handleDraggedViews(AmbientState ambientState, StackScrollState resultState,
Selim Cinekeb973562014-05-02 17:07:49 +0200155 StackScrollAlgorithmState algorithmState) {
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200156 ArrayList<View> draggedViews = ambientState.getDraggedViews();
157 for (View draggedView : draggedViews) {
Selim Cinekeb973562014-05-02 17:07:49 +0200158 int childIndex = algorithmState.visibleChildren.indexOf(draggedView);
159 if (childIndex >= 0 && childIndex < algorithmState.visibleChildren.size() - 1) {
160 View nextChild = algorithmState.visibleChildren.get(childIndex + 1);
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200161 if (!draggedViews.contains(nextChild)) {
Selim Cinekeb973562014-05-02 17:07:49 +0200162 // only if the view is not dragged itself we modify its state to be fully
163 // visible
164 StackScrollState.ViewState viewState = resultState.getViewStateForView(
165 nextChild);
166 // The child below the dragged one must be fully visible
167 viewState.alpha = 1;
168 }
169
170 // Lets set the alpha to the one it currently has, as its currently being dragged
171 StackScrollState.ViewState viewState = resultState.getViewStateForView(draggedView);
172 // The dragged child should keep the set alpha
173 viewState.alpha = draggedView.getAlpha();
174 }
175 }
Selim Cinek343e6e22014-04-11 21:23:30 +0200176 }
Selim Cinek67b22602014-03-10 15:40:16 +0100177
Selim Cinek343e6e22014-04-11 21:23:30 +0200178 /**
Jorim Jaggid4a57442014-04-10 02:45:55 +0200179 * Update the visible children on the state.
180 */
181 private void updateVisibleChildren(StackScrollState resultState,
182 StackScrollAlgorithmState state) {
183 ViewGroup hostView = resultState.getHostView();
184 int childCount = hostView.getChildCount();
185 state.visibleChildren.clear();
186 state.visibleChildren.ensureCapacity(childCount);
187 for (int i = 0; i < childCount; i++) {
Jorim Jaggibe565df2014-04-28 17:51:23 +0200188 ExpandableView v = (ExpandableView) hostView.getChildAt(i);
Jorim Jaggid4a57442014-04-10 02:45:55 +0200189 if (v.getVisibility() != View.GONE) {
190 state.visibleChildren.add(v);
191 }
192 }
193 }
194
195 /**
Selim Cinek67b22602014-03-10 15:40:16 +0100196 * Determine the positions for the views. This is the main part of the algorithm.
197 *
198 * @param resultState The result state to update if a change to the properties of a child occurs
199 * @param algorithmState The state in which the current pass of the algorithm is currently in
200 * and which will be updated
201 */
202 private void updatePositionsForState(StackScrollState resultState,
203 StackScrollAlgorithmState algorithmState) {
Selim Cinek67b22602014-03-10 15:40:16 +0100204
Selim Cinek1685e632014-04-08 02:27:49 +0200205 // The starting position of the bottom stack peek
Jorim Jaggi8c1a44b2014-04-29 19:04:02 +0200206 float bottomPeekStart = mInnerHeight - mBottomStackPeekSize;
Selim Cinek1685e632014-04-08 02:27:49 +0200207
Selim Cinek67b22602014-03-10 15:40:16 +0100208 // The position where the bottom stack starts.
Selim Cinek343e6e22014-04-11 21:23:30 +0200209 float bottomStackStart = bottomPeekStart - mCollapsedSize;
Selim Cinek67b22602014-03-10 15:40:16 +0100210
211 // The y coordinate of the current child.
212 float currentYPosition = 0.0f;
213
214 // How far in is the element currently transitioning into the bottom stack.
215 float yPositionInScrollView = 0.0f;
216
Jorim Jaggid4a57442014-04-10 02:45:55 +0200217 int childCount = algorithmState.visibleChildren.size();
Selim Cinek67b22602014-03-10 15:40:16 +0100218 int numberOfElementsCompletelyIn = (int) algorithmState.itemsInTopStack;
219 for (int i = 0; i < childCount; i++) {
Jorim Jaggibe565df2014-04-28 17:51:23 +0200220 ExpandableView child = algorithmState.visibleChildren.get(i);
Selim Cinek67b22602014-03-10 15:40:16 +0100221 StackScrollState.ViewState childViewState = resultState.getViewStateForView(child);
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200222 childViewState.location = StackScrollState.ViewState.LOCATION_UNKNOWN;
Jorim Jaggibe565df2014-04-28 17:51:23 +0200223 int childHeight = getMaxAllowedChildHeight(child);
Selim Cinek67b22602014-03-10 15:40:16 +0100224 float yPositionInScrollViewAfterElement = yPositionInScrollView
225 + childHeight
226 + mPaddingBetweenElements;
Selim Cinek343e6e22014-04-11 21:23:30 +0200227 float scrollOffset = yPositionInScrollView - algorithmState.scrollY + mCollapsedSize;
228
229 if (i == algorithmState.lastTopStackIndex + 1) {
230 // Normally the position of this child is the position in the regular scrollview,
231 // but if the two stacks are very close to each other,
232 // then have have to push it even more upwards to the position of the bottom
233 // stack start.
234 currentYPosition = Math.min(scrollOffset, bottomStackStart);
235 }
236 childViewState.yTranslation = currentYPosition;
237
238 // The y position after this element
239 float nextYPosition = currentYPosition + childHeight +
240 mPaddingBetweenElements;
241
242 if (i <= algorithmState.lastTopStackIndex) {
Selim Cinek67b22602014-03-10 15:40:16 +0100243 // Case 1:
244 // We are in the top Stack
Selim Cinek343e6e22014-04-11 21:23:30 +0200245 updateStateForTopStackChild(algorithmState,
246 numberOfElementsCompletelyIn, i, childHeight, childViewState, scrollOffset);
247 clampYTranslation(childViewState, childHeight);
248 // check if we are overlapping with the bottom stack
249 if (childViewState.yTranslation + childHeight + mPaddingBetweenElements
Selim Cinek4a1ac842014-05-01 15:51:58 +0200250 >= bottomStackStart && !mIsExpansionChanging && i != 0) {
Selim Cinek343e6e22014-04-11 21:23:30 +0200251 // TODO: handle overlapping sizes with end stack better
252 // we just collapse this element
253 childViewState.height = mCollapsedSize;
254 }
255 } else if (nextYPosition >= bottomStackStart) {
Selim Cinek67b22602014-03-10 15:40:16 +0100256 // Case 2:
Selim Cinek343e6e22014-04-11 21:23:30 +0200257 // We are in the bottom stack.
258 if (currentYPosition >= bottomStackStart) {
Selim Cinek67b22602014-03-10 15:40:16 +0100259 // According to the regular scroll view we are fully translated out of the
260 // bottom of the screen so we are fully in the bottom stack
Selim Cinek343e6e22014-04-11 21:23:30 +0200261 updateStateForChildFullyInBottomStack(algorithmState,
262 bottomStackStart, childViewState, childHeight);
Selim Cinek67b22602014-03-10 15:40:16 +0100263 } else {
Selim Cinek67b22602014-03-10 15:40:16 +0100264 // According to the regular scroll view we are currently translating out of /
265 // into the bottom of the screen
Selim Cinek343e6e22014-04-11 21:23:30 +0200266 updateStateForChildTransitioningInBottom(algorithmState,
267 bottomStackStart, bottomPeekStart, currentYPosition,
268 childViewState, childHeight);
Selim Cinek67b22602014-03-10 15:40:16 +0100269 }
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200270 } else {
Selim Cinek343e6e22014-04-11 21:23:30 +0200271 // Case 3:
272 // We are in the regular scroll area.
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200273 childViewState.location = StackScrollState.ViewState.LOCATION_MAIN_AREA;
Selim Cinek343e6e22014-04-11 21:23:30 +0200274 clampYTranslation(childViewState, childHeight);
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200275 }
Selim Cinek343e6e22014-04-11 21:23:30 +0200276
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200277 // The first card is always rendered.
278 if (i == 0) {
279 childViewState.alpha = 1.0f;
Selim Cinek343e6e22014-04-11 21:23:30 +0200280 childViewState.yTranslation = 0;
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200281 childViewState.location = StackScrollState.ViewState.LOCATION_FIRST_CARD;
282 }
283 if (childViewState.location == StackScrollState.ViewState.LOCATION_UNKNOWN) {
284 Log.wtf(LOG_TAG, "Failed to assign location for child " + i);
Selim Cinek67b22602014-03-10 15:40:16 +0100285 }
Selim Cinek343e6e22014-04-11 21:23:30 +0200286 currentYPosition = childViewState.yTranslation + childHeight + mPaddingBetweenElements;
Selim Cinek67b22602014-03-10 15:40:16 +0100287 yPositionInScrollView = yPositionInScrollViewAfterElement;
Jorim Jaggi8c1a44b2014-04-29 19:04:02 +0200288
289 childViewState.yTranslation += mTopPadding;
Selim Cinek67b22602014-03-10 15:40:16 +0100290 }
291 }
292
Selim Cinek1685e632014-04-08 02:27:49 +0200293 /**
Selim Cinek343e6e22014-04-11 21:23:30 +0200294 * Clamp the yTranslation both up and down to valid positions.
Selim Cinek1685e632014-04-08 02:27:49 +0200295 *
Selim Cinek1685e632014-04-08 02:27:49 +0200296 * @param childViewState the view state of the child
Selim Cinek343e6e22014-04-11 21:23:30 +0200297 * @param childHeight the height of this child
Selim Cinek1685e632014-04-08 02:27:49 +0200298 */
Selim Cinek343e6e22014-04-11 21:23:30 +0200299 private void clampYTranslation(StackScrollState.ViewState childViewState, int childHeight) {
300 clampPositionToBottomStackStart(childViewState, childHeight);
301 clampPositionToTopStackEnd(childViewState, childHeight);
302 }
303
304 /**
305 * Clamp the yTranslation of the child down such that its end is at most on the beginning of
306 * the bottom stack.
307 *
308 * @param childViewState the view state of the child
309 * @param childHeight the height of this child
310 */
311 private void clampPositionToBottomStackStart(StackScrollState.ViewState childViewState,
312 int childHeight) {
313 childViewState.yTranslation = Math.min(childViewState.yTranslation,
Jorim Jaggi8c1a44b2014-04-29 19:04:02 +0200314 mInnerHeight - mBottomStackPeekSize - childHeight);
Selim Cinek343e6e22014-04-11 21:23:30 +0200315 }
316
317 /**
318 * 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 +0200319 * stack.get
Selim Cinek343e6e22014-04-11 21:23:30 +0200320 *
321 * @param childViewState the view state of the child
322 * @param childHeight the height of this child
323 */
324 private void clampPositionToTopStackEnd(StackScrollState.ViewState childViewState,
325 int childHeight) {
326 childViewState.yTranslation = Math.max(childViewState.yTranslation,
327 mCollapsedSize - childHeight);
Selim Cinek1685e632014-04-08 02:27:49 +0200328 }
329
330 private int getMaxAllowedChildHeight(View child) {
331 if (child instanceof ExpandableNotificationRow) {
332 ExpandableNotificationRow row = (ExpandableNotificationRow) child;
Jorim Jaggi9cbadd32014-05-01 20:18:31 +0200333 return row.getIntrinsicHeight();
Jorim Jaggibe565df2014-04-28 17:51:23 +0200334 } else if (child instanceof ExpandableView) {
335 ExpandableView expandableView = (ExpandableView) child;
336 return expandableView.getActualHeight();
Selim Cinek1685e632014-04-08 02:27:49 +0200337 }
Jorim Jaggibe565df2014-04-28 17:51:23 +0200338 return child == null? mCollapsedSize : child.getHeight();
Selim Cinek1685e632014-04-08 02:27:49 +0200339 }
340
Selim Cinek343e6e22014-04-11 21:23:30 +0200341 private void updateStateForChildTransitioningInBottom(StackScrollAlgorithmState algorithmState,
342 float transitioningPositionStart, float bottomPeakStart, float currentYPosition,
343 StackScrollState.ViewState childViewState, int childHeight) {
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200344
Selim Cinek343e6e22014-04-11 21:23:30 +0200345 // This is the transitioning element on top of bottom stack, calculate how far we are in.
346 algorithmState.partialInBottom = 1.0f - (
347 (transitioningPositionStart - currentYPosition) / (childHeight +
348 mPaddingBetweenElements));
349
350 // the offset starting at the transitionPosition of the bottom stack
351 float offset = mBottomStackIndentationFunctor.getValue(algorithmState.partialInBottom);
352 algorithmState.itemsInBottomStack += algorithmState.partialInBottom;
Jorim Jaggife40f7d2014-04-28 15:20:04 +0200353 childViewState.yTranslation = transitioningPositionStart + offset - childHeight
354 - mPaddingBetweenElements;
Selim Cinek343e6e22014-04-11 21:23:30 +0200355
356 // We want at least to be at the end of the top stack when collapsing
357 clampPositionToTopStackEnd(childViewState, childHeight);
358 childViewState.location = StackScrollState.ViewState.LOCATION_MAIN_AREA;
Selim Cinek67b22602014-03-10 15:40:16 +0100359 }
360
Selim Cinek343e6e22014-04-11 21:23:30 +0200361 private void updateStateForChildFullyInBottomStack(StackScrollAlgorithmState algorithmState,
Selim Cinek67b22602014-03-10 15:40:16 +0100362 float transitioningPositionStart, StackScrollState.ViewState childViewState,
363 int childHeight) {
364
Selim Cinek343e6e22014-04-11 21:23:30 +0200365 float currentYPosition;
Selim Cinek67b22602014-03-10 15:40:16 +0100366 algorithmState.itemsInBottomStack += 1.0f;
367 if (algorithmState.itemsInBottomStack < MAX_ITEMS_IN_BOTTOM_STACK) {
368 // We are visually entering the bottom stack
Selim Cinek343e6e22014-04-11 21:23:30 +0200369 currentYPosition = transitioningPositionStart
Jorim Jaggife40f7d2014-04-28 15:20:04 +0200370 + mBottomStackIndentationFunctor.getValue(algorithmState.itemsInBottomStack)
371 - mPaddingBetweenElements;
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200372 childViewState.location = StackScrollState.ViewState.LOCATION_BOTTOM_STACK_PEEKING;
Selim Cinek67b22602014-03-10 15:40:16 +0100373 } else {
374 // we are fully inside the stack
375 if (algorithmState.itemsInBottomStack > MAX_ITEMS_IN_BOTTOM_STACK + 2) {
376 childViewState.alpha = 0.0f;
377 } else if (algorithmState.itemsInBottomStack
378 > MAX_ITEMS_IN_BOTTOM_STACK + 1) {
379 childViewState.alpha = 1.0f - algorithmState.partialInBottom;
380 }
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200381 childViewState.location = StackScrollState.ViewState.LOCATION_BOTTOM_STACK_HIDDEN;
Jorim Jaggi8c1a44b2014-04-29 19:04:02 +0200382 currentYPosition = mInnerHeight;
Selim Cinek67b22602014-03-10 15:40:16 +0100383 }
Selim Cinek343e6e22014-04-11 21:23:30 +0200384 childViewState.yTranslation = currentYPosition - childHeight;
385 clampPositionToTopStackEnd(childViewState, childHeight);
Selim Cinek67b22602014-03-10 15:40:16 +0100386 }
387
Selim Cinek343e6e22014-04-11 21:23:30 +0200388 private void updateStateForTopStackChild(StackScrollAlgorithmState algorithmState,
389 int numberOfElementsCompletelyIn, int i, int childHeight,
390 StackScrollState.ViewState childViewState, float scrollOffset) {
Selim Cinek67b22602014-03-10 15:40:16 +0100391
Selim Cinek67b22602014-03-10 15:40:16 +0100392
393 // First we calculate the index relative to the current stack window of size at most
394 // {@link #MAX_ITEMS_IN_TOP_STACK}
Selim Cinek343e6e22014-04-11 21:23:30 +0200395 int paddedIndex = i - 1
Selim Cinek67b22602014-03-10 15:40:16 +0100396 - Math.max(numberOfElementsCompletelyIn - MAX_ITEMS_IN_TOP_STACK, 0);
397 if (paddedIndex >= 0) {
Selim Cinek343e6e22014-04-11 21:23:30 +0200398
Selim Cinek67b22602014-03-10 15:40:16 +0100399 // We are currently visually entering the top stack
Selim Cinek343e6e22014-04-11 21:23:30 +0200400 float distanceToStack = childHeight - algorithmState.scrolledPixelsTop;
401 if (i == algorithmState.lastTopStackIndex && distanceToStack > mTopStackTotalSize) {
402
403 // Child is currently translating into stack but not yet inside slow down zone.
404 // Handle it like the regular scrollview.
405 childViewState.yTranslation = scrollOffset;
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200406 } else {
Selim Cinek343e6e22014-04-11 21:23:30 +0200407 // Apply stacking logic.
408 float numItemsBefore;
409 if (i == algorithmState.lastTopStackIndex) {
410 numItemsBefore = 1.0f - (distanceToStack / mTopStackTotalSize);
411 } else {
412 numItemsBefore = algorithmState.itemsInTopStack - i;
413 }
414 // The end position of the current child
415 float currentChildEndY = mCollapsedSize + mTopStackTotalSize -
416 mTopStackIndentationFunctor.getValue(numItemsBefore);
417 childViewState.yTranslation = currentChildEndY - childHeight;
Selim Cinek67b22602014-03-10 15:40:16 +0100418 }
Selim Cinek343e6e22014-04-11 21:23:30 +0200419 childViewState.location = StackScrollState.ViewState.LOCATION_TOP_STACK_PEEKING;
Selim Cinek67b22602014-03-10 15:40:16 +0100420 } else {
Selim Cinek343e6e22014-04-11 21:23:30 +0200421 if (paddedIndex == -1) {
422 childViewState.alpha = 1.0f - algorithmState.partialInTop;
423 } else {
424 // We are hidden behind the top card and faded out, so we can hide ourselves.
425 childViewState.alpha = 0.0f;
426 }
427 childViewState.yTranslation = mCollapsedSize - childHeight;
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200428 childViewState.location = StackScrollState.ViewState.LOCATION_TOP_STACK_HIDDEN;
Selim Cinek67b22602014-03-10 15:40:16 +0100429 }
Selim Cinek343e6e22014-04-11 21:23:30 +0200430
431
Selim Cinek67b22602014-03-10 15:40:16 +0100432 }
433
434 /**
435 * Find the number of items in the top stack and update the result state if needed.
436 *
437 * @param resultState The result state to update if a height change of an child occurs
438 * @param algorithmState The state in which the current pass of the algorithm is currently in
439 * and which will be updated
440 */
441 private void findNumberOfItemsInTopStackAndUpdateState(StackScrollState resultState,
442 StackScrollAlgorithmState algorithmState) {
443
444 // The y Position if the element would be in a regular scrollView
445 float yPositionInScrollView = 0.0f;
Jorim Jaggid4a57442014-04-10 02:45:55 +0200446 int childCount = algorithmState.visibleChildren.size();
Selim Cinek67b22602014-03-10 15:40:16 +0100447
448 // find the number of elements in the top stack.
449 for (int i = 0; i < childCount; i++) {
Jorim Jaggibe565df2014-04-28 17:51:23 +0200450 ExpandableView child = algorithmState.visibleChildren.get(i);
Selim Cinek67b22602014-03-10 15:40:16 +0100451 StackScrollState.ViewState childViewState = resultState.getViewStateForView(child);
Jorim Jaggibe565df2014-04-28 17:51:23 +0200452 int childHeight = getMaxAllowedChildHeight(child);
Selim Cinek67b22602014-03-10 15:40:16 +0100453 float yPositionInScrollViewAfterElement = yPositionInScrollView
454 + childHeight
455 + mPaddingBetweenElements;
456 if (yPositionInScrollView < algorithmState.scrollY) {
Selim Cinek343e6e22014-04-11 21:23:30 +0200457 if (i == 0 && algorithmState.scrollY == mCollapsedSize) {
458
459 // The starting position of the bottom stack peek
Jorim Jaggi8c1a44b2014-04-29 19:04:02 +0200460 int bottomPeekStart = mInnerHeight - mBottomStackPeekSize;
Selim Cinek343e6e22014-04-11 21:23:30 +0200461 // Collapse and expand the first child while the shade is being expanded
462 float maxHeight = mIsExpansionChanging && child == mFirstChildWhileExpanding
463 ? mFirstChildMaxHeight
464 : childHeight;
465 childViewState.height = (int) Math.max(Math.min(bottomPeekStart, maxHeight),
466 mCollapsedSize);
467 algorithmState.itemsInTopStack = 1.0f;
468
469 } else if (yPositionInScrollViewAfterElement < algorithmState.scrollY) {
Selim Cinek67b22602014-03-10 15:40:16 +0100470 // According to the regular scroll view we are fully off screen
471 algorithmState.itemsInTopStack += 1.0f;
Selim Cinek343e6e22014-04-11 21:23:30 +0200472 if (i == 0) {
Selim Cinek67b22602014-03-10 15:40:16 +0100473 childViewState.height = mCollapsedSize;
474 }
475 } else {
476 // According to the regular scroll view we are partially off screen
477 // If it is expanded we have to collapse it to a new size
478 float newSize = yPositionInScrollViewAfterElement
479 - mPaddingBetweenElements
480 - algorithmState.scrollY;
481
Selim Cinek343e6e22014-04-11 21:23:30 +0200482 if (i == 0) {
483 newSize += mCollapsedSize;
484 }
485
Selim Cinek67b22602014-03-10 15:40:16 +0100486 // How much did we scroll into this child
Selim Cinek343e6e22014-04-11 21:23:30 +0200487 algorithmState.scrolledPixelsTop = childHeight - newSize;
488 algorithmState.partialInTop = (algorithmState.scrolledPixelsTop) / (childHeight
Selim Cinek67b22602014-03-10 15:40:16 +0100489 + mPaddingBetweenElements);
490
491 // Our element can be expanded, so this can get negative
492 algorithmState.partialInTop = Math.max(0.0f, algorithmState.partialInTop);
493 algorithmState.itemsInTopStack += algorithmState.partialInTop;
Selim Cinek67b22602014-03-10 15:40:16 +0100494 newSize = Math.max(mCollapsedSize, newSize);
Selim Cinek343e6e22014-04-11 21:23:30 +0200495 if (i == 0) {
Selim Cinek67b22602014-03-10 15:40:16 +0100496 childViewState.height = (int) newSize;
Selim Cinek67b22602014-03-10 15:40:16 +0100497 }
Selim Cinek343e6e22014-04-11 21:23:30 +0200498 algorithmState.lastTopStackIndex = i;
499 break;
Selim Cinek67b22602014-03-10 15:40:16 +0100500 }
501 } else {
Selim Cinek343e6e22014-04-11 21:23:30 +0200502 algorithmState.lastTopStackIndex = i - 1;
Selim Cinek67b22602014-03-10 15:40:16 +0100503 // We are already past the stack so we can end the loop
504 break;
505 }
506 yPositionInScrollView = yPositionInScrollViewAfterElement;
507 }
508 }
509
510 /**
511 * Calculate the Z positions for all children based on the number of items in both stacks and
512 * save it in the resultState
513 *
514 * @param resultState The result state to update the zTranslation values
515 * @param algorithmState The state in which the current pass of the algorithm is currently in
516 */
517 private void updateZValuesForState(StackScrollState resultState,
518 StackScrollAlgorithmState algorithmState) {
Jorim Jaggid4a57442014-04-10 02:45:55 +0200519 int childCount = algorithmState.visibleChildren.size();
Selim Cinek67b22602014-03-10 15:40:16 +0100520 for (int i = 0; i < childCount; i++) {
Jorim Jaggid4a57442014-04-10 02:45:55 +0200521 View child = algorithmState.visibleChildren.get(i);
Selim Cinek67b22602014-03-10 15:40:16 +0100522 StackScrollState.ViewState childViewState = resultState.getViewStateForView(child);
523 if (i < algorithmState.itemsInTopStack) {
524 float stackIndex = algorithmState.itemsInTopStack - i;
525 stackIndex = Math.min(stackIndex, MAX_ITEMS_IN_TOP_STACK + 2);
526 childViewState.zTranslation = mZBasicHeight
527 + stackIndex * mZDistanceBetweenElements;
528 } else if (i > (childCount - 1 - algorithmState.itemsInBottomStack)) {
529 float numItemsAbove = i - (childCount - 1 - algorithmState.itemsInBottomStack);
530 float translationZ = mZBasicHeight
531 - numItemsAbove * mZDistanceBetweenElements;
532 childViewState.zTranslation = translationZ;
533 } else {
534 childViewState.zTranslation = mZBasicHeight;
535 }
536 }
537 }
538
Selim Cinek343e6e22014-04-11 21:23:30 +0200539 public void setLayoutHeight(int layoutHeight) {
Selim Cinek67b22602014-03-10 15:40:16 +0100540 this.mLayoutHeight = layoutHeight;
Jorim Jaggi8c1a44b2014-04-29 19:04:02 +0200541 updateInnerHeight();
542 }
543
544 public void setTopPadding(int topPadding) {
545 mTopPadding = topPadding;
546 updateInnerHeight();
547 }
548
549 private void updateInnerHeight() {
550 mInnerHeight = mLayoutHeight - mTopPadding;
Selim Cinek67b22602014-03-10 15:40:16 +0100551 }
552
Selim Cinek1685e632014-04-08 02:27:49 +0200553 public void onExpansionStarted(StackScrollState currentState) {
554 mIsExpansionChanging = true;
555 mExpandedOnStart = mIsExpanded;
556 ViewGroup hostView = currentState.getHostView();
557 updateFirstChildHeightWhileExpanding(hostView);
558 }
559
560 private void updateFirstChildHeightWhileExpanding(ViewGroup hostView) {
Jorim Jaggibe565df2014-04-28 17:51:23 +0200561 mFirstChildWhileExpanding = (ExpandableView) findFirstVisibleChild(hostView);
Jorim Jaggi9f347ae2014-04-11 00:47:18 +0200562 if (mFirstChildWhileExpanding != null) {
Selim Cinek1685e632014-04-08 02:27:49 +0200563 if (mExpandedOnStart) {
564
565 // We are collapsing the shade, so the first child can get as most as high as the
566 // current height.
Jorim Jaggibe565df2014-04-28 17:51:23 +0200567 mFirstChildMaxHeight = mFirstChildWhileExpanding.getActualHeight();
Selim Cinek1685e632014-04-08 02:27:49 +0200568 } else {
569
570 // We are expanding the shade, expand it to its full height.
Selim Cinekb6e0e122014-04-23 17:24:37 +0200571 if (mFirstChildWhileExpanding.getWidth() == 0) {
572
573 // This child was not layouted yet, wait for a layout pass
574 mFirstChildWhileExpanding
575 .addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
576 @Override
577 public void onLayoutChange(View v, int left, int top, int right,
578 int bottom, int oldLeft, int oldTop, int oldRight,
579 int oldBottom) {
Selim Cinek2ba5f1f2014-04-28 20:23:30 +0200580 if (mFirstChildWhileExpanding != null) {
581 mFirstChildMaxHeight = getMaxAllowedChildHeight(
582 mFirstChildWhileExpanding);
583 } else {
584 mFirstChildMaxHeight = 0;
585 }
586 v.removeOnLayoutChangeListener(this);
Selim Cinekb6e0e122014-04-23 17:24:37 +0200587 }
588 });
589 } else {
590 mFirstChildMaxHeight = getMaxAllowedChildHeight(mFirstChildWhileExpanding);
591 }
Selim Cinek1685e632014-04-08 02:27:49 +0200592 }
593 } else {
Selim Cinek1685e632014-04-08 02:27:49 +0200594 mFirstChildMaxHeight = 0;
595 }
596 }
597
Jorim Jaggi9f347ae2014-04-11 00:47:18 +0200598 private View findFirstVisibleChild(ViewGroup container) {
599 int childCount = container.getChildCount();
600 for (int i = 0; i < childCount; i++) {
601 View child = container.getChildAt(i);
602 if (child.getVisibility() != View.GONE) {
603 return child;
604 }
605 }
606 return null;
607 }
608
Selim Cinek1685e632014-04-08 02:27:49 +0200609 public void onExpansionStopped() {
610 mIsExpansionChanging = false;
611 mFirstChildWhileExpanding = null;
612 }
613
614 public void setIsExpanded(boolean isExpanded) {
615 this.mIsExpanded = isExpanded;
616 }
617
618 public void notifyChildrenChanged(ViewGroup hostView) {
619 if (mIsExpansionChanging) {
620 updateFirstChildHeightWhileExpanding(hostView);
621 }
622 }
623
Selim Cinek67b22602014-03-10 15:40:16 +0100624 class StackScrollAlgorithmState {
625
626 /**
627 * The scroll position of the algorithm
628 */
629 public int scrollY;
630
631 /**
632 * The quantity of items which are in the top stack.
633 */
634 public float itemsInTopStack;
635
636 /**
637 * how far in is the element currently transitioning into the top stack
638 */
639 public float partialInTop;
640
641 /**
Selim Cinek343e6e22014-04-11 21:23:30 +0200642 * The number of pixels the last child in the top stack has scrolled in to the stack
643 */
644 public float scrolledPixelsTop;
645
646 /**
Selim Cinek67b22602014-03-10 15:40:16 +0100647 * The last item index which is in the top stack.
Selim Cinek67b22602014-03-10 15:40:16 +0100648 */
649 public int lastTopStackIndex;
650
651 /**
652 * The quantity of items which are in the bottom stack.
653 */
654 public float itemsInBottomStack;
655
656 /**
657 * how far in is the element currently transitioning into the bottom stack
658 */
659 public float partialInBottom;
Jorim Jaggid4a57442014-04-10 02:45:55 +0200660
661 /**
662 * The children from the host view which are not gone.
663 */
Jorim Jaggibe565df2014-04-28 17:51:23 +0200664 public final ArrayList<ExpandableView> visibleChildren = new ArrayList<ExpandableView>();
Selim Cinek67b22602014-03-10 15:40:16 +0100665 }
666
667}