StackScrollAlgorithm.java revision 9b1d30ee900ae49ad717b1ef7eb685b96393402d
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
156            if (newYTranslation < previousNotificationEnd) {
157                // The previous view is overlapping on top, clip!
158                float overlapAmount = previousNotificationEnd - newYTranslation;
159                state.clipTopAmount = (int) overlapAmount;
160            } else {
161                state.clipTopAmount = 0;
162            }
163
164            if (!child.isTransparent()) {
165                // Only update the previous values if we are not transparent,
166                // otherwise we would clip to a transparent view.
167                previousNotificationEnd = newNotificationEnd;
168                previousNotificationStart = newYTranslation;
169            }
170        }
171    }
172
173    public static boolean canChildBeDismissed(View v) {
174        if (v instanceof ExpandableNotificationRow) {
175            ExpandableNotificationRow row = (ExpandableNotificationRow) v;
176            if (row.areGutsExposed()) {
177                return false;
178            }
179        }
180        final View veto = v.findViewById(R.id.veto);
181        return (veto != null && veto.getVisibility() != View.GONE);
182    }
183
184    /**
185     * Updates the dimmed, activated and hiding sensitive states of the children.
186     */
187    private void updateDimmedActivatedHideSensitive(AmbientState ambientState,
188            StackScrollState resultState, StackScrollAlgorithmState algorithmState) {
189        boolean dimmed = ambientState.isDimmed();
190        boolean dark = ambientState.isDark();
191        boolean hideSensitive = ambientState.isHideSensitive();
192        View activatedChild = ambientState.getActivatedChild();
193        int childCount = algorithmState.visibleChildren.size();
194        for (int i = 0; i < childCount; i++) {
195            View child = algorithmState.visibleChildren.get(i);
196            StackViewState childViewState = resultState.getViewStateForView(child);
197            childViewState.dimmed = dimmed;
198            childViewState.dark = dark;
199            childViewState.hideSensitive = hideSensitive;
200            boolean isActivatedChild = activatedChild == child;
201            if (dimmed && isActivatedChild) {
202                childViewState.zTranslation += 2.0f * mZDistanceBetweenElements;
203            }
204        }
205    }
206
207    /**
208     * Handle the special state when views are being dragged
209     */
210    private void handleDraggedViews(AmbientState ambientState, StackScrollState resultState,
211            StackScrollAlgorithmState algorithmState) {
212        ArrayList<View> draggedViews = ambientState.getDraggedViews();
213        for (View draggedView : draggedViews) {
214            int childIndex = algorithmState.visibleChildren.indexOf(draggedView);
215            if (childIndex >= 0 && childIndex < algorithmState.visibleChildren.size() - 1) {
216                View nextChild = algorithmState.visibleChildren.get(childIndex + 1);
217                if (!draggedViews.contains(nextChild)) {
218                    // only if the view is not dragged itself we modify its state to be fully
219                    // visible
220                    StackViewState viewState = resultState.getViewStateForView(
221                            nextChild);
222                    // The child below the dragged one must be fully visible
223                    if (ambientState.isShadeExpanded()) {
224                        viewState.shadowAlpha = 1;
225                        viewState.hidden = false;
226                    }
227                }
228
229                // Lets set the alpha to the one it currently has, as its currently being dragged
230                StackViewState viewState = resultState.getViewStateForView(draggedView);
231                // The dragged child should keep the set alpha
232                viewState.alpha = draggedView.getAlpha();
233            }
234        }
235    }
236
237    /**
238     * Initialize the algorithm state like updating the visible children.
239     */
240    private void initAlgorithmState(StackScrollState resultState, StackScrollAlgorithmState state,
241            AmbientState ambientState) {
242        state.itemsInBottomStack = 0.0f;
243        state.partialInBottom = 0.0f;
244        float bottomOverScroll = ambientState.getOverScrollAmount(false /* onTop */);
245
246        int scrollY = ambientState.getScrollY();
247
248        // Due to the overScroller, the stackscroller can have negative scroll state. This is
249        // already accounted for by the top padding and doesn't need an additional adaption
250        scrollY = Math.max(0, scrollY);
251        state.scrollY = (int) (scrollY + bottomOverScroll);
252
253        //now init the visible children and update paddings
254        ViewGroup hostView = resultState.getHostView();
255        int childCount = hostView.getChildCount();
256        state.visibleChildren.clear();
257        state.visibleChildren.ensureCapacity(childCount);
258        state.increasedPaddingMap.clear();
259        int notGoneIndex = 0;
260        ExpandableView lastView = null;
261        for (int i = 0; i < childCount; i++) {
262            ExpandableView v = (ExpandableView) hostView.getChildAt(i);
263            if (v.getVisibility() != View.GONE) {
264                notGoneIndex = updateNotGoneIndex(resultState, state, notGoneIndex, v);
265                float increasedPadding = v.getIncreasedPaddingAmount();
266                if (increasedPadding != 0.0f) {
267                    state.increasedPaddingMap.put(v, increasedPadding);
268                    if (lastView != null) {
269                        Float prevValue = state.increasedPaddingMap.get(lastView);
270                        float newValue = prevValue != null
271                                ? Math.max(prevValue, increasedPadding)
272                                : increasedPadding;
273                        state.increasedPaddingMap.put(lastView, newValue);
274                    }
275                }
276                if (v instanceof ExpandableNotificationRow) {
277                    ExpandableNotificationRow row = (ExpandableNotificationRow) v;
278
279                    // handle the notgoneIndex for the children as well
280                    List<ExpandableNotificationRow> children =
281                            row.getNotificationChildren();
282                    if (row.isSummaryWithChildren() && children != null) {
283                        for (ExpandableNotificationRow childRow : children) {
284                            if (childRow.getVisibility() != View.GONE) {
285                                StackViewState childState
286                                        = resultState.getViewStateForView(childRow);
287                                childState.notGoneIndex = notGoneIndex;
288                                notGoneIndex++;
289                            }
290                        }
291                    }
292                }
293                lastView = v;
294            }
295        }
296    }
297
298    private int updateNotGoneIndex(StackScrollState resultState,
299            StackScrollAlgorithmState state, int notGoneIndex,
300            ExpandableView v) {
301        StackViewState viewState = resultState.getViewStateForView(v);
302        viewState.notGoneIndex = notGoneIndex;
303        state.visibleChildren.add(v);
304        notGoneIndex++;
305        return notGoneIndex;
306    }
307
308    /**
309     * Determine the positions for the views. This is the main part of the algorithm.
310     *
311     * @param resultState The result state to update if a change to the properties of a child occurs
312     * @param algorithmState The state in which the current pass of the algorithm is currently in
313     * @param ambientState The current ambient state
314     */
315    private void updatePositionsForState(StackScrollState resultState,
316            StackScrollAlgorithmState algorithmState, AmbientState ambientState) {
317
318        // The starting position of the bottom stack peek
319        float bottomPeekStart = ambientState.getInnerHeight() - mBottomStackPeekSize;
320
321        // The position where the bottom stack starts.
322        float bottomStackStart = bottomPeekStart - mBottomStackSlowDownLength;
323
324        // The y coordinate of the current child.
325        float currentYPosition = -algorithmState.scrollY;
326        int childCount = algorithmState.visibleChildren.size();
327        for (int i = 0; i < childCount; i++) {
328            currentYPosition = updateChild(i, resultState, algorithmState, ambientState,
329                    currentYPosition, bottomStackStart);
330        }
331    }
332
333    protected float updateChild(int i, StackScrollState resultState,
334            StackScrollAlgorithmState algorithmState, AmbientState ambientState,
335            float currentYPosition, float bottomStackStart) {
336        ExpandableView child = algorithmState.visibleChildren.get(i);
337        StackViewState childViewState = resultState.getViewStateForView(child);
338        childViewState.location = StackViewState.LOCATION_UNKNOWN;
339        int paddingAfterChild = getPaddingAfterChild(algorithmState, child);
340        int childHeight = getMaxAllowedChildHeight(child);
341        int collapsedHeight = child.getCollapsedHeight();
342        childViewState.yTranslation = currentYPosition;
343        if (i == 0) {
344            updateFirstChildHeight(child, childViewState, childHeight, ambientState);
345        }
346
347        // The y position after this element
348        float nextYPosition = currentYPosition + childHeight +
349                paddingAfterChild;
350        if (nextYPosition >= bottomStackStart) {
351            // Case 1:
352            // We are in the bottom stack.
353            if (currentYPosition >= bottomStackStart) {
354                // According to the regular scroll view we are fully translated out of the
355                // bottom of the screen so we are fully in the bottom stack
356                updateStateForChildFullyInBottomStack(algorithmState,
357                        bottomStackStart, childViewState, collapsedHeight, ambientState, child);
358            } else {
359                // According to the regular scroll view we are currently translating out of /
360                // into the bottom of the screen
361                updateStateForChildTransitioningInBottom(algorithmState,
362                        bottomStackStart, child, currentYPosition,
363                        childViewState, childHeight);
364            }
365        } else {
366            // Case 2:
367            // We are in the regular scroll area.
368            childViewState.location = StackViewState.LOCATION_MAIN_AREA;
369            clampPositionToBottomStackStart(childViewState, childViewState.height, childHeight,
370                    ambientState);
371        }
372
373        if (i == 0 && ambientState.getScrollY() <= 0) {
374            // The first card can get into the bottom stack if it's the only one
375            // on the lockscreen which pushes it up. Let's make sure that doesn't happen and
376            // it stays at the top
377            childViewState.yTranslation = Math.max(0, childViewState.yTranslation);
378        }
379        currentYPosition = childViewState.yTranslation + childHeight + paddingAfterChild;
380        if (currentYPosition <= 0) {
381            childViewState.location = StackViewState.LOCATION_HIDDEN_TOP;
382        }
383        if (childViewState.location == StackViewState.LOCATION_UNKNOWN) {
384            Log.wtf(LOG_TAG, "Failed to assign location for child " + i);
385        }
386
387        childViewState.yTranslation += ambientState.getTopPadding()
388                + ambientState.getStackTranslation();
389        return currentYPosition;
390    }
391
392    protected int getPaddingAfterChild(StackScrollAlgorithmState algorithmState,
393            ExpandableView child) {
394        Float paddingValue = algorithmState.increasedPaddingMap.get(child);
395        return paddingValue == null
396                ? mPaddingBetweenElements
397                : (int) NotificationUtils.interpolate(mPaddingBetweenElements,
398                        mIncreasedPaddingBetweenElements,
399                        paddingValue);
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            StackViewState childState = resultState.getViewStateForView(row);
416            if (topHeadsUpEntry == null) {
417                topHeadsUpEntry = row;
418                childState.location = StackViewState.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                StackViewState topState = resultState.getViewStateForView(topHeadsUpEntry);
431                if (!isTopEntry && (!mIsExpanded
432                        || unmodifiedEndLocation < topState.yTranslation + topState.height)) {
433                    // Ensure that a headsUp doesn't vertically extend further than the heads-up at
434                    // the top most z-position
435                    childState.height = row.getIntrinsicHeight();
436                    childState.yTranslation = topState.yTranslation + topState.height
437                            - childState.height;
438                }
439            }
440        }
441    }
442
443    private void clampHunToTop(AmbientState ambientState, ExpandableNotificationRow row,
444            StackViewState childState) {
445        float newTranslation = Math.max(ambientState.getTopPadding()
446                + ambientState.getStackTranslation(), childState.yTranslation);
447        childState.height = (int) Math.max(childState.height - (newTranslation
448                - childState.yTranslation), row.getCollapsedHeight());
449        childState.yTranslation = newTranslation;
450    }
451
452    private void clampHunToMaxTranslation(AmbientState ambientState, ExpandableNotificationRow row,
453            StackViewState childState) {
454        float newTranslation;
455        float bottomPosition = ambientState.getMaxHeadsUpTranslation() - row.getCollapsedHeight();
456        newTranslation = Math.min(childState.yTranslation, bottomPosition);
457        childState.height = (int) Math.max(childState.height
458                - (childState.yTranslation - newTranslation), row.getCollapsedHeight());
459        childState.yTranslation = newTranslation;
460    }
461
462    /**
463     * Clamp the yTranslation of the child down such that its end is at most on the beginning of
464     * the bottom stack.
465     *
466     * @param childViewState the view state of the child
467     * @param childHeight the height of this child
468     * @param minHeight the minumum Height of the View
469     */
470    private void clampPositionToBottomStackStart(StackViewState childViewState,
471            int childHeight, int minHeight, AmbientState ambientState) {
472
473        int bottomStackStart = ambientState.getInnerHeight()
474                - mBottomStackPeekSize - mBottomStackSlowDownLength;
475        int childStart = bottomStackStart - childHeight;
476        if (childStart < childViewState.yTranslation) {
477            float newHeight = bottomStackStart - childViewState.yTranslation;
478            if (newHeight < minHeight) {
479                newHeight = minHeight;
480                childViewState.yTranslation = bottomStackStart - minHeight;
481            }
482            childViewState.height = (int) newHeight;
483        }
484    }
485
486    protected int getMaxAllowedChildHeight(View child) {
487        if (child instanceof ExpandableView) {
488            ExpandableView expandableView = (ExpandableView) child;
489            return expandableView.getIntrinsicHeight();
490        }
491        return child == null? mCollapsedSize : child.getHeight();
492    }
493
494    private void updateStateForChildTransitioningInBottom(StackScrollAlgorithmState algorithmState,
495            float transitioningPositionStart, ExpandableView child, float currentYPosition,
496            StackViewState childViewState, int childHeight) {
497
498        // This is the transitioning element on top of bottom stack, calculate how far we are in.
499        algorithmState.partialInBottom = 1.0f - (
500                (transitioningPositionStart - currentYPosition) / (childHeight +
501                        getPaddingAfterChild(algorithmState, child)));
502
503        // the offset starting at the transitionPosition of the bottom stack
504        float offset = mBottomStackIndentationFunctor.getValue(algorithmState.partialInBottom);
505        algorithmState.itemsInBottomStack += algorithmState.partialInBottom;
506        int newHeight = childHeight;
507        if (childHeight > child.getCollapsedHeight()) {
508            newHeight = (int) Math.max(Math.min(transitioningPositionStart + offset -
509                    getPaddingAfterChild(algorithmState, child) - currentYPosition, childHeight),
510                    child.getCollapsedHeight());
511            childViewState.height = newHeight;
512        }
513        childViewState.yTranslation = transitioningPositionStart + offset - newHeight
514                - getPaddingAfterChild(algorithmState, child);
515        childViewState.location = StackViewState.LOCATION_MAIN_AREA;
516    }
517
518    private void updateStateForChildFullyInBottomStack(StackScrollAlgorithmState algorithmState,
519            float transitioningPositionStart, StackViewState childViewState,
520            int collapsedHeight, AmbientState ambientState, ExpandableView child) {
521        float currentYPosition;
522        algorithmState.itemsInBottomStack += 1.0f;
523        if (algorithmState.itemsInBottomStack < MAX_ITEMS_IN_BOTTOM_STACK) {
524            // We are visually entering the bottom stack
525            currentYPosition = transitioningPositionStart
526                    + mBottomStackIndentationFunctor.getValue(algorithmState.itemsInBottomStack)
527                    - getPaddingAfterChild(algorithmState, child);
528            childViewState.location = StackViewState.LOCATION_BOTTOM_STACK_PEEKING;
529        } else {
530            // we are fully inside the stack
531            if (algorithmState.itemsInBottomStack > MAX_ITEMS_IN_BOTTOM_STACK + 2) {
532                childViewState.hidden = true;
533                childViewState.shadowAlpha = 0.0f;
534            } else if (algorithmState.itemsInBottomStack
535                    > MAX_ITEMS_IN_BOTTOM_STACK + 1) {
536                childViewState.shadowAlpha = 1.0f - algorithmState.partialInBottom;
537            }
538            childViewState.location = StackViewState.LOCATION_BOTTOM_STACK_HIDDEN;
539            currentYPosition = ambientState.getInnerHeight();
540        }
541        childViewState.height = collapsedHeight;
542        childViewState.yTranslation = currentYPosition - collapsedHeight;
543    }
544
545
546    /**
547     * Update the height of the first child i.e clamp it to the bottom stack
548     *
549     * @param child the child to update
550     * @param childViewState the viewstate of the child
551     * @param childHeight the height of the child
552     * @param ambientState The ambient state of the algorithm
553     */
554    protected void updateFirstChildHeight(ExpandableView child, StackViewState childViewState,
555                                          int childHeight, AmbientState ambientState) {
556
557            // The starting position of the bottom stack peek
558            int bottomPeekStart = ambientState.getInnerHeight() - mBottomStackPeekSize -
559                    mBottomStackSlowDownLength + ambientState.getScrollY();
560            // Collapse and expand the first child while the shade is being expanded
561        childViewState.height = (int) Math.max(Math.min(bottomPeekStart, (float) childHeight),
562                    child.getCollapsedHeight());
563    }
564
565    /**
566     * Calculate the Z positions for all children based on the number of items in both stacks and
567     * save it in the resultState
568     *  @param resultState The result state to update the zTranslation values
569     * @param algorithmState The state in which the current pass of the algorithm is currently in
570     * @param ambientState The ambient state of the algorithm
571     */
572    private void updateZValuesForState(StackScrollState resultState,
573            StackScrollAlgorithmState algorithmState, AmbientState ambientState) {
574        int childCount = algorithmState.visibleChildren.size();
575        float childrenOnTop = 0.0f;
576        for (int i = childCount - 1; i >= 0; i--) {
577            updateChildZValue(i, childCount, childrenOnTop,
578                    resultState, algorithmState, ambientState);
579        }
580    }
581
582    protected void updateChildZValue(int i, int childCount, float childrenOnTop,
583            StackScrollState resultState, StackScrollAlgorithmState algorithmState,
584            AmbientState ambientState) {
585        ExpandableView child = algorithmState.visibleChildren.get(i);
586        StackViewState childViewState = resultState.getViewStateForView(child);
587        if (i > (childCount - 1 - algorithmState.itemsInBottomStack)) {
588            // We are in the bottom stack
589            float numItemsAbove = i - (childCount - 1 - algorithmState.itemsInBottomStack);
590            float zSubtraction;
591            if (numItemsAbove <= 1.0f) {
592                float factor = 0.2f;
593                // Lets fade in slower to the threshold to make the shadow fade in look nicer
594                if (numItemsAbove <= factor) {
595                    zSubtraction = FakeShadowView.SHADOW_SIBLING_TRESHOLD
596                            * numItemsAbove * (1.0f / factor);
597                } else {
598                    zSubtraction = FakeShadowView.SHADOW_SIBLING_TRESHOLD
599                            + (numItemsAbove - factor) * (1.0f / (1.0f - factor))
600                            * (mZDistanceBetweenElements
601                            - FakeShadowView.SHADOW_SIBLING_TRESHOLD);
602                }
603            } else {
604                zSubtraction = numItemsAbove * mZDistanceBetweenElements;
605            }
606            childViewState.zTranslation = mZBasicHeight - zSubtraction;
607        } else if (child.mustStayOnScreen()
608                && childViewState.yTranslation < ambientState.getTopPadding()
609                + ambientState.getStackTranslation()) {
610            if (childrenOnTop != 0.0f) {
611                childrenOnTop++;
612            } else {
613                float overlap = ambientState.getTopPadding()
614                        + ambientState.getStackTranslation() - childViewState.yTranslation;
615                childrenOnTop += Math.min(1.0f, overlap / childViewState.height);
616            }
617            childViewState.zTranslation = mZBasicHeight
618                    + childrenOnTop * mZDistanceBetweenElements;
619        } else {
620            childViewState.zTranslation = mZBasicHeight;
621        }
622    }
623
624    private boolean isMaxSizeInitialized(ExpandableView child) {
625        if (child instanceof ExpandableNotificationRow) {
626            ExpandableNotificationRow row = (ExpandableNotificationRow) child;
627            return row.isMaxExpandHeightInitialized();
628        }
629        return child == null || child.getWidth() != 0;
630    }
631
632    private View findFirstVisibleChild(ViewGroup container) {
633        int childCount = container.getChildCount();
634        for (int i = 0; i < childCount; i++) {
635            View child = container.getChildAt(i);
636            if (child.getVisibility() != View.GONE) {
637                return child;
638            }
639        }
640        return null;
641    }
642
643    public void setIsExpanded(boolean isExpanded) {
644        this.mIsExpanded = isExpanded;
645    }
646
647    protected class StackScrollAlgorithmState {
648
649        /**
650         * The scroll position of the algorithm
651         */
652        public int scrollY;
653
654        /**
655         * The quantity of items which are in the bottom stack.
656         */
657        public float itemsInBottomStack;
658
659        /**
660         * how far in is the element currently transitioning into the bottom stack
661         */
662        public float partialInBottom;
663
664        /**
665         * The children from the host view which are not gone.
666         */
667        public final ArrayList<ExpandableView> visibleChildren = new ArrayList<ExpandableView>();
668
669        /**
670         * The children from the host that need an increased padding after them. A value of 0 means
671         * no increased padding, a value of 1 means full padding.
672         */
673        public final HashMap<ExpandableView, Float> increasedPaddingMap = new HashMap<>();
674    }
675
676}
677