blob: e4a1c27471e1c545f021e336ab1316936bf40e81 [file] [log] [blame]
Selim Cinek67b22602014-03-10 15:40:16 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
15 */
16
17package com.android.systemui.statusbar.stack;
18
19import android.content.Context;
Jorim Jaggid7c1fae2014-08-13 18:27:47 +020020import android.util.DisplayMetrics;
Christoph Studer6e3eceb2014-04-01 18:40:27 +020021import android.util.Log;
Selim Cinek67b22602014-03-10 15:40:16 +010022import android.view.View;
23import android.view.ViewGroup;
Selim Cinek343e6e22014-04-11 21:23:30 +020024
Selim Cinek67b22602014-03-10 15:40:16 +010025import com.android.systemui.R;
Selim Cinek1685e632014-04-08 02:27:49 +020026import com.android.systemui.statusbar.ExpandableNotificationRow;
Jorim Jaggibe565df2014-04-28 17:51:23 +020027import com.android.systemui.statusbar.ExpandableView;
Selim Cinek67b22602014-03-10 15:40:16 +010028
Jorim Jaggid4a57442014-04-10 02:45:55 +020029import java.util.ArrayList;
30
Selim Cinek67b22602014-03-10 15:40:16 +010031/**
32 * The Algorithm of the {@link com.android.systemui.statusbar.stack
33 * .NotificationStackScrollLayout} which can be queried for {@link com.android.systemui.statusbar
34 * .stack.StackScrollState}
35 */
36public class StackScrollAlgorithm {
37
Christoph Studer6e3eceb2014-04-01 18:40:27 +020038 private static final String LOG_TAG = "StackScrollAlgorithm";
39
Selim Cinek67b22602014-03-10 15:40:16 +010040 private static final int MAX_ITEMS_IN_BOTTOM_STACK = 3;
41 private static final int MAX_ITEMS_IN_TOP_STACK = 3;
42
Jorim Jaggi362dd6d2014-07-09 19:04:07 +020043 public static final float DIMMED_SCALE = 0.95f;
Jorim Jaggid552d9d2014-05-07 19:41:13 +020044
Selim Cinek67b22602014-03-10 15:40:16 +010045 private int mPaddingBetweenElements;
46 private int mCollapsedSize;
47 private int mTopStackPeekSize;
48 private int mBottomStackPeekSize;
49 private int mZDistanceBetweenElements;
50 private int mZBasicHeight;
Selim Cinek708a6c12014-05-28 14:16:02 +020051 private int mRoundedRectCornerRadius;
Selim Cinek67b22602014-03-10 15:40:16 +010052
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 Cinek34c0a8d2014-05-12 00:01:43 +020068 private int mPaddingBetweenElementsDimmed;
69 private int mPaddingBetweenElementsNormal;
70 private int mBottomStackSlowDownLength;
Selim Cinekad3e5af2014-07-04 12:24:11 +020071 private int mTopStackSlowDownLength;
Selim Cinekd83771e2014-07-04 16:45:31 +020072 private int mCollapseSecondCardPadding;
Selim Cinek3afd00e2014-08-11 22:32:57 +020073 private boolean mIsSmallScreen;
74 private int mMaxNotificationHeight;
Jorim Jaggid7c1fae2014-08-13 18:27:47 +020075 private boolean mScaleDimmed;
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) {
Jorim Jaggid7c1fae2014-08-13 18:27:47 +020083 mPaddingBetweenElements = dimmed && mScaleDimmed
Selim Cinek34c0a8d2014-05-12 00:01:43 +020084 ? 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);
Jorim Jaggid7c1fae2014-08-13 18:27:47 +0200128 mScaleDimmed = context.getResources().getDisplayMetrics().densityDpi
129 >= DisplayMetrics.DENSITY_XXHIGH;
Selim Cinek67b22602014-03-10 15:40:16 +0100130 }
131
Jorim Jaggid7c1fae2014-08-13 18:27:47 +0200132 public boolean shouldScaleDimmed() {
133 return mScaleDimmed;
134 }
Selim Cinek67b22602014-03-10 15:40:16 +0100135
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200136 public void getStackScrollState(AmbientState ambientState, StackScrollState resultState) {
Selim Cinek67b22602014-03-10 15:40:16 +0100137 // The state of the local variables are saved in an algorithmState to easily subdivide it
138 // into multiple phases.
139 StackScrollAlgorithmState algorithmState = mTempAlgorithmState;
140
141 // First we reset the view states to their default values.
142 resultState.resetViewStates();
143
Selim Cinek343e6e22014-04-11 21:23:30 +0200144 algorithmState.itemsInTopStack = 0.0f;
Selim Cinek67b22602014-03-10 15:40:16 +0100145 algorithmState.partialInTop = 0.0f;
146 algorithmState.lastTopStackIndex = 0;
Selim Cinek343e6e22014-04-11 21:23:30 +0200147 algorithmState.scrolledPixelsTop = 0;
Selim Cinek67b22602014-03-10 15:40:16 +0100148 algorithmState.itemsInBottomStack = 0.0f;
Selim Cinek343e6e22014-04-11 21:23:30 +0200149 algorithmState.partialInBottom = 0.0f;
Selim Cinek8d9ff9c2014-05-12 15:13:04 +0200150 float bottomOverScroll = ambientState.getOverScrollAmount(false /* onTop */);
Selim Cinek1408eb52014-06-02 14:45:38 +0200151
152 int scrollY = ambientState.getScrollY();
153
154 // Due to the overScroller, the stackscroller can have negative scroll state. This is
155 // already accounted for by the top padding and doesn't need an additional adaption
156 scrollY = Math.max(0, scrollY);
157 algorithmState.scrollY = (int) (scrollY + mCollapsedSize + bottomOverScroll);
Selim Cinek343e6e22014-04-11 21:23:30 +0200158
Jorim Jaggid4a57442014-04-10 02:45:55 +0200159 updateVisibleChildren(resultState, algorithmState);
Selim Cinek67b22602014-03-10 15:40:16 +0100160
161 // Phase 1:
162 findNumberOfItemsInTopStackAndUpdateState(resultState, algorithmState);
163
164 // Phase 2:
165 updatePositionsForState(resultState, algorithmState);
166
167 // Phase 3:
168 updateZValuesForState(resultState, algorithmState);
Selim Cinekeb973562014-05-02 17:07:49 +0200169
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200170 handleDraggedViews(ambientState, resultState, algorithmState);
Jorim Jaggiae441282014-08-01 02:45:18 +0200171 updateDimmedActivatedHideSensitive(ambientState, resultState, algorithmState);
Selim Cinek708a6c12014-05-28 14:16:02 +0200172 updateClipping(resultState, algorithmState);
Selim Cinek3d2b94bf2014-07-02 22:12:47 +0200173 updateSpeedBumpState(resultState, algorithmState, ambientState.getSpeedBumpIndex());
174 }
175
176 private void updateSpeedBumpState(StackScrollState resultState,
177 StackScrollAlgorithmState algorithmState, int speedBumpIndex) {
178 int childCount = algorithmState.visibleChildren.size();
179 for (int i = 0; i < childCount; i++) {
180 View child = algorithmState.visibleChildren.get(i);
181 StackScrollState.ViewState childViewState = resultState.getViewStateForView(child);
Selim Cinek3107cfa2014-07-22 15:24:29 +0200182
183 // The speed bump can also be gone, so equality needs to be taken when comparing
184 // indices.
185 childViewState.belowSpeedBump = speedBumpIndex != -1 && i >= speedBumpIndex;
Selim Cinek3d2b94bf2014-07-02 22:12:47 +0200186 }
Selim Cinekf54090e2014-06-17 17:24:51 -0700187 }
188
Selim Cinek708a6c12014-05-28 14:16:02 +0200189 private void updateClipping(StackScrollState resultState,
190 StackScrollAlgorithmState algorithmState) {
191 float previousNotificationEnd = 0;
192 float previousNotificationStart = 0;
193 boolean previousNotificationIsSwiped = false;
194 int childCount = algorithmState.visibleChildren.size();
195 for (int i = 0; i < childCount; i++) {
196 ExpandableView child = algorithmState.visibleChildren.get(i);
197 StackScrollState.ViewState state = resultState.getViewStateForView(child);
Jorim Jaggi2e34ec32014-07-01 03:17:05 +0200198 float newYTranslation = state.yTranslation + state.height * (1f - state.scale) / 2f;
199 float newHeight = state.height * state.scale;
Selim Cinek708a6c12014-05-28 14:16:02 +0200200 // apply clipping and shadow
201 float newNotificationEnd = newYTranslation + newHeight;
202
203 // In the unlocked shade we have to clip a little bit higher because of the rounded
204 // corners of the notifications.
Jorim Jaggi2e34ec32014-07-01 03:17:05 +0200205 float clippingCorrection = state.dimmed ? 0 : mRoundedRectCornerRadius * state.scale;
Selim Cinek708a6c12014-05-28 14:16:02 +0200206
207 // When the previous notification is swiped, we don't clip the content to the
208 // bottom of it.
209 float clipHeight = previousNotificationIsSwiped
210 ? newHeight
211 : newNotificationEnd - (previousNotificationEnd - clippingCorrection);
212
213 updateChildClippingAndBackground(state, newHeight, clipHeight,
Jorim Jaggi2e34ec32014-07-01 03:17:05 +0200214 newHeight - (previousNotificationStart - newYTranslation));
Selim Cinek708a6c12014-05-28 14:16:02 +0200215
216 if (!child.isTransparent()) {
217 // Only update the previous values if we are not transparent,
218 // otherwise we would clip to a transparent view.
Jorim Jaggi2e34ec32014-07-01 03:17:05 +0200219 previousNotificationStart = newYTranslation + state.clipTopAmount * state.scale;
Selim Cinek708a6c12014-05-28 14:16:02 +0200220 previousNotificationEnd = newNotificationEnd;
221 previousNotificationIsSwiped = child.getTranslationX() != 0;
222 }
223 }
224 }
225
226 /**
227 * Updates the shadow outline and the clipping for a view.
228 *
229 * @param state the viewState to update
230 * @param realHeight the currently applied height of the view
231 * @param clipHeight the desired clip height, the rest of the view will be clipped from the top
232 * @param backgroundHeight the desired background height. The shadows of the view will be
233 * based on this height and the content will be clipped from the top
234 */
Jorim Jaggi2e34ec32014-07-01 03:17:05 +0200235 private void updateChildClippingAndBackground(StackScrollState.ViewState state,
236 float realHeight, float clipHeight, float backgroundHeight) {
Selim Cinek708a6c12014-05-28 14:16:02 +0200237 if (realHeight > clipHeight) {
Jorim Jaggi2e34ec32014-07-01 03:17:05 +0200238 // Rather overlap than create a hole.
239 state.topOverLap = (int) Math.floor((realHeight - clipHeight) / state.scale);
Selim Cinek708a6c12014-05-28 14:16:02 +0200240 } else {
241 state.topOverLap = 0;
242 }
243 if (realHeight > backgroundHeight) {
Jorim Jaggi2e34ec32014-07-01 03:17:05 +0200244 // Rather overlap than create a hole.
245 state.clipTopAmount = (int) Math.floor((realHeight - backgroundHeight) / state.scale);
Selim Cinek708a6c12014-05-28 14:16:02 +0200246 } else {
247 state.clipTopAmount = 0;
248 }
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200249 }
250
251 /**
Jorim Jaggiae441282014-08-01 02:45:18 +0200252 * Updates the dimmed, activated and hiding sensitive states of the children.
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200253 */
Jorim Jaggiae441282014-08-01 02:45:18 +0200254 private void updateDimmedActivatedHideSensitive(AmbientState ambientState,
255 StackScrollState resultState, StackScrollAlgorithmState algorithmState) {
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200256 boolean dimmed = ambientState.isDimmed();
John Spurlockbf370992014-06-17 13:58:31 -0400257 boolean dark = ambientState.isDark();
Jorim Jaggiae441282014-08-01 02:45:18 +0200258 boolean hideSensitive = ambientState.isHideSensitive();
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200259 View activatedChild = ambientState.getActivatedChild();
260 int childCount = algorithmState.visibleChildren.size();
261 for (int i = 0; i < childCount; i++) {
262 View child = algorithmState.visibleChildren.get(i);
263 StackScrollState.ViewState childViewState = resultState.getViewStateForView(child);
264 childViewState.dimmed = dimmed;
John Spurlockbf370992014-06-17 13:58:31 -0400265 childViewState.dark = dark;
Jorim Jaggiae441282014-08-01 02:45:18 +0200266 childViewState.hideSensitive = hideSensitive;
Selim Cinekb89de4e2014-06-10 10:47:05 +0200267 boolean isActivatedChild = activatedChild == child;
Jorim Jaggid7c1fae2014-08-13 18:27:47 +0200268 childViewState.scale = !mScaleDimmed || !dimmed || isActivatedChild
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200269 ? 1.0f
270 : DIMMED_SCALE;
Jorim Jaggi4538cee2014-09-09 15:21:38 +0200271 if (dimmed && isActivatedChild) {
272 childViewState.zTranslation += 2.0f * mZDistanceBetweenElements;
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200273 }
274 }
Selim Cinekeb973562014-05-02 17:07:49 +0200275 }
276
277 /**
278 * Handle the special state when views are being dragged
279 */
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200280 private void handleDraggedViews(AmbientState ambientState, StackScrollState resultState,
Selim Cinekeb973562014-05-02 17:07:49 +0200281 StackScrollAlgorithmState algorithmState) {
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200282 ArrayList<View> draggedViews = ambientState.getDraggedViews();
283 for (View draggedView : draggedViews) {
Selim Cinekeb973562014-05-02 17:07:49 +0200284 int childIndex = algorithmState.visibleChildren.indexOf(draggedView);
285 if (childIndex >= 0 && childIndex < algorithmState.visibleChildren.size() - 1) {
286 View nextChild = algorithmState.visibleChildren.get(childIndex + 1);
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200287 if (!draggedViews.contains(nextChild)) {
Selim Cinekeb973562014-05-02 17:07:49 +0200288 // only if the view is not dragged itself we modify its state to be fully
289 // visible
290 StackScrollState.ViewState viewState = resultState.getViewStateForView(
291 nextChild);
292 // The child below the dragged one must be fully visible
293 viewState.alpha = 1;
294 }
295
296 // Lets set the alpha to the one it currently has, as its currently being dragged
297 StackScrollState.ViewState viewState = resultState.getViewStateForView(draggedView);
298 // The dragged child should keep the set alpha
299 viewState.alpha = draggedView.getAlpha();
300 }
301 }
Selim Cinek343e6e22014-04-11 21:23:30 +0200302 }
Selim Cinek67b22602014-03-10 15:40:16 +0100303
Selim Cinek343e6e22014-04-11 21:23:30 +0200304 /**
Jorim Jaggid4a57442014-04-10 02:45:55 +0200305 * Update the visible children on the state.
306 */
307 private void updateVisibleChildren(StackScrollState resultState,
308 StackScrollAlgorithmState state) {
309 ViewGroup hostView = resultState.getHostView();
310 int childCount = hostView.getChildCount();
311 state.visibleChildren.clear();
312 state.visibleChildren.ensureCapacity(childCount);
313 for (int i = 0; i < childCount; i++) {
Jorim Jaggibe565df2014-04-28 17:51:23 +0200314 ExpandableView v = (ExpandableView) hostView.getChildAt(i);
Jorim Jaggid4a57442014-04-10 02:45:55 +0200315 if (v.getVisibility() != View.GONE) {
Selim Cinek8efa6dd2014-05-19 16:27:37 +0200316 StackScrollState.ViewState viewState = resultState.getViewStateForView(v);
317 viewState.notGoneIndex = state.visibleChildren.size();
Jorim Jaggid4a57442014-04-10 02:45:55 +0200318 state.visibleChildren.add(v);
319 }
320 }
321 }
322
323 /**
Selim Cinek67b22602014-03-10 15:40:16 +0100324 * Determine the positions for the views. This is the main part of the algorithm.
325 *
326 * @param resultState The result state to update if a change to the properties of a child occurs
327 * @param algorithmState The state in which the current pass of the algorithm is currently in
Selim Cinek67b22602014-03-10 15:40:16 +0100328 */
329 private void updatePositionsForState(StackScrollState resultState,
330 StackScrollAlgorithmState algorithmState) {
Selim Cinek67b22602014-03-10 15:40:16 +0100331
Selim Cinek1685e632014-04-08 02:27:49 +0200332 // The starting position of the bottom stack peek
Jorim Jaggi8c1a44b2014-04-29 19:04:02 +0200333 float bottomPeekStart = mInnerHeight - mBottomStackPeekSize;
Selim Cinek1685e632014-04-08 02:27:49 +0200334
Selim Cinek67b22602014-03-10 15:40:16 +0100335 // The position where the bottom stack starts.
Selim Cinek34c0a8d2014-05-12 00:01:43 +0200336 float bottomStackStart = bottomPeekStart - mBottomStackSlowDownLength;
Selim Cinek67b22602014-03-10 15:40:16 +0100337
338 // The y coordinate of the current child.
339 float currentYPosition = 0.0f;
340
341 // How far in is the element currently transitioning into the bottom stack.
342 float yPositionInScrollView = 0.0f;
343
Jorim Jaggid4a57442014-04-10 02:45:55 +0200344 int childCount = algorithmState.visibleChildren.size();
Selim Cinek67b22602014-03-10 15:40:16 +0100345 int numberOfElementsCompletelyIn = (int) algorithmState.itemsInTopStack;
346 for (int i = 0; i < childCount; i++) {
Jorim Jaggibe565df2014-04-28 17:51:23 +0200347 ExpandableView child = algorithmState.visibleChildren.get(i);
Selim Cinek67b22602014-03-10 15:40:16 +0100348 StackScrollState.ViewState childViewState = resultState.getViewStateForView(child);
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200349 childViewState.location = StackScrollState.ViewState.LOCATION_UNKNOWN;
Jorim Jaggibe565df2014-04-28 17:51:23 +0200350 int childHeight = getMaxAllowedChildHeight(child);
Selim Cinek67b22602014-03-10 15:40:16 +0100351 float yPositionInScrollViewAfterElement = yPositionInScrollView
352 + childHeight
353 + mPaddingBetweenElements;
Selim Cinek343e6e22014-04-11 21:23:30 +0200354 float scrollOffset = yPositionInScrollView - algorithmState.scrollY + mCollapsedSize;
355
356 if (i == algorithmState.lastTopStackIndex + 1) {
357 // Normally the position of this child is the position in the regular scrollview,
358 // but if the two stacks are very close to each other,
359 // then have have to push it even more upwards to the position of the bottom
360 // stack start.
361 currentYPosition = Math.min(scrollOffset, bottomStackStart);
362 }
363 childViewState.yTranslation = currentYPosition;
364
365 // The y position after this element
366 float nextYPosition = currentYPosition + childHeight +
367 mPaddingBetweenElements;
368
369 if (i <= algorithmState.lastTopStackIndex) {
Selim Cinek67b22602014-03-10 15:40:16 +0100370 // Case 1:
371 // We are in the top Stack
Selim Cinek343e6e22014-04-11 21:23:30 +0200372 updateStateForTopStackChild(algorithmState,
373 numberOfElementsCompletelyIn, i, childHeight, childViewState, scrollOffset);
Selim Cinek3afd00e2014-08-11 22:32:57 +0200374 clampPositionToTopStackEnd(childViewState, childHeight);
375
Selim Cinek343e6e22014-04-11 21:23:30 +0200376 // check if we are overlapping with the bottom stack
377 if (childViewState.yTranslation + childHeight + mPaddingBetweenElements
Selim Cinek4581cf82014-08-12 12:40:32 +0200378 >= bottomStackStart && !mIsExpansionChanging && i != 0 && mIsSmallScreen) {
Selim Cinek3afd00e2014-08-11 22:32:57 +0200379 // we just collapse this element slightly
380 int newSize = (int) Math.max(bottomStackStart - mPaddingBetweenElements -
381 childViewState.yTranslation, mCollapsedSize);
382 childViewState.height = newSize;
Selim Cinek4581cf82014-08-12 12:40:32 +0200383 updateStateForChildTransitioningInBottom(algorithmState, bottomStackStart,
384 bottomPeekStart, childViewState.yTranslation, childViewState,
385 childHeight);
Selim Cinek343e6e22014-04-11 21:23:30 +0200386 }
Selim Cinek3afd00e2014-08-11 22:32:57 +0200387 clampPositionToBottomStackStart(childViewState, childViewState.height);
Selim Cinek343e6e22014-04-11 21:23:30 +0200388 } else if (nextYPosition >= bottomStackStart) {
Selim Cinek67b22602014-03-10 15:40:16 +0100389 // Case 2:
Selim Cinek343e6e22014-04-11 21:23:30 +0200390 // We are in the bottom stack.
391 if (currentYPosition >= bottomStackStart) {
Selim Cinek67b22602014-03-10 15:40:16 +0100392 // According to the regular scroll view we are fully translated out of the
393 // bottom of the screen so we are fully in the bottom stack
Selim Cinek343e6e22014-04-11 21:23:30 +0200394 updateStateForChildFullyInBottomStack(algorithmState,
395 bottomStackStart, childViewState, childHeight);
Selim Cinek67b22602014-03-10 15:40:16 +0100396 } else {
Selim Cinek67b22602014-03-10 15:40:16 +0100397 // According to the regular scroll view we are currently translating out of /
398 // into the bottom of the screen
Selim Cinek343e6e22014-04-11 21:23:30 +0200399 updateStateForChildTransitioningInBottom(algorithmState,
400 bottomStackStart, bottomPeekStart, currentYPosition,
401 childViewState, childHeight);
Selim Cinek67b22602014-03-10 15:40:16 +0100402 }
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200403 } else {
Selim Cinek343e6e22014-04-11 21:23:30 +0200404 // Case 3:
405 // We are in the regular scroll area.
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200406 childViewState.location = StackScrollState.ViewState.LOCATION_MAIN_AREA;
Selim Cinek343e6e22014-04-11 21:23:30 +0200407 clampYTranslation(childViewState, childHeight);
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200408 }
Selim Cinek343e6e22014-04-11 21:23:30 +0200409
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200410 // The first card is always rendered.
411 if (i == 0) {
412 childViewState.alpha = 1.0f;
Selim Cinek8d9ff9c2014-05-12 15:13:04 +0200413 childViewState.yTranslation = Math.max(mCollapsedSize - algorithmState.scrollY, 0);
Selim Cinekd83771e2014-07-04 16:45:31 +0200414 if (childViewState.yTranslation + childViewState.height
415 > bottomPeekStart - mCollapseSecondCardPadding) {
Selim Cinek4fe3e472014-07-03 16:32:54 +0200416 childViewState.height = (int) Math.max(
Selim Cinekd83771e2014-07-04 16:45:31 +0200417 bottomPeekStart - mCollapseSecondCardPadding
418 - childViewState.yTranslation, mCollapsedSize);
Selim Cinek4fe3e472014-07-03 16:32:54 +0200419 }
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200420 childViewState.location = StackScrollState.ViewState.LOCATION_FIRST_CARD;
421 }
422 if (childViewState.location == StackScrollState.ViewState.LOCATION_UNKNOWN) {
423 Log.wtf(LOG_TAG, "Failed to assign location for child " + i);
Selim Cinek67b22602014-03-10 15:40:16 +0100424 }
Selim Cinek343e6e22014-04-11 21:23:30 +0200425 currentYPosition = childViewState.yTranslation + childHeight + mPaddingBetweenElements;
Selim Cinek67b22602014-03-10 15:40:16 +0100426 yPositionInScrollView = yPositionInScrollViewAfterElement;
Jorim Jaggi8c1a44b2014-04-29 19:04:02 +0200427
428 childViewState.yTranslation += mTopPadding;
Selim Cinek67b22602014-03-10 15:40:16 +0100429 }
430 }
431
Selim Cinek1685e632014-04-08 02:27:49 +0200432 /**
Selim Cinek343e6e22014-04-11 21:23:30 +0200433 * Clamp the yTranslation both up and down to valid positions.
Selim Cinek1685e632014-04-08 02:27:49 +0200434 *
Selim Cinek1685e632014-04-08 02:27:49 +0200435 * @param childViewState the view state of the child
Selim Cinek343e6e22014-04-11 21:23:30 +0200436 * @param childHeight the height of this child
Selim Cinek1685e632014-04-08 02:27:49 +0200437 */
Selim Cinek343e6e22014-04-11 21:23:30 +0200438 private void clampYTranslation(StackScrollState.ViewState childViewState, int childHeight) {
439 clampPositionToBottomStackStart(childViewState, childHeight);
440 clampPositionToTopStackEnd(childViewState, childHeight);
441 }
442
443 /**
444 * Clamp the yTranslation of the child down such that its end is at most on the beginning of
445 * the bottom stack.
446 *
447 * @param childViewState the view state of the child
448 * @param childHeight the height of this child
449 */
450 private void clampPositionToBottomStackStart(StackScrollState.ViewState childViewState,
451 int childHeight) {
452 childViewState.yTranslation = Math.min(childViewState.yTranslation,
Selim Cinekd83771e2014-07-04 16:45:31 +0200453 mInnerHeight - mBottomStackPeekSize - mCollapseSecondCardPadding - childHeight);
Selim Cinek343e6e22014-04-11 21:23:30 +0200454 }
455
456 /**
457 * 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 +0200458 * stack.get
Selim Cinek343e6e22014-04-11 21:23:30 +0200459 *
460 * @param childViewState the view state of the child
461 * @param childHeight the height of this child
462 */
463 private void clampPositionToTopStackEnd(StackScrollState.ViewState childViewState,
464 int childHeight) {
465 childViewState.yTranslation = Math.max(childViewState.yTranslation,
466 mCollapsedSize - childHeight);
Selim Cinek1685e632014-04-08 02:27:49 +0200467 }
468
469 private int getMaxAllowedChildHeight(View child) {
470 if (child instanceof ExpandableNotificationRow) {
471 ExpandableNotificationRow row = (ExpandableNotificationRow) child;
Jorim Jaggi9cbadd32014-05-01 20:18:31 +0200472 return row.getIntrinsicHeight();
Jorim Jaggibe565df2014-04-28 17:51:23 +0200473 } else if (child instanceof ExpandableView) {
474 ExpandableView expandableView = (ExpandableView) child;
475 return expandableView.getActualHeight();
Selim Cinek1685e632014-04-08 02:27:49 +0200476 }
Jorim Jaggibe565df2014-04-28 17:51:23 +0200477 return child == null? mCollapsedSize : child.getHeight();
Selim Cinek1685e632014-04-08 02:27:49 +0200478 }
479
Selim Cinek343e6e22014-04-11 21:23:30 +0200480 private void updateStateForChildTransitioningInBottom(StackScrollAlgorithmState algorithmState,
481 float transitioningPositionStart, float bottomPeakStart, float currentYPosition,
482 StackScrollState.ViewState childViewState, int childHeight) {
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200483
Selim Cinek343e6e22014-04-11 21:23:30 +0200484 // This is the transitioning element on top of bottom stack, calculate how far we are in.
485 algorithmState.partialInBottom = 1.0f - (
486 (transitioningPositionStart - currentYPosition) / (childHeight +
487 mPaddingBetweenElements));
488
489 // the offset starting at the transitionPosition of the bottom stack
490 float offset = mBottomStackIndentationFunctor.getValue(algorithmState.partialInBottom);
491 algorithmState.itemsInBottomStack += algorithmState.partialInBottom;
Selim Cinek3afd00e2014-08-11 22:32:57 +0200492 int newHeight = childHeight;
493 if (childHeight > mCollapsedSize && mIsSmallScreen) {
494 newHeight = (int) Math.max(Math.min(transitioningPositionStart + offset -
495 mPaddingBetweenElements - currentYPosition, childHeight), mCollapsedSize);
496 childViewState.height = newHeight;
497 }
498 childViewState.yTranslation = transitioningPositionStart + offset - newHeight
Jorim Jaggife40f7d2014-04-28 15:20:04 +0200499 - mPaddingBetweenElements;
Selim Cinek3afd00e2014-08-11 22:32:57 +0200500
Selim Cinek343e6e22014-04-11 21:23:30 +0200501 // We want at least to be at the end of the top stack when collapsing
Selim Cinek3afd00e2014-08-11 22:32:57 +0200502 clampPositionToTopStackEnd(childViewState, newHeight);
Selim Cinek343e6e22014-04-11 21:23:30 +0200503 childViewState.location = StackScrollState.ViewState.LOCATION_MAIN_AREA;
Selim Cinek67b22602014-03-10 15:40:16 +0100504 }
505
Selim Cinek343e6e22014-04-11 21:23:30 +0200506 private void updateStateForChildFullyInBottomStack(StackScrollAlgorithmState algorithmState,
Selim Cinek67b22602014-03-10 15:40:16 +0100507 float transitioningPositionStart, StackScrollState.ViewState childViewState,
508 int childHeight) {
509
Selim Cinek343e6e22014-04-11 21:23:30 +0200510 float currentYPosition;
Selim Cinek67b22602014-03-10 15:40:16 +0100511 algorithmState.itemsInBottomStack += 1.0f;
512 if (algorithmState.itemsInBottomStack < MAX_ITEMS_IN_BOTTOM_STACK) {
513 // We are visually entering the bottom stack
Selim Cinek343e6e22014-04-11 21:23:30 +0200514 currentYPosition = transitioningPositionStart
Jorim Jaggife40f7d2014-04-28 15:20:04 +0200515 + mBottomStackIndentationFunctor.getValue(algorithmState.itemsInBottomStack)
516 - mPaddingBetweenElements;
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200517 childViewState.location = StackScrollState.ViewState.LOCATION_BOTTOM_STACK_PEEKING;
Selim Cinek67b22602014-03-10 15:40:16 +0100518 } else {
519 // we are fully inside the stack
520 if (algorithmState.itemsInBottomStack > MAX_ITEMS_IN_BOTTOM_STACK + 2) {
521 childViewState.alpha = 0.0f;
522 } else if (algorithmState.itemsInBottomStack
523 > MAX_ITEMS_IN_BOTTOM_STACK + 1) {
524 childViewState.alpha = 1.0f - algorithmState.partialInBottom;
525 }
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200526 childViewState.location = StackScrollState.ViewState.LOCATION_BOTTOM_STACK_HIDDEN;
Jorim Jaggi8c1a44b2014-04-29 19:04:02 +0200527 currentYPosition = mInnerHeight;
Selim Cinek67b22602014-03-10 15:40:16 +0100528 }
Selim Cinek343e6e22014-04-11 21:23:30 +0200529 childViewState.yTranslation = currentYPosition - childHeight;
530 clampPositionToTopStackEnd(childViewState, childHeight);
Selim Cinek67b22602014-03-10 15:40:16 +0100531 }
532
Selim Cinek343e6e22014-04-11 21:23:30 +0200533 private void updateStateForTopStackChild(StackScrollAlgorithmState algorithmState,
534 int numberOfElementsCompletelyIn, int i, int childHeight,
535 StackScrollState.ViewState childViewState, float scrollOffset) {
Selim Cinek67b22602014-03-10 15:40:16 +0100536
Selim Cinek67b22602014-03-10 15:40:16 +0100537
538 // First we calculate the index relative to the current stack window of size at most
539 // {@link #MAX_ITEMS_IN_TOP_STACK}
Selim Cinek343e6e22014-04-11 21:23:30 +0200540 int paddedIndex = i - 1
Selim Cinek67b22602014-03-10 15:40:16 +0100541 - Math.max(numberOfElementsCompletelyIn - MAX_ITEMS_IN_TOP_STACK, 0);
542 if (paddedIndex >= 0) {
Selim Cinek343e6e22014-04-11 21:23:30 +0200543
Selim Cinek67b22602014-03-10 15:40:16 +0100544 // We are currently visually entering the top stack
Selim Cinekad3e5af2014-07-04 12:24:11 +0200545 float distanceToStack = (childHeight + mPaddingBetweenElements)
546 - algorithmState.scrolledPixelsTop;
547 if (i == algorithmState.lastTopStackIndex
548 && distanceToStack > (mTopStackTotalSize + mPaddingBetweenElements)) {
Selim Cinek343e6e22014-04-11 21:23:30 +0200549
550 // Child is currently translating into stack but not yet inside slow down zone.
551 // Handle it like the regular scrollview.
552 childViewState.yTranslation = scrollOffset;
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200553 } else {
Selim Cinek343e6e22014-04-11 21:23:30 +0200554 // Apply stacking logic.
555 float numItemsBefore;
556 if (i == algorithmState.lastTopStackIndex) {
Selim Cinekad3e5af2014-07-04 12:24:11 +0200557 numItemsBefore = 1.0f
558 - (distanceToStack / (mTopStackTotalSize + mPaddingBetweenElements));
Selim Cinek343e6e22014-04-11 21:23:30 +0200559 } else {
560 numItemsBefore = algorithmState.itemsInTopStack - i;
561 }
562 // The end position of the current child
Selim Cinekad3e5af2014-07-04 12:24:11 +0200563 float currentChildEndY = mCollapsedSize + mTopStackTotalSize
564 - mTopStackIndentationFunctor.getValue(numItemsBefore);
Selim Cinek343e6e22014-04-11 21:23:30 +0200565 childViewState.yTranslation = currentChildEndY - childHeight;
Selim Cinek67b22602014-03-10 15:40:16 +0100566 }
Selim Cinek343e6e22014-04-11 21:23:30 +0200567 childViewState.location = StackScrollState.ViewState.LOCATION_TOP_STACK_PEEKING;
Selim Cinek67b22602014-03-10 15:40:16 +0100568 } else {
Selim Cinek343e6e22014-04-11 21:23:30 +0200569 if (paddedIndex == -1) {
570 childViewState.alpha = 1.0f - algorithmState.partialInTop;
571 } else {
572 // We are hidden behind the top card and faded out, so we can hide ourselves.
573 childViewState.alpha = 0.0f;
574 }
575 childViewState.yTranslation = mCollapsedSize - childHeight;
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200576 childViewState.location = StackScrollState.ViewState.LOCATION_TOP_STACK_HIDDEN;
Selim Cinek67b22602014-03-10 15:40:16 +0100577 }
Selim Cinek343e6e22014-04-11 21:23:30 +0200578
579
Selim Cinek67b22602014-03-10 15:40:16 +0100580 }
581
582 /**
583 * Find the number of items in the top stack and update the result state if needed.
584 *
585 * @param resultState The result state to update if a height change of an child occurs
586 * @param algorithmState The state in which the current pass of the algorithm is currently in
Selim Cinek67b22602014-03-10 15:40:16 +0100587 */
588 private void findNumberOfItemsInTopStackAndUpdateState(StackScrollState resultState,
589 StackScrollAlgorithmState algorithmState) {
590
591 // The y Position if the element would be in a regular scrollView
592 float yPositionInScrollView = 0.0f;
Jorim Jaggid4a57442014-04-10 02:45:55 +0200593 int childCount = algorithmState.visibleChildren.size();
Selim Cinek67b22602014-03-10 15:40:16 +0100594
595 // find the number of elements in the top stack.
596 for (int i = 0; i < childCount; i++) {
Jorim Jaggibe565df2014-04-28 17:51:23 +0200597 ExpandableView child = algorithmState.visibleChildren.get(i);
Selim Cinek67b22602014-03-10 15:40:16 +0100598 StackScrollState.ViewState childViewState = resultState.getViewStateForView(child);
Jorim Jaggibe565df2014-04-28 17:51:23 +0200599 int childHeight = getMaxAllowedChildHeight(child);
Selim Cinek67b22602014-03-10 15:40:16 +0100600 float yPositionInScrollViewAfterElement = yPositionInScrollView
601 + childHeight
602 + mPaddingBetweenElements;
603 if (yPositionInScrollView < algorithmState.scrollY) {
Selim Cinek8d9ff9c2014-05-12 15:13:04 +0200604 if (i == 0 && algorithmState.scrollY <= mCollapsedSize) {
Selim Cinek343e6e22014-04-11 21:23:30 +0200605
606 // The starting position of the bottom stack peek
Selim Cinekd83771e2014-07-04 16:45:31 +0200607 int bottomPeekStart = mInnerHeight - mBottomStackPeekSize -
608 mCollapseSecondCardPadding;
Selim Cinek343e6e22014-04-11 21:23:30 +0200609 // Collapse and expand the first child while the shade is being expanded
610 float maxHeight = mIsExpansionChanging && child == mFirstChildWhileExpanding
611 ? mFirstChildMaxHeight
612 : childHeight;
613 childViewState.height = (int) Math.max(Math.min(bottomPeekStart, maxHeight),
614 mCollapsedSize);
615 algorithmState.itemsInTopStack = 1.0f;
616
617 } else if (yPositionInScrollViewAfterElement < algorithmState.scrollY) {
Selim Cinek67b22602014-03-10 15:40:16 +0100618 // According to the regular scroll view we are fully off screen
619 algorithmState.itemsInTopStack += 1.0f;
Selim Cinek343e6e22014-04-11 21:23:30 +0200620 if (i == 0) {
Selim Cinek67b22602014-03-10 15:40:16 +0100621 childViewState.height = mCollapsedSize;
622 }
623 } else {
624 // According to the regular scroll view we are partially off screen
Selim Cinek343e6e22014-04-11 21:23:30 +0200625
Selim Cinek67b22602014-03-10 15:40:16 +0100626 // How much did we scroll into this child
Selim Cinekad3e5af2014-07-04 12:24:11 +0200627 algorithmState.scrolledPixelsTop = algorithmState.scrollY
628 - yPositionInScrollView;
Selim Cinek343e6e22014-04-11 21:23:30 +0200629 algorithmState.partialInTop = (algorithmState.scrolledPixelsTop) / (childHeight
Selim Cinek67b22602014-03-10 15:40:16 +0100630 + mPaddingBetweenElements);
631
632 // Our element can be expanded, so this can get negative
633 algorithmState.partialInTop = Math.max(0.0f, algorithmState.partialInTop);
634 algorithmState.itemsInTopStack += algorithmState.partialInTop;
Selim Cinekad3e5af2014-07-04 12:24:11 +0200635
Selim Cinek343e6e22014-04-11 21:23:30 +0200636 if (i == 0) {
Selim Cinekad3e5af2014-07-04 12:24:11 +0200637 // If it is expanded we have to collapse it to a new size
638 float newSize = yPositionInScrollViewAfterElement
639 - mPaddingBetweenElements
640 - algorithmState.scrollY + mCollapsedSize;
641 newSize = Math.max(mCollapsedSize, newSize);
Selim Cinek4e456be2014-06-12 18:09:43 +0200642 algorithmState.itemsInTopStack = 1.0f;
Selim Cinek67b22602014-03-10 15:40:16 +0100643 childViewState.height = (int) newSize;
Selim Cinek67b22602014-03-10 15:40:16 +0100644 }
Selim Cinek343e6e22014-04-11 21:23:30 +0200645 algorithmState.lastTopStackIndex = i;
646 break;
Selim Cinek67b22602014-03-10 15:40:16 +0100647 }
648 } else {
Selim Cinek343e6e22014-04-11 21:23:30 +0200649 algorithmState.lastTopStackIndex = i - 1;
Selim Cinek67b22602014-03-10 15:40:16 +0100650 // We are already past the stack so we can end the loop
651 break;
652 }
653 yPositionInScrollView = yPositionInScrollViewAfterElement;
654 }
655 }
656
657 /**
658 * Calculate the Z positions for all children based on the number of items in both stacks and
659 * save it in the resultState
660 *
661 * @param resultState The result state to update the zTranslation values
662 * @param algorithmState The state in which the current pass of the algorithm is currently in
663 */
664 private void updateZValuesForState(StackScrollState resultState,
665 StackScrollAlgorithmState algorithmState) {
Jorim Jaggid4a57442014-04-10 02:45:55 +0200666 int childCount = algorithmState.visibleChildren.size();
Selim Cinek67b22602014-03-10 15:40:16 +0100667 for (int i = 0; i < childCount; i++) {
Jorim Jaggid4a57442014-04-10 02:45:55 +0200668 View child = algorithmState.visibleChildren.get(i);
Selim Cinek67b22602014-03-10 15:40:16 +0100669 StackScrollState.ViewState childViewState = resultState.getViewStateForView(child);
670 if (i < algorithmState.itemsInTopStack) {
671 float stackIndex = algorithmState.itemsInTopStack - i;
672 stackIndex = Math.min(stackIndex, MAX_ITEMS_IN_TOP_STACK + 2);
Selim Cinek4e456be2014-06-12 18:09:43 +0200673 if (i == 0 && algorithmState.itemsInTopStack < 2.0f) {
674
675 // We only have the top item and an additional item in the top stack,
676 // Interpolate the index from 0 to 2 while the second item is
677 // translating in.
678 stackIndex -= 1.0f;
679 if (algorithmState.scrollY > mCollapsedSize) {
680
681 // Since there is a shadow treshhold, we cant just interpolate from 0 to
682 // 2 but we interpolate from 0.1f to 2.0f when scrolled in. The jump in
683 // height will not be noticable since we have padding in between.
684 stackIndex = 0.1f + stackIndex * 1.9f;
685 }
686 }
Selim Cinek67b22602014-03-10 15:40:16 +0100687 childViewState.zTranslation = mZBasicHeight
688 + stackIndex * mZDistanceBetweenElements;
689 } else if (i > (childCount - 1 - algorithmState.itemsInBottomStack)) {
690 float numItemsAbove = i - (childCount - 1 - algorithmState.itemsInBottomStack);
691 float translationZ = mZBasicHeight
692 - numItemsAbove * mZDistanceBetweenElements;
693 childViewState.zTranslation = translationZ;
694 } else {
695 childViewState.zTranslation = mZBasicHeight;
696 }
697 }
698 }
699
Selim Cinek343e6e22014-04-11 21:23:30 +0200700 public void setLayoutHeight(int layoutHeight) {
Selim Cinek67b22602014-03-10 15:40:16 +0100701 this.mLayoutHeight = layoutHeight;
Jorim Jaggi8c1a44b2014-04-29 19:04:02 +0200702 updateInnerHeight();
703 }
704
705 public void setTopPadding(int topPadding) {
706 mTopPadding = topPadding;
707 updateInnerHeight();
708 }
709
710 private void updateInnerHeight() {
711 mInnerHeight = mLayoutHeight - mTopPadding;
Selim Cinek67b22602014-03-10 15:40:16 +0100712 }
713
Selim Cinek3afd00e2014-08-11 22:32:57 +0200714
715 /**
716 * Update whether the device is very small, i.e. Notifications can be in both the top and the
717 * bottom stack at the same time
718 *
719 * @param panelHeight The normal height of the panel when it's open
720 */
721 public void updateIsSmallScreen(int panelHeight) {
722 mIsSmallScreen = panelHeight <
723 mCollapsedSize /* top stack */
724 + mBottomStackSlowDownLength + mBottomStackPeekSize /* bottom stack */
725 + mMaxNotificationHeight; /* max notification height */
726 }
727
Selim Cinek1685e632014-04-08 02:27:49 +0200728 public void onExpansionStarted(StackScrollState currentState) {
729 mIsExpansionChanging = true;
730 mExpandedOnStart = mIsExpanded;
731 ViewGroup hostView = currentState.getHostView();
732 updateFirstChildHeightWhileExpanding(hostView);
733 }
734
735 private void updateFirstChildHeightWhileExpanding(ViewGroup hostView) {
Jorim Jaggibe565df2014-04-28 17:51:23 +0200736 mFirstChildWhileExpanding = (ExpandableView) findFirstVisibleChild(hostView);
Jorim Jaggi9f347ae2014-04-11 00:47:18 +0200737 if (mFirstChildWhileExpanding != null) {
Selim Cinek1685e632014-04-08 02:27:49 +0200738 if (mExpandedOnStart) {
739
740 // We are collapsing the shade, so the first child can get as most as high as the
741 // current height.
Jorim Jaggibe565df2014-04-28 17:51:23 +0200742 mFirstChildMaxHeight = mFirstChildWhileExpanding.getActualHeight();
Selim Cinek1685e632014-04-08 02:27:49 +0200743 } else {
Selim Cinek31094df2014-08-14 19:28:15 +0200744 updateFirstChildMaxSizeToMaxHeight();
Selim Cinek1685e632014-04-08 02:27:49 +0200745 }
746 } else {
Selim Cinek1685e632014-04-08 02:27:49 +0200747 mFirstChildMaxHeight = 0;
748 }
749 }
750
Selim Cinek31094df2014-08-14 19:28:15 +0200751 private void updateFirstChildMaxSizeToMaxHeight() {
752 // We are expanding the shade, expand it to its full height.
753 if (!isMaxSizeInitialized(mFirstChildWhileExpanding)) {
754
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) {
762 if (mFirstChildWhileExpanding != null) {
763 mFirstChildMaxHeight = getMaxAllowedChildHeight(
764 mFirstChildWhileExpanding);
765 } else {
766 mFirstChildMaxHeight = 0;
767 }
768 v.removeOnLayoutChangeListener(this);
769 }
770 });
771 } else {
772 mFirstChildMaxHeight = getMaxAllowedChildHeight(mFirstChildWhileExpanding);
773 }
774 }
775
Selim Cinek7d447722014-06-10 15:51:59 +0200776 private boolean isMaxSizeInitialized(ExpandableView child) {
777 if (child instanceof ExpandableNotificationRow) {
778 ExpandableNotificationRow row = (ExpandableNotificationRow) child;
Selim Cinek31094df2014-08-14 19:28:15 +0200779 return row.isMaxExpandHeightInitialized();
Selim Cinek7d447722014-06-10 15:51:59 +0200780 }
781 return child == null || child.getWidth() != 0;
782 }
783
Jorim Jaggi9f347ae2014-04-11 00:47:18 +0200784 private View findFirstVisibleChild(ViewGroup container) {
785 int childCount = container.getChildCount();
786 for (int i = 0; i < childCount; i++) {
787 View child = container.getChildAt(i);
788 if (child.getVisibility() != View.GONE) {
789 return child;
790 }
791 }
792 return null;
793 }
794
Selim Cinek1685e632014-04-08 02:27:49 +0200795 public void onExpansionStopped() {
796 mIsExpansionChanging = false;
797 mFirstChildWhileExpanding = null;
798 }
799
800 public void setIsExpanded(boolean isExpanded) {
801 this.mIsExpanded = isExpanded;
802 }
803
804 public void notifyChildrenChanged(ViewGroup hostView) {
805 if (mIsExpansionChanging) {
806 updateFirstChildHeightWhileExpanding(hostView);
807 }
808 }
809
Selim Cinek34c0a8d2014-05-12 00:01:43 +0200810 public void setDimmed(boolean dimmed) {
811 updatePadding(dimmed);
812 }
813
Selim Cinek31094df2014-08-14 19:28:15 +0200814 public void onReset(ExpandableView view) {
815 if (view.equals(mFirstChildWhileExpanding)) {
816 updateFirstChildMaxSizeToMaxHeight();
817 }
818 }
819
Selim Cinek67b22602014-03-10 15:40:16 +0100820 class StackScrollAlgorithmState {
821
822 /**
823 * The scroll position of the algorithm
824 */
825 public int scrollY;
826
827 /**
828 * The quantity of items which are in the top stack.
829 */
830 public float itemsInTopStack;
831
832 /**
833 * how far in is the element currently transitioning into the top stack
834 */
835 public float partialInTop;
836
837 /**
Selim Cinek343e6e22014-04-11 21:23:30 +0200838 * The number of pixels the last child in the top stack has scrolled in to the stack
839 */
840 public float scrolledPixelsTop;
841
842 /**
Selim Cinek67b22602014-03-10 15:40:16 +0100843 * The last item index which is in the top stack.
Selim Cinek67b22602014-03-10 15:40:16 +0100844 */
845 public int lastTopStackIndex;
846
847 /**
848 * The quantity of items which are in the bottom stack.
849 */
850 public float itemsInBottomStack;
851
852 /**
853 * how far in is the element currently transitioning into the bottom stack
854 */
855 public float partialInBottom;
Jorim Jaggid4a57442014-04-10 02:45:55 +0200856
857 /**
858 * The children from the host view which are not gone.
859 */
Jorim Jaggibe565df2014-04-28 17:51:23 +0200860 public final ArrayList<ExpandableView> visibleChildren = new ArrayList<ExpandableView>();
Selim Cinek67b22602014-03-10 15:40:16 +0100861 }
862
863}