blob: a4598e9cec9fbfc9518fa9c909812bcc3c1d3c47 [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
Rohan Shah20790b82018-07-02 17:21:04 -070017package com.android.systemui.statusbar.notification.stack;
Selim Cinek67b22602014-03-10 15:40:16 +010018
Evan Laird25f02752019-08-14 19:25:06 -040019import android.annotation.NonNull;
20import android.annotation.Nullable;
Selim Cinek67b22602014-03-10 15:40:16 +010021import android.content.Context;
Anthony Chen9fe1ee72017-04-07 13:53:37 -070022import android.content.res.Resources;
Christoph Studer6e3eceb2014-04-01 18:40:27 +020023import android.util.Log;
Selim Cinek67b22602014-03-10 15:40:16 +010024import android.view.View;
25import android.view.ViewGroup;
Gus Prevase2d6f042018-10-17 15:25:30 -040026
Selim Cinek67b22602014-03-10 15:40:16 +010027import com.android.systemui.R;
Selim Cinekcde90e52016-12-22 21:01:49 +010028import com.android.systemui.statusbar.EmptyShadeView;
Gus Prevase2d6f042018-10-17 15:25:30 -040029import com.android.systemui.statusbar.NotificationShelf;
30import com.android.systemui.statusbar.notification.NotificationUtils;
Rohan Shah20790b82018-07-02 17:21:04 -070031import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow;
32import com.android.systemui.statusbar.notification.row.ExpandableView;
33import com.android.systemui.statusbar.notification.row.FooterView;
Selim Cinek67b22602014-03-10 15:40:16 +010034
Jorim Jaggid4a57442014-04-10 02:45:55 +020035import java.util.ArrayList;
Selim Cinek42357e02016-02-24 18:48:01 -080036import java.util.HashMap;
Selim Cinekb5605e52015-02-20 18:21:41 +010037import java.util.List;
Jorim Jaggid4a57442014-04-10 02:45:55 +020038
Selim Cinek67b22602014-03-10 15:40:16 +010039/**
Rohan Shah20790b82018-07-02 17:21:04 -070040 * The Algorithm of the {@link com.android.systemui.statusbar.notification.stack
Selim Cinek67b22602014-03-10 15:40:16 +010041 * .NotificationStackScrollLayout} which can be queried for {@link com.android.systemui.statusbar
42 * .stack.StackScrollState}
43 */
44public class StackScrollAlgorithm {
45
Gus Prevas0fa58d62019-01-11 13:58:40 -050046 static final boolean ANCHOR_SCROLLING = false;
47
Christoph Studer6e3eceb2014-04-01 18:40:27 +020048 private static final String LOG_TAG = "StackScrollAlgorithm";
Dave Mankoffa4d195d2018-11-16 13:33:27 -050049 private final ViewGroup mHostView;
Christoph Studer6e3eceb2014-04-01 18:40:27 +020050
Selim Cinek67b22602014-03-10 15:40:16 +010051 private int mPaddingBetweenElements;
Selim Cinek61633a82016-01-25 15:54:10 -080052 private int mIncreasedPaddingBetweenElements;
Gus Prevase2d6f042018-10-17 15:25:30 -040053 private int mGapHeight;
Selim Cinek67b22602014-03-10 15:40:16 +010054 private int mCollapsedSize;
Selim Cinek67b22602014-03-10 15:40:16 +010055
Selim Cinek67b22602014-03-10 15:40:16 +010056 private StackScrollAlgorithmState mTempAlgorithmState = new StackScrollAlgorithmState();
Selim Cinek1685e632014-04-08 02:27:49 +020057 private boolean mIsExpanded;
Anthony Chen9fe1ee72017-04-07 13:53:37 -070058 private boolean mClipNotificationScrollToTop;
Selim Cinekcafa87f2016-10-26 17:00:17 -070059 private int mStatusBarHeight;
Selim Cinekaa9db1f2018-02-27 17:35:47 -080060 private float mHeadsUpInset;
Selim Cinek99e9adf2018-03-15 09:17:47 -070061 private int mPinnedZTranslationExtra;
Selim Cinek67b22602014-03-10 15:40:16 +010062
Ned Burns9eb06332019-04-23 16:02:12 -040063 public StackScrollAlgorithm(
64 Context context,
65 ViewGroup hostView) {
Dave Mankoffa4d195d2018-11-16 13:33:27 -050066 mHostView = hostView;
Selim Cinekaf0dc312015-12-15 17:01:44 -080067 initView(context);
Selim Cinek34c0a8d2014-05-12 00:01:43 +020068 }
69
Selim Cinekaf0dc312015-12-15 17:01:44 -080070 public void initView(Context context) {
71 initConstants(context);
Selim Cinek34c0a8d2014-05-12 00:01:43 +020072 }
73
Selim Cinek67b22602014-03-10 15:40:16 +010074 private void initConstants(Context context) {
Anthony Chen9fe1ee72017-04-07 13:53:37 -070075 Resources res = context.getResources();
76 mPaddingBetweenElements = res.getDimensionPixelSize(
Selim Cinekdb167372016-11-17 15:41:17 -080077 R.dimen.notification_divider_height);
Anthony Chen9fe1ee72017-04-07 13:53:37 -070078 mIncreasedPaddingBetweenElements =
79 res.getDimensionPixelSize(R.dimen.notification_divider_height_increased);
80 mCollapsedSize = res.getDimensionPixelSize(R.dimen.notification_min_height);
81 mStatusBarHeight = res.getDimensionPixelSize(R.dimen.status_bar_height);
82 mClipNotificationScrollToTop = res.getBoolean(R.bool.config_clipNotificationScrollToTop);
Selim Cinekaa9db1f2018-02-27 17:35:47 -080083 mHeadsUpInset = mStatusBarHeight + res.getDimensionPixelSize(
84 R.dimen.heads_up_status_bar_padding);
Selim Cinek99e9adf2018-03-15 09:17:47 -070085 mPinnedZTranslationExtra = res.getDimensionPixelSize(
86 R.dimen.heads_up_pinned_elevation);
Gus Prevase2d6f042018-10-17 15:25:30 -040087 mGapHeight = res.getDimensionPixelSize(R.dimen.notification_section_divider_height);
Jorim Jaggid7c1fae2014-08-13 18:27:47 +020088 }
Selim Cinek67b22602014-03-10 15:40:16 +010089
Dave Mankoffa4d195d2018-11-16 13:33:27 -050090 /**
91 * Updates the state of all children in the hostview based on this algorithm.
92 */
93 public void resetViewStates(AmbientState ambientState) {
Selim Cinek67b22602014-03-10 15:40:16 +010094 // The state of the local variables are saved in an algorithmState to easily subdivide it
95 // into multiple phases.
96 StackScrollAlgorithmState algorithmState = mTempAlgorithmState;
97
98 // First we reset the view states to their default values.
Dave Mankoffa4d195d2018-11-16 13:33:27 -050099 resetChildViewStates();
Selim Cinek67b22602014-03-10 15:40:16 +0100100
Dave Mankoffa4d195d2018-11-16 13:33:27 -0500101 initAlgorithmState(mHostView, algorithmState, ambientState);
Selim Cinek1408eb52014-06-02 14:45:38 +0200102
Dave Mankoffa4d195d2018-11-16 13:33:27 -0500103 updatePositionsForState(algorithmState, ambientState);
Selim Cinek67b22602014-03-10 15:40:16 +0100104
Dave Mankoffa4d195d2018-11-16 13:33:27 -0500105 updateZValuesForState(algorithmState, ambientState);
Selim Cinek3776fe02016-02-04 13:32:43 -0800106
Dave Mankoffa4d195d2018-11-16 13:33:27 -0500107 updateHeadsUpStates(algorithmState, ambientState);
Selim Cinek5040f2e2019-02-14 18:22:42 -0800108 updatePulsingStates(algorithmState, ambientState);
Selim Cinekeb973562014-05-02 17:07:49 +0200109
Dave Mankoffa4d195d2018-11-16 13:33:27 -0500110 updateDimmedActivatedHideSensitive(ambientState, algorithmState);
111 updateClipping(algorithmState, ambientState);
112 updateSpeedBumpState(algorithmState, ambientState);
113 updateShelfState(ambientState);
114 getNotificationChildrenStates(algorithmState, ambientState);
Selim Cinekb5605e52015-02-20 18:21:41 +0100115 }
116
Dave Mankoffa4d195d2018-11-16 13:33:27 -0500117 private void resetChildViewStates() {
118 int numChildren = mHostView.getChildCount();
119 for (int i = 0; i < numChildren; i++) {
120 ExpandableView child = (ExpandableView) mHostView.getChildAt(i);
121 child.resetViewState();
122 }
123 }
124
125 private void getNotificationChildrenStates(StackScrollAlgorithmState algorithmState,
Selim Cinekc25989e2018-02-16 16:42:14 -0800126 AmbientState ambientState) {
Selim Cinekb5605e52015-02-20 18:21:41 +0100127 int childCount = algorithmState.visibleChildren.size();
128 for (int i = 0; i < childCount; i++) {
129 ExpandableView v = algorithmState.visibleChildren.get(i);
130 if (v instanceof ExpandableNotificationRow) {
131 ExpandableNotificationRow row = (ExpandableNotificationRow) v;
Dave Mankoffa4d195d2018-11-16 13:33:27 -0500132 row.updateChildrenStates(ambientState);
Selim Cinekb5605e52015-02-20 18:21:41 +0100133 }
134 }
Selim Cinek3d2b94bf2014-07-02 22:12:47 +0200135 }
136
Dave Mankoffa4d195d2018-11-16 13:33:27 -0500137 private void updateSpeedBumpState(StackScrollAlgorithmState algorithmState,
138 AmbientState ambientState) {
Selim Cinek3d2b94bf2014-07-02 22:12:47 +0200139 int childCount = algorithmState.visibleChildren.size();
Selim Cinekdb167372016-11-17 15:41:17 -0800140 int belowSpeedBump = ambientState.getSpeedBumpIndex();
Selim Cinek3d2b94bf2014-07-02 22:12:47 +0200141 for (int i = 0; i < childCount; i++) {
Dave Mankoffa4d195d2018-11-16 13:33:27 -0500142 ExpandableView child = algorithmState.visibleChildren.get(i);
143 ExpandableViewState childViewState = child.getViewState();
Selim Cinek3107cfa2014-07-22 15:24:29 +0200144
145 // The speed bump can also be gone, so equality needs to be taken when comparing
146 // indices.
Selim Cinekdb167372016-11-17 15:41:17 -0800147 childViewState.belowSpeedBump = i >= belowSpeedBump;
Selim Cinek3d2b94bf2014-07-02 22:12:47 +0200148 }
Selim Cinekdb167372016-11-17 15:41:17 -0800149
150 }
Dave Mankoffa4d195d2018-11-16 13:33:27 -0500151
152 private void updateShelfState(AmbientState ambientState) {
Selim Cinek281c2022016-10-13 19:14:43 -0700153 NotificationShelf shelf = ambientState.getShelf();
Eliot Courtney5d3d2d02018-01-18 15:59:03 +0900154 if (shelf != null) {
Dave Mankoffa4d195d2018-11-16 13:33:27 -0500155 shelf.updateState(ambientState);
Eliot Courtney5d3d2d02018-01-18 15:59:03 +0900156 }
Selim Cinekf54090e2014-06-17 17:24:51 -0700157 }
158
Dave Mankoffa4d195d2018-11-16 13:33:27 -0500159 private void updateClipping(StackScrollAlgorithmState algorithmState,
160 AmbientState ambientState) {
Selim Cinek355652a2016-12-07 13:32:12 -0800161 float drawStart = !ambientState.isOnKeyguard() ? ambientState.getTopPadding()
Selim Cinek2627d722018-01-19 12:16:49 -0800162 + ambientState.getStackTranslation() + ambientState.getExpandAnimationTopChange()
163 : 0;
Selim Cinek8f3f03f2019-07-18 14:19:54 -0700164 float clipStart = 0;
Selim Cinek708a6c12014-05-28 14:16:02 +0200165 int childCount = algorithmState.visibleChildren.size();
Selim Cinek3ff6f502019-07-29 16:33:48 -0700166 boolean firstHeadsUp = true;
Selim Cinek708a6c12014-05-28 14:16:02 +0200167 for (int i = 0; i < childCount; i++) {
168 ExpandableView child = algorithmState.visibleChildren.get(i);
Dave Mankoffa4d195d2018-11-16 13:33:27 -0500169 ExpandableViewState state = child.getViewState();
Selim Cinek9b9d6e12017-11-30 12:29:47 +0100170 if (!child.mustStayOnScreen() || state.headsUpIsVisible) {
Selim Cinek8f3f03f2019-07-18 14:19:54 -0700171 clipStart = Math.max(drawStart, clipStart);
Selim Cinek3776fe02016-02-04 13:32:43 -0800172 }
Selim Cinek587cbf32016-01-19 11:36:18 -0800173 float newYTranslation = state.yTranslation;
174 float newHeight = state.height;
Selim Cinek708a6c12014-05-28 14:16:02 +0200175 float newNotificationEnd = newYTranslation + newHeight;
Mady Mellor53ac1ef2016-06-20 13:11:38 -0700176 boolean isHeadsUp = (child instanceof ExpandableNotificationRow)
177 && ((ExpandableNotificationRow) child).isPinned();
Anthony Chen9fe1ee72017-04-07 13:53:37 -0700178 if (mClipNotificationScrollToTop
Selim Cinek3ff6f502019-07-29 16:33:48 -0700179 && (!state.inShelf || (isHeadsUp && !firstHeadsUp))
Selim Cinek8f3f03f2019-07-18 14:19:54 -0700180 && newYTranslation < clipStart) {
Mady Mellorc128f222016-04-26 11:42:46 -0700181 // The previous view is overlapping on top, clip!
Selim Cinek8f3f03f2019-07-18 14:19:54 -0700182 float overlapAmount = clipStart - newYTranslation;
Mady Mellorc128f222016-04-26 11:42:46 -0700183 state.clipTopAmount = (int) overlapAmount;
Selim Cinekc5baa3e2014-10-29 19:04:19 +0100184 } else {
Mady Mellorc128f222016-04-26 11:42:46 -0700185 state.clipTopAmount = 0;
Selim Cinek9c17b772015-07-07 20:37:09 -0700186 }
Selim Cinek3ff6f502019-07-29 16:33:48 -0700187 if (isHeadsUp) {
188 firstHeadsUp = false;
189 }
Selim Cinek708a6c12014-05-28 14:16:02 +0200190 if (!child.isTransparent()) {
191 // Only update the previous values if we are not transparent,
192 // otherwise we would clip to a transparent view.
Selim Cinek8f3f03f2019-07-18 14:19:54 -0700193 clipStart = Math.max(clipStart, isHeadsUp ? newYTranslation : newNotificationEnd);
Selim Cinek708a6c12014-05-28 14:16:02 +0200194 }
195 }
196 }
197
198 /**
Jorim Jaggiae441282014-08-01 02:45:18 +0200199 * Updates the dimmed, activated and hiding sensitive states of the children.
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200200 */
Jorim Jaggiae441282014-08-01 02:45:18 +0200201 private void updateDimmedActivatedHideSensitive(AmbientState ambientState,
Dave Mankoffa4d195d2018-11-16 13:33:27 -0500202 StackScrollAlgorithmState algorithmState) {
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200203 boolean dimmed = ambientState.isDimmed();
Jorim Jaggiae441282014-08-01 02:45:18 +0200204 boolean hideSensitive = ambientState.isHideSensitive();
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200205 View activatedChild = ambientState.getActivatedChild();
206 int childCount = algorithmState.visibleChildren.size();
207 for (int i = 0; i < childCount; i++) {
Dave Mankoffa4d195d2018-11-16 13:33:27 -0500208 ExpandableView child = algorithmState.visibleChildren.get(i);
209 ExpandableViewState childViewState = child.getViewState();
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200210 childViewState.dimmed = dimmed;
Jorim Jaggiae441282014-08-01 02:45:18 +0200211 childViewState.hideSensitive = hideSensitive;
Selim Cinekb89de4e2014-06-10 10:47:05 +0200212 boolean isActivatedChild = activatedChild == child;
Jorim Jaggi4538cee2014-09-09 15:21:38 +0200213 if (dimmed && isActivatedChild) {
Selim Cinek281c2022016-10-13 19:14:43 -0700214 childViewState.zTranslation += 2.0f * ambientState.getZDistanceBetweenElements();
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200215 }
216 }
Selim Cinekeb973562014-05-02 17:07:49 +0200217 }
218
219 /**
Selim Cinek61633a82016-01-25 15:54:10 -0800220 * Initialize the algorithm state like updating the visible children.
Jorim Jaggid4a57442014-04-10 02:45:55 +0200221 */
Dave Mankoffa4d195d2018-11-16 13:33:27 -0500222 private void initAlgorithmState(ViewGroup hostView, StackScrollAlgorithmState state,
Selim Cinek3776fe02016-02-04 13:32:43 -0800223 AmbientState ambientState) {
Selim Cinek3776fe02016-02-04 13:32:43 -0800224 float bottomOverScroll = ambientState.getOverScrollAmount(false /* onTop */);
225
226 int scrollY = ambientState.getScrollY();
227
228 // Due to the overScroller, the stackscroller can have negative scroll state. This is
229 // already accounted for by the top padding and doesn't need an additional adaption
230 scrollY = Math.max(0, scrollY);
231 state.scrollY = (int) (scrollY + bottomOverScroll);
232
Gus Prevas0fa58d62019-01-11 13:58:40 -0500233 if (ANCHOR_SCROLLING) {
234 state.anchorViewY = (int) (ambientState.getAnchorViewY() - bottomOverScroll);
235 }
236
Selim Cinek3776fe02016-02-04 13:32:43 -0800237 //now init the visible children and update paddings
Jorim Jaggid4a57442014-04-10 02:45:55 +0200238 int childCount = hostView.getChildCount();
239 state.visibleChildren.clear();
240 state.visibleChildren.ensureCapacity(childCount);
Selim Cineka7ed2c12017-01-23 20:47:24 -0800241 state.paddingMap.clear();
Selim Cinekb036ca42015-02-20 15:56:28 +0100242 int notGoneIndex = 0;
Selim Cinek61633a82016-01-25 15:54:10 -0800243 ExpandableView lastView = null;
Selim Cinekc1d9ab22019-05-21 18:08:30 -0700244 int firstHiddenIndex = ambientState.isDozing()
Selim Cinekd96ed402017-07-28 18:19:03 -0700245 ? (ambientState.hasPulsingNotifications() ? 1 : 0)
246 : childCount;
247
248 // The goal here is to fill the padding map, by iterating over how much padding each child
249 // needs. The map is thereby reused, by first filling it with the padding amount and when
250 // iterating over it again, it's filled with the actual resolved value.
251
Jorim Jaggid4a57442014-04-10 02:45:55 +0200252 for (int i = 0; i < childCount; i++) {
Gus Prevas0fa58d62019-01-11 13:58:40 -0500253 if (ANCHOR_SCROLLING) {
254 if (i == ambientState.getAnchorViewIndex()) {
255 state.anchorViewIndex = state.visibleChildren.size();
256 }
257 }
Jorim Jaggibe565df2014-04-28 17:51:23 +0200258 ExpandableView v = (ExpandableView) hostView.getChildAt(i);
Jorim Jaggid4a57442014-04-10 02:45:55 +0200259 if (v.getVisibility() != View.GONE) {
Selim Cinek281c2022016-10-13 19:14:43 -0700260 if (v == ambientState.getShelf()) {
261 continue;
262 }
Selim Cinekd96ed402017-07-28 18:19:03 -0700263 if (i >= firstHiddenIndex) {
264 // we need normal padding now, to be in sync with what the stack calculates
265 lastView = null;
Selim Cinekd96ed402017-07-28 18:19:03 -0700266 }
Dave Mankoffa4d195d2018-11-16 13:33:27 -0500267 notGoneIndex = updateNotGoneIndex(state, notGoneIndex, v);
Selim Cinekd96ed402017-07-28 18:19:03 -0700268 float increasedPadding = v.getIncreasedPaddingAmount();
Selim Cinek42357e02016-02-24 18:48:01 -0800269 if (increasedPadding != 0.0f) {
Selim Cineka7ed2c12017-01-23 20:47:24 -0800270 state.paddingMap.put(v, increasedPadding);
Selim Cinek61633a82016-01-25 15:54:10 -0800271 if (lastView != null) {
Selim Cineka7ed2c12017-01-23 20:47:24 -0800272 Float prevValue = state.paddingMap.get(lastView);
273 float newValue = getPaddingForValue(increasedPadding);
274 if (prevValue != null) {
275 float prevPadding = getPaddingForValue(prevValue);
276 if (increasedPadding > 0) {
277 newValue = NotificationUtils.interpolate(
278 prevPadding,
279 newValue,
280 increasedPadding);
281 } else if (prevValue > 0) {
282 newValue = NotificationUtils.interpolate(
283 newValue,
284 prevPadding,
285 prevValue);
286 }
287 }
288 state.paddingMap.put(lastView, newValue);
Selim Cinek61633a82016-01-25 15:54:10 -0800289 }
Selim Cineka7ed2c12017-01-23 20:47:24 -0800290 } else if (lastView != null) {
Selim Cinekd96ed402017-07-28 18:19:03 -0700291
292 // Let's now resolve the value to an actual padding
Selim Cineka7ed2c12017-01-23 20:47:24 -0800293 float newValue = getPaddingForValue(state.paddingMap.get(lastView));
294 state.paddingMap.put(lastView, newValue);
Selim Cinek61633a82016-01-25 15:54:10 -0800295 }
Selim Cinekb5605e52015-02-20 18:21:41 +0100296 if (v instanceof ExpandableNotificationRow) {
297 ExpandableNotificationRow row = (ExpandableNotificationRow) v;
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700298
299 // handle the notgoneIndex for the children as well
Kevin Han43077f92020-02-28 12:51:53 -0800300 List<ExpandableNotificationRow> children = row.getAttachedChildren();
Selim Cinek83bc7832015-10-22 13:26:54 -0700301 if (row.isSummaryWithChildren() && children != null) {
Selim Cinekb5605e52015-02-20 18:21:41 +0100302 for (ExpandableNotificationRow childRow : children) {
303 if (childRow.getVisibility() != View.GONE) {
Dave Mankoffa4d195d2018-11-16 13:33:27 -0500304 ExpandableViewState childState = childRow.getViewState();
Selim Cinekb5605e52015-02-20 18:21:41 +0100305 childState.notGoneIndex = notGoneIndex;
306 notGoneIndex++;
307 }
308 }
309 }
310 }
Selim Cinek61633a82016-01-25 15:54:10 -0800311 lastView = v;
Jorim Jaggid4a57442014-04-10 02:45:55 +0200312 }
313 }
Selim Cinek2627d722018-01-19 12:16:49 -0800314 ExpandableNotificationRow expandingNotification = ambientState.getExpandingNotification();
315 state.indexOfExpandingNotification = expandingNotification != null
Selim Cinekc25989e2018-02-16 16:42:14 -0800316 ? expandingNotification.isChildInGroup()
Dave Mankoffa4d195d2018-11-16 13:33:27 -0500317 ? state.visibleChildren.indexOf(expandingNotification.getNotificationParent())
318 : state.visibleChildren.indexOf(expandingNotification)
Selim Cinek2627d722018-01-19 12:16:49 -0800319 : -1;
Jorim Jaggid4a57442014-04-10 02:45:55 +0200320 }
321
Selim Cineka7ed2c12017-01-23 20:47:24 -0800322 private float getPaddingForValue(Float increasedPadding) {
323 if (increasedPadding == null) {
324 return mPaddingBetweenElements;
325 } else if (increasedPadding >= 0.0f) {
326 return NotificationUtils.interpolate(
327 mPaddingBetweenElements,
328 mIncreasedPaddingBetweenElements,
329 increasedPadding);
330 } else {
331 return NotificationUtils.interpolate(
332 0,
333 mPaddingBetweenElements,
334 1.0f + increasedPadding);
335 }
336 }
337
Dave Mankoffa4d195d2018-11-16 13:33:27 -0500338 private int updateNotGoneIndex(StackScrollAlgorithmState state, int notGoneIndex,
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700339 ExpandableView v) {
Dave Mankoffa4d195d2018-11-16 13:33:27 -0500340 ExpandableViewState viewState = v.getViewState();
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700341 viewState.notGoneIndex = notGoneIndex;
342 state.visibleChildren.add(v);
343 notGoneIndex++;
344 return notGoneIndex;
345 }
346
Jorim Jaggid4a57442014-04-10 02:45:55 +0200347 /**
Selim Cinek67b22602014-03-10 15:40:16 +0100348 * Determine the positions for the views. This is the main part of the algorithm.
349 *
Selim Cinek67b22602014-03-10 15:40:16 +0100350 * @param algorithmState The state in which the current pass of the algorithm is currently in
Dave Mankoffa4d195d2018-11-16 13:33:27 -0500351 * @param ambientState The current ambient state
Selim Cinek67b22602014-03-10 15:40:16 +0100352 */
Dave Mankoffa4d195d2018-11-16 13:33:27 -0500353 private void updatePositionsForState(StackScrollAlgorithmState algorithmState,
354 AmbientState ambientState) {
Gus Prevas0fa58d62019-01-11 13:58:40 -0500355 if (ANCHOR_SCROLLING) {
356 float currentYPosition = algorithmState.anchorViewY;
357 int childCount = algorithmState.visibleChildren.size();
358 for (int i = algorithmState.anchorViewIndex; i < childCount; i++) {
Gus Prevas0fa58d62019-01-11 13:58:40 -0500359 currentYPosition = updateChild(i, algorithmState, ambientState, currentYPosition,
360 false /* reverse */);
361 }
362 currentYPosition = algorithmState.anchorViewY;
363 for (int i = algorithmState.anchorViewIndex - 1; i >= 0; i--) {
Gus Prevas0fa58d62019-01-11 13:58:40 -0500364 currentYPosition = updateChild(i, algorithmState, ambientState, currentYPosition,
365 true /* reverse */);
366 }
367 } else {
368 // The y coordinate of the current child.
369 float currentYPosition = -algorithmState.scrollY;
370 int childCount = algorithmState.visibleChildren.size();
371 for (int i = 0; i < childCount; i++) {
Gus Prevas0fa58d62019-01-11 13:58:40 -0500372 currentYPosition = updateChild(i, algorithmState, ambientState, currentYPosition,
373 false /* reverse */);
374 }
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700375 }
376 }
377
Gus Prevas0fa58d62019-01-11 13:58:40 -0500378 /**
379 * Populates the {@link ExpandableViewState} for a single child.
380 *
381 * @param i The index of the child in
382 * {@link StackScrollAlgorithmState#visibleChildren}.
383 * @param algorithmState The overall output state of the algorithm.
384 * @param ambientState The input state provided to the algorithm.
385 * @param currentYPosition The Y position of the current pass of the algorithm. For a forward
386 * pass, this should be the top of the child; for a reverse pass, the
387 * bottom of the child.
388 * @param reverse Whether we're laying out children in the reverse direction (Y
389 * positions
390 * decreasing) instead of the forward direction (Y positions
391 * increasing).
392 * @return The Y position after laying out the child. This will be the {@code currentYPosition}
393 * for the next call to this method, after adjusting for any gaps between children.
394 */
Dave Mankoffa4d195d2018-11-16 13:33:27 -0500395 protected float updateChild(
396 int i,
397 StackScrollAlgorithmState algorithmState,
398 AmbientState ambientState,
Gus Prevas0fa58d62019-01-11 13:58:40 -0500399 float currentYPosition,
400 boolean reverse) {
Muyuan Li87798022016-04-07 17:51:25 -0700401 ExpandableView child = algorithmState.visibleChildren.get(i);
Evan Laird25f02752019-08-14 19:25:06 -0400402 ExpandableView previousChild = i > 0 ? algorithmState.visibleChildren.get(i - 1) : null;
Ned Burns9eb06332019-04-23 16:02:12 -0400403 final boolean applyGapHeight =
Evan Laird25f02752019-08-14 19:25:06 -0400404 childNeedsGapHeight(
Selim Cinek54ef7be2020-05-27 19:09:21 -0700405 ambientState.getSectionProvider(), algorithmState.anchorViewIndex, i,
406 child, previousChild);
Dave Mankoffa4d195d2018-11-16 13:33:27 -0500407 ExpandableViewState childViewState = child.getViewState();
Selim Cinekbbcebde2016-11-09 18:28:20 -0800408 childViewState.location = ExpandableViewState.LOCATION_UNKNOWN;
Ned Burns9eb06332019-04-23 16:02:12 -0400409
410 if (applyGapHeight && !reverse) {
411 currentYPosition += mGapHeight;
412 }
413
Muyuan Li87798022016-04-07 17:51:25 -0700414 int paddingAfterChild = getPaddingAfterChild(algorithmState, child);
415 int childHeight = getMaxAllowedChildHeight(child);
Gus Prevas0fa58d62019-01-11 13:58:40 -0500416 if (reverse) {
417 childViewState.yTranslation = currentYPosition - (childHeight + paddingAfterChild);
418 if (currentYPosition <= 0) {
419 childViewState.location = ExpandableViewState.LOCATION_HIDDEN_TOP;
420 }
421 } else {
422 childViewState.yTranslation = currentYPosition;
Gus Prevase2d6f042018-10-17 15:25:30 -0400423 }
Julia Reynoldsed1c9af2018-03-21 15:21:09 -0400424 boolean isFooterView = child instanceof FooterView;
Selim Cinekcde90e52016-12-22 21:01:49 +0100425 boolean isEmptyShadeView = child instanceof EmptyShadeView;
Muyuan Li87798022016-04-07 17:51:25 -0700426
Selim Cinekdb167372016-11-17 15:41:17 -0800427 childViewState.location = ExpandableViewState.LOCATION_MAIN_AREA;
Selim Cinek9b9d6e12017-11-30 12:29:47 +0100428 float inset = ambientState.getTopPadding() + ambientState.getStackTranslation();
Selim Cinekc25989e2018-02-16 16:42:14 -0800429 if (i <= algorithmState.getIndexOfExpandingNotification()) {
Selim Cinek2627d722018-01-19 12:16:49 -0800430 inset += ambientState.getExpandAnimationTopChange();
431 }
Selim Cinek9b9d6e12017-11-30 12:29:47 +0100432 if (child.mustStayOnScreen() && childViewState.yTranslation >= 0) {
433 // Even if we're not scrolled away we're in view and we're also not in the
434 // shelf. We can relax the constraints and let us scroll off the top!
435 float end = childViewState.yTranslation + childViewState.height + inset;
436 childViewState.headsUpIsVisible = end < ambientState.getMaxHeadsUpTranslation();
437 }
Julia Reynoldsed1c9af2018-03-21 15:21:09 -0400438 if (isFooterView) {
Selim Cinekdb167372016-11-17 15:41:17 -0800439 childViewState.yTranslation = Math.min(childViewState.yTranslation,
440 ambientState.getInnerHeight() - childHeight);
Selim Cinekcde90e52016-12-22 21:01:49 +0100441 } else if (isEmptyShadeView) {
442 childViewState.yTranslation = ambientState.getInnerHeight() - childHeight
443 + ambientState.getStackTranslation() * 0.25f;
Muyuan Li87798022016-04-07 17:51:25 -0700444 } else {
Selim Cinek2627d722018-01-19 12:16:49 -0800445 clampPositionToShelf(child, childViewState, ambientState);
Muyuan Li87798022016-04-07 17:51:25 -0700446 }
447
Gus Prevas0fa58d62019-01-11 13:58:40 -0500448 if (reverse) {
449 currentYPosition = childViewState.yTranslation;
Ned Burns9eb06332019-04-23 16:02:12 -0400450 if (applyGapHeight) {
451 currentYPosition -= mGapHeight;
452 }
Gus Prevas0fa58d62019-01-11 13:58:40 -0500453 } else {
454 currentYPosition = childViewState.yTranslation + childHeight + paddingAfterChild;
455 if (currentYPosition <= 0) {
456 childViewState.location = ExpandableViewState.LOCATION_HIDDEN_TOP;
457 }
Muyuan Li87798022016-04-07 17:51:25 -0700458 }
Selim Cinekbbcebde2016-11-09 18:28:20 -0800459 if (childViewState.location == ExpandableViewState.LOCATION_UNKNOWN) {
Muyuan Li87798022016-04-07 17:51:25 -0700460 Log.wtf(LOG_TAG, "Failed to assign location for child " + i);
461 }
462
Selim Cinek9b9d6e12017-11-30 12:29:47 +0100463 childViewState.yTranslation += inset;
Muyuan Li87798022016-04-07 17:51:25 -0700464 return currentYPosition;
465 }
466
Selim Cinek54ef7be2020-05-27 19:09:21 -0700467 /**
468 * Get the gap height needed for before a view
469 *
470 * @param sectionProvider the sectionProvider used to understand the sections
471 * @param anchorViewIndex the anchorView index when anchor scrolling, can be 0 if not
472 * @param visibleIndex the visible index of this view in the list
473 * @param child the child asked about
474 * @param previousChild the child right before it or null if none
475 * @return the size of the gap needed or 0 if none is needed
476 */
477 public float getGapHeightForChild(
478 SectionProvider sectionProvider,
479 int anchorViewIndex,
480 int visibleIndex,
481 View child,
482 View previousChild) {
483
484 if (childNeedsGapHeight(sectionProvider, anchorViewIndex, visibleIndex, child,
485 previousChild)) {
486 return mGapHeight;
487 } else {
488 return 0;
489 }
490 }
491
492 /**
493 * Does a given child need a gap, i.e spacing before a view?
494 *
495 * @param sectionProvider the sectionProvider used to understand the sections
496 * @param anchorViewIndex the anchorView index when anchor scrolling, can be 0 if not
497 * @param visibleIndex the visible index of this view in the list
498 * @param child the child asked about
499 * @param previousChild the child right before it or null if none
500 * @return if the child needs a gap height
501 */
Ned Burns9eb06332019-04-23 16:02:12 -0400502 private boolean childNeedsGapHeight(
503 SectionProvider sectionProvider,
Selim Cinek54ef7be2020-05-27 19:09:21 -0700504 int anchorViewIndex,
Ned Burns9eb06332019-04-23 16:02:12 -0400505 int visibleIndex,
Evan Laird25f02752019-08-14 19:25:06 -0400506 View child,
507 View previousChild) {
508
509 boolean needsGapHeight = sectionProvider.beginsSection(child, previousChild)
510 && visibleIndex > 0;
Ned Burns9eb06332019-04-23 16:02:12 -0400511 if (ANCHOR_SCROLLING) {
Selim Cinek54ef7be2020-05-27 19:09:21 -0700512 needsGapHeight &= visibleIndex != anchorViewIndex;
Ned Burns9eb06332019-04-23 16:02:12 -0400513 }
514 return needsGapHeight;
515 }
516
Muyuan Li87798022016-04-07 17:51:25 -0700517 protected int getPaddingAfterChild(StackScrollAlgorithmState algorithmState,
Selim Cinek61633a82016-01-25 15:54:10 -0800518 ExpandableView child) {
Selim Cinek281c2022016-10-13 19:14:43 -0700519 return algorithmState.getPaddingAfterChild(child);
Selim Cinek61633a82016-01-25 15:54:10 -0800520 }
521
Selim Cinek5040f2e2019-02-14 18:22:42 -0800522 private void updatePulsingStates(StackScrollAlgorithmState algorithmState,
523 AmbientState ambientState) {
524 int childCount = algorithmState.visibleChildren.size();
525 for (int i = 0; i < childCount; i++) {
526 View child = algorithmState.visibleChildren.get(i);
527 if (!(child instanceof ExpandableNotificationRow)) {
528 continue;
529 }
530 ExpandableNotificationRow row = (ExpandableNotificationRow) child;
Selim Cinekc3fec682019-06-06 18:11:07 -0700531 if (!row.showingPulsing() || (i == 0 && ambientState.isPulseExpanding())) {
Selim Cinek5040f2e2019-02-14 18:22:42 -0800532 continue;
533 }
534 ExpandableViewState viewState = row.getViewState();
535 viewState.hidden = false;
536 }
537 }
538
Dave Mankoffa4d195d2018-11-16 13:33:27 -0500539 private void updateHeadsUpStates(StackScrollAlgorithmState algorithmState,
540 AmbientState ambientState) {
Selim Cineka4baaa32015-04-20 14:27:54 -0700541 int childCount = algorithmState.visibleChildren.size();
542 ExpandableNotificationRow topHeadsUpEntry = null;
543 for (int i = 0; i < childCount; i++) {
544 View child = algorithmState.visibleChildren.get(i);
545 if (!(child instanceof ExpandableNotificationRow)) {
Selim Cineka3f6f882019-07-04 19:20:32 -0700546 continue;
Selim Cineka4baaa32015-04-20 14:27:54 -0700547 }
548 ExpandableNotificationRow row = (ExpandableNotificationRow) child;
549 if (!row.isHeadsUp()) {
Selim Cineka3f6f882019-07-04 19:20:32 -0700550 continue;
Selim Cineka4baaa32015-04-20 14:27:54 -0700551 }
Dave Mankoffa4d195d2018-11-16 13:33:27 -0500552 ExpandableViewState childState = row.getViewState();
Selim Cinek9b9d6e12017-11-30 12:29:47 +0100553 if (topHeadsUpEntry == null && row.mustStayOnScreen() && !childState.headsUpIsVisible) {
Selim Cinek3776fe02016-02-04 13:32:43 -0800554 topHeadsUpEntry = row;
Selim Cinekbbcebde2016-11-09 18:28:20 -0800555 childState.location = ExpandableViewState.LOCATION_FIRST_HUN;
Selim Cinek3776fe02016-02-04 13:32:43 -0800556 }
Selim Cinek1f3f5442015-04-10 17:54:46 -0700557 boolean isTopEntry = topHeadsUpEntry == row;
Selim Cinek3776fe02016-02-04 13:32:43 -0800558 float unmodifiedEndLocation = childState.yTranslation + childState.height;
Selim Cinek131c1e22015-05-11 19:04:49 -0700559 if (mIsExpanded) {
Selim Cinekc3fec682019-06-06 18:11:07 -0700560 if (row.mustStayOnScreen() && !childState.headsUpIsVisible
561 && !row.showingPulsing()) {
Selim Cinek9b9d6e12017-11-30 12:29:47 +0100562 // Ensure that the heads up is always visible even when scrolled off
563 clampHunToTop(ambientState, row, childState);
Selim Cinek459aee32019-02-20 11:18:56 -0800564 if (i == 0 && row.isAboveShelf()) {
Selim Cinek9b9d6e12017-11-30 12:29:47 +0100565 // the first hun can't get off screen.
566 clampHunToMaxTranslation(ambientState, row, childState);
567 childState.hidden = false;
568 }
Selim Cinekd127d792016-11-01 19:11:41 -0700569 }
Selim Cinek131c1e22015-05-11 19:04:49 -0700570 }
Selim Cinek684a4422015-04-15 16:18:39 -0700571 if (row.isPinned()) {
Selim Cinekaa9db1f2018-02-27 17:35:47 -0800572 childState.yTranslation = Math.max(childState.yTranslation, mHeadsUpInset);
Selim Cinek31aada42015-12-18 17:51:15 -0800573 childState.height = Math.max(row.getIntrinsicHeight(), childState.height);
Selim Cinekcafa87f2016-10-26 17:00:17 -0700574 childState.hidden = false;
Dave Mankoffa4d195d2018-11-16 13:33:27 -0500575 ExpandableViewState topState =
576 topHeadsUpEntry == null ? null : topHeadsUpEntry.getViewState();
Selim Cinek61cfd4b2017-12-08 12:42:36 -0800577 if (topState != null && !isTopEntry && (!mIsExpanded
Selim Cineke3c6e462019-06-24 19:37:06 -0700578 || unmodifiedEndLocation > topState.yTranslation + topState.height)) {
Selim Cinek684a4422015-04-15 16:18:39 -0700579 // Ensure that a headsUp doesn't vertically extend further than the heads-up at
580 // the top most z-position
Selim Cinek31aada42015-12-18 17:51:15 -0800581 childState.height = row.getIntrinsicHeight();
Selim Cineke3c6e462019-06-24 19:37:06 -0700582 childState.yTranslation = Math.min(topState.yTranslation + topState.height
583 - childState.height, childState.yTranslation);
Selim Cinek1f3f5442015-04-10 17:54:46 -0700584 }
felkachang529bfe62018-07-04 12:51:44 +0800585
586 // heads up notification show and this row is the top entry of heads up
587 // notifications. i.e. this row should be the only one row that has input field
588 // To check if the row need to do translation according to scroll Y
589 // heads up show full of row's content and any scroll y indicate that the
590 // translationY need to move up the HUN.
Gus Prevas0fa58d62019-01-11 13:58:40 -0500591 // TODO: fix this check for anchor scrolling.
felkachang529bfe62018-07-04 12:51:44 +0800592 if (!mIsExpanded && isTopEntry && ambientState.getScrollY() > 0) {
593 childState.yTranslation -= ambientState.getScrollY();
594 }
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700595 }
Selim Cinekcafa87f2016-10-26 17:00:17 -0700596 if (row.isHeadsUpAnimatingAway()) {
597 childState.hidden = false;
598 }
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700599 }
Selim Cinek67b22602014-03-10 15:40:16 +0100600 }
601
Selim Cinek3776fe02016-02-04 13:32:43 -0800602 private void clampHunToTop(AmbientState ambientState, ExpandableNotificationRow row,
Selim Cinekbbcebde2016-11-09 18:28:20 -0800603 ExpandableViewState childState) {
Selim Cinek3776fe02016-02-04 13:32:43 -0800604 float newTranslation = Math.max(ambientState.getTopPadding()
605 + ambientState.getStackTranslation(), childState.yTranslation);
606 childState.height = (int) Math.max(childState.height - (newTranslation
Selim Cinek567e8452016-03-24 10:54:56 -0700607 - childState.yTranslation), row.getCollapsedHeight());
Selim Cinek3776fe02016-02-04 13:32:43 -0800608 childState.yTranslation = newTranslation;
609 }
610
611 private void clampHunToMaxTranslation(AmbientState ambientState, ExpandableNotificationRow row,
Selim Cinekbbcebde2016-11-09 18:28:20 -0800612 ExpandableViewState childState) {
Selim Cinek3776fe02016-02-04 13:32:43 -0800613 float newTranslation;
Selim Cinek7e0f9482017-05-22 20:00:56 -0700614 float maxHeadsUpTranslation = ambientState.getMaxHeadsUpTranslation();
615 float maxShelfPosition = ambientState.getInnerHeight() + ambientState.getTopPadding()
616 + ambientState.getStackTranslation();
617 maxHeadsUpTranslation = Math.min(maxHeadsUpTranslation, maxShelfPosition);
618 float bottomPosition = maxHeadsUpTranslation - row.getCollapsedHeight();
Selim Cinek3776fe02016-02-04 13:32:43 -0800619 newTranslation = Math.min(childState.yTranslation, bottomPosition);
Selim Cinek7e0f9482017-05-22 20:00:56 -0700620 childState.height = (int) Math.min(childState.height, maxHeadsUpTranslation
621 - newTranslation);
Selim Cinek3776fe02016-02-04 13:32:43 -0800622 childState.yTranslation = newTranslation;
Selim Cinek343e6e22014-04-11 21:23:30 +0200623 }
624
625 /**
Selim Cinek281c2022016-10-13 19:14:43 -0700626 * Clamp the height of the child down such that its end is at most on the beginning of
627 * the shelf.
628 *
Selim Cinek281c2022016-10-13 19:14:43 -0700629 * @param childViewState the view state of the child
Dave Mankoffa4d195d2018-11-16 13:33:27 -0500630 * @param ambientState the ambient state
Selim Cinek281c2022016-10-13 19:14:43 -0700631 */
Selim Cinek2627d722018-01-19 12:16:49 -0800632 private void clampPositionToShelf(ExpandableView child,
633 ExpandableViewState childViewState,
Selim Cinek281c2022016-10-13 19:14:43 -0700634 AmbientState ambientState) {
Eliot Courtney5d3d2d02018-01-18 15:59:03 +0900635 if (ambientState.getShelf() == null) {
636 return;
637 }
638
Selim Cineka686b2c2016-10-26 13:58:27 -0700639 int shelfStart = ambientState.getInnerHeight()
640 - ambientState.getShelf().getIntrinsicHeight();
shawnlin5be1f7c2018-05-21 20:50:54 +0800641 if (ambientState.isAppearing() && !child.isAboveShelf()) {
642 // Don't show none heads-up notifications while in appearing phase.
643 childViewState.yTranslation = Math.max(childViewState.yTranslation, shelfStart);
644 }
Selim Cinek281c2022016-10-13 19:14:43 -0700645 childViewState.yTranslation = Math.min(childViewState.yTranslation, shelfStart);
Selim Cinekc383fd02016-10-21 15:31:26 -0700646 if (childViewState.yTranslation >= shelfStart) {
Selim Cinekc25989e2018-02-16 16:42:14 -0800647 childViewState.hidden = !child.isExpandAnimationRunning() && !child.hasExpandingChild();
Selim Cinekeccb5de2016-10-28 15:04:05 -0700648 childViewState.inShelf = true;
Selim Cinek9b9d6e12017-11-30 12:29:47 +0100649 childViewState.headsUpIsVisible = false;
Selim Cinekc383fd02016-10-21 15:31:26 -0700650 }
Selim Cinek281c2022016-10-13 19:14:43 -0700651 }
652
Muyuan Li87798022016-04-07 17:51:25 -0700653 protected int getMaxAllowedChildHeight(View child) {
Selim Cinek31aada42015-12-18 17:51:15 -0800654 if (child instanceof ExpandableView) {
Jorim Jaggibe565df2014-04-28 17:51:23 +0200655 ExpandableView expandableView = (ExpandableView) child;
Selim Cinek8d5727f2015-04-28 19:17:32 -0700656 return expandableView.getIntrinsicHeight();
Selim Cinek1685e632014-04-08 02:27:49 +0200657 }
Dave Mankoffa4d195d2018-11-16 13:33:27 -0500658 return child == null ? mCollapsedSize : child.getHeight();
Selim Cinek1685e632014-04-08 02:27:49 +0200659 }
660
Selim Cinek67b22602014-03-10 15:40:16 +0100661 /**
Selim Cinek67b22602014-03-10 15:40:16 +0100662 * Calculate the Z positions for all children based on the number of items in both stacks and
663 * save it in the resultState
Dave Mankoffa4d195d2018-11-16 13:33:27 -0500664 *
Selim Cinek67b22602014-03-10 15:40:16 +0100665 * @param algorithmState The state in which the current pass of the algorithm is currently in
Dave Mankoffa4d195d2018-11-16 13:33:27 -0500666 * @param ambientState The ambient state of the algorithm
Selim Cinek67b22602014-03-10 15:40:16 +0100667 */
Dave Mankoffa4d195d2018-11-16 13:33:27 -0500668 private void updateZValuesForState(StackScrollAlgorithmState algorithmState,
669 AmbientState ambientState) {
Jorim Jaggid4a57442014-04-10 02:45:55 +0200670 int childCount = algorithmState.visibleChildren.size();
Selim Cinek33223572016-02-19 19:32:22 -0800671 float childrenOnTop = 0.0f;
Selim Cinek3776fe02016-02-04 13:32:43 -0800672 for (int i = childCount - 1; i >= 0; i--) {
Selim Cinekdaab6f52017-04-06 16:46:34 -0700673 childrenOnTop = updateChildZValue(i, childrenOnTop,
Dave Mankoffa4d195d2018-11-16 13:33:27 -0500674 algorithmState, ambientState);
Muyuan Li87798022016-04-07 17:51:25 -0700675 }
676 }
677
Selim Cinekdaab6f52017-04-06 16:46:34 -0700678 protected float updateChildZValue(int i, float childrenOnTop,
Dave Mankoffa4d195d2018-11-16 13:33:27 -0500679 StackScrollAlgorithmState algorithmState,
Muyuan Li87798022016-04-07 17:51:25 -0700680 AmbientState ambientState) {
681 ExpandableView child = algorithmState.visibleChildren.get(i);
Dave Mankoffa4d195d2018-11-16 13:33:27 -0500682 ExpandableViewState childViewState = child.getViewState();
Selim Cinek281c2022016-10-13 19:14:43 -0700683 int zDistanceBetweenElements = ambientState.getZDistanceBetweenElements();
684 float baseZ = ambientState.getBaseZHeight();
Selim Cinek9b9d6e12017-11-30 12:29:47 +0100685 if (child.mustStayOnScreen() && !childViewState.headsUpIsVisible
686 && !ambientState.isDozingAndNotPulsing(child)
Muyuan Li87798022016-04-07 17:51:25 -0700687 && childViewState.yTranslation < ambientState.getTopPadding()
688 + ambientState.getStackTranslation()) {
689 if (childrenOnTop != 0.0f) {
690 childrenOnTop++;
691 } else {
692 float overlap = ambientState.getTopPadding()
693 + ambientState.getStackTranslation() - childViewState.yTranslation;
694 childrenOnTop += Math.min(1.0f, overlap / childViewState.height);
695 }
Selim Cinek281c2022016-10-13 19:14:43 -0700696 childViewState.zTranslation = baseZ
697 + childrenOnTop * zDistanceBetweenElements;
Selim Cinekc3fec682019-06-06 18:11:07 -0700698 } else if (i == 0 && (child.isAboveShelf() || child.showingPulsing())) {
Selim Cinekd127d792016-11-01 19:11:41 -0700699 // In case this is a new view that has never been measured before, we don't want to
700 // elevate if we are currently expanded more then the notification
Eliot Courtney5d3d2d02018-01-18 15:59:03 +0900701 int shelfHeight = ambientState.getShelf() == null ? 0 :
702 ambientState.getShelf().getIntrinsicHeight();
Selim Cinekd127d792016-11-01 19:11:41 -0700703 float shelfStart = ambientState.getInnerHeight()
704 - shelfHeight + ambientState.getTopPadding()
705 + ambientState.getStackTranslation();
706 float notificationEnd = childViewState.yTranslation + child.getPinnedHeadsUpHeight()
707 + mPaddingBetweenElements;
708 if (shelfStart > notificationEnd) {
709 childViewState.zTranslation = baseZ;
710 } else {
711 float factor = (notificationEnd - shelfStart) / shelfHeight;
712 factor = Math.min(factor, 1.0f);
713 childViewState.zTranslation = baseZ + factor * zDistanceBetweenElements;
714 }
Muyuan Li87798022016-04-07 17:51:25 -0700715 } else {
Selim Cinek281c2022016-10-13 19:14:43 -0700716 childViewState.zTranslation = baseZ;
Selim Cinek67b22602014-03-10 15:40:16 +0100717 }
Selim Cinek99e9adf2018-03-15 09:17:47 -0700718
719 // We need to scrim the notification more from its surrounding content when we are pinned,
720 // and we therefore elevate it higher.
721 // We can use the headerVisibleAmount for this, since the value nicely goes from 0 to 1 when
722 // expanding after which we have a normal elevation again.
723 childViewState.zTranslation += (1.0f - child.getHeaderVisibleAmount())
724 * mPinnedZTranslationExtra;
Selim Cinekdaab6f52017-04-06 16:46:34 -0700725 return childrenOnTop;
Selim Cinek67b22602014-03-10 15:40:16 +0100726 }
727
Selim Cinek1685e632014-04-08 02:27:49 +0200728 public void setIsExpanded(boolean isExpanded) {
729 this.mIsExpanded = isExpanded;
730 }
731
Selim Cinek281c2022016-10-13 19:14:43 -0700732 public class StackScrollAlgorithmState {
Selim Cinek67b22602014-03-10 15:40:16 +0100733
734 /**
Gus Prevas0fa58d62019-01-11 13:58:40 -0500735 * The scroll position of the algorithm (absolute scrolling).
Selim Cinek67b22602014-03-10 15:40:16 +0100736 */
737 public int scrollY;
738
Gus Prevas0fa58d62019-01-11 13:58:40 -0500739 /** The index of the anchor view (anchor scrolling). */
740 public int anchorViewIndex;
741
742 /**
743 * The Y position, relative to the top of the screen, of the anchor view (anchor scrolling).
744 */
745 public int anchorViewY;
746
Selim Cinek67b22602014-03-10 15:40:16 +0100747 /**
Jorim Jaggid4a57442014-04-10 02:45:55 +0200748 * The children from the host view which are not gone.
749 */
Jorim Jaggibe565df2014-04-28 17:51:23 +0200750 public final ArrayList<ExpandableView> visibleChildren = new ArrayList<ExpandableView>();
Selim Cinek61633a82016-01-25 15:54:10 -0800751
752 /**
Selim Cineka7ed2c12017-01-23 20:47:24 -0800753 * The padding after each child measured in pixels.
Selim Cinek61633a82016-01-25 15:54:10 -0800754 */
Selim Cineka7ed2c12017-01-23 20:47:24 -0800755 public final HashMap<ExpandableView, Float> paddingMap = new HashMap<>();
Selim Cinek2627d722018-01-19 12:16:49 -0800756 private int indexOfExpandingNotification;
Selim Cinek281c2022016-10-13 19:14:43 -0700757
758 public int getPaddingAfterChild(ExpandableView child) {
Selim Cineka7ed2c12017-01-23 20:47:24 -0800759 Float padding = paddingMap.get(child);
760 if (padding == null) {
761 // Should only happen for the last view
762 return mPaddingBetweenElements;
763 }
764 return (int) padding.floatValue();
Selim Cinek281c2022016-10-13 19:14:43 -0700765 }
Selim Cinek2627d722018-01-19 12:16:49 -0800766
767 public int getIndexOfExpandingNotification() {
768 return indexOfExpandingNotification;
769 }
Selim Cinek67b22602014-03-10 15:40:16 +0100770 }
771
Ned Burns9eb06332019-04-23 16:02:12 -0400772 /**
773 * Interface for telling the SSA when a new notification section begins (so it can add in
774 * appropriate margins).
775 */
776 public interface SectionProvider {
777 /**
778 * True if this view starts a new "section" of notifications, such as the gentle
779 * notifications section. False if sections are not enabled.
780 */
Evan Laird25f02752019-08-14 19:25:06 -0400781 boolean beginsSection(@NonNull View view, @Nullable View previous);
Ned Burns9eb06332019-04-23 16:02:12 -0400782 }
Selim Cinek67b22602014-03-10 15:40:16 +0100783}