summaryrefslogtreecommitdiffstats
path: root/packages/SystemUI/src/com/android/systemui/statusbar/stack/StackScrollAlgorithm.java
blob: 4745f3bd535655357c27ebe521efa25e50f0b1d2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License
 */

package com.android.systemui.statusbar.stack;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import com.android.systemui.R;

/**
 * The Algorithm of the {@link com.android.systemui.statusbar.stack
 * .NotificationStackScrollLayout} which can be queried for {@link com.android.systemui.statusbar
 * .stack.StackScrollState}
 */
public class StackScrollAlgorithm {

    private static final String LOG_TAG = "StackScrollAlgorithm";

    private static final int MAX_ITEMS_IN_BOTTOM_STACK = 3;
    private static final int MAX_ITEMS_IN_TOP_STACK = 3;

    private int mPaddingBetweenElements;
    private int mCollapsedSize;
    private int mTopStackPeekSize;
    private int mBottomStackPeekSize;
    private int mZDistanceBetweenElements;
    private int mZBasicHeight;

    private StackIndentationFunctor mTopStackIndentationFunctor;
    private StackIndentationFunctor mBottomStackIndentationFunctor;

    private float mLayoutHeight;
    private StackScrollAlgorithmState mTempAlgorithmState = new StackScrollAlgorithmState();

    public StackScrollAlgorithm(Context context) {
        initConstants(context);
    }

    private void initConstants(Context context) {

        // currently the padding is in the elements themself
        mPaddingBetweenElements = 0;
        mCollapsedSize = context.getResources()
                .getDimensionPixelSize(R.dimen.notification_row_min_height);
        mTopStackPeekSize = context.getResources()
                .getDimensionPixelSize(R.dimen.top_stack_peek_amount);
        mBottomStackPeekSize = context.getResources()
                .getDimensionPixelSize(R.dimen.bottom_stack_peek_amount);
        mZDistanceBetweenElements = context.getResources()
                .getDimensionPixelSize(R.dimen.z_distance_between_notifications);
        mZBasicHeight = (MAX_ITEMS_IN_BOTTOM_STACK + 1) * mZDistanceBetweenElements;

        mTopStackIndentationFunctor = new PiecewiseLinearIndentationFunctor(
                MAX_ITEMS_IN_TOP_STACK,
                mTopStackPeekSize,
                mCollapsedSize + mPaddingBetweenElements,
                0.5f);
        mBottomStackIndentationFunctor = new PiecewiseLinearIndentationFunctor(
                MAX_ITEMS_IN_BOTTOM_STACK,
                mBottomStackPeekSize,
                mBottomStackPeekSize,
                0.5f);
    }


    public void getStackScrollState(StackScrollState resultState) {
        // The state of the local variables are saved in an algorithmState to easily subdivide it
        // into multiple phases.
        StackScrollAlgorithmState algorithmState = mTempAlgorithmState;

        // First we reset the view states to their default values.
        resultState.resetViewStates();

        // The first element is always in there so it's initialized with 1.0f;
        algorithmState.itemsInTopStack = 1.0f;
        algorithmState.partialInTop = 0.0f;
        algorithmState.lastTopStackIndex = 0;
        algorithmState.scrollY = resultState.getScrollY();
        algorithmState.itemsInBottomStack = 0.0f;

        // Phase 1:
        findNumberOfItemsInTopStackAndUpdateState(resultState, algorithmState);

        // Phase 2:
        updatePositionsForState(resultState, algorithmState);

        // Phase 3:
        updateZValuesForState(resultState, algorithmState);

        // write the algorithm state to the result
        resultState.setScrollY(algorithmState.scrollY);
    }

