StackScrollAlgorithm.java revision 53ac1ef993e4bdce0d155fa00a133540f29af0ed
1/*
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;
20import android.util.Log;
21import android.view.View;
22import android.view.ViewGroup;
23
24import com.android.systemui.R;
25import com.android.systemui.statusbar.ExpandableNotificationRow;
26import com.android.systemui.statusbar.ExpandableView;
27import com.android.systemui.statusbar.notification.FakeShadowView;
28import com.android.systemui.statusbar.notification.NotificationUtils;
29
30import java.util.ArrayList;
31import java.util.HashMap;
32import java.util.List;
33
34/**
35 * The Algorithm of the {@link com.android.systemui.statusbar.stack
36 * .NotificationStackScrollLayout} which can be queried for {@link com.android.systemui.statusbar
37 * .stack.StackScrollState}
38 */
39public class StackScrollAlgorithm {
40
41    private static final String LOG_TAG = "StackScrollAlgorithm";
42
43    private static final int MAX_ITEMS_IN_BOTTOM_STACK = 3;
44
45    private int mPaddingBetweenElements;
46    private int mIncreasedPaddingBetweenElements;
47    private int mCollapsedSize;
48    private int mBottomStackPeekSize;
49    private int mZDistanceBetweenElements;
50    private int mZBasicHeight;
51
52    private StackIndentationFunctor mBottomStackIndentationFunctor;
53
54    private StackScrollAlgorithmState mTempAlgorithmState = new StackScrollAlgorithmState();
55    private boolean mIsExpanded;
56    private int mBottomStackSlowDownLength;
57
58    public StackScrollAlgorithm(Context context) {
59        initView(context);
60    }
61
62    public void initView(Context context) {
63        initConstants(context);
64    }
65
66    public int getBottomStackSlowDownLength() {
67        return mBottomStackSlowDownLength + mPaddingBetweenElements;
68    }
69
70    private void initConstants(Context context) {
71        mPaddingBetweenElements = Math.max(1, context.getResources()
72                .getDimensionPixelSize(R.dimen.notification_divider_height));
73        mIncreasedPaddingBetweenElements = context.getResources()
74                .getDimensionPixelSize(R.dimen.notification_divider_height_increased);
75        mCollapsedSize = context.getResources()
76                .getDimensionPixelSize(R.dimen.notification_min_height);
77        mBottomStackPeekSize = context.getResources()
78                .getDimensionPixelSize(R.dimen.bottom_stack_peek_amount);
79        mZDistanceBetweenElements = Math.max(1, context.getResources()
80                .getDimensionPixelSize(R.dimen.z_distance_between_notifications));
81        mZBasicHeight = (MAX_ITEMS_IN_BOTTOM_STACK + 1) * mZDistanceBetweenElements;
82        mBottomStackSlowDownLength = context.getResources()
83                .getDimensionPixelSize(R.dimen.bottom_stack_slow_down_length);
84        mBottomStackIndentationFunctor = new PiecewiseLinearIndentationFunctor(
85                MAX_ITEMS_IN_BOTTOM_STACK,
86                mBottomStackPeekSize,
87                getBottomStackSlowDownLength(),
88                0.5f);
89    }
90
91    public void getStackScrollState(AmbientState ambientState, StackScrollState resultState) {
92        // The state of the local variables are saved in an algorithmState to easily subdivide it
93        // into multiple phases.
94        StackScrollAlgorithmState algorithmState = mTempAlgorithmState;
95
96        // First we reset the view states to their default values.
97        resultState.resetViewStates();
98
99        initAlgorithmState(resultState, algorithmState, ambientState);
100
101        updatePositionsForState(resultState, algorithmState, ambientState);
102
103        updateZValuesForState(resultState, algorithmState, ambientState);
104
105        updateHeadsUpStates(resultState, algorithmState, ambientState);
106
107        handleDraggedViews(ambientState, resultState, algorithmState);
108        updateDimmedActivatedHideSensitive(ambientState, resultState, algorithmState);
109        updateClipping(resultState, algorithmState, ambientState);
110        updateSpeedBumpState(resultState, algorithmState, ambientState.getSpeedBumpIndex());
111        getNotificationChildrenStates(resultState, algorithmState);
112    }
113
114    private void getNotificationChildrenStates(StackScrollState resultState,
115            StackScrollAlgorithmState algorithmState) {
116        int childCount = algorithmState.visibleChildren.size();
117        for (int i = 0; i < childCount; i++) {
118            ExpandableView v = algorithmState.visibleChildren.get(i);
119            if (v instanceof ExpandableNotificationRow) {
120                ExpandableNotificationRow row = (ExpandableNotificationRow) v;
121                row.getChildrenStates(resultState);
122            }
123        }
124    }
125
126    private void updateSpeedBumpState(StackScrollState resultState,
127            StackScrollAlgorithmState algorithmState, int speedBumpIndex) {
128        int childCount = algorithmState.visibleChildren.size();
129        for (int i = 0; i < childCount; i++) {
130            View child = algorithmState.visibleChildren.get(i);
131            StackViewState childViewState = resultState.getViewStateForView(child);
132
133            // The speed bump can also be gone, so equality needs to be taken when comparing
134            // indices.
135            childViewState.belowSpeedBump = speedBumpIndex != -1 && i >= speedBumpIndex;
136        }
137    }
138
139    private void updateClipping(StackScrollState resultState,
140            StackScrollAlgorithmState algorithmState, AmbientState ambientState) {
141        float drawStart = ambientState.getTopPadding() + ambientState.getStackTranslation();
142        float previousNotificationEnd = 0;
143        float previousNotificationStart = 0;
144        int childCount = algorithmState.visibleChildren.size();
145        for (int i = 0; i < childCount; i++) {
146            ExpandableView child = algorithmState.visibleChildren.get(i);
147            StackViewState state = resultState.getViewStateForView(child);
148            if (!child.mustStayOnScreen()) {
149                previousNotificationEnd = Math.max(drawStart, previousNotificationEnd);
150                previousNotificationStart = Math.max(drawStart, previousNotificationStart);
151            }
152            float newYTranslation = state.yTranslation;
153            float newHeight = state.height;
154            float newNotificationEnd = newYTranslation + newHeight;
155            boolean isHeadsUp = (child instanceof ExpandableNotificationRow)
156                    && ((ExpandableNotificationRow) child).isPinned();
157            if (newYTranslation < previousNotificationEnd && ambientState.isShadeExpanded()
158                    && !isHeadsUp) {
159                // The previous view is overlapping on top, clip!
160                float overlapAmount = previousNotificationEnd - newYTranslation;
161                state.clipTopAmount = (int) overlapAmount;
162            } else {
163                state.clipTopAmount = 0;
164            }
165
166            if (!child.isTransparent()) {
167                // Only update the previous values if we are not transparent,
168                // otherwise we would clip to a transparent view.
169                previousNotificationEnd = newNotificationEnd;
170                previousNotificationStart = newYTranslation;
171            }
172        }
173    }
174
175    public static boolean canChildBeDismissed(View v) {
176        final View veto = v.findViewById(R.id.veto);
177        return (veto != null && veto.getVisibility() != View.GONE);
178    }
179
180    /**
181     * Updates the dimmed, activated and hiding sensitive states of the children.
182     */
183    private void updateDimmedActivatedHideSensitive(AmbientState ambientState,
184            StackScrollState resultState, StackScrollAlgorithmState algorithmState) {
185        boolean dimmed = ambientState.isDimmed();
186        boolean dark = ambientState.isDark();
187        boolean hideSensitive = ambientState.isHideSensitive();
188        View activatedChild = ambientState.getActivatedChild();
189        int childCount = algorithmState.visibleChildren.size();
190        for (int i = 0; i < childCount; i++) {
191            View child = algorithmState.visibleChildren.get(i);
192            StackViewState childViewState = resultState.getViewStateForView(child);
193            childViewState.dimmed = dimmed;
194            childViewState.dark = dark;
195            childViewState.hideSensitive = hideSensitive;
196            boolean isActivatedChild = activatedChild == child;
197            if (dimmed && isActivatedChild) {
198                childViewState.zTranslation += 2.0f * mZDistanceBetweenElements;
199            }
200        }
201    }
202
203    /**
204     * Handle the special state when views are being dragged
205     */
206    private void handleDraggedViews(AmbientState ambientState, StackScrollState resultState,
207            StackScrollAlgorithmState algorithmState) {
208        ArrayList<View> draggedViews = ambientState.getDraggedViews();
209        for (View draggedView : draggedViews) {
210            int childIndex = algorithmState.visibleChildren.indexOf(draggedView);
211            if (childIndex >= 0 && childIndex < algorithmState.visibleChildren.size() - 1) {
212                View nextChild = algorithmState.visibleChildren.get(childIndex + 1);
213                if (!draggedViews.contains(nextChild)) {
214                    // only if the view is not dragged itself we modify its state to be fully
215                    // visible
216                    StackViewState viewState = resultState.getViewStateForView(
217                            nextChild);
218                    // The child below the dragged one must be fully visible
219                    if (ambientState.isShadeExpanded()) {
220                        viewState.shadowAlpha = 1;
221                        viewState.hidden = false;
222                    }
223                }
224
225                // Lets set the alpha to the one it currently has, as its currently being dragged
226                StackViewState viewState = resultState.getViewStateForView(draggedView);
227                // The dragged child should keep the set alpha
228                viewState.alpha = draggedView.getAlpha();
229            }
230        }
231    }
232
233    /**
234     * Initialize the algorithm state like updating the visible children.
235     */
236    private void initAlgorithmState(StackScrollState resultState, StackScrollAlgorithmState state,
237            AmbientState ambientState) {
238        state.itemsInBottomStack = 0.0f;
239        state.partialInBottom = 0.0f;
240        float bottomOverScroll = ambientState.getOverScrollAmount(false /* onTop */);
241
242        int scrollY = ambientState.getScrollY();
243
244        // Due to the overScroller, the stackscroller can have negative scroll state. This is
245        // already accounted for by the top padding and doesn't need an additional adaption
246        scrollY = Math.max(0, scrollY);
247        state.scrollY = (int) (scrollY + bottomOverScroll);
248
249        //now init the visible children and update paddings
250        ViewGroup hostView = resultState.getHostView();
251        int childCount = hostView.getChildCount();
252        state.visibleChildren.clear();
253        state.visibleChildren.ensureCapacity(childCount);
254        state.increasedPaddingMap.clear();
255        int notGoneIndex = 0;
256        ExpandableView lastView = null;
257        for (int i = 0; i < childCount; i++) {
258            ExpandableView v = (ExpandableView) hostView.getChildAt(i);
259            if (v.getVisibility() != View.GONE) {
260                notGoneIndex = updateNotGoneIndex(resultState, state, notGoneIndex, v);
261                float increasedPadding = v.getIncreasedPaddingAmount();
262                if (increasedPadding != 0.0f) {
263                    state.increasedPaddingMap.put(v, increasedPadding);
264                    if (lastView != null) {
265                        Float prevValue = state.increasedPaddingMap.get(lastView);
266                        float newValue = prevValue != null
267                                ? Math.max(prevValue, increasedPadding)
268                                : increasedPadding;
269                        state.increasedPaddingMap.put(lastView, newValue);
270                    }
271                }
272                if (v instanceof ExpandableNotificationRow) {
273                    ExpandableNotificationRow row = (ExpandableNotificationRow) v;
274
275                    // handle the notgoneIndex for the children as well
276                    List<ExpandableNotificationRow> children =
277                            row.getNotificationChildren();
278                    if (row.isSummaryWithChildren() && children != null) {
279                        for (ExpandableNotificationRow childRow : children) {
280                            if (childRow.getVisibility() != View.GONE) {
281                                StackViewState childState
282                                        = resultState.getViewStateForView(childRow);
283                                childState.notGoneIndex = notGoneIndex;
284                                notGoneIndex++;
285                            }
286                        }
287                    }
288                }
289                lastView = v;
290            }
291        }
292    }
293
294    private int updateNotGoneIndex(StackScrollState resultState,
295            StackScrollAlgorithmState state, int notGoneIndex,
296            ExpandableView v) {
297        StackViewState viewState = resultState.getViewStateForView(v);
298        viewState.notGoneIndex = notGoneIndex;
299        state.visibleChildren.add(v);
300        notGoneIndex++;
301        return notGoneIndex;
302    }
303
304    /**
305     * Determine the positions for the views. This is the main part of the algorithm.
306     *
307     * @param resultState The result state to update if a change to the properties of a child occurs
308     * @param algorithmState The state in which the current pass of the algorithm is currently in
309     * @param ambientState The current ambient state
310     */
311    private void updatePositionsForState(StackScrollState resultState,
312            StackScrollAlgorithmState algorithmState, AmbientState ambientState) {
313
314        // The starting position of the bottom stack peek
315        float bottomPeekStart = ambientState.getInnerHeight() - mBottomStackPeekSize;
316
317        // The position where the bottom stack starts.
318        float bottomStackStart = bottomPeekStart - mBottomStackSlowDownLength;
319
320        // The y coordinate of the current child.
321        float currentYPosition = -algorithmState.scrollY;
322
323        int childCount = algorithmState.visibleChildren.size();
324        int paddingAfterChild;
325        for (int i = 0; i < childCount; i++) {
326            ExpandableView child = algorithmState.visibleChildren.get(i);
327            StackViewState childViewState = resultState.getViewStateForView(child);
328            childViewState.location = StackViewState.LOCATION_UNKNOWN;
329            paddingAfterChild = getPaddingAfterChild(algorithmState, child);
330            int childHeight = getMaxAllowedChildHeight(child);
331            int collapsedHeight = child.getCollapsedHeight();
332            childViewState.yTranslation = currentYPosition;
333            if (i == 0) {
334                updateFirstChildHeight(child, childViewState, childHeight, ambientState);
335            }
336
337            // The y position after this element
338            float nextYPosition = currentYPosition + childHeight +
339                    paddingAfterChild;
340            if (nextYPosition >= bottomStackStart) {
341                // Case 1:
342                // We are in the bottom stack.
343                if (currentYPosition >= bottomStackStart) {
344                    // According to the regular scroll view we are fully translated out of the
345                    // bottom of the screen so we are fully in the bottom stack
346                    updateStateForChildFullyInBottomStack(algorithmState,
347                            bottomStackStart, childViewState, collapsedHeight, ambientState, child);
348                } else {
349                    // According to the regular scroll view we are currently translating out of /
350                    // into the bottom of the screen
351                    updateStateForChildTransitioningInBottom(algorithmState,
352                            bottomStackStart, child, currentYPosition,
353                            childViewState, childHeight);
354                }
355            } else {
356                // Case 2:
357                // We are in the regular scroll area.
358                childViewState.location = StackViewState.LOCATION_MAIN_AREA;
359                clampPositionToBottomStackStart(childViewState, childViewState.height, childHeight,
360                        ambientState);
361            }
362
363            if (i == 0 && ambientState.getScrollY() <= 0) {
364                // The first card can get into the bottom stack if it's the only one
365                // on the lockscreen which pushes it up. Let's make sure that doesn't happen and
366                // it stays at the top
367                childViewState.yTranslation = Math.max(0, childViewState.yTranslation);
368            }
369            currentYPosition = childViewState.yTranslation + childHeight + paddingAfterChild;
370            if (currentYPosition <= 0) {
371                childViewState.location = StackViewState.LOCATION_HIDDEN_TOP;
372            }
373            if (childViewState.location == StackViewState.LOCATION_UNKNOWN) {
374                Log.wtf(LOG_TAG, "Failed to assign location for child " + i);
375            }
376
377            childViewState.yTranslation += ambientState.getTopPadding()
378                    + ambientState.getStackTranslation();
379        }
380    }
381
382    private int getPaddingAfterChild(StackScrollAlgorithmState algorithmState,
383            ExpandableView child) {
384        Float paddingValue = algorithmState.increasedPaddingMap.get(child);
385        return paddingValue == null
386                ? mPaddingBetweenElements
387                : (int) NotificationUtils.interpolate(mPaddingBetweenElements,
388                        mIncreasedPaddingBetweenElements,
389                        paddingValue);
390    }
391
392    private void updateHeadsUpStates(StackScrollState resultState,
393            StackScrollAlgorithmState algorithmState, AmbientState ambientState) {
394        int childCount = algorithmState.visibleChildren.size();
395        ExpandableNotificationRow topHeadsUpEntry = null;
396        for (int i = 0; i < childCount; i++) {
397            View child = algorithmState.visibleChildren.get(i);
398            if (!(child instanceof ExpandableNotificationRow)) {
399                break;
400            }
401            ExpandableNotificationRow row = (ExpandableNotificationRow) child;
402            if (!row.isHeadsUp()) {
403                break;
404            }
405            StackViewState childState = resultState.getViewStateForView(row);
406            if (topHeadsUpEntry == null) {
407                topHeadsUpEntry = row;
408                childState.location = StackViewState.LOCATION_FIRST_HUN;
409            }
410            boolean isTopEntry = topHeadsUpEntry == row;
411            float unmodifiedEndLocation = childState.yTranslation + childState.height;
412            if (mIsExpanded) {
413                // Ensure that the heads up is always visible even when scrolled off
414                clampHunToTop(ambientState, row, childState);
415                clampHunToMaxTranslation(ambientState, row, childState);
416            }
417            if (row.isPinned()) {
418                childState.yTranslation = Math.max(childState.yTranslation, 0);
419                childState.height = Math.max(row.getIntrinsicHeight(), childState.height);
420                StackViewState topState = resultState.getViewStateForView(topHeadsUpEntry);
421                if (!isTopEntry && (!mIsExpanded
422                        || unmodifiedEndLocation < topState.yTranslation + topState.height)) {
423                    // Ensure that a headsUp doesn't vertically extend further than the heads-up at
424                    // the top most z-position
425                    childState.height = row.getIntrinsicHeight();
426                    childState.yTranslation = topState.yTranslation + topState.height
427                            - childState.height;
428                }
429            }
430        }
431    }
432
433    private void clampHunToTop(AmbientState ambientState, ExpandableNotificationRow row,
434            StackViewState childState) {
435        float newTranslation = Math.max(ambientState.getTopPadding()
436                + ambientState.getStackTranslation(), childState.yTranslation);
437        childState.height = (int) Math.max(childState.height - (newTranslation
438                - childState.yTranslation), row.getCollapsedHeight());
439        childState.yTranslation = newTranslation;
440    }
441
442    private void clampHunToMaxTranslation(AmbientState ambientState, ExpandableNotificationRow row,
443            StackViewState childState) {
444        float newTranslation;
445        float bottomPosition = ambientState.getMaxHeadsUpTranslation() - row.getCollapsedHeight();
446        newTranslation = Math.min(childState.yTranslation, bottomPosition);
447        childState.height = (int) Math.max(childState.height
448                - (childState.yTranslation - newTranslation), row.getCollapsedHeight());
449        childState.yTranslation = newTranslation;
450    }
451
452    /**
453     * Clamp the yTranslation of the child down such that its end is at most on the beginning of
454     * the bottom stack.
455     *
456     * @param childViewState the view state of the child
457     * @param childHeight the height of this child
458     * @param minHeight the minumum Height of the View
459     */
460    private void clampPositionToBottomStackStart(StackViewState childViewState,
461            int childHeight, int minHeight, AmbientState ambientState) {
462
463        int bottomStackStart = ambientState.getInnerHeight()
464                - mBottomStackPeekSize - mBottomStackSlowDownLength;
465        int childStart = bottomStackStart - childHeight;
466        if (childStart < childViewState.yTranslation) {
467            float newHeight = bottomStackStart - childViewState.yTranslation;
468            if (newHeight < minHeight) {
469                newHeight = minHeight;
470                childViewState.yTranslation = bottomStackStart - minHeight;
471            }
472            childViewState.height = (int) newHeight;
473        }
474    }
475
476    private int getMaxAllowedChildHeight(View child) {
477        if (child instanceof ExpandableView) {
478            ExpandableView expandableView = (ExpandableView) child;
479            return expandableView.getIntrinsicHeight();
480        }
481        return child == null? mCollapsedSize : child.getHeight();
482    }
483
484    private void updateStateForChildTransitioningInBottom(StackScrollAlgorithmState algorithmState,
485            float transitioningPositionStart, ExpandableView child, float currentYPosition,
486            StackViewState childViewState, int childHeight) {
487
488        // This is the transitioning element on top of bottom stack, calculate how far we are in.
489        algorithmState.partialInBottom = 1.0f - (
490                (transitioningPositionStart - currentYPosition) / (childHeight +
491                        getPaddingAfterChild(algorithmState, child)));
492
493        // the offset starting at the transitionPosition of the bottom stack
494        float offset = mBottomStackIndentationFunctor.getValue(algorithmState.partialInBottom);
495        algorithmState.itemsInBottomStack += algorithmState.partialInBottom;
496        int newHeight = childHeight;
497        if (childHeight > child.getCollapsedHeight()) {
498            newHeight = (int) Math.max(Math.min(transitioningPositionStart + offset -
499                    getPaddingAfterChild(algorithmState, child) - currentYPosition, childHeight),
500                    child.getCollapsedHeight());
501            childViewState.height = newHeight;
502        }
503        childViewState.yTranslation = transitioningPositionStart + offset - newHeight
504                - getPaddingAfterChild(algorithmState, child);
505        childViewState.location = StackViewState.LOCATION_MAIN_AREA;
506    }
507
508    private void updateStateForChildFullyInBottomStack(StackScrollAlgorithmState algorithmState,
509            float transitioningPositionStart, StackViewState childViewState,
510            int collapsedHeight, AmbientState ambientState, ExpandableView child) {
511        float currentYPosition;
512        algorithmState.itemsInBottomStack += 1.0f;
513        if (algorithmState.itemsInBottomStack < MAX_ITEMS_IN_BOTTOM_STACK) {
514            // We are visually entering the bottom stack
515            currentYPosition = transitioningPositionStart
516                    + mBottomStackIndentationFunctor.getValue(algorithmState.itemsInBottomStack)
517                    - getPaddingAfterChild(algorithmState, child);
518            childViewState.location = StackViewState.LOCATION_BOTTOM_STACK_PEEKING;
519        } else {
520            // we are fully inside the stack
521            if (algorithmState.itemsInBottomStack > MAX_ITEMS_IN_BOTTOM_STACK + 2) {
522                childViewState.hidden = true;
523                childViewState.shadowAlpha = 0.0f;
524            } else if (algorithmState.itemsInBottomStack
525                    > MAX_ITEMS_IN_BOTTOM_STACK + 1) {
526                childViewState.shadowAlpha = 1.0f - algorithmState.partialInBottom;
527            }
528            childViewState.location = StackViewState.LOCATION_BOTTOM_STACK_HIDDEN;
529            currentYPosition = ambientState.getInnerHeight();
530        }
531        childViewState.height = collapsedHeight;
532        childViewState.yTranslation = currentYPosition - collapsedHeight;
533    }
534
535
536    /**
537     * Update the height of the first child i.e clamp it to the bottom stack
538     *
539     * @param child the child to update
540     * @param childViewState the viewstate of the child
541     * @param childHeight the height of the child
542     * @param ambientState The ambient state of the algorithm
543     */
544    private void updateFirstChildHeight(ExpandableView child, StackViewState childViewState,
545            int childHeight, AmbientState ambientState) {
546
547            // The starting position of the bottom stack peek
548            int bottomPeekStart = ambientState.getInnerHeight() - mBottomStackPeekSize -
549                    mBottomStackSlowDownLength + ambientState.getScrollY();
550            // Collapse and expand the first child while the shade is being expanded
551        childViewState.height = (int) Math.max(Math.min(bottomPeekStart, (float) childHeight),
552                    child.getCollapsedHeight());
553    }
554
555    /**
556     * Calculate the Z positions for all children based on the number of items in both stacks and
557     * save it in the resultState
558     *  @param resultState The result state to update the zTranslation values
559     * @param algorithmState The state in which the current pass of the algorithm is currently in
560     * @param ambientState The ambient state of the algorithm
561     */
562    private void updateZValuesForState(StackScrollState resultState,
563            StackScrollAlgorithmState algorithmState, AmbientState ambientState) {
564        int childCount = algorithmState.visibleChildren.size();
565        float childrenOnTop = 0.0f;
566        for (int i = childCount - 1; i >= 0; i--) {
567            ExpandableView child = algorithmState.visibleChildren.get(i);
568            StackViewState childViewState = resultState.getViewStateForView(child);
569            if (i > (childCount - 1 - algorithmState.itemsInBottomStack)) {
570                // We are in the bottom stack
571                float numItemsAbove = i - (childCount - 1 - algorithmState.itemsInBottomStack);
572                float zSubtraction;
573                if (numItemsAbove <= 1.0f) {
574                    float factor = 0.2f;
575                    // Lets fade in slower to the threshold to make the shadow fade in look nicer
576                    if (numItemsAbove <= factor) {
577                        zSubtraction = FakeShadowView.SHADOW_SIBLING_TRESHOLD
578                                * numItemsAbove * (1.0f / factor);
579                    } else {
580                        zSubtraction = FakeShadowView.SHADOW_SIBLING_TRESHOLD
581                                + (numItemsAbove - factor) * (1.0f / (1.0f - factor))
582                                        * (mZDistanceBetweenElements
583                                                - FakeShadowView.SHADOW_SIBLING_TRESHOLD);
584                    }
585                } else {
586                    zSubtraction = numItemsAbove * mZDistanceBetweenElements;
587                }
588                childViewState.zTranslation = mZBasicHeight - zSubtraction;
589            } else if (child.mustStayOnScreen()
590                    && childViewState.yTranslation < ambientState.getTopPadding()
591                    + ambientState.getStackTranslation()) {
592                if (childrenOnTop != 0.0f) {
593                    childrenOnTop++;
594                } else {
595                    float overlap = ambientState.getTopPadding()
596                            + ambientState.getStackTranslation() - childViewState.yTranslation;
597                    childrenOnTop += Math.min(1.0f, overlap / childViewState.height);
598                }
599                childViewState.zTranslation = mZBasicHeight
600                        + childrenOnTop * mZDistanceBetweenElements;
601            } else {
602                childViewState.zTranslation = mZBasicHeight;
603            }
604        }
605    }
606
607    private boolean isMaxSizeInitialized(ExpandableView child) {
608        if (child instanceof ExpandableNotificationRow) {
609            ExpandableNotificationRow row = (ExpandableNotificationRow) child;
610            return row.isMaxExpandHeightInitialized();
611        }
612        return child == null || child.getWidth() != 0;
613    }
614
615    private View findFirstVisibleChild(ViewGroup container) {
616        int childCount = container.getChildCount();
617        for (int i = 0; i < childCount; i++) {
618            View child = container.getChildAt(i);
619            if (child.getVisibility() != View.GONE) {
620                return child;
621            }
622        }
623        return null;
624    }
625
626    public void setIsExpanded(boolean isExpanded) {
627        this.mIsExpanded = isExpanded;
628    }
629
630    class StackScrollAlgorithmState {
631
632        /**
633         * The scroll position of the algorithm
634         */
635        public int scrollY;
636
637        /**
638         * The quantity of items which are in the bottom stack.
639         */
640        public float itemsInBottomStack;
641
642        /**
643         * how far in is the element currently transitioning into the bottom stack
644         */
645        public float partialInBottom;
646
647        /**
648         * The children from the host view which are not gone.
649         */
650        public final ArrayList<ExpandableView> visibleChildren = new ArrayList<ExpandableView>();
651
652        /**
653         * The children from the host that need an increased padding after them. A value of 0 means
654         * no increased padding, a value of 1 means full padding.
655         */
656        public final HashMap<ExpandableView, Float> increasedPaddingMap = new HashMap<>();
657    }
658
659}
660