blob: 47c7c7988c75e2c89261eccd7e00e624c5c78aee [file] [log] [blame]
Selim Cinek67b22602014-03-10 15:40:16 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
15 */
16
17package com.android.systemui.statusbar.stack;
18
19import android.content.Context;
Christoph Studer6e3eceb2014-04-01 18:40:27 +020020import android.util.Log;
Selim Cinek67b22602014-03-10 15:40:16 +010021import android.view.View;
22import android.view.ViewGroup;
23import com.android.systemui.R;
24
Jorim Jaggid4a57442014-04-10 02:45:55 +020025import java.util.ArrayList;
26
Selim Cinek67b22602014-03-10 15:40:16 +010027/**
28 * The Algorithm of the {@link com.android.systemui.statusbar.stack
29 * .NotificationStackScrollLayout} which can be queried for {@link com.android.systemui.statusbar
30 * .stack.StackScrollState}
31 */
32public class StackScrollAlgorithm {
33
Christoph Studer6e3eceb2014-04-01 18:40:27 +020034 private static final String LOG_TAG = "StackScrollAlgorithm";
35
Selim Cinek67b22602014-03-10 15:40:16 +010036 private static final int MAX_ITEMS_IN_BOTTOM_STACK = 3;
37 private static final int MAX_ITEMS_IN_TOP_STACK = 3;
38
39 private int mPaddingBetweenElements;
40 private int mCollapsedSize;
41 private int mTopStackPeekSize;
42 private int mBottomStackPeekSize;
43 private int mZDistanceBetweenElements;
44 private int mZBasicHeight;
45
46 private StackIndentationFunctor mTopStackIndentationFunctor;
47 private StackIndentationFunctor mBottomStackIndentationFunctor;
48
49 private float mLayoutHeight;
50 private StackScrollAlgorithmState mTempAlgorithmState = new StackScrollAlgorithmState();
51
52 public StackScrollAlgorithm(Context context) {
53 initConstants(context);
54 }
55
56 private void initConstants(Context context) {
57
58 // currently the padding is in the elements themself
59 mPaddingBetweenElements = 0;
60 mCollapsedSize = context.getResources()
61 .getDimensionPixelSize(R.dimen.notification_row_min_height);
62 mTopStackPeekSize = context.getResources()
63 .getDimensionPixelSize(R.dimen.top_stack_peek_amount);
64 mBottomStackPeekSize = context.getResources()
65 .getDimensionPixelSize(R.dimen.bottom_stack_peek_amount);
66 mZDistanceBetweenElements = context.getResources()
67 .getDimensionPixelSize(R.dimen.z_distance_between_notifications);
68 mZBasicHeight = (MAX_ITEMS_IN_BOTTOM_STACK + 1) * mZDistanceBetweenElements;
69
70 mTopStackIndentationFunctor = new PiecewiseLinearIndentationFunctor(
71 MAX_ITEMS_IN_TOP_STACK,
72 mTopStackPeekSize,
73 mCollapsedSize + mPaddingBetweenElements,
74 0.5f);
75 mBottomStackIndentationFunctor = new PiecewiseLinearIndentationFunctor(
76 MAX_ITEMS_IN_BOTTOM_STACK,
77 mBottomStackPeekSize,
78 mBottomStackPeekSize,
79 0.5f);
80 }
81
82
83 public void getStackScrollState(StackScrollState resultState) {
84 // The state of the local variables are saved in an algorithmState to easily subdivide it
85 // into multiple phases.
86 StackScrollAlgorithmState algorithmState = mTempAlgorithmState;
87
88 // First we reset the view states to their default values.
89 resultState.resetViewStates();
90
Selim Cinekb6d85eb2014-03-28 20:21:01 +010091 // The first element is always in there so it's initialized with 1.0f;
Selim Cinek67b22602014-03-10 15:40:16 +010092 algorithmState.itemsInTopStack = 1.0f;
93 algorithmState.partialInTop = 0.0f;
94 algorithmState.lastTopStackIndex = 0;
95 algorithmState.scrollY = resultState.getScrollY();
96 algorithmState.itemsInBottomStack = 0.0f;
Jorim Jaggid4a57442014-04-10 02:45:55 +020097 updateVisibleChildren(resultState, algorithmState);
Selim Cinek67b22602014-03-10 15:40:16 +010098
99 // Phase 1:
100 findNumberOfItemsInTopStackAndUpdateState(resultState, algorithmState);
101
102 // Phase 2:
103 updatePositionsForState(resultState, algorithmState);
104
105 // Phase 3:
106 updateZValuesForState(resultState, algorithmState);
107
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100108 // write the algorithm state to the result
Selim Cinek67b22602014-03-10 15:40:16 +0100109 resultState.setScrollY(algorithmState.scrollY);
110 }
111
112 /**
Jorim Jaggid4a57442014-04-10 02:45:55 +0200113 * Update the visible children on the state.
114 */
115 private void updateVisibleChildren(StackScrollState resultState,
116 StackScrollAlgorithmState state) {
117 ViewGroup hostView = resultState.getHostView();
118 int childCount = hostView.getChildCount();
119 state.visibleChildren.clear();
120 state.visibleChildren.ensureCapacity(childCount);
121 for (int i = 0; i < childCount; i++) {
122 View v = hostView.getChildAt(i);
123 if (v.getVisibility() != View.GONE) {
124 state.visibleChildren.add(v);
125 }
126 }
127 }
128
129 /**
Selim Cinek67b22602014-03-10 15:40:16 +0100130 * Determine the positions for the views. This is the main part of the algorithm.
131 *
132 * @param resultState The result state to update if a change to the properties of a child occurs
133 * @param algorithmState The state in which the current pass of the algorithm is currently in
134 * and which will be updated
135 */
136 private void updatePositionsForState(StackScrollState resultState,
137 StackScrollAlgorithmState algorithmState) {
138 float stackHeight = getLayoutHeight();
139
140 // The position where the bottom stack starts.
141 float transitioningPositionStart = stackHeight - mCollapsedSize - mBottomStackPeekSize;
142
143 // The y coordinate of the current child.
144 float currentYPosition = 0.0f;
145
146 // How far in is the element currently transitioning into the bottom stack.
147 float yPositionInScrollView = 0.0f;
148
Jorim Jaggid4a57442014-04-10 02:45:55 +0200149 int childCount = algorithmState.visibleChildren.size();
Selim Cinek67b22602014-03-10 15:40:16 +0100150 int numberOfElementsCompletelyIn = (int) algorithmState.itemsInTopStack;
151 for (int i = 0; i < childCount; i++) {
Jorim Jaggid4a57442014-04-10 02:45:55 +0200152 View child = algorithmState.visibleChildren.get(i);
Selim Cinek67b22602014-03-10 15:40:16 +0100153 StackScrollState.ViewState childViewState = resultState.getViewStateForView(child);
154 childViewState.yTranslation = currentYPosition;
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200155 childViewState.location = StackScrollState.ViewState.LOCATION_UNKNOWN;
Selim Cinek67b22602014-03-10 15:40:16 +0100156 int childHeight = child.getHeight();
157 // The y position after this element
158 float nextYPosition = currentYPosition + childHeight + mPaddingBetweenElements;
159 float yPositionInScrollViewAfterElement = yPositionInScrollView
160 + childHeight
161 + mPaddingBetweenElements;
162 float scrollOffset = yPositionInScrollViewAfterElement - algorithmState.scrollY;
163 if (i < algorithmState.lastTopStackIndex) {
164 // Case 1:
165 // We are in the top Stack
166 nextYPosition = updateStateForTopStackChild(algorithmState,
167 numberOfElementsCompletelyIn,
168 i, childViewState);
Selim Cinek67b22602014-03-10 15:40:16 +0100169 } else if (i == algorithmState.lastTopStackIndex) {
170 // Case 2:
171 // First element of regular scrollview comes next, so the position is just the
172 // scrolling position
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100173 nextYPosition = Math.min(scrollOffset, transitioningPositionStart);
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200174 childViewState.location = StackScrollState.ViewState.LOCATION_TOP_STACK_PEEKING;
Selim Cinek67b22602014-03-10 15:40:16 +0100175 } else if (nextYPosition >= transitioningPositionStart) {
176 if (currentYPosition >= transitioningPositionStart) {
177 // Case 3:
178 // According to the regular scroll view we are fully translated out of the
179 // bottom of the screen so we are fully in the bottom stack
180 nextYPosition = updateStateForChildFullyInBottomStack(algorithmState,
181 transitioningPositionStart, childViewState, childHeight);
Selim Cinek67b22602014-03-10 15:40:16 +0100182 } else {
183 // Case 4:
184 // According to the regular scroll view we are currently translating out of /
185 // into the bottom of the screen
186 nextYPosition = updateStateForChildTransitioningInBottom(
187 algorithmState, stackHeight, transitioningPositionStart,
188 currentYPosition, childViewState,
189 childHeight, nextYPosition);
190 }
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200191 } else {
192 childViewState.location = StackScrollState.ViewState.LOCATION_MAIN_AREA;
193 }
194 // The first card is always rendered.
195 if (i == 0) {
196 childViewState.alpha = 1.0f;
197 childViewState.location = StackScrollState.ViewState.LOCATION_FIRST_CARD;
198 }
199 if (childViewState.location == StackScrollState.ViewState.LOCATION_UNKNOWN) {
200 Log.wtf(LOG_TAG, "Failed to assign location for child " + i);
Selim Cinek67b22602014-03-10 15:40:16 +0100201 }
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100202 nextYPosition = Math.max(0, nextYPosition);
Selim Cinek67b22602014-03-10 15:40:16 +0100203 currentYPosition = nextYPosition;
204 yPositionInScrollView = yPositionInScrollViewAfterElement;
205 }
206 }
207
208 private float updateStateForChildTransitioningInBottom(StackScrollAlgorithmState algorithmState,
209 float stackHeight, float transitioningPositionStart, float currentYPosition,
210 StackScrollState.ViewState childViewState, int childHeight, float nextYPosition) {
211 float newSize = transitioningPositionStart + mCollapsedSize - currentYPosition;
212 newSize = Math.min(childHeight, newSize);
213 // Transitioning element on top of bottom stack:
214 algorithmState.partialInBottom = 1.0f - (
215 (stackHeight - mBottomStackPeekSize - nextYPosition) / mCollapsedSize);
216 // Our element can be expanded, so we might even have to scroll further than
217 // mCollapsedSize
218 algorithmState.partialInBottom = Math.min(1.0f, algorithmState.partialInBottom);
219 float offset = mBottomStackIndentationFunctor.getValue(
220 algorithmState.partialInBottom);
221 nextYPosition = transitioningPositionStart + offset;
222 algorithmState.itemsInBottomStack += algorithmState.partialInBottom;
223 // TODO: only temporarily collapse
224 if (childHeight != (int) newSize) {
225 childViewState.height = (int) newSize;
226 }
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200227 childViewState.location = StackScrollState.ViewState.LOCATION_MAIN_AREA;
228
Selim Cinek67b22602014-03-10 15:40:16 +0100229 return nextYPosition;
230 }
231
232 private float updateStateForChildFullyInBottomStack(StackScrollAlgorithmState algorithmState,
233 float transitioningPositionStart, StackScrollState.ViewState childViewState,
234 int childHeight) {
235
236 float nextYPosition;
237 algorithmState.itemsInBottomStack += 1.0f;
238 if (algorithmState.itemsInBottomStack < MAX_ITEMS_IN_BOTTOM_STACK) {
239 // We are visually entering the bottom stack
240 nextYPosition = transitioningPositionStart
241 + mBottomStackIndentationFunctor.getValue(
242 algorithmState.itemsInBottomStack);
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200243 childViewState.location = StackScrollState.ViewState.LOCATION_BOTTOM_STACK_PEEKING;
Selim Cinek67b22602014-03-10 15:40:16 +0100244 } else {
245 // we are fully inside the stack
246 if (algorithmState.itemsInBottomStack > MAX_ITEMS_IN_BOTTOM_STACK + 2) {
247 childViewState.alpha = 0.0f;
248 } else if (algorithmState.itemsInBottomStack
249 > MAX_ITEMS_IN_BOTTOM_STACK + 1) {
250 childViewState.alpha = 1.0f - algorithmState.partialInBottom;
251 }
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200252 childViewState.location = StackScrollState.ViewState.LOCATION_BOTTOM_STACK_HIDDEN;
Selim Cinek67b22602014-03-10 15:40:16 +0100253 nextYPosition = transitioningPositionStart + mBottomStackPeekSize;
254 }
255 // TODO: only temporarily collapse
256 if (childHeight != mCollapsedSize) {
257 childViewState.height = mCollapsedSize;
258 }
259 return nextYPosition;
260 }
261
262 private float updateStateForTopStackChild(StackScrollAlgorithmState algorithmState,
263 int numberOfElementsCompletelyIn, int i, StackScrollState.ViewState childViewState) {
264
265 float nextYPosition = 0;
266
267 // First we calculate the index relative to the current stack window of size at most
268 // {@link #MAX_ITEMS_IN_TOP_STACK}
269 int paddedIndex = i
270 - Math.max(numberOfElementsCompletelyIn - MAX_ITEMS_IN_TOP_STACK, 0);
271 if (paddedIndex >= 0) {
272 // We are currently visually entering the top stack
273 nextYPosition = mCollapsedSize + mPaddingBetweenElements -
274 mTopStackIndentationFunctor.getValue(
275 algorithmState.itemsInTopStack - i - 1);
Selim Cinekb6d85eb2014-03-28 20:21:01 +0100276 nextYPosition = Math.min(nextYPosition, mLayoutHeight - mCollapsedSize
277 - mBottomStackPeekSize);
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200278 if (paddedIndex == 0) {
Selim Cinek67b22602014-03-10 15:40:16 +0100279 childViewState.alpha = 1.0f - algorithmState.partialInTop;
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200280 childViewState.location = StackScrollState.ViewState.LOCATION_TOP_STACK_HIDDEN;
281 } else {
282 childViewState.location = StackScrollState.ViewState.LOCATION_TOP_STACK_PEEKING;
Selim Cinek67b22602014-03-10 15:40:16 +0100283 }
284 } else {
Christoph Studer6e3eceb2014-04-01 18:40:27 +0200285 // We are hidden behind the top card and faded out, so we can hide ourselves.
286 childViewState.alpha = 0.0f;
287 childViewState.location = StackScrollState.ViewState.LOCATION_TOP_STACK_HIDDEN;
Selim Cinek67b22602014-03-10 15:40:16 +0100288 }
289 return nextYPosition;
290 }
291
292 /**
293 * Find the number of items in the top stack and update the result state if needed.
294 *
295 * @param resultState The result state to update if a height change of an child occurs
296 * @param algorithmState The state in which the current pass of the algorithm is currently in
297 * and which will be updated
298 */
299 private void findNumberOfItemsInTopStackAndUpdateState(StackScrollState resultState,
300 StackScrollAlgorithmState algorithmState) {
301
302 // The y Position if the element would be in a regular scrollView
303 float yPositionInScrollView = 0.0f;
Jorim Jaggid4a57442014-04-10 02:45:55 +0200304 int childCount = algorithmState.visibleChildren.size();
Selim Cinek67b22602014-03-10 15:40:16 +0100305
306 // find the number of elements in the top stack.
307 for (int i = 0; i < childCount; i++) {
Jorim Jaggid4a57442014-04-10 02:45:55 +0200308 View child = algorithmState.visibleChildren.get(i);
Selim Cinek67b22602014-03-10 15:40:16 +0100309 StackScrollState.ViewState childViewState = resultState.getViewStateForView(child);
310 int childHeight = child.getHeight();
311 float yPositionInScrollViewAfterElement = yPositionInScrollView
312 + childHeight
313 + mPaddingBetweenElements;
314 if (yPositionInScrollView < algorithmState.scrollY) {
315 if (yPositionInScrollViewAfterElement <= algorithmState.scrollY) {
316 // According to the regular scroll view we are fully off screen
317 algorithmState.itemsInTopStack += 1.0f;
318 if (childHeight != mCollapsedSize) {
319 childViewState.height = mCollapsedSize;
320 }
321 } else {
322 // According to the regular scroll view we are partially off screen
323 // If it is expanded we have to collapse it to a new size
324 float newSize = yPositionInScrollViewAfterElement
325 - mPaddingBetweenElements
326 - algorithmState.scrollY;
327
328 // How much did we scroll into this child
329 algorithmState.partialInTop = (mCollapsedSize - newSize) / (mCollapsedSize
330 + mPaddingBetweenElements);
331
332 // Our element can be expanded, so this can get negative
333 algorithmState.partialInTop = Math.max(0.0f, algorithmState.partialInTop);
334 algorithmState.itemsInTopStack += algorithmState.partialInTop;
335 // TODO: handle overlapping sizes with end stack
336 newSize = Math.max(mCollapsedSize, newSize);
337 // TODO: only temporarily collapse
338 if (newSize != childHeight) {
339 childViewState.height = (int) newSize;
340
341 // We decrease scrollY by the same amount we made this child smaller.
342 // The new scroll position is therefore the start of the element
343 algorithmState.scrollY = (int) yPositionInScrollView;
344 resultState.setScrollY(algorithmState.scrollY);
345 }
346 if (childHeight > mCollapsedSize) {
347 // If we are just resizing this child, this element is not treated to be
348 // transitioning into the stack and therefore it is the last element in
349 // the stack.
350 algorithmState.lastTopStackIndex = i;
351 break;
352 }
353 }
354 } else {
355 algorithmState.lastTopStackIndex = i;
356
357 // We are already past the stack so we can end the loop
358 break;
359 }
360 yPositionInScrollView = yPositionInScrollViewAfterElement;
361 }
362 }
363
364 /**
365 * Calculate the Z positions for all children based on the number of items in both stacks and
366 * save it in the resultState
367 *
368 * @param resultState The result state to update the zTranslation values
369 * @param algorithmState The state in which the current pass of the algorithm is currently in
370 */
371 private void updateZValuesForState(StackScrollState resultState,
372 StackScrollAlgorithmState algorithmState) {
373 ViewGroup hostView = resultState.getHostView();
Jorim Jaggid4a57442014-04-10 02:45:55 +0200374 int childCount = algorithmState.visibleChildren.size();
Selim Cinek67b22602014-03-10 15:40:16 +0100375 for (int i = 0; i < childCount; i++) {
Jorim Jaggid4a57442014-04-10 02:45:55 +0200376 View child = algorithmState.visibleChildren.get(i);
377 if (child.getVisibility() == View.GONE) continue;
Selim Cinek67b22602014-03-10 15:40:16 +0100378 StackScrollState.ViewState childViewState = resultState.getViewStateForView(child);
379 if (i < algorithmState.itemsInTopStack) {
380 float stackIndex = algorithmState.itemsInTopStack - i;
381 stackIndex = Math.min(stackIndex, MAX_ITEMS_IN_TOP_STACK + 2);
382 childViewState.zTranslation = mZBasicHeight
383 + stackIndex * mZDistanceBetweenElements;
384 } else if (i > (childCount - 1 - algorithmState.itemsInBottomStack)) {
385 float numItemsAbove = i - (childCount - 1 - algorithmState.itemsInBottomStack);
386 float translationZ = mZBasicHeight
387 - numItemsAbove * mZDistanceBetweenElements;
388 childViewState.zTranslation = translationZ;
389 } else {
390 childViewState.zTranslation = mZBasicHeight;
391 }
392 }
393 }
394
395 public float getLayoutHeight() {
396 return mLayoutHeight;
397 }
398
399 public void setLayoutHeight(float layoutHeight) {
400 this.mLayoutHeight = layoutHeight;
401 }
402
403 class StackScrollAlgorithmState {
404
405 /**
406 * The scroll position of the algorithm
407 */
408 public int scrollY;
409
410 /**
411 * The quantity of items which are in the top stack.
412 */
413 public float itemsInTopStack;
414
415 /**
416 * how far in is the element currently transitioning into the top stack
417 */
418 public float partialInTop;
419
420 /**
421 * The last item index which is in the top stack.
422 * NOTE: In the top stack the item after the transitioning element is also in the stack!
423 * This is needed to ensure a smooth transition between the y position in the regular
424 * scrollview and the one in the stack.
425 */
426 public int lastTopStackIndex;
427
428 /**
429 * The quantity of items which are in the bottom stack.
430 */
431 public float itemsInBottomStack;
432
433 /**
434 * how far in is the element currently transitioning into the bottom stack
435 */
436 public float partialInBottom;
Jorim Jaggid4a57442014-04-10 02:45:55 +0200437
438 /**
439 * The children from the host view which are not gone.
440 */
441 public final ArrayList<View> visibleChildren = new ArrayList<View>();
Selim Cinek67b22602014-03-10 15:40:16 +0100442 }
443
444}