    /**
     * Determine the positions for the views. This is the main part of the algorithm.
     *
     * @param resultState The result state to update if a change to the properties of a child occurs
     * @param algorithmState The state in which the current pass of the algorithm is currently in
     *                       and which will be updated
     */
    private void updatePositionsForState(StackScrollState resultState,
            StackScrollAlgorithmState algorithmState) {
        float stackHeight = getLayoutHeight();

        // The position where the bottom stack starts.
        float transitioningPositionStart = stackHeight - mCollapsedSize - mBottomStackPeekSize;

        // The y coordinate of the current child.
        float currentYPosition = 0.0f;

        // How far in is the element currently transitioning into the bottom stack.
        float yPositionInScrollView = 0.0f;

        ViewGroup hostView = resultState.getHostView();
        int childCount = hostView.getChildCount();
        int numberOfElementsCompletelyIn = (int) algorithmState.itemsInTopStack;
        for (int i = 0; i < childCount; i++) {
            View child = hostView.getChildAt(i);
            StackScrollState.ViewState childViewState = resultState.getViewStateForView(child);
            childViewState.yTranslation = currentYPosition;
            childViewState.location = StackScrollState.ViewState.LOCATION_UNKNOWN;
            int childHeight = child.getHeight();
            // The y position after this element
            float nextYPosition = currentYPosition + childHeight + mPaddingBetweenElements;
            float yPositionInScrollViewAfterElement = yPositionInScrollView
                    + childHeight
                    + mPaddingBetweenElements;
            float scrollOffset = yPositionInScrollViewAfterElement - algorithmState.scrollY;
            if (i < algorithmState.lastTopStackIndex) {
                // Case 1:
                // We are in the top Stack
                nextYPosition = updateStateForTopStackChild(algorithmState,
                        numberOfElementsCompletelyIn,
                        i, childViewState);
            } else if (i == algorithmState.lastTopStackIndex) {
                // Case 2:
                // First element of regular scrollview comes next, so the position is just the
                // scrolling position
                nextYPosition = Math.min(scrollOffset, transitioningPositionStart);
                childViewState.location = StackScrollState.ViewState.LOCATION_TOP_STACK_PEEKING;
            } else if (nextYPosition >= transitioningPositionStart) {
                if (currentYPosition >= transitioningPositionStart) {
                    // Case 3:
                    // According to the regular scroll view we are fully translated out of the
                    // bottom of the screen so we are fully in the bottom stack
                    nextYPosition = updateStateForChildFullyInBottomStack(algorithmState,
                            transitioningPositionStart, childViewState, childHeight);
                } else {
                    // Case 4:
                    // According to the regular scroll view we are currently translating out of /
                    // into the bottom of the screen
                    nextYPosition = updateStateForChildTransitioningInBottom(
                            algorithmState, stackHeight, transitioningPositionStart,
                            currentYPosition, childViewState,
                            childHeight, nextYPosition);
                }
            } else {
                childViewState.location = StackScrollState.ViewState.LOCATION_MAIN_AREA;
            }
            // The first card is always rendered.
            if (i == 0) {
                childViewState.alpha = 1.0f;
                childViewState.location = StackScrollState.ViewState.LOCATION_FIRST_CARD;
            }
            if (childViewState.location == StackScrollState.ViewState.LOCATION_UNKNOWN) {
                Log.wtf(LOG_TAG, "Failed to assign location for child " + i);
            }
            nextYPosition = Math.max(0, nextYPosition);
            currentYPosition = nextYPosition;
            yPositionInScrollView = yPositionInScrollViewAfterElement;
        }
    }

    private float updateStateForChildTransitioningInBottom(StackScrollAlgorithmState algorithmState,
            float stackHeight, float transitioningPositionStart, float currentYPosition,
            StackScrollState.ViewState childViewState, int childHeight, float nextYPosition) {
        float newSize = transitioningPositionStart + mCollapsedSize - currentYPosition;
        newSize = Math.min(childHeight, newSize);
        // Transitioning element on top of bottom stack:
        algorithmState.partialInBottom = 1.0f - (
                (stackHeight - mBottomStackPeekSize - nextYPosition) / mCollapsedSize);
        // Our element can be expanded, so we might even have to scroll further than
        // mCollapsedSize
        algorithmState.partialInBottom = Math.min(1.0f, algorithmState.partialInBottom);
        float offset = mBottomStackIndentationFunctor.getValue(
                algorithmState.partialInBottom);
        nextYPosition = transitioningPositionStart + offset;
        algorithmState.itemsInBottomStack += algorithmState.partialInBottom;
        // TODO: only temporarily collapse
        if (childHeight != (int) newSize) {
            childViewState.height = (int) newSize;
        }
        childViewState.location = StackScrollState.ViewState.LOCATION_MAIN_AREA;

        return nextYPosition;
    }

