blob: 79a83db68387fbb61bf9a8cc7305b149e3838437 [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;
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;
Selim Cinek67b22602014-03-10 15:40:16 +010076
77 public StackScrollAlgorithm(Context context) {
78 initConstants(context);
Selim Cinek34c0a8d2014-05-12 00:01:43 +020079 updatePadding(false);
80 }
81
82 private void updatePadding(boolean dimmed) {
83 mPaddingBetweenElements = dimmed
84 ? mPaddingBetweenElementsDimmed
85 : mPaddingBetweenElementsNormal;
Selim Cinekad3e5af2014-07-04 12:24:11 +020086 mTopStackTotalSize = mTopStackSlowDownLength + mPaddingBetweenElements
87 + mTopStackPeekSize;
Selim Cinek34c0a8d2014-05-12 00:01:43 +020088 mTopStackIndentationFunctor = new PiecewiseLinearIndentationFunctor(
89 MAX_ITEMS_IN_TOP_STACK,
90 mTopStackPeekSize,
Selim Cinekb96924d2014-05-12 15:11:25 +020091 mTopStackTotalSize - mTopStackPeekSize,
Selim Cinek34c0a8d2014-05-12 00:01:43 +020092 0.5f);
93 mBottomStackIndentationFunctor = new PiecewiseLinearIndentationFunctor(
94 MAX_ITEMS_IN_BOTTOM_STACK,
95 mBottomStackPeekSize,
96 getBottomStackSlowDownLength(),
97 0.5f);
98 }
99
100 public int getBottomStackSlowDownLength() {
101 return mBottomStackSlowDownLength + mPaddingBetweenElements;
Selim Cinek67b22602014-03-10 15:40:16 +0100102 }
103
104 private void initConstants(Context context) {
Selim Cinek34c0a8d2014-05-12 00:01:43 +0200105 mPaddingBetweenElementsDimmed = context.getResources()
106 .getDimensionPixelSize(R.dimen.notification_padding_dimmed);
107 mPaddingBetweenElementsNormal = context.getResources()
Jorim Jaggife40f7d2014-04-28 15:20:04 +0200108 .getDimensionPixelSize(R.dimen.notification_padding);
Selim Cinek67b22602014-03-10 15:40:16 +0100109 mCollapsedSize = context.getResources()
Jorim Jaggife40f7d2014-04-28 15:20:04 +0200110 .getDimensionPixelSize(R.dimen.notification_min_height);
Selim Cinek3afd00e2014-08-11 22:32:57 +0200111 mMaxNotificationHeight = context.getResources()
112 .getDimensionPixelSize(R.dimen.notification_max_height);
Selim Cinek67b22602014-03-10 15:40:16 +0100113 mTopStackPeekSize = context.getResources()
114 .getDimensionPixelSize(R.dimen.top_stack_peek_amount);
115 mBottomStackPeekSize = context.getResources()
116 .getDimensionPixelSize(R.dimen.bottom_stack_peek_amount);
117 mZDistanceBetweenElements = context.getResources()
118 .getDimensionPixelSize(R.dimen.z_distance_between_notifications);
119 mZBasicHeight = (MAX_ITEMS_IN_BOTTOM_STACK + 1) * mZDistanceBetweenElements;
Selim Cinek34c0a8d2014-05-12 00:01:43 +0200120 mBottomStackSlowDownLength = context.getResources()
121 .getDimensionPixelSize(R.dimen.bottom_stack_slow_down_length);
Selim Cinekad3e5af2014-07-04 12:24:11 +0200122 mTopStackSlowDownLength = context.getResources()
123 .getDimensionPixelSize(R.dimen.top_stack_slow_down_length);
Selim Cinek708a6c12014-05-28 14:16:02 +0200124 mRoundedRectCornerRadius = context.getResources().getDimensionPixelSize(
Selim Cinek697178b2014-07-02 19:40:30 +0200125 R.dimen.notification_material_rounded_rect_radius);
Selim Cinekd83771e2014-07-04 16:45:31 +0200126 mCollapseSecondCardPadding = context.getResources().getDimensionPixelSize(
127 R.dimen.notification_collapse_second_card_padding);
Selim Cinek67b22602014-03-10 15:40:16 +0100128 }
129
130
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200131 public void getStackScrollState(AmbientState ambientState, StackScrollState resultState) {
Selim Cinek67b22602014-03-10 15:40:16 +0100132 // The state of the local variables are saved in an algorithmState to easily subdivide it
133 // into multiple phases.
134 StackScrollAlgorithmState algorithmState = mTempAlgorithmState;
135
136 // First we reset the view states to their default values.
137 resultState.resetViewStates();
138
Selim Cinek343e6e22014-04-11 21:23:30 +0200139 algorithmState.itemsInTopStack = 0.0f;
Selim Cinek67b22602014-03-10 15:40:16 +0100140 algorithmState.partialInTop = 0.0f;
141 algorithmState.lastTopStackIndex = 0;
Selim Cinek343e6e22014-04-11 21:23:30 +0200142 algorithmState.scrolledPixelsTop = 0;
Selim Cinek67b22602014-03-10 15:40:16 +0100143 algorithmState.itemsInBottomStack = 0.0f;
Selim Cinek343e6e22014-04-11 21:23:30 +0200144 algorithmState.partialInBottom = 0.0f;
Selim Cinek8d9ff9c2014-05-12 15:13:04 +0200145 float bottomOverScroll = ambientState.getOverScrollAmount(false /* onTop */);
Selim Cinek1408eb52014-06-02 14:45:38 +0200146
147 int scrollY = ambientState.getScrollY();
148
149 // Due to the overScroller, the stackscroller can have negative scroll state. This is
150 // already accounted for by the top padding and doesn't need an additional adaption
151 scrollY = Math.max(0, scrollY);
152 algorithmState.scrollY = (int) (scrollY + mCollapsedSize + bottomOverScroll);
Selim Cinek343e6e22014-04-11 21:23:30 +0200153
Jorim Jaggid4a57442014-04-10 02:45:55 +0200154 updateVisibleChildren(resultState, algorithmState);
Selim Cinek67b22602014-03-10 15:40:16 +0100155
156 // Phase 1:
157 findNumberOfItemsInTopStackAndUpdateState(resultState, algorithmState);
158
159 // Phase 2:
160 updatePositionsForState(resultState, algorithmState);
161
162 // Phase 3:
163 updateZValuesForState(resultState, algorithmState);
Selim Cinekeb973562014-05-02 17:07:49 +0200164
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200165 handleDraggedViews(ambientState, resultState, algorithmState);
Jorim Jaggiae441282014-08-01 02:45:18 +0200166 updateDimmedActivatedHideSensitive(ambientState, resultState, algorithmState);
Selim Cinek708a6c12014-05-28 14:16:02 +0200167 updateClipping(resultState, algorithmState);
Selim Cinekf54090e2014-06-17 17:24:51 -0700168 updateScrimAmount(resultState, algorithmState, ambientState.getScrimAmount());
Selim Cinek3d2b94bf2014-07-02 22:12:47 +0200169 updateSpeedBumpState(resultState, algorithmState, ambientState.getSpeedBumpIndex());
170 }
171
172 private void updateSpeedBumpState(StackScrollState resultState,
173 StackScrollAlgorithmState algorithmState, int speedBumpIndex) {
174 int childCount = algorithmState.visibleChildren.size();
175 for (int i = 0; i < childCount; i++) {
176 View child = algorithmState.visibleChildren.get(i);
177 StackScrollState.ViewState childViewState = resultState.getViewStateForView(child);
Selim Cinek3107cfa2014-07-22 15:24:29 +0200178
179 // The speed bump can also be gone, so equality needs to be taken when comparing
180 // indices.
181 childViewState.belowSpeedBump = speedBumpIndex != -1 && i >= speedBumpIndex;
Selim Cinek3d2b94bf2014-07-02 22:12:47 +0200182 }
Selim Cinekf54090e2014-06-17 17:24:51 -0700183 }
184
185 private void updateScrimAmount(StackScrollState resultState,
186 StackScrollAlgorithmState algorithmState, float scrimAmount) {
187 int childCount = algorithmState.visibleChildren.size();
188 for (int i = 0; i < childCount; i++) {
189 View child = algorithmState.visibleChildren.get(i);
190 StackScrollState.ViewState childViewState = resultState.getViewStateForView(child);
191 childViewState.scrimAmount = scrimAmount;
192 }
Selim Cinek708a6c12014-05-28 14:16:02 +0200193 }
194
195 private void updateClipping(StackScrollState resultState,
196 StackScrollAlgorithmState algorithmState) {
197 float previousNotificationEnd = 0;
198 float previousNotificationStart = 0;
199 boolean previousNotificationIsSwiped = false;
200 int childCount = algorithmState.visibleChildren.size();
201 for (int i = 0; i < childCount; i++) {
202 ExpandableView child = algorithmState.visibleChildren.get(i);
203 StackScrollState.ViewState state = resultState.getViewStateForView(child);
Jorim Jaggi2e34ec32014-07-01 03:17:05 +0200204 float newYTranslation = state.yTranslation + state.height * (1f - state.scale) / 2f;
205 float newHeight = state.height * state.scale;
Selim Cinek708a6c12014-05-28 14:16:02 +0200206 // apply clipping and shadow
207 float newNotificationEnd = newYTranslation + newHeight;
208
209 // In the unlocked shade we have to clip a little bit higher because of the rounded
210 // corners of the notifications.
Jorim Jaggi2e34ec32014-07-01 03:17:05 +0200211 float clippingCorrection = state.dimmed ? 0 : mRoundedRectCornerRadius * state.scale;
Selim Cinek708a6c12014-05-28 14:16:02 +0200212
213 // When the previous notification is swiped, we don't clip the content to the
214 // bottom of it.
215 float clipHeight = previousNotificationIsSwiped
216 ? newHeight
217 : newNotificationEnd - (previousNotificationEnd - clippingCorrection);
218
219 updateChildClippingAndBackground(state, newHeight, clipHeight,
Jorim Jaggi2e34ec32014-07-01 03:17:05 +0200220 newHeight - (previousNotificationStart - newYTranslation));
Selim Cinek708a6c12014-05-28 14:16:02 +0200221
222 if (!child.isTransparent()) {
223 // Only update the previous values if we are not transparent,
224 // otherwise we would clip to a transparent view.
Jorim Jaggi2e34ec32014-07-01 03:17:05 +0200225 previousNotificationStart = newYTranslation + state.clipTopAmount * state.scale;
Selim Cinek708a6c12014-05-28 14:16:02 +0200226 previousNotificationEnd = newNotificationEnd;
227 previousNotificationIsSwiped = child.getTranslationX() != 0;
228 }
229 }
230 }
231
232 /**
233 * Updates the shadow outline and the clipping for a view.
234 *
235 * @param state the viewState to update
236 * @param realHeight the currently applied height of the view
237 * @param clipHeight the desired clip height, the rest of the view will be clipped from the top
238 * @param backgroundHeight the desired background height. The shadows of the view will be
239 * based on this height and the content will be clipped from the top
240 */
Jorim Jaggi2e34ec32014-07-01 03:17:05 +0200241 private void updateChildClippingAndBackground(StackScrollState.ViewState state,
242 float realHeight, float clipHeight, float backgroundHeight) {
Selim Cinek708a6c12014-05-28 14:16:02 +0200243 if (realHeight > clipHeight) {
Jorim Jaggi2e34ec32014-07-01 03:17:05 +0200244 // Rather overlap than create a hole.
245 state.topOverLap = (int) Math.floor((realHeight - clipHeight) / state.scale);
Selim Cinek708a6c12014-05-28 14:16:02 +0200246 } else {
247 state.topOverLap = 0;
248 }
249 if (realHeight > backgroundHeight) {
Jorim Jaggi2e34ec32014-07-01 03:17:05 +0200250 // Rather overlap than create a hole.
251 state.clipTopAmount = (int) Math.floor((realHeight - backgroundHeight) / state.scale);
Selim Cinek708a6c12014-05-28 14:16:02 +0200252 } else {
253 state.clipTopAmount = 0;
254 }
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200255 }
256
257 /**
Jorim Jaggiae441282014-08-01 02:45:18 +0200258 * Updates the dimmed, activated and hiding sensitive states of the children.
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200259 */
Jorim Jaggiae441282014-08-01 02:45:18 +0200260 private void updateDimmedActivatedHideSensitive(AmbientState ambientState,
261 StackScrollState resultState, StackScrollAlgorithmState algorithmState) {
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200262 boolean dimmed = ambientState.isDimmed();
John Spurlockbf370992014-06-17 13:58:31 -0400263 boolean dark = ambientState.isDark();
Jorim Jaggiae441282014-08-01 02:45:18 +0200264 boolean hideSensitive = ambientState.isHideSensitive();
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200265 View activatedChild = ambientState.getActivatedChild();
266 int childCount = algorithmState.visibleChildren.size();
267 for (int i = 0; i < childCount; i++) {
268 View child = algorithmState.visibleChildren.get(i);
269 StackScrollState.ViewState childViewState = resultState.getViewStateForView(child);
270 childViewState.dimmed = dimmed;
John Spurlockbf370992014-06-17 13:58:31 -0400271 childViewState.dark = dark;
Jorim Jaggiae441282014-08-01 02:45:18 +0200272 childViewState.hideSensitive = hideSensitive;
Selim Cinekb89de4e2014-06-10 10:47:05 +0200273 boolean isActivatedChild = activatedChild == child;
274 childViewState.scale = !dimmed || isActivatedChild
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200275 ? 1.0f
276 : DIMMED_SCALE;
Selim Cinekb89de4e2014-06-10 10:47:05 +0200277 if (dimmed && activatedChild != null) {
278 if (!isActivatedChild) {
279 childViewState.alpha *= ACTIVATED_INVERSE_ALPHA;
280 } else {
281 childViewState.zTranslation += 2.0f * mZDistanceBetweenElements;
282 }
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200283 }
284 }
Selim Cinekeb973562014-05-02 17:07:49 +0200285 }
286
287 /**
288 * Handle the special state when views are being dragged
289 */
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200290 private void handleDraggedViews(AmbientState ambientState, StackScrollState resultState,
Selim Cinekeb973562014-05-02 17:07:49 +0200291 StackScrollAlgorithmState algorithmState) {
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200292 ArrayList<View> draggedViews = ambientState.getDraggedViews();
293 for (View draggedView : draggedViews) {
Selim Cinekeb973562014-05-02 17:07:49 +0200294 int childIndex = algorithmState.visibleChildren.indexOf(draggedView);
295 if (childIndex >= 0 && childIndex < algorithmState.visibleChildren.size() - 1) {
296 View nextChild = algorithmState.visibleChildren.get(childIndex + 1);
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200297 if (!draggedViews.contains(nextChild)) {
Selim Cinekeb973562014-05-02 17:07:49 +0200298 // only if the view is not dragged itself we modify its state to be fully
299 // visible
300 StackScrollState.ViewState viewState = resultState.getViewStateForView(
301 nextChild);
302 // The child below the dragged one must be fully visible
303 viewState.alpha = 1;
304 }
305
306 // Lets set the alpha to the one it currently has, as its currently being dragged
307 StackScrollState.ViewState viewState = resultState.getViewStateForView(draggedView);
308 // The dragged child should keep the set alpha
309 viewState.alpha = draggedView.getAlpha();
310 }
311 }
Selim Cinek343e6e22014-04-11 21:23:30 +0200312 }
Selim Cinek67b22602014-03-10 15:40:16 +0100313
Selim Cinek343e6e22014-04-11 21:23:30 +0200314 /**
Jorim Jaggid4a57442014-04-10 02:45:55 +0200315 * Update the visible children on the state.
316 */
317 private void updateVisibleChildren(StackScrollState resultState,
318 StackScrollAlgorithmState state) {
319 ViewGroup hostView = resultState.getHostView();
320 int childCount = hostView.getChildCount();
321 state.visibleChildren.clear();
322 state.visibleChildren.ensureCapacity(childCount);
323 for (int i = 0; i < childCount; i++) {
Jorim Jaggibe565df2014-04-28 17:51:23 +0200324 ExpandableView v = (ExpandableView) hostView.getChildAt(i);
Jorim Jaggid4a57442014-04-10 02:45:55 +0200325 if (v.getVisibility() != View.GONE) {
Selim Cinek8efa6dd2014-05-19 16:27:37 +0200326 StackScrollState.ViewState viewState = resultState.getViewStateForView(v);
327 viewState.notGoneIndex = state.visibleChildren.size();
Jorim Jaggid4a57442014-04-10 02:45:55 +0200328 state.visibleChildren.add(v);
329 }
330 }
331 }
332
333 /**
Selim Cinek67b22602014-03-10 15:40:16 +0100334 * Determine the positions for the views. This is the main part of the algorithm.
335 *
336 * @param resultState The result state to update if a change to the properties of a child occurs
337 * @param algorithmState The state in which the current pass of the algorithm is currently in
Selim Cinek67b22602014-03-10 15:40:16 +0100338 */
339 private void updatePositionsForState(StackScrollState resultState,
340 StackScrollAlgorithmState algorithmState) {
Selim Cinek67b22602014-03-10 15:40:16 +0100341
Selim Cinek1685e632014-04-08 02:27:49 +0200342 // The starting position of the bottom stack peek
Jorim Jaggi8c1a44b2014-04-29 19:04:02 +0200343 float bottomPeekStart = mInnerHeight - mBottomStackPeekSize;
Selim Cinek1685e632014-04-08 02:27:49 +0200344
Selim Cinek67b22602014-03-10 15:40:16 +0100345 // The position where the bottom stack starts.
Selim Cinek34c0a8d2014-05-12 00:01:43 +0200346 float bottomStackStart = bottomPeekStart - mBottomStackSlowDownLength;
Selim Cinek67b22602014-03-10 15:40:16 +0100347
348 // The y coordinate of the current child.
349 float currentYPosition = 0.0f;
350
351 // How far in is the element currently transitioning into the bottom stack.
352 float yPositionInScrollView = 0.0f;
353
Jorim Jaggid4a57442014-04-10 02:45:55 +0200354 int childCount = algorithmState.visibleChildren.size();
Selim Cinek67b22602014-03-10 15:40:16 +0100355 int numberOfElementsCompletelyIn = (int) algorithmState.itemsInTopStack;
356 for (int i = 0; i < childCount; i++) {
Jorim Jaggibe565df2014-04-28 17:51:23 +0200357 ExpandableView child = algorithmState.visibleChildren.get(i);
Selim Cinek67b22602014-03-10 15:40:16 +0100358 StackScrollState.ViewState childViewState = resultState.getViewStateForView(child);
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200359 childViewState.location = StackScrollState.ViewState.LOCATION_UNKNOWN;
Jorim Jaggibe565df2014-04-28 17:51:23 +0200360 int childHeight = getMaxAllowedChildHeight(child);
Selim Cinek67b22602014-03-10 15:40:16 +0100361 float yPositionInScrollViewAfterElement = yPositionInScrollView
362 + childHeight
363 + mPaddingBetweenElements;
Selim Cinek343e6e22014-04-11 21:23:30 +0200364 float scrollOffset = yPositionInScrollView - algorithmState.scrollY + mCollapsedSize;
365
366 if (i == algorithmState.lastTopStackIndex + 1) {
367 // Normally the position of this child is the position in the regular scrollview,
368 // but if the two stacks are very close to each other,
369 // then have have to push it even more upwards to the position of the bottom
370 // stack start.
371 currentYPosition = Math.min(scrollOffset, bottomStackStart);
372 }
373 childViewState.yTranslation = currentYPosition;
374
375 // The y position after this element
376 float nextYPosition = currentYPosition + childHeight +
377 mPaddingBetweenElements;
378
379 if (i <= algorithmState.lastTopStackIndex) {
Selim Cinek67b22602014-03-10 15:40:16 +0100380 // Case 1:
381 // We are in the top Stack
Selim Cinek343e6e22014-04-11 21:23:30 +0200382 updateStateForTopStackChild(algorithmState,
383 numberOfElementsCompletelyIn, i, childHeight, childViewState, scrollOffset);
Selim Cinek3afd00e2014-08-11 22:32:57 +0200384 clampPositionToTopStackEnd(childViewState, childHeight);
385
Selim Cinek343e6e22014-04-11 21:23:30 +0200386 // check if we are overlapping with the bottom stack
387 if (childViewState.yTranslation + childHeight + mPaddingBetweenElements
Selim Cinek4a1ac842014-05-01 15:51:58 +0200388 >= bottomStackStart && !mIsExpansionChanging && i != 0) {
Selim Cinek3afd00e2014-08-11 22:32:57 +0200389 // we just collapse this element slightly
390 int newSize = (int) Math.max(bottomStackStart - mPaddingBetweenElements -
391 childViewState.yTranslation, mCollapsedSize);
392 childViewState.height = newSize;
Selim Cinek343e6e22014-04-11 21:23:30 +0200393 }
Selim Cinek3afd00e2014-08-11 22:32:57 +0200394 clampPositionToBottomStackStart(childViewState, childViewState.height);
Selim Cinek343e6e22014-04-11 21:23:30 +0200395 } else if (nextYPosition >= bottomStackStart) {
Selim Cinek67b22602014-03-10 15:40:16 +0100396 // Case 2:
Selim Cinek343e6e22014-04-11 21:23:30 +0200397 // We are in the bottom stack.
398 if (currentYPosition >= bottomStackStart) {
Selim Cinek67b22602014-03-10 15:40:16 +0100399 // According to the regular scroll view we are fully translated out of the
400 // bottom of the screen so we are fully in the bottom stack
Selim Cinek343e6e22014-04-11 21:23:30 +0200401 updateStateForChildFullyInBottomStack(algorithmState,
402 bottomStackStart, childViewState, childHeight);
Selim Cinek67b22602014-03-10 15:40:16 +0100403 } else {
Selim Cinek67b22602014-03-10 15:40:16 +0100404 // According to the regular scroll view we are currently translating out of /
405 // into the bottom of the screen
Selim Cinek343e6e22014-04-11 21:23:30 +0200406 updateStateForChildTransitioningInBottom(algorithmState,
407 bottomStackStart, bottomPeekStart, currentYPosition,
408 childViewState, childHeight);
Selim Cinek67b22602014-03-10 15:40:16 +0100409 }
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200410 } else {
Selim Cinek343e6e22014-04-11 21:23:30 +0200411 // Case 3:
412 // We are in the regular scroll area.
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200413 childViewState.location = StackScrollState.ViewState.LOCATION_MAIN_AREA;
Selim Cinek343e6e22014-04-11 21:23:30 +0200414 clampYTranslation(childViewState, childHeight);
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200415 }
Selim Cinek343e6e22014-04-11 21:23:30 +0200416
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200417 // The first card is always rendered.
418 if (i == 0) {
419 childViewState.alpha = 1.0f;
Selim Cinek8d9ff9c2014-05-12 15:13:04 +0200420 childViewState.yTranslation = Math.max(mCollapsedSize - algorithmState.scrollY, 0);
Selim Cinekd83771e2014-07-04 16:45:31 +0200421 if (childViewState.yTranslation + childViewState.height
422 > bottomPeekStart - mCollapseSecondCardPadding) {
Selim Cinek4fe3e472014-07-03 16:32:54 +0200423 childViewState.height = (int) Math.max(
Selim Cinekd83771e2014-07-04 16:45:31 +0200424 bottomPeekStart - mCollapseSecondCardPadding
425 - childViewState.yTranslation, mCollapsedSize);
Selim Cinek4fe3e472014-07-03 16:32:54 +0200426 }
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200427 childViewState.location = StackScrollState.ViewState.LOCATION_FIRST_CARD;
428 }
429 if (childViewState.location == StackScrollState.ViewState.LOCATION_UNKNOWN) {
430 Log.wtf(LOG_TAG, "Failed to assign location for child " + i);
Selim Cinek67b22602014-03-10 15:40:16 +0100431 }
Selim Cinek343e6e22014-04-11 21:23:30 +0200432 currentYPosition = childViewState.yTranslation + childHeight + mPaddingBetweenElements;
Selim Cinek67b22602014-03-10 15:40:16 +0100433 yPositionInScrollView = yPositionInScrollViewAfterElement;
Jorim Jaggi8c1a44b2014-04-29 19:04:02 +0200434
435 childViewState.yTranslation += mTopPadding;
Selim Cinek67b22602014-03-10 15:40:16 +0100436 }
437 }
438
Selim Cinek1685e632014-04-08 02:27:49 +0200439 /**
Selim Cinek343e6e22014-04-11 21:23:30 +0200440 * Clamp the yTranslation both up and down to valid positions.
Selim Cinek1685e632014-04-08 02:27:49 +0200441 *
Selim Cinek1685e632014-04-08 02:27:49 +0200442 * @param childViewState the view state of the child
Selim Cinek343e6e22014-04-11 21:23:30 +0200443 * @param childHeight the height of this child
Selim Cinek1685e632014-04-08 02:27:49 +0200444 */
Selim Cinek343e6e22014-04-11 21:23:30 +0200445 private void clampYTranslation(StackScrollState.ViewState childViewState, int childHeight) {
446 clampPositionToBottomStackStart(childViewState, childHeight);
447 clampPositionToTopStackEnd(childViewState, childHeight);
448 }
449
450 /**
451 * Clamp the yTranslation of the child down such that its end is at most on the beginning of
452 * the bottom stack.
453 *
454 * @param childViewState the view state of the child
455 * @param childHeight the height of this child
456 */
457 private void clampPositionToBottomStackStart(StackScrollState.ViewState childViewState,
458 int childHeight) {
459 childViewState.yTranslation = Math.min(childViewState.yTranslation,
Selim Cinekd83771e2014-07-04 16:45:31 +0200460 mInnerHeight - mBottomStackPeekSize - mCollapseSecondCardPadding - childHeight);
Selim Cinek343e6e22014-04-11 21:23:30 +0200461 }
462
463 /**
464 * 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 +0200465 * stack.get
Selim Cinek343e6e22014-04-11 21:23:30 +0200466 *
467 * @param childViewState the view state of the child
468 * @param childHeight the height of this child
469 */
470 private void clampPositionToTopStackEnd(StackScrollState.ViewState childViewState,
471 int childHeight) {
472 childViewState.yTranslation = Math.max(childViewState.yTranslation,
473 mCollapsedSize - childHeight);
Selim Cinek1685e632014-04-08 02:27:49 +0200474 }
475
476 private int getMaxAllowedChildHeight(View child) {
477 if (child instanceof ExpandableNotificationRow) {
478 ExpandableNotificationRow row = (ExpandableNotificationRow) child;
Jorim Jaggi9cbadd32014-05-01 20:18:31 +0200479 return row.getIntrinsicHeight();
Jorim Jaggibe565df2014-04-28 17:51:23 +0200480 } else if (child instanceof ExpandableView) {
481 ExpandableView expandableView = (ExpandableView) child;
482 return expandableView.getActualHeight();
Selim Cinek1685e632014-04-08 02:27:49 +0200483 }
Jorim Jaggibe565df2014-04-28 17:51:23 +0200484 return child == null? mCollapsedSize : child.getHeight();
Selim Cinek1685e632014-04-08 02:27:49 +0200485 }
486
Selim Cinek343e6e22014-04-11 21:23:30 +0200487 private void updateStateForChildTransitioningInBottom(StackScrollAlgorithmState algorithmState,
488 float transitioningPositionStart, float bottomPeakStart, float currentYPosition,
489 StackScrollState.ViewState childViewState, int childHeight) {
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200490
Selim Cinek343e6e22014-04-11 21:23:30 +0200491 // This is the transitioning element on top of bottom stack, calculate how far we are in.
492 algorithmState.partialInBottom = 1.0f - (
493 (transitioningPositionStart - currentYPosition) / (childHeight +
494 mPaddingBetweenElements));
495
496 // the offset starting at the transitionPosition of the bottom stack
497 float offset = mBottomStackIndentationFunctor.getValue(algorithmState.partialInBottom);
498 algorithmState.itemsInBottomStack += algorithmState.partialInBottom;
Selim Cinek3afd00e2014-08-11 22:32:57 +0200499 int newHeight = childHeight;
500 if (childHeight > mCollapsedSize && mIsSmallScreen) {
501 newHeight = (int) Math.max(Math.min(transitioningPositionStart + offset -
502 mPaddingBetweenElements - currentYPosition, childHeight), mCollapsedSize);
503 childViewState.height = newHeight;
504 }
505 childViewState.yTranslation = transitioningPositionStart + offset - newHeight
Jorim Jaggife40f7d2014-04-28 15:20:04 +0200506 - mPaddingBetweenElements;
Selim Cinek3afd00e2014-08-11 22:32:57 +0200507
Selim Cinek343e6e22014-04-11 21:23:30 +0200508 // We want at least to be at the end of the top stack when collapsing
Selim Cinek3afd00e2014-08-11 22:32:57 +0200509 clampPositionToTopStackEnd(childViewState, newHeight);
Selim Cinek343e6e22014-04-11 21:23:30 +0200510 childViewState.location = StackScrollState.ViewState.LOCATION_MAIN_AREA;
Selim Cinek67b22602014-03-10 15:40:16 +0100511 }
512
Selim Cinek343e6e22014-04-11 21:23:30 +0200513 private void updateStateForChildFullyInBottomStack(StackScrollAlgorithmState algorithmState,
Selim Cinek67b22602014-03-10 15:40:16 +0100514 float transitioningPositionStart, StackScrollState.ViewState childViewState,
515 int childHeight) {
516
Selim Cinek343e6e22014-04-11 21:23:30 +0200517 float currentYPosition;
Selim Cinek67b22602014-03-10 15:40:16 +0100518 algorithmState.itemsInBottomStack += 1.0f;
519 if (algorithmState.itemsInBottomStack < MAX_ITEMS_IN_BOTTOM_STACK) {
520 // We are visually entering the bottom stack
Selim Cinek343e6e22014-04-11 21:23:30 +0200521 currentYPosition = transitioningPositionStart
Jorim Jaggife40f7d2014-04-28 15:20:04 +0200522 + mBottomStackIndentationFunctor.getValue(algorithmState.itemsInBottomStack)
523 - mPaddingBetweenElements;
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200524 childViewState.location = StackScrollState.ViewState.LOCATION_BOTTOM_STACK_PEEKING;
Selim Cinek67b22602014-03-10 15:40:16 +0100525 } else {
526 // we are fully inside the stack
527 if (algorithmState.itemsInBottomStack > MAX_ITEMS_IN_BOTTOM_STACK + 2) {
528 childViewState.alpha = 0.0f;
529 } else if (algorithmState.itemsInBottomStack
530 > MAX_ITEMS_IN_BOTTOM_STACK + 1) {
531 childViewState.alpha = 1.0f - algorithmState.partialInBottom;
532 }
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200533 childViewState.location = StackScrollState.ViewState.LOCATION_BOTTOM_STACK_HIDDEN;
Jorim Jaggi8c1a44b2014-04-29 19:04:02 +0200534 currentYPosition = mInnerHeight;
Selim Cinek67b22602014-03-10 15:40:16 +0100535 }
Selim Cinek343e6e22014-04-11 21:23:30 +0200536 childViewState.yTranslation = currentYPosition - childHeight;
537 clampPositionToTopStackEnd(childViewState, childHeight);
Selim Cinek67b22602014-03-10 15:40:16 +0100538 }
539
Selim Cinek343e6e22014-04-11 21:23:30 +0200540 private void updateStateForTopStackChild(StackScrollAlgorithmState algorithmState,
541 int numberOfElementsCompletelyIn, int i, int childHeight,
542 StackScrollState.ViewState childViewState, float scrollOffset) {
Selim Cinek67b22602014-03-10 15:40:16 +0100543
Selim Cinek67b22602014-03-10 15:40:16 +0100544
545 // First we calculate the index relative to the current stack window of size at most
546 // {@link #MAX_ITEMS_IN_TOP_STACK}
Selim Cinek343e6e22014-04-11 21:23:30 +0200547 int paddedIndex = i - 1
Selim Cinek67b22602014-03-10 15:40:16 +0100548 - Math.max(numberOfElementsCompletelyIn - MAX_ITEMS_IN_TOP_STACK, 0);
549 if (paddedIndex >= 0) {
Selim Cinek343e6e22014-04-11 21:23:30 +0200550
Selim Cinek67b22602014-03-10 15:40:16 +0100551 // We are currently visually entering the top stack
Selim Cinekad3e5af2014-07-04 12:24:11 +0200552 float distanceToStack = (childHeight + mPaddingBetweenElements)
553 - algorithmState.scrolledPixelsTop;
554 if (i == algorithmState.lastTopStackIndex
555 && distanceToStack > (mTopStackTotalSize + mPaddingBetweenElements)) {
Selim Cinek343e6e22014-04-11 21:23:30 +0200556
557 // Child is currently translating into stack but not yet inside slow down zone.
558 // Handle it like the regular scrollview.
559 childViewState.yTranslation = scrollOffset;
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200560 } else {
Selim Cinek343e6e22014-04-11 21:23:30 +0200561 // Apply stacking logic.
562 float numItemsBefore;
563 if (i == algorithmState.lastTopStackIndex) {
Selim Cinekad3e5af2014-07-04 12:24:11 +0200564 numItemsBefore = 1.0f
565 - (distanceToStack / (mTopStackTotalSize + mPaddingBetweenElements));
Selim Cinek343e6e22014-04-11 21:23:30 +0200566 } else {
567 numItemsBefore = algorithmState.itemsInTopStack - i;
568 }
569 // The end position of the current child
Selim Cinekad3e5af2014-07-04 12:24:11 +0200570 float currentChildEndY = mCollapsedSize + mTopStackTotalSize
571 - mTopStackIndentationFunctor.getValue(numItemsBefore);
Selim Cinek343e6e22014-04-11 21:23:30 +0200572 childViewState.yTranslation = currentChildEndY - childHeight;
Selim Cinek67b22602014-03-10 15:40:16 +0100573 }
Selim Cinek343e6e22014-04-11 21:23:30 +0200574 childViewState.location = StackScrollState.ViewState.LOCATION_TOP_STACK_PEEKING;
Selim Cinek67b22602014-03-10 15:40:16 +0100575 } else {
Selim Cinek343e6e22014-04-11 21:23:30 +0200576 if (paddedIndex == -1) {
577 childViewState.alpha = 1.0f - algorithmState.partialInTop;
578 } else {
579 // We are hidden behind the top card and faded out, so we can hide ourselves.
580 childViewState.alpha = 0.0f;
581 }
582 childViewState.yTranslation = mCollapsedSize - childHeight;
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200583 childViewState.location = StackScrollState.ViewState.LOCATION_TOP_STACK_HIDDEN;
Selim Cinek67b22602014-03-10 15:40:16 +0100584 }
Selim Cinek343e6e22014-04-11 21:23:30 +0200585
586
Selim Cinek67b22602014-03-10 15:40:16 +0100587 }
588
589 /**
590 * Find the number of items in the top stack and update the result state if needed.
591 *
592 * @param resultState The result state to update if a height change of an child occurs
593 * @param algorithmState The state in which the current pass of the algorithm is currently in
Selim Cinek67b22602014-03-10 15:40:16 +0100594 */
595 private void findNumberOfItemsInTopStackAndUpdateState(StackScrollState resultState,
596 StackScrollAlgorithmState algorithmState) {
597
598 // The y Position if the element would be in a regular scrollView
599 float yPositionInScrollView = 0.0f;
Jorim Jaggid4a57442014-04-10 02:45:55 +0200600 int childCount = algorithmState.visibleChildren.size();
Selim Cinek67b22602014-03-10 15:40:16 +0100601
602 // find the number of elements in the top stack.
603 for (int i = 0; i < childCount; i++) {
Jorim Jaggibe565df2014-04-28 17:51:23 +0200604 ExpandableView child = algorithmState.visibleChildren.get(i);
Selim Cinek67b22602014-03-10 15:40:16 +0100605 StackScrollState.ViewState childViewState = resultState.getViewStateForView(child);
Jorim Jaggibe565df2014-04-28 17:51:23 +0200606 int childHeight = getMaxAllowedChildHeight(child);
Selim Cinek67b22602014-03-10 15:40:16 +0100607 float yPositionInScrollViewAfterElement = yPositionInScrollView
608 + childHeight
609 + mPaddingBetweenElements;
610 if (yPositionInScrollView < algorithmState.scrollY) {
Selim Cinek8d9ff9c2014-05-12 15:13:04 +0200611 if (i == 0 && algorithmState.scrollY <= mCollapsedSize) {
Selim Cinek343e6e22014-04-11 21:23:30 +0200612
613 // The starting position of the bottom stack peek
Selim Cinekd83771e2014-07-04 16:45:31 +0200614 int bottomPeekStart = mInnerHeight - mBottomStackPeekSize -
615 mCollapseSecondCardPadding;
Selim Cinek343e6e22014-04-11 21:23:30 +0200616 // Collapse and expand the first child while the shade is being expanded
617 float maxHeight = mIsExpansionChanging && child == mFirstChildWhileExpanding
618 ? mFirstChildMaxHeight
619 : childHeight;
620 childViewState.height = (int) Math.max(Math.min(bottomPeekStart, maxHeight),
621 mCollapsedSize);
622 algorithmState.itemsInTopStack = 1.0f;
623
624 } else if (yPositionInScrollViewAfterElement < algorithmState.scrollY) {
Selim Cinek67b22602014-03-10 15:40:16 +0100625 // According to the regular scroll view we are fully off screen
626 algorithmState.itemsInTopStack += 1.0f;
Selim Cinek343e6e22014-04-11 21:23:30 +0200627 if (i == 0) {
Selim Cinek67b22602014-03-10 15:40:16 +0100628 childViewState.height = mCollapsedSize;
629 }
630 } else {
631 // According to the regular scroll view we are partially off screen
Selim Cinek343e6e22014-04-11 21:23:30 +0200632
Selim Cinek67b22602014-03-10 15:40:16 +0100633 // How much did we scroll into this child
Selim Cinekad3e5af2014-07-04 12:24:11 +0200634 algorithmState.scrolledPixelsTop = algorithmState.scrollY
635 - yPositionInScrollView;
Selim Cinek343e6e22014-04-11 21:23:30 +0200636 algorithmState.partialInTop = (algorithmState.scrolledPixelsTop) / (childHeight
Selim Cinek67b22602014-03-10 15:40:16 +0100637 + mPaddingBetweenElements);
638
639 // Our element can be expanded, so this can get negative
640 algorithmState.partialInTop = Math.max(0.0f, algorithmState.partialInTop);
641 algorithmState.itemsInTopStack += algorithmState.partialInTop;
Selim Cinekad3e5af2014-07-04 12:24:11 +0200642
Selim Cinek343e6e22014-04-11 21:23:30 +0200643 if (i == 0) {
Selim Cinekad3e5af2014-07-04 12:24:11 +0200644 // If it is expanded we have to collapse it to a new size
645 float newSize = yPositionInScrollViewAfterElement
646 - mPaddingBetweenElements
647 - algorithmState.scrollY + mCollapsedSize;
648 newSize = Math.max(mCollapsedSize, newSize);
Selim Cinek4e456be2014-06-12 18:09:43 +0200649 algorithmState.itemsInTopStack = 1.0f;
Selim Cinek67b22602014-03-10 15:40:16 +0100650 childViewState.height = (int) newSize;
Selim Cinek67b22602014-03-10 15:40:16 +0100651 }
Selim Cinek343e6e22014-04-11 21:23:30 +0200652 algorithmState.lastTopStackIndex = i;
653 break;
Selim Cinek67b22602014-03-10 15:40:16 +0100654 }
655 } else {
Selim Cinek343e6e22014-04-11 21:23:30 +0200656 algorithmState.lastTopStackIndex = i - 1;
Selim Cinek67b22602014-03-10 15:40:16 +0100657 // We are already past the stack so we can end the loop
658 break;
659 }
660 yPositionInScrollView = yPositionInScrollViewAfterElement;
661 }
662 }
663
664 /**
665 * Calculate the Z positions for all children based on the number of items in both stacks and
666 * save it in the resultState
667 *
668 * @param resultState The result state to update the zTranslation values
669 * @param algorithmState The state in which the current pass of the algorithm is currently in
670 */
671 private void updateZValuesForState(StackScrollState resultState,
672 StackScrollAlgorithmState algorithmState) {
Jorim Jaggid4a57442014-04-10 02:45:55 +0200673 int childCount = algorithmState.visibleChildren.size();
Selim Cinek67b22602014-03-10 15:40:16 +0100674 for (int i = 0; i < childCount; i++) {
Jorim Jaggid4a57442014-04-10 02:45:55 +0200675 View child = algorithmState.visibleChildren.get(i);
Selim Cinek67b22602014-03-10 15:40:16 +0100676 StackScrollState.ViewState childViewState = resultState.getViewStateForView(child);
677 if (i < algorithmState.itemsInTopStack) {
678 float stackIndex = algorithmState.itemsInTopStack - i;
679 stackIndex = Math.min(stackIndex, MAX_ITEMS_IN_TOP_STACK + 2);
Selim Cinek4e456be2014-06-12 18:09:43 +0200680 if (i == 0 && algorithmState.itemsInTopStack < 2.0f) {
681
682 // We only have the top item and an additional item in the top stack,
683 // Interpolate the index from 0 to 2 while the second item is
684 // translating in.
685 stackIndex -= 1.0f;
686 if (algorithmState.scrollY > mCollapsedSize) {
687
688 // Since there is a shadow treshhold, we cant just interpolate from 0 to
689 // 2 but we interpolate from 0.1f to 2.0f when scrolled in. The jump in
690 // height will not be noticable since we have padding in between.
691 stackIndex = 0.1f + stackIndex * 1.9f;
692 }
693 }
Selim Cinek67b22602014-03-10 15:40:16 +0100694 childViewState.zTranslation = mZBasicHeight
695 + stackIndex * mZDistanceBetweenElements;
696 } else if (i > (childCount - 1 - algorithmState.itemsInBottomStack)) {
697 float numItemsAbove = i - (childCount - 1 - algorithmState.itemsInBottomStack);
698 float translationZ = mZBasicHeight
699 - numItemsAbove * mZDistanceBetweenElements;
700 childViewState.zTranslation = translationZ;
701 } else {
702 childViewState.zTranslation = mZBasicHeight;
703 }
704 }
705 }
706
Selim Cinek343e6e22014-04-11 21:23:30 +0200707 public void setLayoutHeight(int layoutHeight) {
Selim Cinek67b22602014-03-10 15:40:16 +0100708 this.mLayoutHeight = layoutHeight;
Jorim Jaggi8c1a44b2014-04-29 19:04:02 +0200709 updateInnerHeight();
710 }
711
712 public void setTopPadding(int topPadding) {
713 mTopPadding = topPadding;
714 updateInnerHeight();
715 }
716
717 private void updateInnerHeight() {
718 mInnerHeight = mLayoutHeight - mTopPadding;
Selim Cinek67b22602014-03-10 15:40:16 +0100719 }
720
Selim Cinek3afd00e2014-08-11 22:32:57 +0200721
722 /**
723 * Update whether the device is very small, i.e. Notifications can be in both the top and the
724 * bottom stack at the same time
725 *
726 * @param panelHeight The normal height of the panel when it's open
727 */
728 public void updateIsSmallScreen(int panelHeight) {
729 mIsSmallScreen = panelHeight <
730 mCollapsedSize /* top stack */
731 + mBottomStackSlowDownLength + mBottomStackPeekSize /* bottom stack */
732 + mMaxNotificationHeight; /* max notification height */
733 }
734
Selim Cinek1685e632014-04-08 02:27:49 +0200735 public void onExpansionStarted(StackScrollState currentState) {
736 mIsExpansionChanging = true;
737 mExpandedOnStart = mIsExpanded;
738 ViewGroup hostView = currentState.getHostView();
739 updateFirstChildHeightWhileExpanding(hostView);
740 }
741
742 private void updateFirstChildHeightWhileExpanding(ViewGroup hostView) {
Jorim Jaggibe565df2014-04-28 17:51:23 +0200743 mFirstChildWhileExpanding = (ExpandableView) findFirstVisibleChild(hostView);
Jorim Jaggi9f347ae2014-04-11 00:47:18 +0200744 if (mFirstChildWhileExpanding != null) {
Selim Cinek1685e632014-04-08 02:27:49 +0200745 if (mExpandedOnStart) {
746
747 // We are collapsing the shade, so the first child can get as most as high as the
748 // current height.
Jorim Jaggibe565df2014-04-28 17:51:23 +0200749 mFirstChildMaxHeight = mFirstChildWhileExpanding.getActualHeight();
Selim Cinek1685e632014-04-08 02:27:49 +0200750 } else {
751
752 // We are expanding the shade, expand it to its full height.
Selim Cinek7d447722014-06-10 15:51:59 +0200753 if (!isMaxSizeInitialized(mFirstChildWhileExpanding)) {
Selim Cinekb6e0e122014-04-23 17:24:37 +0200754
755 // This child was not layouted yet, wait for a layout pass
756 mFirstChildWhileExpanding
757 .addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
758 @Override
759 public void onLayoutChange(View v, int left, int top, int right,
760 int bottom, int oldLeft, int oldTop, int oldRight,
761 int oldBottom) {
Selim Cinek2ba5f1f2014-04-28 20:23:30 +0200762 if (mFirstChildWhileExpanding != null) {
763 mFirstChildMaxHeight = getMaxAllowedChildHeight(
764 mFirstChildWhileExpanding);
765 } else {
766 mFirstChildMaxHeight = 0;
767 }
768 v.removeOnLayoutChangeListener(this);
Selim Cinekb6e0e122014-04-23 17:24:37 +0200769 }
770 });
771 } else {
772 mFirstChildMaxHeight = getMaxAllowedChildHeight(mFirstChildWhileExpanding);
773 }
Selim Cinek1685e632014-04-08 02:27:49 +0200774 }
775 } else {
Selim Cinek1685e632014-04-08 02:27:49 +0200776 mFirstChildMaxHeight = 0;
777 }
778 }
779
Selim Cinek7d447722014-06-10 15:51:59 +0200780 private boolean isMaxSizeInitialized(ExpandableView child) {
781 if (child instanceof ExpandableNotificationRow) {
782 ExpandableNotificationRow row = (ExpandableNotificationRow) child;
783 return row.isShowingLayoutLayouted();
784 }
785 return child == null || child.getWidth() != 0;
786 }
787
Jorim Jaggi9f347ae2014-04-11 00:47:18 +0200788 private View findFirstVisibleChild(ViewGroup container) {
789 int childCount = container.getChildCount();
790 for (int i = 0; i < childCount; i++) {
791 View child = container.getChildAt(i);
792 if (child.getVisibility() != View.GONE) {
793 return child;
794 }
795 }
796 return null;
797 }
798
Selim Cinek1685e632014-04-08 02:27:49 +0200799 public void onExpansionStopped() {
800 mIsExpansionChanging = false;
801 mFirstChildWhileExpanding = null;
802 }
803
804 public void setIsExpanded(boolean isExpanded) {
805 this.mIsExpanded = isExpanded;
806 }
807
808 public void notifyChildrenChanged(ViewGroup hostView) {
809 if (mIsExpansionChanging) {
810 updateFirstChildHeightWhileExpanding(hostView);
811 }
812 }
813
Selim Cinek34c0a8d2014-05-12 00:01:43 +0200814 public void setDimmed(boolean dimmed) {
815 updatePadding(dimmed);
816 }
817
Selim Cinek67b22602014-03-10 15:40:16 +0100818 class StackScrollAlgorithmState {
819
820 /**
821 * The scroll position of the algorithm
822 */
823 public int scrollY;
824
825 /**
826 * The quantity of items which are in the top stack.
827 */
828 public float itemsInTopStack;
829
830 /**
831 * how far in is the element currently transitioning into the top stack
832 */
833 public float partialInTop;
834
835 /**
Selim Cinek343e6e22014-04-11 21:23:30 +0200836 * The number of pixels the last child in the top stack has scrolled in to the stack
837 */
838 public float scrolledPixelsTop;
839
840 /**
Selim Cinek67b22602014-03-10 15:40:16 +0100841 * The last item index which is in the top stack.
Selim Cinek67b22602014-03-10 15:40:16 +0100842 */
843 public int lastTopStackIndex;
844
845 /**
846 * The quantity of items which are in the bottom stack.
847 */
848 public float itemsInBottomStack;
849
850 /**
851 * how far in is the element currently transitioning into the bottom stack
852 */
853 public float partialInBottom;
Jorim Jaggid4a57442014-04-10 02:45:55 +0200854
855 /**
856 * The children from the host view which are not gone.
857 */
Jorim Jaggibe565df2014-04-28 17:51:23 +0200858 public final ArrayList<ExpandableView> visibleChildren = new ArrayList<ExpandableView>();
Selim Cinek67b22602014-03-10 15:40:16 +0100859 }
860
861}