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