    private float updateStateForChildFullyInBottomStack(StackScrollAlgorithmState algorithmState,
            float transitioningPositionStart, StackScrollState.ViewState childViewState,
            int childHeight) {

        float nextYPosition;
        algorithmState.itemsInBottomStack += 1.0f;
        if (algorithmState.itemsInBottomStack < MAX_ITEMS_IN_BOTTOM_STACK) {
            // We are visually entering the bottom stack
            nextYPosition = transitioningPositionStart
                    + mBottomStackIndentationFunctor.getValue(
                            algorithmState.itemsInBottomStack);
            childViewState.location = StackScrollState.ViewState.LOCATION_BOTTOM_STACK_PEEKING;
        } else {
            // we are fully inside the stack
            if (algorithmState.itemsInBottomStack > MAX_ITEMS_IN_BOTTOM_STACK + 2) {
                childViewState.alpha = 0.0f;
            } else if (algorithmState.itemsInBottomStack
                    > MAX_ITEMS_IN_BOTTOM_STACK + 1) {
                childViewState.alpha = 1.0f - algorithmState.partialInBottom;
            }
            childViewState.location = StackScrollState.ViewState.LOCATION_BOTTOM_STACK_HIDDEN;
            nextYPosition = transitioningPositionStart + mBottomStackPeekSize;
        }
        // TODO: only temporarily collapse
        if (childHeight != mCollapsedSize) {
            childViewState.height = mCollapsedSize;
        }
        return nextYPosition;
    }

    private float updateStateForTopStackChild(StackScrollAlgorithmState algorithmState,
            int numberOfElementsCompletelyIn, int i, StackScrollState.ViewState childViewState) {

        float nextYPosition = 0;

        // First we calculate the index relative to the current stack window of size at most
        // {@link #MAX_ITEMS_IN_TOP_STACK}
        int paddedIndex = i
                - Math.max(numberOfElementsCompletelyIn - MAX_ITEMS_IN_TOP_STACK, 0);
        if (paddedIndex >= 0) {
            // We are currently visually entering the top stack
            nextYPosition = mCollapsedSize + mPaddingBetweenElements -
                    mTopStackIndentationFunctor.getValue(
                            algorithmState.itemsInTopStack - i - 1);
            nextYPosition = Math.min(nextYPosition, mLayoutHeight - mCollapsedSize
                    - mBottomStackPeekSize);
            if (paddedIndex == 0) {
                childViewState.alpha = 1.0f - algorithmState.partialInTop;
                childViewState.location = StackScrollState.ViewState.LOCATION_TOP_STACK_HIDDEN;
            } else {
                childViewState.location = StackScrollState.ViewState.LOCATION_TOP_STACK_PEEKING;
            }
        } else {
            // We are hidden behind the top card and faded out, so we can hide ourselves.
            childViewState.alpha = 0.0f;
            childViewState.location = StackScrollState.ViewState.LOCATION_TOP_STACK_HIDDEN;
        }
        return nextYPosition;
    }

