blob: d68a7b1dc20570afe9ee802168322505b4a20403 [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;
Anthony Chen9fe1ee72017-04-07 13:53:37 -070020import android.content.res.Resources;
Christoph Studer6e3eceb2014-04-01 18:40:27 +020021import android.util.Log;
Selim Cinek67b22602014-03-10 15:40:16 +010022import android.view.View;
23import android.view.ViewGroup;
24import com.android.systemui.R;
Selim Cinekc9c640f2016-11-14 17:27:19 -080025import com.android.systemui.statusbar.DismissView;
Selim Cinekcde90e52016-12-22 21:01:49 +010026import com.android.systemui.statusbar.EmptyShadeView;
Selim Cinek1685e632014-04-08 02:27:49 +020027import com.android.systemui.statusbar.ExpandableNotificationRow;
Jorim Jaggibe565df2014-04-28 17:51:23 +020028import com.android.systemui.statusbar.ExpandableView;
Selim Cinek281c2022016-10-13 19:14:43 -070029import com.android.systemui.statusbar.NotificationShelf;
Selim Cinek42357e02016-02-24 18:48:01 -080030import com.android.systemui.statusbar.notification.NotificationUtils;
Selim Cinek67b22602014-03-10 15:40:16 +010031
Jorim Jaggid4a57442014-04-10 02:45:55 +020032import java.util.ArrayList;
Selim Cinek42357e02016-02-24 18:48:01 -080033import java.util.HashMap;
Selim Cinekb5605e52015-02-20 18:21:41 +010034import java.util.List;
Jorim Jaggid4a57442014-04-10 02:45:55 +020035
Selim Cinek67b22602014-03-10 15:40:16 +010036/**
37 * The Algorithm of the {@link com.android.systemui.statusbar.stack
38 * .NotificationStackScrollLayout} which can be queried for {@link com.android.systemui.statusbar
39 * .stack.StackScrollState}
40 */
41public class StackScrollAlgorithm {
42
Christoph Studer6e3eceb2014-04-01 18:40:27 +020043 private static final String LOG_TAG = "StackScrollAlgorithm";
44
Selim Cinek67b22602014-03-10 15:40:16 +010045 private int mPaddingBetweenElements;
Selim Cinek61633a82016-01-25 15:54:10 -080046 private int mIncreasedPaddingBetweenElements;
Selim Cinek67b22602014-03-10 15:40:16 +010047 private int mCollapsedSize;
Selim Cinek67b22602014-03-10 15:40:16 +010048
Selim Cinek67b22602014-03-10 15:40:16 +010049 private StackScrollAlgorithmState mTempAlgorithmState = new StackScrollAlgorithmState();
Selim Cinek1685e632014-04-08 02:27:49 +020050 private boolean mIsExpanded;
Anthony Chen9fe1ee72017-04-07 13:53:37 -070051 private boolean mClipNotificationScrollToTop;
Selim Cinekcafa87f2016-10-26 17:00:17 -070052 private int mStatusBarHeight;
Selim Cinek67b22602014-03-10 15:40:16 +010053
54 public StackScrollAlgorithm(Context context) {
Selim Cinekaf0dc312015-12-15 17:01:44 -080055 initView(context);
Selim Cinek34c0a8d2014-05-12 00:01:43 +020056 }
57
Selim Cinekaf0dc312015-12-15 17:01:44 -080058 public void initView(Context context) {
59 initConstants(context);
Selim Cinek34c0a8d2014-05-12 00:01:43 +020060 }
61
Selim Cinek67b22602014-03-10 15:40:16 +010062 private void initConstants(Context context) {
Anthony Chen9fe1ee72017-04-07 13:53:37 -070063 Resources res = context.getResources();
64 mPaddingBetweenElements = res.getDimensionPixelSize(
Selim Cinekdb167372016-11-17 15:41:17 -080065 R.dimen.notification_divider_height);
Anthony Chen9fe1ee72017-04-07 13:53:37 -070066 mIncreasedPaddingBetweenElements =
67 res.getDimensionPixelSize(R.dimen.notification_divider_height_increased);
68 mCollapsedSize = res.getDimensionPixelSize(R.dimen.notification_min_height);
69 mStatusBarHeight = res.getDimensionPixelSize(R.dimen.status_bar_height);
70 mClipNotificationScrollToTop = res.getBoolean(R.bool.config_clipNotificationScrollToTop);
Jorim Jaggid7c1fae2014-08-13 18:27:47 +020071 }
Selim Cinek67b22602014-03-10 15:40:16 +010072
Jorim Jaggid552d9d2014-05-07 19:41:13 +020073 public void getStackScrollState(AmbientState ambientState, StackScrollState resultState) {
Selim Cinek67b22602014-03-10 15:40:16 +010074 // The state of the local variables are saved in an algorithmState to easily subdivide it
75 // into multiple phases.
76 StackScrollAlgorithmState algorithmState = mTempAlgorithmState;
77
78 // First we reset the view states to their default values.
79 resultState.resetViewStates();
80
Selim Cinek3776fe02016-02-04 13:32:43 -080081 initAlgorithmState(resultState, algorithmState, ambientState);
Selim Cinek1408eb52014-06-02 14:45:38 +020082
Selim Cinekb8f09cf2015-03-16 17:09:28 -070083 updatePositionsForState(resultState, algorithmState, ambientState);
Selim Cinek67b22602014-03-10 15:40:16 +010084
Selim Cinek3776fe02016-02-04 13:32:43 -080085 updateZValuesForState(resultState, algorithmState, ambientState);
86
87 updateHeadsUpStates(resultState, algorithmState, ambientState);
Selim Cinekeb973562014-05-02 17:07:49 +020088
Jorim Jaggid552d9d2014-05-07 19:41:13 +020089 handleDraggedViews(ambientState, resultState, algorithmState);
Jorim Jaggiae441282014-08-01 02:45:18 +020090 updateDimmedActivatedHideSensitive(ambientState, resultState, algorithmState);
Selim Cineka59ecc32015-04-07 10:51:49 -070091 updateClipping(resultState, algorithmState, ambientState);
Selim Cinekdb167372016-11-17 15:41:17 -080092 updateSpeedBumpState(resultState, algorithmState, ambientState);
93 updateShelfState(resultState, ambientState);
Selim Cinekb5605e52015-02-20 18:21:41 +010094 getNotificationChildrenStates(resultState, algorithmState);
95 }
96
97 private void getNotificationChildrenStates(StackScrollState resultState,
98 StackScrollAlgorithmState algorithmState) {
99 int childCount = algorithmState.visibleChildren.size();
100 for (int i = 0; i < childCount; i++) {
101 ExpandableView v = algorithmState.visibleChildren.get(i);
102 if (v instanceof ExpandableNotificationRow) {
103 ExpandableNotificationRow row = (ExpandableNotificationRow) v;
104 row.getChildrenStates(resultState);
105 }
106 }
Selim Cinek3d2b94bf2014-07-02 22:12:47 +0200107 }
108
Selim Cinekdb167372016-11-17 15:41:17 -0800109 private void updateSpeedBumpState(StackScrollState resultState,
Selim Cinek281c2022016-10-13 19:14:43 -0700110 StackScrollAlgorithmState algorithmState, AmbientState ambientState) {
Selim Cinek3d2b94bf2014-07-02 22:12:47 +0200111 int childCount = algorithmState.visibleChildren.size();
Selim Cinekdb167372016-11-17 15:41:17 -0800112 int belowSpeedBump = ambientState.getSpeedBumpIndex();
Selim Cinek3d2b94bf2014-07-02 22:12:47 +0200113 for (int i = 0; i < childCount; i++) {
114 View child = algorithmState.visibleChildren.get(i);
Selim Cinekbbcebde2016-11-09 18:28:20 -0800115 ExpandableViewState childViewState = resultState.getViewStateForView(child);
Selim Cinek3107cfa2014-07-22 15:24:29 +0200116
117 // The speed bump can also be gone, so equality needs to be taken when comparing
118 // indices.
Selim Cinekdb167372016-11-17 15:41:17 -0800119 childViewState.belowSpeedBump = i >= belowSpeedBump;
Selim Cinek3d2b94bf2014-07-02 22:12:47 +0200120 }
Selim Cinekdb167372016-11-17 15:41:17 -0800121
122 }
123 private void updateShelfState(StackScrollState resultState, AmbientState ambientState) {
Selim Cinek281c2022016-10-13 19:14:43 -0700124 NotificationShelf shelf = ambientState.getShelf();
Eliot Courtney5d3d2d02018-01-18 15:59:03 +0900125 if (shelf != null) {
126 shelf.updateState(resultState, ambientState);
127 }
Selim Cinekf54090e2014-06-17 17:24:51 -0700128 }
129
Selim Cinek708a6c12014-05-28 14:16:02 +0200130 private void updateClipping(StackScrollState resultState,
Selim Cineka59ecc32015-04-07 10:51:49 -0700131 StackScrollAlgorithmState algorithmState, AmbientState ambientState) {
Selim Cinek355652a2016-12-07 13:32:12 -0800132 float drawStart = !ambientState.isOnKeyguard() ? ambientState.getTopPadding()
Selim Cinek2627d722018-01-19 12:16:49 -0800133 + ambientState.getStackTranslation() + ambientState.getExpandAnimationTopChange()
134 : 0;
Selim Cinek708a6c12014-05-28 14:16:02 +0200135 float previousNotificationEnd = 0;
136 float previousNotificationStart = 0;
Selim Cinek708a6c12014-05-28 14:16:02 +0200137 int childCount = algorithmState.visibleChildren.size();
138 for (int i = 0; i < childCount; i++) {
139 ExpandableView child = algorithmState.visibleChildren.get(i);
Selim Cinekbbcebde2016-11-09 18:28:20 -0800140 ExpandableViewState state = resultState.getViewStateForView(child);
Selim Cinek9b9d6e12017-11-30 12:29:47 +0100141 if (!child.mustStayOnScreen() || state.headsUpIsVisible) {
Selim Cinek3776fe02016-02-04 13:32:43 -0800142 previousNotificationEnd = Math.max(drawStart, previousNotificationEnd);
143 previousNotificationStart = Math.max(drawStart, previousNotificationStart);
144 }
Selim Cinek587cbf32016-01-19 11:36:18 -0800145 float newYTranslation = state.yTranslation;
146 float newHeight = state.height;
Selim Cinek708a6c12014-05-28 14:16:02 +0200147 float newNotificationEnd = newYTranslation + newHeight;
Mady Mellor53ac1ef2016-06-20 13:11:38 -0700148 boolean isHeadsUp = (child instanceof ExpandableNotificationRow)
149 && ((ExpandableNotificationRow) child).isPinned();
Anthony Chen9fe1ee72017-04-07 13:53:37 -0700150 if (mClipNotificationScrollToTop
151 && !state.inShelf && newYTranslation < previousNotificationEnd
Jorim Jaggi0fdf5742016-06-27 11:50:58 -0700152 && (!isHeadsUp || ambientState.isShadeExpanded())) {
Mady Mellorc128f222016-04-26 11:42:46 -0700153 // The previous view is overlapping on top, clip!
154 float overlapAmount = previousNotificationEnd - newYTranslation;
155 state.clipTopAmount = (int) overlapAmount;
Selim Cinekc5baa3e2014-10-29 19:04:19 +0100156 } else {
Mady Mellorc128f222016-04-26 11:42:46 -0700157 state.clipTopAmount = 0;
Selim Cinek9c17b772015-07-07 20:37:09 -0700158 }
159
Selim Cinek708a6c12014-05-28 14:16:02 +0200160 if (!child.isTransparent()) {
161 // Only update the previous values if we are not transparent,
162 // otherwise we would clip to a transparent view.
Mady Mellorc128f222016-04-26 11:42:46 -0700163 previousNotificationEnd = newNotificationEnd;
164 previousNotificationStart = newYTranslation;
Selim Cinek708a6c12014-05-28 14:16:02 +0200165 }
166 }
167 }
168
Selim Cinek9c17b772015-07-07 20:37:09 -0700169 public static boolean canChildBeDismissed(View v) {
Selim Cinek9e624e72016-07-20 13:46:49 -0700170 if (!(v instanceof ExpandableNotificationRow)) {
171 return false;
Selim Cinek38d429f2016-06-03 11:46:56 -0700172 }
Selim Cinek9e624e72016-07-20 13:46:49 -0700173 ExpandableNotificationRow row = (ExpandableNotificationRow) v;
174 if (row.areGutsExposed()) {
175 return false;
176 }
177 return row.canViewBeDismissed();
Selim Cinek9c17b772015-07-07 20:37:09 -0700178 }
179
Selim Cinek708a6c12014-05-28 14:16:02 +0200180 /**
Jorim Jaggiae441282014-08-01 02:45:18 +0200181 * Updates the dimmed, activated and hiding sensitive states of the children.
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200182 */
Jorim Jaggiae441282014-08-01 02:45:18 +0200183 private void updateDimmedActivatedHideSensitive(AmbientState ambientState,
184 StackScrollState resultState, StackScrollAlgorithmState algorithmState) {
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200185 boolean dimmed = ambientState.isDimmed();
John Spurlockbf370992014-06-17 13:58:31 -0400186 boolean dark = ambientState.isDark();
Jorim Jaggiae441282014-08-01 02:45:18 +0200187 boolean hideSensitive = ambientState.isHideSensitive();
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200188 View activatedChild = ambientState.getActivatedChild();
189 int childCount = algorithmState.visibleChildren.size();
190 for (int i = 0; i < childCount; i++) {
191 View child = algorithmState.visibleChildren.get(i);
Selim Cinekbbcebde2016-11-09 18:28:20 -0800192 ExpandableViewState childViewState = resultState.getViewStateForView(child);
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200193 childViewState.dimmed = dimmed;
John Spurlockbf370992014-06-17 13:58:31 -0400194 childViewState.dark = dark;
Jorim Jaggiae441282014-08-01 02:45:18 +0200195 childViewState.hideSensitive = hideSensitive;
Selim Cinekb89de4e2014-06-10 10:47:05 +0200196 boolean isActivatedChild = activatedChild == child;
Jorim Jaggi4538cee2014-09-09 15:21:38 +0200197 if (dimmed && isActivatedChild) {
Selim Cinek281c2022016-10-13 19:14:43 -0700198 childViewState.zTranslation += 2.0f * ambientState.getZDistanceBetweenElements();
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200199 }
200 }
Selim Cinekeb973562014-05-02 17:07:49 +0200201 }
202
203 /**
204 * Handle the special state when views are being dragged
205 */
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200206 private void handleDraggedViews(AmbientState ambientState, StackScrollState resultState,
Selim Cinekeb973562014-05-02 17:07:49 +0200207 StackScrollAlgorithmState algorithmState) {
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200208 ArrayList<View> draggedViews = ambientState.getDraggedViews();
209 for (View draggedView : draggedViews) {
Selim Cinekeb973562014-05-02 17:07:49 +0200210 int childIndex = algorithmState.visibleChildren.indexOf(draggedView);
211 if (childIndex >= 0 && childIndex < algorithmState.visibleChildren.size() - 1) {
212 View nextChild = algorithmState.visibleChildren.get(childIndex + 1);
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200213 if (!draggedViews.contains(nextChild)) {
Selim Cinekeb973562014-05-02 17:07:49 +0200214 // only if the view is not dragged itself we modify its state to be fully
215 // visible
Selim Cinekbbcebde2016-11-09 18:28:20 -0800216 ExpandableViewState viewState = resultState.getViewStateForView(
Selim Cinekeb973562014-05-02 17:07:49 +0200217 nextChild);
218 // The child below the dragged one must be fully visible
Selim Cinek131c1e22015-05-11 19:04:49 -0700219 if (ambientState.isShadeExpanded()) {
Selim Cinek277a8aa2016-01-22 12:12:37 -0800220 viewState.shadowAlpha = 1;
221 viewState.hidden = false;
Selim Cineka59ecc32015-04-07 10:51:49 -0700222 }
Selim Cinekeb973562014-05-02 17:07:49 +0200223 }
224
225 // Lets set the alpha to the one it currently has, as its currently being dragged
Selim Cinekbbcebde2016-11-09 18:28:20 -0800226 ExpandableViewState viewState = resultState.getViewStateForView(draggedView);
Selim Cinekeb973562014-05-02 17:07:49 +0200227 // The dragged child should keep the set alpha
228 viewState.alpha = draggedView.getAlpha();
229 }
230 }
Selim Cinek343e6e22014-04-11 21:23:30 +0200231 }
Selim Cinek67b22602014-03-10 15:40:16 +0100232
Selim Cinek343e6e22014-04-11 21:23:30 +0200233 /**
Selim Cinek61633a82016-01-25 15:54:10 -0800234 * Initialize the algorithm state like updating the visible children.
Jorim Jaggid4a57442014-04-10 02:45:55 +0200235 */
Selim Cinek3776fe02016-02-04 13:32:43 -0800236 private void initAlgorithmState(StackScrollState resultState, StackScrollAlgorithmState state,
237 AmbientState ambientState) {
Selim Cinek3776fe02016-02-04 13:32:43 -0800238 float bottomOverScroll = ambientState.getOverScrollAmount(false /* onTop */);
239
240 int scrollY = ambientState.getScrollY();
241
242 // Due to the overScroller, the stackscroller can have negative scroll state. This is
243 // already accounted for by the top padding and doesn't need an additional adaption
244 scrollY = Math.max(0, scrollY);
245 state.scrollY = (int) (scrollY + bottomOverScroll);
246
247 //now init the visible children and update paddings
Jorim Jaggid4a57442014-04-10 02:45:55 +0200248 ViewGroup hostView = resultState.getHostView();
249 int childCount = hostView.getChildCount();
250 state.visibleChildren.clear();
251 state.visibleChildren.ensureCapacity(childCount);
Selim Cineka7ed2c12017-01-23 20:47:24 -0800252 state.paddingMap.clear();
Selim Cinekb036ca42015-02-20 15:56:28 +0100253 int notGoneIndex = 0;
Selim Cinek61633a82016-01-25 15:54:10 -0800254 ExpandableView lastView = null;
Selim Cinekd96ed402017-07-28 18:19:03 -0700255 int firstHiddenIndex = ambientState.isDark()
256 ? (ambientState.hasPulsingNotifications() ? 1 : 0)
257 : childCount;
258
259 // The goal here is to fill the padding map, by iterating over how much padding each child
260 // needs. The map is thereby reused, by first filling it with the padding amount and when
261 // iterating over it again, it's filled with the actual resolved value.
262
Jorim Jaggid4a57442014-04-10 02:45:55 +0200263 for (int i = 0; i < childCount; i++) {
Jorim Jaggibe565df2014-04-28 17:51:23 +0200264 ExpandableView v = (ExpandableView) hostView.getChildAt(i);
Jorim Jaggid4a57442014-04-10 02:45:55 +0200265 if (v.getVisibility() != View.GONE) {
Selim Cinek281c2022016-10-13 19:14:43 -0700266 if (v == ambientState.getShelf()) {
267 continue;
268 }
Selim Cinekd96ed402017-07-28 18:19:03 -0700269 if (i >= firstHiddenIndex) {
270 // we need normal padding now, to be in sync with what the stack calculates
271 lastView = null;
272 ExpandableViewState viewState = resultState.getViewStateForView(v);
273 viewState.hidden = true;
274 }
Selim Cineka4baaa32015-04-20 14:27:54 -0700275 notGoneIndex = updateNotGoneIndex(resultState, state, notGoneIndex, v);
Selim Cinekd96ed402017-07-28 18:19:03 -0700276 float increasedPadding = v.getIncreasedPaddingAmount();
Selim Cinek42357e02016-02-24 18:48:01 -0800277 if (increasedPadding != 0.0f) {
Selim Cineka7ed2c12017-01-23 20:47:24 -0800278 state.paddingMap.put(v, increasedPadding);
Selim Cinek61633a82016-01-25 15:54:10 -0800279 if (lastView != null) {
Selim Cineka7ed2c12017-01-23 20:47:24 -0800280 Float prevValue = state.paddingMap.get(lastView);
281 float newValue = getPaddingForValue(increasedPadding);
282 if (prevValue != null) {
283 float prevPadding = getPaddingForValue(prevValue);
284 if (increasedPadding > 0) {
285 newValue = NotificationUtils.interpolate(
286 prevPadding,
287 newValue,
288 increasedPadding);
289 } else if (prevValue > 0) {
290 newValue = NotificationUtils.interpolate(
291 newValue,
292 prevPadding,
293 prevValue);
294 }
295 }
296 state.paddingMap.put(lastView, newValue);
Selim Cinek61633a82016-01-25 15:54:10 -0800297 }
Selim Cineka7ed2c12017-01-23 20:47:24 -0800298 } else if (lastView != null) {
Selim Cinekd96ed402017-07-28 18:19:03 -0700299
300 // Let's now resolve the value to an actual padding
Selim Cineka7ed2c12017-01-23 20:47:24 -0800301 float newValue = getPaddingForValue(state.paddingMap.get(lastView));
302 state.paddingMap.put(lastView, newValue);
Selim Cinek61633a82016-01-25 15:54:10 -0800303 }
Selim Cinekb5605e52015-02-20 18:21:41 +0100304 if (v instanceof ExpandableNotificationRow) {
305 ExpandableNotificationRow row = (ExpandableNotificationRow) v;
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700306
307 // handle the notgoneIndex for the children as well
Selim Cinekb5605e52015-02-20 18:21:41 +0100308 List<ExpandableNotificationRow> children =
309 row.getNotificationChildren();
Selim Cinek83bc7832015-10-22 13:26:54 -0700310 if (row.isSummaryWithChildren() && children != null) {
Selim Cinekb5605e52015-02-20 18:21:41 +0100311 for (ExpandableNotificationRow childRow : children) {
312 if (childRow.getVisibility() != View.GONE) {
Selim Cinekbbcebde2016-11-09 18:28:20 -0800313 ExpandableViewState childState
Selim Cinekb5605e52015-02-20 18:21:41 +0100314 = resultState.getViewStateForView(childRow);
315 childState.notGoneIndex = notGoneIndex;
316 notGoneIndex++;
317 }
318 }
319 }
320 }
Selim Cinek61633a82016-01-25 15:54:10 -0800321 lastView = v;
Jorim Jaggid4a57442014-04-10 02:45:55 +0200322 }
323 }
Selim Cinek2627d722018-01-19 12:16:49 -0800324 ExpandableNotificationRow expandingNotification = ambientState.getExpandingNotification();
325 state.indexOfExpandingNotification = expandingNotification != null
326 ? state.visibleChildren.indexOf(expandingNotification)
327 : -1;
Jorim Jaggid4a57442014-04-10 02:45:55 +0200328 }
329
Selim Cineka7ed2c12017-01-23 20:47:24 -0800330 private float getPaddingForValue(Float increasedPadding) {
331 if (increasedPadding == null) {
332 return mPaddingBetweenElements;
333 } else if (increasedPadding >= 0.0f) {
334 return NotificationUtils.interpolate(
335 mPaddingBetweenElements,
336 mIncreasedPaddingBetweenElements,
337 increasedPadding);
338 } else {
339 return NotificationUtils.interpolate(
340 0,
341 mPaddingBetweenElements,
342 1.0f + increasedPadding);
343 }
344 }
345
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700346 private int updateNotGoneIndex(StackScrollState resultState,
347 StackScrollAlgorithmState state, int notGoneIndex,
348 ExpandableView v) {
Selim Cinekbbcebde2016-11-09 18:28:20 -0800349 ExpandableViewState viewState = resultState.getViewStateForView(v);
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700350 viewState.notGoneIndex = notGoneIndex;
351 state.visibleChildren.add(v);
352 notGoneIndex++;
353 return notGoneIndex;
354 }
355
Jorim Jaggid4a57442014-04-10 02:45:55 +0200356 /**
Selim Cinek67b22602014-03-10 15:40:16 +0100357 * Determine the positions for the views. This is the main part of the algorithm.
358 *
Selim Cinek684a4422015-04-15 16:18:39 -0700359 * @param resultState The result state to update if a change to the properties of a child occurs
Selim Cinek67b22602014-03-10 15:40:16 +0100360 * @param algorithmState The state in which the current pass of the algorithm is currently in
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700361 * @param ambientState The current ambient state
Selim Cinek67b22602014-03-10 15:40:16 +0100362 */
363 private void updatePositionsForState(StackScrollState resultState,
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700364 StackScrollAlgorithmState algorithmState, AmbientState ambientState) {
Selim Cinek67b22602014-03-10 15:40:16 +0100365
Selim Cinek67b22602014-03-10 15:40:16 +0100366 // The y coordinate of the current child.
Selim Cinek3776fe02016-02-04 13:32:43 -0800367 float currentYPosition = -algorithmState.scrollY;
Jorim Jaggid4a57442014-04-10 02:45:55 +0200368 int childCount = algorithmState.visibleChildren.size();
Selim Cinek67b22602014-03-10 15:40:16 +0100369 for (int i = 0; i < childCount; i++) {
Muyuan Li87798022016-04-07 17:51:25 -0700370 currentYPosition = updateChild(i, resultState, algorithmState, ambientState,
Selim Cinekdb167372016-11-17 15:41:17 -0800371 currentYPosition);
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700372 }
373 }
374
Muyuan Li87798022016-04-07 17:51:25 -0700375 protected float updateChild(int i, StackScrollState resultState,
376 StackScrollAlgorithmState algorithmState, AmbientState ambientState,
Selim Cinekdb167372016-11-17 15:41:17 -0800377 float currentYPosition) {
Muyuan Li87798022016-04-07 17:51:25 -0700378 ExpandableView child = algorithmState.visibleChildren.get(i);
Selim Cinekbbcebde2016-11-09 18:28:20 -0800379 ExpandableViewState childViewState = resultState.getViewStateForView(child);
380 childViewState.location = ExpandableViewState.LOCATION_UNKNOWN;
Muyuan Li87798022016-04-07 17:51:25 -0700381 int paddingAfterChild = getPaddingAfterChild(algorithmState, child);
382 int childHeight = getMaxAllowedChildHeight(child);
Muyuan Li87798022016-04-07 17:51:25 -0700383 childViewState.yTranslation = currentYPosition;
Selim Cinekc9c640f2016-11-14 17:27:19 -0800384 boolean isDismissView = child instanceof DismissView;
Selim Cinekcde90e52016-12-22 21:01:49 +0100385 boolean isEmptyShadeView = child instanceof EmptyShadeView;
Muyuan Li87798022016-04-07 17:51:25 -0700386
Selim Cinekdb167372016-11-17 15:41:17 -0800387 childViewState.location = ExpandableViewState.LOCATION_MAIN_AREA;
Selim Cinek9b9d6e12017-11-30 12:29:47 +0100388 float inset = ambientState.getTopPadding() + ambientState.getStackTranslation();
Selim Cinek2627d722018-01-19 12:16:49 -0800389 if (i < algorithmState.getIndexOfExpandingNotification()) {
390 inset += ambientState.getExpandAnimationTopChange();
391 }
Selim Cinek9b9d6e12017-11-30 12:29:47 +0100392 if (child.mustStayOnScreen() && childViewState.yTranslation >= 0) {
393 // Even if we're not scrolled away we're in view and we're also not in the
394 // shelf. We can relax the constraints and let us scroll off the top!
395 float end = childViewState.yTranslation + childViewState.height + inset;
396 childViewState.headsUpIsVisible = end < ambientState.getMaxHeadsUpTranslation();
397 }
Selim Cinekdb167372016-11-17 15:41:17 -0800398 if (isDismissView) {
399 childViewState.yTranslation = Math.min(childViewState.yTranslation,
400 ambientState.getInnerHeight() - childHeight);
Selim Cinekcde90e52016-12-22 21:01:49 +0100401 } else if (isEmptyShadeView) {
402 childViewState.yTranslation = ambientState.getInnerHeight() - childHeight
403 + ambientState.getStackTranslation() * 0.25f;
Muyuan Li87798022016-04-07 17:51:25 -0700404 } else {
Selim Cinek2627d722018-01-19 12:16:49 -0800405 clampPositionToShelf(child, childViewState, ambientState);
Muyuan Li87798022016-04-07 17:51:25 -0700406 }
407
Muyuan Li87798022016-04-07 17:51:25 -0700408 currentYPosition = childViewState.yTranslation + childHeight + paddingAfterChild;
409 if (currentYPosition <= 0) {
Selim Cinekbbcebde2016-11-09 18:28:20 -0800410 childViewState.location = ExpandableViewState.LOCATION_HIDDEN_TOP;
Muyuan Li87798022016-04-07 17:51:25 -0700411 }
Selim Cinekbbcebde2016-11-09 18:28:20 -0800412 if (childViewState.location == ExpandableViewState.LOCATION_UNKNOWN) {
Muyuan Li87798022016-04-07 17:51:25 -0700413 Log.wtf(LOG_TAG, "Failed to assign location for child " + i);
414 }
415
Selim Cinek9b9d6e12017-11-30 12:29:47 +0100416 childViewState.yTranslation += inset;
Muyuan Li87798022016-04-07 17:51:25 -0700417 return currentYPosition;
418 }
419
420 protected int getPaddingAfterChild(StackScrollAlgorithmState algorithmState,
Selim Cinek61633a82016-01-25 15:54:10 -0800421 ExpandableView child) {
Selim Cinek281c2022016-10-13 19:14:43 -0700422 return algorithmState.getPaddingAfterChild(child);
Selim Cinek61633a82016-01-25 15:54:10 -0800423 }
424
Selim Cineka4baaa32015-04-20 14:27:54 -0700425 private void updateHeadsUpStates(StackScrollState resultState,
426 StackScrollAlgorithmState algorithmState, AmbientState ambientState) {
427 int childCount = algorithmState.visibleChildren.size();
428 ExpandableNotificationRow topHeadsUpEntry = null;
429 for (int i = 0; i < childCount; i++) {
430 View child = algorithmState.visibleChildren.get(i);
431 if (!(child instanceof ExpandableNotificationRow)) {
432 break;
433 }
434 ExpandableNotificationRow row = (ExpandableNotificationRow) child;
435 if (!row.isHeadsUp()) {
436 break;
Selim Cineka4baaa32015-04-20 14:27:54 -0700437 }
Selim Cinekbbcebde2016-11-09 18:28:20 -0800438 ExpandableViewState childState = resultState.getViewStateForView(row);
Selim Cinek9b9d6e12017-11-30 12:29:47 +0100439 if (topHeadsUpEntry == null && row.mustStayOnScreen() && !childState.headsUpIsVisible) {
Selim Cinek3776fe02016-02-04 13:32:43 -0800440 topHeadsUpEntry = row;
Selim Cinekbbcebde2016-11-09 18:28:20 -0800441 childState.location = ExpandableViewState.LOCATION_FIRST_HUN;
Selim Cinek3776fe02016-02-04 13:32:43 -0800442 }
Selim Cinek1f3f5442015-04-10 17:54:46 -0700443 boolean isTopEntry = topHeadsUpEntry == row;
Selim Cinek3776fe02016-02-04 13:32:43 -0800444 float unmodifiedEndLocation = childState.yTranslation + childState.height;
Selim Cinek131c1e22015-05-11 19:04:49 -0700445 if (mIsExpanded) {
Selim Cinek9b9d6e12017-11-30 12:29:47 +0100446 if (row.mustStayOnScreen() && !childState.headsUpIsVisible) {
447 // Ensure that the heads up is always visible even when scrolled off
448 clampHunToTop(ambientState, row, childState);
449 if (i == 0 && ambientState.isAboveShelf(row)) {
450 // the first hun can't get off screen.
451 clampHunToMaxTranslation(ambientState, row, childState);
452 childState.hidden = false;
453 }
Selim Cinekd127d792016-11-01 19:11:41 -0700454 }
Selim Cinek131c1e22015-05-11 19:04:49 -0700455 }
Selim Cinek684a4422015-04-15 16:18:39 -0700456 if (row.isPinned()) {
Selim Cinek33d46142016-01-15 18:58:39 -0800457 childState.yTranslation = Math.max(childState.yTranslation, 0);
Selim Cinek31aada42015-12-18 17:51:15 -0800458 childState.height = Math.max(row.getIntrinsicHeight(), childState.height);
Selim Cinekcafa87f2016-10-26 17:00:17 -0700459 childState.hidden = false;
Selim Cinekbbcebde2016-11-09 18:28:20 -0800460 ExpandableViewState topState = resultState.getViewStateForView(topHeadsUpEntry);
Selim Cinek61cfd4b2017-12-08 12:42:36 -0800461 if (topState != null && !isTopEntry && (!mIsExpanded
Selim Cinek3776fe02016-02-04 13:32:43 -0800462 || unmodifiedEndLocation < topState.yTranslation + topState.height)) {
Selim Cinek684a4422015-04-15 16:18:39 -0700463 // Ensure that a headsUp doesn't vertically extend further than the heads-up at
464 // the top most z-position
Selim Cinek31aada42015-12-18 17:51:15 -0800465 childState.height = row.getIntrinsicHeight();
Selim Cineke53e6bb2015-04-13 16:14:26 -0700466 childState.yTranslation = topState.yTranslation + topState.height
467 - childState.height;
Selim Cinek1f3f5442015-04-10 17:54:46 -0700468 }
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700469 }
Selim Cinekcafa87f2016-10-26 17:00:17 -0700470 if (row.isHeadsUpAnimatingAway()) {
471 childState.hidden = false;
472 }
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700473 }
Selim Cinek67b22602014-03-10 15:40:16 +0100474 }
475
Selim Cinek3776fe02016-02-04 13:32:43 -0800476 private void clampHunToTop(AmbientState ambientState, ExpandableNotificationRow row,
Selim Cinekbbcebde2016-11-09 18:28:20 -0800477 ExpandableViewState childState) {
Selim Cinek3776fe02016-02-04 13:32:43 -0800478 float newTranslation = Math.max(ambientState.getTopPadding()
479 + ambientState.getStackTranslation(), childState.yTranslation);
480 childState.height = (int) Math.max(childState.height - (newTranslation
Selim Cinek567e8452016-03-24 10:54:56 -0700481 - childState.yTranslation), row.getCollapsedHeight());
Selim Cinek3776fe02016-02-04 13:32:43 -0800482 childState.yTranslation = newTranslation;
483 }
484
485 private void clampHunToMaxTranslation(AmbientState ambientState, ExpandableNotificationRow row,
Selim Cinekbbcebde2016-11-09 18:28:20 -0800486 ExpandableViewState childState) {
Selim Cinek3776fe02016-02-04 13:32:43 -0800487 float newTranslation;
Selim Cinek7e0f9482017-05-22 20:00:56 -0700488 float maxHeadsUpTranslation = ambientState.getMaxHeadsUpTranslation();
489 float maxShelfPosition = ambientState.getInnerHeight() + ambientState.getTopPadding()
490 + ambientState.getStackTranslation();
491 maxHeadsUpTranslation = Math.min(maxHeadsUpTranslation, maxShelfPosition);
492 float bottomPosition = maxHeadsUpTranslation - row.getCollapsedHeight();
Selim Cinek3776fe02016-02-04 13:32:43 -0800493 newTranslation = Math.min(childState.yTranslation, bottomPosition);
Selim Cinek7e0f9482017-05-22 20:00:56 -0700494 childState.height = (int) Math.min(childState.height, maxHeadsUpTranslation
495 - newTranslation);
Selim Cinek3776fe02016-02-04 13:32:43 -0800496 childState.yTranslation = newTranslation;
Selim Cinek343e6e22014-04-11 21:23:30 +0200497 }
498
499 /**
Selim Cinek281c2022016-10-13 19:14:43 -0700500 * Clamp the height of the child down such that its end is at most on the beginning of
501 * the shelf.
502 *
Selim Cinek2627d722018-01-19 12:16:49 -0800503 * @param child
Selim Cinek281c2022016-10-13 19:14:43 -0700504 * @param childViewState the view state of the child
505 * @param ambientState the ambient state
506 */
Selim Cinek2627d722018-01-19 12:16:49 -0800507 private void clampPositionToShelf(ExpandableView child,
508 ExpandableViewState childViewState,
Selim Cinek281c2022016-10-13 19:14:43 -0700509 AmbientState ambientState) {
Eliot Courtney5d3d2d02018-01-18 15:59:03 +0900510 if (ambientState.getShelf() == null) {
511 return;
512 }
513
Selim Cineka686b2c2016-10-26 13:58:27 -0700514 int shelfStart = ambientState.getInnerHeight()
515 - ambientState.getShelf().getIntrinsicHeight();
Selim Cinek281c2022016-10-13 19:14:43 -0700516 childViewState.yTranslation = Math.min(childViewState.yTranslation, shelfStart);
Selim Cinekc383fd02016-10-21 15:31:26 -0700517 if (childViewState.yTranslation >= shelfStart) {
Selim Cinek2627d722018-01-19 12:16:49 -0800518 childViewState.hidden = !child.isExpandAnimationRunning();
Selim Cinekeccb5de2016-10-28 15:04:05 -0700519 childViewState.inShelf = true;
Selim Cinek9b9d6e12017-11-30 12:29:47 +0100520 childViewState.headsUpIsVisible = false;
Selim Cinekc383fd02016-10-21 15:31:26 -0700521 }
Selim Cinekcafa87f2016-10-26 17:00:17 -0700522 if (!ambientState.isShadeExpanded()) {
523 childViewState.height = (int) (mStatusBarHeight - childViewState.yTranslation);
524 }
Selim Cinek281c2022016-10-13 19:14:43 -0700525 }
526
Muyuan Li87798022016-04-07 17:51:25 -0700527 protected int getMaxAllowedChildHeight(View child) {
Selim Cinek31aada42015-12-18 17:51:15 -0800528 if (child instanceof ExpandableView) {
Jorim Jaggibe565df2014-04-28 17:51:23 +0200529 ExpandableView expandableView = (ExpandableView) child;
Selim Cinek8d5727f2015-04-28 19:17:32 -0700530 return expandableView.getIntrinsicHeight();
Selim Cinek1685e632014-04-08 02:27:49 +0200531 }
Jorim Jaggibe565df2014-04-28 17:51:23 +0200532 return child == null? mCollapsedSize : child.getHeight();
Selim Cinek1685e632014-04-08 02:27:49 +0200533 }
534
Selim Cinek67b22602014-03-10 15:40:16 +0100535 /**
Selim Cinek67b22602014-03-10 15:40:16 +0100536 * Calculate the Z positions for all children based on the number of items in both stacks and
537 * save it in the resultState
Selim Cinek3776fe02016-02-04 13:32:43 -0800538 * @param resultState The result state to update the zTranslation values
Selim Cinek67b22602014-03-10 15:40:16 +0100539 * @param algorithmState The state in which the current pass of the algorithm is currently in
Selim Cinek3776fe02016-02-04 13:32:43 -0800540 * @param ambientState The ambient state of the algorithm
Selim Cinek67b22602014-03-10 15:40:16 +0100541 */
542 private void updateZValuesForState(StackScrollState resultState,
Selim Cinek3776fe02016-02-04 13:32:43 -0800543 StackScrollAlgorithmState algorithmState, AmbientState ambientState) {
Jorim Jaggid4a57442014-04-10 02:45:55 +0200544 int childCount = algorithmState.visibleChildren.size();
Selim Cinek33223572016-02-19 19:32:22 -0800545 float childrenOnTop = 0.0f;
Selim Cinek3776fe02016-02-04 13:32:43 -0800546 for (int i = childCount - 1; i >= 0; i--) {
Selim Cinekdaab6f52017-04-06 16:46:34 -0700547 childrenOnTop = updateChildZValue(i, childrenOnTop,
Muyuan Li87798022016-04-07 17:51:25 -0700548 resultState, algorithmState, ambientState);
549 }
550 }
551
Selim Cinekdaab6f52017-04-06 16:46:34 -0700552 protected float updateChildZValue(int i, float childrenOnTop,
Muyuan Li87798022016-04-07 17:51:25 -0700553 StackScrollState resultState, StackScrollAlgorithmState algorithmState,
554 AmbientState ambientState) {
555 ExpandableView child = algorithmState.visibleChildren.get(i);
Selim Cinekbbcebde2016-11-09 18:28:20 -0800556 ExpandableViewState childViewState = resultState.getViewStateForView(child);
Selim Cinek281c2022016-10-13 19:14:43 -0700557 int zDistanceBetweenElements = ambientState.getZDistanceBetweenElements();
558 float baseZ = ambientState.getBaseZHeight();
Selim Cinek9b9d6e12017-11-30 12:29:47 +0100559 if (child.mustStayOnScreen() && !childViewState.headsUpIsVisible
560 && !ambientState.isDozingAndNotPulsing(child)
Muyuan Li87798022016-04-07 17:51:25 -0700561 && childViewState.yTranslation < ambientState.getTopPadding()
562 + ambientState.getStackTranslation()) {
563 if (childrenOnTop != 0.0f) {
564 childrenOnTop++;
565 } else {
566 float overlap = ambientState.getTopPadding()
567 + ambientState.getStackTranslation() - childViewState.yTranslation;
568 childrenOnTop += Math.min(1.0f, overlap / childViewState.height);
569 }
Selim Cinek281c2022016-10-13 19:14:43 -0700570 childViewState.zTranslation = baseZ
571 + childrenOnTop * zDistanceBetweenElements;
Selim Cinekebf42342017-07-13 15:46:10 +0200572 } else if (i == 0 && ambientState.isAboveShelf(child)) {
Selim Cinekd127d792016-11-01 19:11:41 -0700573 // In case this is a new view that has never been measured before, we don't want to
574 // elevate if we are currently expanded more then the notification
Eliot Courtney5d3d2d02018-01-18 15:59:03 +0900575 int shelfHeight = ambientState.getShelf() == null ? 0 :
576 ambientState.getShelf().getIntrinsicHeight();
Selim Cinekd127d792016-11-01 19:11:41 -0700577 float shelfStart = ambientState.getInnerHeight()
578 - shelfHeight + ambientState.getTopPadding()
579 + ambientState.getStackTranslation();
580 float notificationEnd = childViewState.yTranslation + child.getPinnedHeadsUpHeight()
581 + mPaddingBetweenElements;
582 if (shelfStart > notificationEnd) {
583 childViewState.zTranslation = baseZ;
584 } else {
585 float factor = (notificationEnd - shelfStart) / shelfHeight;
586 factor = Math.min(factor, 1.0f);
587 childViewState.zTranslation = baseZ + factor * zDistanceBetweenElements;
588 }
Muyuan Li87798022016-04-07 17:51:25 -0700589 } else {
Selim Cinek281c2022016-10-13 19:14:43 -0700590 childViewState.zTranslation = baseZ;
Selim Cinek67b22602014-03-10 15:40:16 +0100591 }
Selim Cinekdaab6f52017-04-06 16:46:34 -0700592 return childrenOnTop;
Selim Cinek67b22602014-03-10 15:40:16 +0100593 }
594
Selim Cinek1685e632014-04-08 02:27:49 +0200595 public void setIsExpanded(boolean isExpanded) {
596 this.mIsExpanded = isExpanded;
597 }
598
Selim Cinek281c2022016-10-13 19:14:43 -0700599 public class StackScrollAlgorithmState {
Selim Cinek67b22602014-03-10 15:40:16 +0100600
601 /**
602 * The scroll position of the algorithm
603 */
604 public int scrollY;
605
606 /**
Jorim Jaggid4a57442014-04-10 02:45:55 +0200607 * The children from the host view which are not gone.
608 */
Jorim Jaggibe565df2014-04-28 17:51:23 +0200609 public final ArrayList<ExpandableView> visibleChildren = new ArrayList<ExpandableView>();
Selim Cinek61633a82016-01-25 15:54:10 -0800610
611 /**
Selim Cineka7ed2c12017-01-23 20:47:24 -0800612 * The padding after each child measured in pixels.
Selim Cinek61633a82016-01-25 15:54:10 -0800613 */
Selim Cineka7ed2c12017-01-23 20:47:24 -0800614 public final HashMap<ExpandableView, Float> paddingMap = new HashMap<>();
Selim Cinek2627d722018-01-19 12:16:49 -0800615 private int indexOfExpandingNotification;
Selim Cinek281c2022016-10-13 19:14:43 -0700616
617 public int getPaddingAfterChild(ExpandableView child) {
Selim Cineka7ed2c12017-01-23 20:47:24 -0800618 Float padding = paddingMap.get(child);
619 if (padding == null) {
620 // Should only happen for the last view
621 return mPaddingBetweenElements;
622 }
623 return (int) padding.floatValue();
Selim Cinek281c2022016-10-13 19:14:43 -0700624 }
Selim Cinek2627d722018-01-19 12:16:49 -0800625
626 public int getIndexOfExpandingNotification() {
627 return indexOfExpandingNotification;
628 }
Selim Cinek67b22602014-03-10 15:40:16 +0100629 }
630
631}