    /**
     * Find the number of items in the top stack and update the result state if needed.
     *
     * @param resultState The result state to update if a height change of an child occurs
     * @param algorithmState The state in which the current pass of the algorithm is currently in
     *                       and which will be updated
     */
    private void findNumberOfItemsInTopStackAndUpdateState(StackScrollState resultState,
            StackScrollAlgorithmState algorithmState) {

        // The y Position if the element would be in a regular scrollView
        float yPositionInScrollView = 0.0f;
        ViewGroup hostView = resultState.getHostView();
        int childCount = hostView.getChildCount();

        // find the number of elements in the top stack.
        for (int i = 0; i < childCount; i++) {
            View child = hostView.getChildAt(i);
            StackScrollState.ViewState childViewState = resultState.getViewStateForView(child);
            int childHeight = child.getHeight();
            float yPositionInScrollViewAfterElement = yPositionInScrollView
                    + childHeight
                    + mPaddingBetweenElements;
            if (yPositionInScrollView < algorithmState.scrollY) {
                if (yPositionInScrollViewAfterElement <= algorithmState.scrollY) {
                    // According to the regular scroll view we are fully off screen
                    algorithmState.itemsInTopStack += 1.0f;
                    if (childHeight != mCollapsedSize) {
                        childViewState.height = mCollapsedSize;
                    }
                } else {
                    // According to the regular scroll view we are partially off screen
                    // If it is expanded we have to collapse it to a new size
                    float newSize = yPositionInScrollViewAfterElement
                            - mPaddingBetweenElements
                            - algorithmState.scrollY;

                    // How much did we scroll into this child
                    algorithmState.partialInTop = (mCollapsedSize - newSize) / (mCollapsedSize
                            + mPaddingBetweenElements);

                    // Our element can be expanded, so this can get negative
                    algorithmState.partialInTop = Math.max(0.0f, algorithmState.partialInTop);
                    algorithmState.itemsInTopStack += algorithmState.partialInTop;
                    // TODO: handle overlapping sizes with end stack
                    newSize = Math.max(mCollapsedSize, newSize);
                    // TODO: only temporarily collapse
                    if (newSize != childHeight) {
                        childViewState.height = (int) newSize;

                        // We decrease scrollY by the same amount we made this child smaller.
                        // The new scroll position is therefore the start of the element
                        algorithmState.scrollY = (int) yPositionInScrollView;
                        resultState.setScrollY(algorithmState.scrollY);
                    }
                    if (childHeight > mCollapsedSize) {
                        // If we are just resizing this child, this element is not treated to be
                        // transitioning into the stack and therefore it is the last element in
                        // the stack.
                        algorithmState.lastTopStackIndex = i;
                        break;
                    }
                }
            } else {
                algorithmState.lastTopStackIndex = i;

                // We are already past the stack so we can end the loop
                break;
            }
            yPositionInScrollView = yPositionInScrollViewAfterElement;
        }
    }

    /**
     * Calculate the Z positions for all children based on the number of items in both stacks and
     * save it in the resultState
     *
     * @param resultState The result state to update the zTranslation values
     * @param algorithmState The state in which the current pass of the algorithm is currently in
     */
    private void updateZValuesForState(StackScrollState resultState,
            StackScrollAlgorithmState algorithmState) {
        ViewGroup hostView = resultState.getHostView();
        int childCount = hostView.getChildCount();
        for (int i = 0; i < childCount; i++) {
            View child = hostView.getChildAt(i);
            StackScrollState.ViewState childViewState = resultState.getViewStateForView(child);
            if (i < algorithmState.itemsInTopStack) {
                float stackIndex = algorithmState.itemsInTopStack - i;
                stackIndex = Math.min(stackIndex, MAX_ITEMS_IN_TOP_STACK + 2);
                childViewState.zTranslation = mZBasicHeight
                        + stackIndex * mZDistanceBetweenElements;
            } else if (i > (childCount - 1 - algorithmState.itemsInBottomStack)) {
                float numItemsAbove = i - (childCount - 1 - algorithmState.itemsInBottomStack);
                float translationZ = mZBasicHeight
                        - numItemsAbove * mZDistanceBetweenElements;
                childViewState.zTranslation = translationZ;
            } else {
                childViewState.zTranslation = mZBasicHeight;
            }
        }
    }

    public float getLayoutHeight() {
        return mLayoutHeight;
    }

    public void setLayoutHeight(float layoutHeight) {
        this.mLayoutHeight = layoutHeight;
    }

    class StackScrollAlgorithmState {

        /**
         * The scroll position of the algorithm
         */
        public int scrollY;

        /**
         *  The quantity of items which are in the top stack.
         */
        public float itemsInTopStack;

        /**
         * how far in is the element currently transitioning into the top stack
         */
        public float partialInTop;

        /**
         * The last item index which is in the top stack.
         * NOTE: In the top stack the item after the transitioning element is also in the stack!
         * This is needed to ensure a smooth transition between the y position in the regular
         * scrollview and the one in the stack.
         */
        public int lastTopStackIndex;

        /**
         * The quantity of items which are in the bottom stack.
         */
        public float itemsInBottomStack;

        /**
         * how far in is the element currently transitioning into the bottom stack
         */
        public float partialInBottom;
    }

}