blob: 0d50f5a9eef797dcba60d0b4c52178e9ccc78e29 [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 Cinekcde90e52016-12-22 21:01:49 +010025import com.android.systemui.statusbar.EmptyShadeView;
Selim Cinek1685e632014-04-08 02:27:49 +020026import com.android.systemui.statusbar.ExpandableNotificationRow;
Jorim Jaggibe565df2014-04-28 17:51:23 +020027import com.android.systemui.statusbar.ExpandableView;
Julia Reynoldsed1c9af2018-03-21 15:21:09 -040028import com.android.systemui.statusbar.FooterView;
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 Cinekaa9db1f2018-02-27 17:35:47 -080053 private float mHeadsUpInset;
Selim Cinek99e9adf2018-03-15 09:17:47 -070054 private int mPinnedZTranslationExtra;
Selim Cinek67b22602014-03-10 15:40:16 +010055
56 public StackScrollAlgorithm(Context context) {
Selim Cinekaf0dc312015-12-15 17:01:44 -080057 initView(context);
Selim Cinek34c0a8d2014-05-12 00:01:43 +020058 }
59
Selim Cinekaf0dc312015-12-15 17:01:44 -080060 public void initView(Context context) {
61 initConstants(context);
Selim Cinek34c0a8d2014-05-12 00:01:43 +020062 }
63
Selim Cinek67b22602014-03-10 15:40:16 +010064 private void initConstants(Context context) {
Anthony Chen9fe1ee72017-04-07 13:53:37 -070065 Resources res = context.getResources();
66 mPaddingBetweenElements = res.getDimensionPixelSize(
Selim Cinekdb167372016-11-17 15:41:17 -080067 R.dimen.notification_divider_height);
Anthony Chen9fe1ee72017-04-07 13:53:37 -070068 mIncreasedPaddingBetweenElements =
69 res.getDimensionPixelSize(R.dimen.notification_divider_height_increased);
70 mCollapsedSize = res.getDimensionPixelSize(R.dimen.notification_min_height);
71 mStatusBarHeight = res.getDimensionPixelSize(R.dimen.status_bar_height);
72 mClipNotificationScrollToTop = res.getBoolean(R.bool.config_clipNotificationScrollToTop);
Selim Cinekaa9db1f2018-02-27 17:35:47 -080073 mHeadsUpInset = mStatusBarHeight + res.getDimensionPixelSize(
74 R.dimen.heads_up_status_bar_padding);
Selim Cinek99e9adf2018-03-15 09:17:47 -070075 mPinnedZTranslationExtra = res.getDimensionPixelSize(
76 R.dimen.heads_up_pinned_elevation);
Jorim Jaggid7c1fae2014-08-13 18:27:47 +020077 }
Selim Cinek67b22602014-03-10 15:40:16 +010078
Jorim Jaggid552d9d2014-05-07 19:41:13 +020079 public void getStackScrollState(AmbientState ambientState, StackScrollState resultState) {
Selim Cinek67b22602014-03-10 15:40:16 +010080 // The state of the local variables are saved in an algorithmState to easily subdivide it
81 // into multiple phases.
82 StackScrollAlgorithmState algorithmState = mTempAlgorithmState;
83
84 // First we reset the view states to their default values.
85 resultState.resetViewStates();
86
Selim Cinek3776fe02016-02-04 13:32:43 -080087 initAlgorithmState(resultState, algorithmState, ambientState);
Selim Cinek1408eb52014-06-02 14:45:38 +020088
Selim Cinekb8f09cf2015-03-16 17:09:28 -070089 updatePositionsForState(resultState, algorithmState, ambientState);
Selim Cinek67b22602014-03-10 15:40:16 +010090
Selim Cinek3776fe02016-02-04 13:32:43 -080091 updateZValuesForState(resultState, algorithmState, ambientState);
92
93 updateHeadsUpStates(resultState, algorithmState, ambientState);
Selim Cinekeb973562014-05-02 17:07:49 +020094
Jorim Jaggid552d9d2014-05-07 19:41:13 +020095 handleDraggedViews(ambientState, resultState, algorithmState);
Jorim Jaggiae441282014-08-01 02:45:18 +020096 updateDimmedActivatedHideSensitive(ambientState, resultState, algorithmState);
Selim Cineka59ecc32015-04-07 10:51:49 -070097 updateClipping(resultState, algorithmState, ambientState);
Selim Cinekdb167372016-11-17 15:41:17 -080098 updateSpeedBumpState(resultState, algorithmState, ambientState);
99 updateShelfState(resultState, ambientState);
Selim Cinekc25989e2018-02-16 16:42:14 -0800100 getNotificationChildrenStates(resultState, algorithmState, ambientState);
Selim Cinekb5605e52015-02-20 18:21:41 +0100101 }
102
103 private void getNotificationChildrenStates(StackScrollState resultState,
Selim Cinekc25989e2018-02-16 16:42:14 -0800104 StackScrollAlgorithmState algorithmState,
105 AmbientState ambientState) {
Selim Cinekb5605e52015-02-20 18:21:41 +0100106 int childCount = algorithmState.visibleChildren.size();
107 for (int i = 0; i < childCount; i++) {
108 ExpandableView v = algorithmState.visibleChildren.get(i);
109 if (v instanceof ExpandableNotificationRow) {
110 ExpandableNotificationRow row = (ExpandableNotificationRow) v;
Selim Cinekc25989e2018-02-16 16:42:14 -0800111 row.getChildrenStates(resultState, ambientState);
Selim Cinekb5605e52015-02-20 18:21:41 +0100112 }
113 }
Selim Cinek3d2b94bf2014-07-02 22:12:47 +0200114 }
115
Selim Cinekdb167372016-11-17 15:41:17 -0800116 private void updateSpeedBumpState(StackScrollState resultState,
Selim Cinek281c2022016-10-13 19:14:43 -0700117 StackScrollAlgorithmState algorithmState, AmbientState ambientState) {
Selim Cinek3d2b94bf2014-07-02 22:12:47 +0200118 int childCount = algorithmState.visibleChildren.size();
Selim Cinekdb167372016-11-17 15:41:17 -0800119 int belowSpeedBump = ambientState.getSpeedBumpIndex();
Selim Cinek3d2b94bf2014-07-02 22:12:47 +0200120 for (int i = 0; i < childCount; i++) {
121 View child = algorithmState.visibleChildren.get(i);
Selim Cinekbbcebde2016-11-09 18:28:20 -0800122 ExpandableViewState childViewState = resultState.getViewStateForView(child);
Selim Cinek3107cfa2014-07-22 15:24:29 +0200123
124 // The speed bump can also be gone, so equality needs to be taken when comparing
125 // indices.
Selim Cinekdb167372016-11-17 15:41:17 -0800126 childViewState.belowSpeedBump = i >= belowSpeedBump;
Selim Cinek3d2b94bf2014-07-02 22:12:47 +0200127 }
Selim Cinekdb167372016-11-17 15:41:17 -0800128
129 }
130 private void updateShelfState(StackScrollState resultState, AmbientState ambientState) {
Selim Cinek281c2022016-10-13 19:14:43 -0700131 NotificationShelf shelf = ambientState.getShelf();
Eliot Courtney5d3d2d02018-01-18 15:59:03 +0900132 if (shelf != null) {
133 shelf.updateState(resultState, ambientState);
134 }
Selim Cinekf54090e2014-06-17 17:24:51 -0700135 }
136
Selim Cinek708a6c12014-05-28 14:16:02 +0200137 private void updateClipping(StackScrollState resultState,
Selim Cineka59ecc32015-04-07 10:51:49 -0700138 StackScrollAlgorithmState algorithmState, AmbientState ambientState) {
Selim Cinek355652a2016-12-07 13:32:12 -0800139 float drawStart = !ambientState.isOnKeyguard() ? ambientState.getTopPadding()
Selim Cinek2627d722018-01-19 12:16:49 -0800140 + ambientState.getStackTranslation() + ambientState.getExpandAnimationTopChange()
141 : 0;
Selim Cinek708a6c12014-05-28 14:16:02 +0200142 float previousNotificationEnd = 0;
143 float previousNotificationStart = 0;
Selim Cinek708a6c12014-05-28 14:16:02 +0200144 int childCount = algorithmState.visibleChildren.size();
145 for (int i = 0; i < childCount; i++) {
146 ExpandableView child = algorithmState.visibleChildren.get(i);
Selim Cinekbbcebde2016-11-09 18:28:20 -0800147 ExpandableViewState state = resultState.getViewStateForView(child);
Selim Cinek9b9d6e12017-11-30 12:29:47 +0100148 if (!child.mustStayOnScreen() || state.headsUpIsVisible) {
Selim Cinek3776fe02016-02-04 13:32:43 -0800149 previousNotificationEnd = Math.max(drawStart, previousNotificationEnd);
150 previousNotificationStart = Math.max(drawStart, previousNotificationStart);
151 }
Selim Cinek587cbf32016-01-19 11:36:18 -0800152 float newYTranslation = state.yTranslation;
153 float newHeight = state.height;
Selim Cinek708a6c12014-05-28 14:16:02 +0200154 float newNotificationEnd = newYTranslation + newHeight;
Mady Mellor53ac1ef2016-06-20 13:11:38 -0700155 boolean isHeadsUp = (child instanceof ExpandableNotificationRow)
156 && ((ExpandableNotificationRow) child).isPinned();
Anthony Chen9fe1ee72017-04-07 13:53:37 -0700157 if (mClipNotificationScrollToTop
158 && !state.inShelf && newYTranslation < previousNotificationEnd
Jorim Jaggi0fdf5742016-06-27 11:50:58 -0700159 && (!isHeadsUp || ambientState.isShadeExpanded())) {
Mady Mellorc128f222016-04-26 11:42:46 -0700160 // The previous view is overlapping on top, clip!
161 float overlapAmount = previousNotificationEnd - newYTranslation;
162 state.clipTopAmount = (int) overlapAmount;
Selim Cinekc5baa3e2014-10-29 19:04:19 +0100163 } else {
Mady Mellorc128f222016-04-26 11:42:46 -0700164 state.clipTopAmount = 0;
Selim Cinek9c17b772015-07-07 20:37:09 -0700165 }
166
Selim Cinek708a6c12014-05-28 14:16:02 +0200167 if (!child.isTransparent()) {
168 // Only update the previous values if we are not transparent,
169 // otherwise we would clip to a transparent view.
Mady Mellorc128f222016-04-26 11:42:46 -0700170 previousNotificationEnd = newNotificationEnd;
171 previousNotificationStart = newYTranslation;
Selim Cinek708a6c12014-05-28 14:16:02 +0200172 }
173 }
174 }
175
Selim Cinek9c17b772015-07-07 20:37:09 -0700176 public static boolean canChildBeDismissed(View v) {
Selim Cinek9e624e72016-07-20 13:46:49 -0700177 if (!(v instanceof ExpandableNotificationRow)) {
178 return false;
Selim Cinek38d429f2016-06-03 11:46:56 -0700179 }
Selim Cinek9e624e72016-07-20 13:46:49 -0700180 ExpandableNotificationRow row = (ExpandableNotificationRow) v;
181 if (row.areGutsExposed()) {
182 return false;
183 }
184 return row.canViewBeDismissed();
Selim Cinek9c17b772015-07-07 20:37:09 -0700185 }
186
Selim Cinek708a6c12014-05-28 14:16:02 +0200187 /**
Jorim Jaggiae441282014-08-01 02:45:18 +0200188 * Updates the dimmed, activated and hiding sensitive states of the children.
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200189 */
Jorim Jaggiae441282014-08-01 02:45:18 +0200190 private void updateDimmedActivatedHideSensitive(AmbientState ambientState,
191 StackScrollState resultState, StackScrollAlgorithmState algorithmState) {
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200192 boolean dimmed = ambientState.isDimmed();
Lucas Dupin16cfe452018-02-08 13:14:50 -0800193 boolean dark = ambientState.isFullyDark();
Jorim Jaggiae441282014-08-01 02:45:18 +0200194 boolean hideSensitive = ambientState.isHideSensitive();
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200195 View activatedChild = ambientState.getActivatedChild();
196 int childCount = algorithmState.visibleChildren.size();
197 for (int i = 0; i < childCount; i++) {
198 View child = algorithmState.visibleChildren.get(i);
Selim Cinekbbcebde2016-11-09 18:28:20 -0800199 ExpandableViewState childViewState = resultState.getViewStateForView(child);
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200200 childViewState.dimmed = dimmed;
John Spurlockbf370992014-06-17 13:58:31 -0400201 childViewState.dark = dark;
Jorim Jaggiae441282014-08-01 02:45:18 +0200202 childViewState.hideSensitive = hideSensitive;
Selim Cinekb89de4e2014-06-10 10:47:05 +0200203 boolean isActivatedChild = activatedChild == child;
Jorim Jaggi4538cee2014-09-09 15:21:38 +0200204 if (dimmed && isActivatedChild) {
Selim Cinek281c2022016-10-13 19:14:43 -0700205 childViewState.zTranslation += 2.0f * ambientState.getZDistanceBetweenElements();
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200206 }
207 }
Selim Cinekeb973562014-05-02 17:07:49 +0200208 }
209
210 /**
211 * Handle the special state when views are being dragged
212 */
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200213 private void handleDraggedViews(AmbientState ambientState, StackScrollState resultState,
Selim Cinekeb973562014-05-02 17:07:49 +0200214 StackScrollAlgorithmState algorithmState) {
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200215 ArrayList<View> draggedViews = ambientState.getDraggedViews();
216 for (View draggedView : draggedViews) {
Selim Cinekeb973562014-05-02 17:07:49 +0200217 int childIndex = algorithmState.visibleChildren.indexOf(draggedView);
218 if (childIndex >= 0 && childIndex < algorithmState.visibleChildren.size() - 1) {
219 View nextChild = algorithmState.visibleChildren.get(childIndex + 1);
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200220 if (!draggedViews.contains(nextChild)) {
Selim Cinekeb973562014-05-02 17:07:49 +0200221 // only if the view is not dragged itself we modify its state to be fully
222 // visible
Selim Cinekbbcebde2016-11-09 18:28:20 -0800223 ExpandableViewState viewState = resultState.getViewStateForView(
Selim Cinekeb973562014-05-02 17:07:49 +0200224 nextChild);
225 // The child below the dragged one must be fully visible
Selim Cinek131c1e22015-05-11 19:04:49 -0700226 if (ambientState.isShadeExpanded()) {
Selim Cinek277a8aa2016-01-22 12:12:37 -0800227 viewState.shadowAlpha = 1;
228 viewState.hidden = false;
Selim Cineka59ecc32015-04-07 10:51:49 -0700229 }
Selim Cinekeb973562014-05-02 17:07:49 +0200230 }
231
232 // Lets set the alpha to the one it currently has, as its currently being dragged
Selim Cinekbbcebde2016-11-09 18:28:20 -0800233 ExpandableViewState viewState = resultState.getViewStateForView(draggedView);
Selim Cinekeb973562014-05-02 17:07:49 +0200234 // The dragged child should keep the set alpha
235 viewState.alpha = draggedView.getAlpha();
236 }
237 }
Selim Cinek343e6e22014-04-11 21:23:30 +0200238 }
Selim Cinek67b22602014-03-10 15:40:16 +0100239
Selim Cinek343e6e22014-04-11 21:23:30 +0200240 /**
Selim Cinek61633a82016-01-25 15:54:10 -0800241 * Initialize the algorithm state like updating the visible children.
Jorim Jaggid4a57442014-04-10 02:45:55 +0200242 */
Selim Cinek3776fe02016-02-04 13:32:43 -0800243 private void initAlgorithmState(StackScrollState resultState, StackScrollAlgorithmState state,
244 AmbientState ambientState) {
Selim Cinek3776fe02016-02-04 13:32:43 -0800245 float bottomOverScroll = ambientState.getOverScrollAmount(false /* onTop */);
246
247 int scrollY = ambientState.getScrollY();
248
249 // Due to the overScroller, the stackscroller can have negative scroll state. This is
250 // already accounted for by the top padding and doesn't need an additional adaption
251 scrollY = Math.max(0, scrollY);
252 state.scrollY = (int) (scrollY + bottomOverScroll);
253
254 //now init the visible children and update paddings
Jorim Jaggid4a57442014-04-10 02:45:55 +0200255 ViewGroup hostView = resultState.getHostView();
256 int childCount = hostView.getChildCount();
257 state.visibleChildren.clear();
258 state.visibleChildren.ensureCapacity(childCount);
Selim Cineka7ed2c12017-01-23 20:47:24 -0800259 state.paddingMap.clear();
Selim Cinekb036ca42015-02-20 15:56:28 +0100260 int notGoneIndex = 0;
Selim Cinek61633a82016-01-25 15:54:10 -0800261 ExpandableView lastView = null;
Selim Cinekd96ed402017-07-28 18:19:03 -0700262 int firstHiddenIndex = ambientState.isDark()
263 ? (ambientState.hasPulsingNotifications() ? 1 : 0)
264 : childCount;
265
266 // The goal here is to fill the padding map, by iterating over how much padding each child
267 // needs. The map is thereby reused, by first filling it with the padding amount and when
268 // iterating over it again, it's filled with the actual resolved value.
269
Jorim Jaggid4a57442014-04-10 02:45:55 +0200270 for (int i = 0; i < childCount; i++) {
Jorim Jaggibe565df2014-04-28 17:51:23 +0200271 ExpandableView v = (ExpandableView) hostView.getChildAt(i);
Jorim Jaggid4a57442014-04-10 02:45:55 +0200272 if (v.getVisibility() != View.GONE) {
Selim Cinek281c2022016-10-13 19:14:43 -0700273 if (v == ambientState.getShelf()) {
274 continue;
275 }
Selim Cinekd96ed402017-07-28 18:19:03 -0700276 if (i >= firstHiddenIndex) {
277 // we need normal padding now, to be in sync with what the stack calculates
278 lastView = null;
Selim Cinekd96ed402017-07-28 18:19:03 -0700279 }
Selim Cineka4baaa32015-04-20 14:27:54 -0700280 notGoneIndex = updateNotGoneIndex(resultState, state, notGoneIndex, v);
Selim Cinekd96ed402017-07-28 18:19:03 -0700281 float increasedPadding = v.getIncreasedPaddingAmount();
Selim Cinek42357e02016-02-24 18:48:01 -0800282 if (increasedPadding != 0.0f) {
Selim Cineka7ed2c12017-01-23 20:47:24 -0800283 state.paddingMap.put(v, increasedPadding);
Selim Cinek61633a82016-01-25 15:54:10 -0800284 if (lastView != null) {
Selim Cineka7ed2c12017-01-23 20:47:24 -0800285 Float prevValue = state.paddingMap.get(lastView);
286 float newValue = getPaddingForValue(increasedPadding);
287 if (prevValue != null) {
288 float prevPadding = getPaddingForValue(prevValue);
289 if (increasedPadding > 0) {
290 newValue = NotificationUtils.interpolate(
291 prevPadding,
292 newValue,
293 increasedPadding);
294 } else if (prevValue > 0) {
295 newValue = NotificationUtils.interpolate(
296 newValue,
297 prevPadding,
298 prevValue);
299 }
300 }
301 state.paddingMap.put(lastView, newValue);
Selim Cinek61633a82016-01-25 15:54:10 -0800302 }
Selim Cineka7ed2c12017-01-23 20:47:24 -0800303 } else if (lastView != null) {
Selim Cinekd96ed402017-07-28 18:19:03 -0700304
305 // Let's now resolve the value to an actual padding
Selim Cineka7ed2c12017-01-23 20:47:24 -0800306 float newValue = getPaddingForValue(state.paddingMap.get(lastView));
307 state.paddingMap.put(lastView, newValue);
Selim Cinek61633a82016-01-25 15:54:10 -0800308 }
Selim Cinekb5605e52015-02-20 18:21:41 +0100309 if (v instanceof ExpandableNotificationRow) {
310 ExpandableNotificationRow row = (ExpandableNotificationRow) v;
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700311
312 // handle the notgoneIndex for the children as well
Selim Cinekb5605e52015-02-20 18:21:41 +0100313 List<ExpandableNotificationRow> children =
314 row.getNotificationChildren();
Selim Cinek83bc7832015-10-22 13:26:54 -0700315 if (row.isSummaryWithChildren() && children != null) {
Selim Cinekb5605e52015-02-20 18:21:41 +0100316 for (ExpandableNotificationRow childRow : children) {
317 if (childRow.getVisibility() != View.GONE) {
Selim Cinekbbcebde2016-11-09 18:28:20 -0800318 ExpandableViewState childState
Selim Cinekb5605e52015-02-20 18:21:41 +0100319 = resultState.getViewStateForView(childRow);
320 childState.notGoneIndex = notGoneIndex;
321 notGoneIndex++;
322 }
323 }
324 }
325 }
Selim Cinek61633a82016-01-25 15:54:10 -0800326 lastView = v;
Jorim Jaggid4a57442014-04-10 02:45:55 +0200327 }
328 }
Selim Cinek2627d722018-01-19 12:16:49 -0800329 ExpandableNotificationRow expandingNotification = ambientState.getExpandingNotification();
330 state.indexOfExpandingNotification = expandingNotification != null
Selim Cinekc25989e2018-02-16 16:42:14 -0800331 ? expandingNotification.isChildInGroup()
332 ? state.visibleChildren.indexOf(expandingNotification.getNotificationParent())
333 : state.visibleChildren.indexOf(expandingNotification)
Selim Cinek2627d722018-01-19 12:16:49 -0800334 : -1;
Jorim Jaggid4a57442014-04-10 02:45:55 +0200335 }
336
Selim Cineka7ed2c12017-01-23 20:47:24 -0800337 private float getPaddingForValue(Float increasedPadding) {
338 if (increasedPadding == null) {
339 return mPaddingBetweenElements;
340 } else if (increasedPadding >= 0.0f) {
341 return NotificationUtils.interpolate(
342 mPaddingBetweenElements,
343 mIncreasedPaddingBetweenElements,
344 increasedPadding);
345 } else {
346 return NotificationUtils.interpolate(
347 0,
348 mPaddingBetweenElements,
349 1.0f + increasedPadding);
350 }
351 }
352
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700353 private int updateNotGoneIndex(StackScrollState resultState,
354 StackScrollAlgorithmState state, int notGoneIndex,
355 ExpandableView v) {
Selim Cinekbbcebde2016-11-09 18:28:20 -0800356 ExpandableViewState viewState = resultState.getViewStateForView(v);
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700357 viewState.notGoneIndex = notGoneIndex;
358 state.visibleChildren.add(v);
359 notGoneIndex++;
360 return notGoneIndex;
361 }
362
Jorim Jaggid4a57442014-04-10 02:45:55 +0200363 /**
Selim Cinek67b22602014-03-10 15:40:16 +0100364 * Determine the positions for the views. This is the main part of the algorithm.
365 *
Selim Cinek684a4422015-04-15 16:18:39 -0700366 * @param resultState The result state to update if a change to the properties of a child occurs
Selim Cinek67b22602014-03-10 15:40:16 +0100367 * @param algorithmState The state in which the current pass of the algorithm is currently in
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700368 * @param ambientState The current ambient state
Selim Cinek67b22602014-03-10 15:40:16 +0100369 */
370 private void updatePositionsForState(StackScrollState resultState,
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700371 StackScrollAlgorithmState algorithmState, AmbientState ambientState) {
Selim Cinek67b22602014-03-10 15:40:16 +0100372
Selim Cinek67b22602014-03-10 15:40:16 +0100373 // The y coordinate of the current child.
Selim Cinek3776fe02016-02-04 13:32:43 -0800374 float currentYPosition = -algorithmState.scrollY;
Jorim Jaggid4a57442014-04-10 02:45:55 +0200375 int childCount = algorithmState.visibleChildren.size();
Selim Cinek67b22602014-03-10 15:40:16 +0100376 for (int i = 0; i < childCount; i++) {
Muyuan Li87798022016-04-07 17:51:25 -0700377 currentYPosition = updateChild(i, resultState, algorithmState, ambientState,
Selim Cinekdb167372016-11-17 15:41:17 -0800378 currentYPosition);
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700379 }
380 }
381
Muyuan Li87798022016-04-07 17:51:25 -0700382 protected float updateChild(int i, StackScrollState resultState,
383 StackScrollAlgorithmState algorithmState, AmbientState ambientState,
Selim Cinekdb167372016-11-17 15:41:17 -0800384 float currentYPosition) {
Muyuan Li87798022016-04-07 17:51:25 -0700385 ExpandableView child = algorithmState.visibleChildren.get(i);
Selim Cinekbbcebde2016-11-09 18:28:20 -0800386 ExpandableViewState childViewState = resultState.getViewStateForView(child);
387 childViewState.location = ExpandableViewState.LOCATION_UNKNOWN;
Muyuan Li87798022016-04-07 17:51:25 -0700388 int paddingAfterChild = getPaddingAfterChild(algorithmState, child);
389 int childHeight = getMaxAllowedChildHeight(child);
Muyuan Li87798022016-04-07 17:51:25 -0700390 childViewState.yTranslation = currentYPosition;
Julia Reynoldsed1c9af2018-03-21 15:21:09 -0400391 boolean isFooterView = child instanceof FooterView;
Selim Cinekcde90e52016-12-22 21:01:49 +0100392 boolean isEmptyShadeView = child instanceof EmptyShadeView;
Muyuan Li87798022016-04-07 17:51:25 -0700393
Selim Cinekdb167372016-11-17 15:41:17 -0800394 childViewState.location = ExpandableViewState.LOCATION_MAIN_AREA;
Selim Cinek9b9d6e12017-11-30 12:29:47 +0100395 float inset = ambientState.getTopPadding() + ambientState.getStackTranslation();
Selim Cinekc25989e2018-02-16 16:42:14 -0800396 if (i <= algorithmState.getIndexOfExpandingNotification()) {
Selim Cinek2627d722018-01-19 12:16:49 -0800397 inset += ambientState.getExpandAnimationTopChange();
398 }
Selim Cinek9b9d6e12017-11-30 12:29:47 +0100399 if (child.mustStayOnScreen() && childViewState.yTranslation >= 0) {
400 // Even if we're not scrolled away we're in view and we're also not in the
401 // shelf. We can relax the constraints and let us scroll off the top!
402 float end = childViewState.yTranslation + childViewState.height + inset;
403 childViewState.headsUpIsVisible = end < ambientState.getMaxHeadsUpTranslation();
404 }
Julia Reynoldsed1c9af2018-03-21 15:21:09 -0400405 if (isFooterView) {
Selim Cinekdb167372016-11-17 15:41:17 -0800406 childViewState.yTranslation = Math.min(childViewState.yTranslation,
407 ambientState.getInnerHeight() - childHeight);
Selim Cinekcde90e52016-12-22 21:01:49 +0100408 } else if (isEmptyShadeView) {
409 childViewState.yTranslation = ambientState.getInnerHeight() - childHeight
410 + ambientState.getStackTranslation() * 0.25f;
Muyuan Li87798022016-04-07 17:51:25 -0700411 } else {
Selim Cinek2627d722018-01-19 12:16:49 -0800412 clampPositionToShelf(child, childViewState, ambientState);
Muyuan Li87798022016-04-07 17:51:25 -0700413 }
414
Muyuan Li87798022016-04-07 17:51:25 -0700415 currentYPosition = childViewState.yTranslation + childHeight + paddingAfterChild;
416 if (currentYPosition <= 0) {
Selim Cinekbbcebde2016-11-09 18:28:20 -0800417 childViewState.location = ExpandableViewState.LOCATION_HIDDEN_TOP;
Muyuan Li87798022016-04-07 17:51:25 -0700418 }
Selim Cinekbbcebde2016-11-09 18:28:20 -0800419 if (childViewState.location == ExpandableViewState.LOCATION_UNKNOWN) {
Muyuan Li87798022016-04-07 17:51:25 -0700420 Log.wtf(LOG_TAG, "Failed to assign location for child " + i);
421 }
422
Selim Cinek9b9d6e12017-11-30 12:29:47 +0100423 childViewState.yTranslation += inset;
Muyuan Li87798022016-04-07 17:51:25 -0700424 return currentYPosition;
425 }
426
427 protected int getPaddingAfterChild(StackScrollAlgorithmState algorithmState,
Selim Cinek61633a82016-01-25 15:54:10 -0800428 ExpandableView child) {
Selim Cinek281c2022016-10-13 19:14:43 -0700429 return algorithmState.getPaddingAfterChild(child);
Selim Cinek61633a82016-01-25 15:54:10 -0800430 }
431
Selim Cineka4baaa32015-04-20 14:27:54 -0700432 private void updateHeadsUpStates(StackScrollState resultState,
433 StackScrollAlgorithmState algorithmState, AmbientState ambientState) {
434 int childCount = algorithmState.visibleChildren.size();
435 ExpandableNotificationRow topHeadsUpEntry = null;
436 for (int i = 0; i < childCount; i++) {
437 View child = algorithmState.visibleChildren.get(i);
438 if (!(child instanceof ExpandableNotificationRow)) {
439 break;
440 }
441 ExpandableNotificationRow row = (ExpandableNotificationRow) child;
442 if (!row.isHeadsUp()) {
443 break;
Selim Cineka4baaa32015-04-20 14:27:54 -0700444 }
Selim Cinekbbcebde2016-11-09 18:28:20 -0800445 ExpandableViewState childState = resultState.getViewStateForView(row);
Selim Cinek9b9d6e12017-11-30 12:29:47 +0100446 if (topHeadsUpEntry == null && row.mustStayOnScreen() && !childState.headsUpIsVisible) {
Selim Cinek3776fe02016-02-04 13:32:43 -0800447 topHeadsUpEntry = row;
Selim Cinekbbcebde2016-11-09 18:28:20 -0800448 childState.location = ExpandableViewState.LOCATION_FIRST_HUN;
Selim Cinek3776fe02016-02-04 13:32:43 -0800449 }
Selim Cinek1f3f5442015-04-10 17:54:46 -0700450 boolean isTopEntry = topHeadsUpEntry == row;
Selim Cinek3776fe02016-02-04 13:32:43 -0800451 float unmodifiedEndLocation = childState.yTranslation + childState.height;
Selim Cinek131c1e22015-05-11 19:04:49 -0700452 if (mIsExpanded) {
Selim Cinek9b9d6e12017-11-30 12:29:47 +0100453 if (row.mustStayOnScreen() && !childState.headsUpIsVisible) {
454 // Ensure that the heads up is always visible even when scrolled off
455 clampHunToTop(ambientState, row, childState);
456 if (i == 0 && ambientState.isAboveShelf(row)) {
457 // the first hun can't get off screen.
458 clampHunToMaxTranslation(ambientState, row, childState);
459 childState.hidden = false;
460 }
Selim Cinekd127d792016-11-01 19:11:41 -0700461 }
Selim Cinek131c1e22015-05-11 19:04:49 -0700462 }
Selim Cinek684a4422015-04-15 16:18:39 -0700463 if (row.isPinned()) {
Selim Cinekaa9db1f2018-02-27 17:35:47 -0800464 childState.yTranslation = Math.max(childState.yTranslation, mHeadsUpInset);
Selim Cinek31aada42015-12-18 17:51:15 -0800465 childState.height = Math.max(row.getIntrinsicHeight(), childState.height);
Selim Cinekcafa87f2016-10-26 17:00:17 -0700466 childState.hidden = false;
Selim Cinekbbcebde2016-11-09 18:28:20 -0800467 ExpandableViewState topState = resultState.getViewStateForView(topHeadsUpEntry);
Selim Cinek61cfd4b2017-12-08 12:42:36 -0800468 if (topState != null && !isTopEntry && (!mIsExpanded
Selim Cinek3776fe02016-02-04 13:32:43 -0800469 || unmodifiedEndLocation < topState.yTranslation + topState.height)) {
Selim Cinek684a4422015-04-15 16:18:39 -0700470 // Ensure that a headsUp doesn't vertically extend further than the heads-up at
471 // the top most z-position
Selim Cinek31aada42015-12-18 17:51:15 -0800472 childState.height = row.getIntrinsicHeight();
Selim Cineke53e6bb2015-04-13 16:14:26 -0700473 childState.yTranslation = topState.yTranslation + topState.height
474 - childState.height;
Selim Cinek1f3f5442015-04-10 17:54:46 -0700475 }
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700476 }
Selim Cinekcafa87f2016-10-26 17:00:17 -0700477 if (row.isHeadsUpAnimatingAway()) {
478 childState.hidden = false;
479 }
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700480 }
Selim Cinek67b22602014-03-10 15:40:16 +0100481 }
482
Selim Cinek3776fe02016-02-04 13:32:43 -0800483 private void clampHunToTop(AmbientState ambientState, ExpandableNotificationRow row,
Selim Cinekbbcebde2016-11-09 18:28:20 -0800484 ExpandableViewState childState) {
Selim Cinek3776fe02016-02-04 13:32:43 -0800485 float newTranslation = Math.max(ambientState.getTopPadding()
486 + ambientState.getStackTranslation(), childState.yTranslation);
487 childState.height = (int) Math.max(childState.height - (newTranslation
Selim Cinek567e8452016-03-24 10:54:56 -0700488 - childState.yTranslation), row.getCollapsedHeight());
Selim Cinek3776fe02016-02-04 13:32:43 -0800489 childState.yTranslation = newTranslation;
490 }
491
492 private void clampHunToMaxTranslation(AmbientState ambientState, ExpandableNotificationRow row,
Selim Cinekbbcebde2016-11-09 18:28:20 -0800493 ExpandableViewState childState) {
Selim Cinek3776fe02016-02-04 13:32:43 -0800494 float newTranslation;
Selim Cinek7e0f9482017-05-22 20:00:56 -0700495 float maxHeadsUpTranslation = ambientState.getMaxHeadsUpTranslation();
496 float maxShelfPosition = ambientState.getInnerHeight() + ambientState.getTopPadding()
497 + ambientState.getStackTranslation();
498 maxHeadsUpTranslation = Math.min(maxHeadsUpTranslation, maxShelfPosition);
499 float bottomPosition = maxHeadsUpTranslation - row.getCollapsedHeight();
Selim Cinek3776fe02016-02-04 13:32:43 -0800500 newTranslation = Math.min(childState.yTranslation, bottomPosition);
Selim Cinek7e0f9482017-05-22 20:00:56 -0700501 childState.height = (int) Math.min(childState.height, maxHeadsUpTranslation
502 - newTranslation);
Selim Cinek3776fe02016-02-04 13:32:43 -0800503 childState.yTranslation = newTranslation;
Selim Cinek343e6e22014-04-11 21:23:30 +0200504 }
505
506 /**
Selim Cinek281c2022016-10-13 19:14:43 -0700507 * Clamp the height of the child down such that its end is at most on the beginning of
508 * the shelf.
509 *
Selim Cinek2627d722018-01-19 12:16:49 -0800510 * @param child
Selim Cinek281c2022016-10-13 19:14:43 -0700511 * @param childViewState the view state of the child
512 * @param ambientState the ambient state
513 */
Selim Cinek2627d722018-01-19 12:16:49 -0800514 private void clampPositionToShelf(ExpandableView child,
515 ExpandableViewState childViewState,
Selim Cinek281c2022016-10-13 19:14:43 -0700516 AmbientState ambientState) {
Eliot Courtney5d3d2d02018-01-18 15:59:03 +0900517 if (ambientState.getShelf() == null) {
518 return;
519 }
520
Selim Cineka686b2c2016-10-26 13:58:27 -0700521 int shelfStart = ambientState.getInnerHeight()
522 - ambientState.getShelf().getIntrinsicHeight();
shawnlin5be1f7c2018-05-21 20:50:54 +0800523 if (ambientState.isAppearing() && !child.isAboveShelf()) {
524 // Don't show none heads-up notifications while in appearing phase.
525 childViewState.yTranslation = Math.max(childViewState.yTranslation, shelfStart);
526 }
Selim Cinek281c2022016-10-13 19:14:43 -0700527 childViewState.yTranslation = Math.min(childViewState.yTranslation, shelfStart);
Selim Cinekc383fd02016-10-21 15:31:26 -0700528 if (childViewState.yTranslation >= shelfStart) {
Selim Cinekc25989e2018-02-16 16:42:14 -0800529 childViewState.hidden = !child.isExpandAnimationRunning() && !child.hasExpandingChild();
Selim Cinekeccb5de2016-10-28 15:04:05 -0700530 childViewState.inShelf = true;
Selim Cinek9b9d6e12017-11-30 12:29:47 +0100531 childViewState.headsUpIsVisible = false;
Selim Cinekc383fd02016-10-21 15:31:26 -0700532 }
Selim Cinek281c2022016-10-13 19:14:43 -0700533 }
534
Muyuan Li87798022016-04-07 17:51:25 -0700535 protected int getMaxAllowedChildHeight(View child) {
Selim Cinek31aada42015-12-18 17:51:15 -0800536 if (child instanceof ExpandableView) {
Jorim Jaggibe565df2014-04-28 17:51:23 +0200537 ExpandableView expandableView = (ExpandableView) child;
Selim Cinek8d5727f2015-04-28 19:17:32 -0700538 return expandableView.getIntrinsicHeight();
Selim Cinek1685e632014-04-08 02:27:49 +0200539 }
Jorim Jaggibe565df2014-04-28 17:51:23 +0200540 return child == null? mCollapsedSize : child.getHeight();
Selim Cinek1685e632014-04-08 02:27:49 +0200541 }
542
Selim Cinek67b22602014-03-10 15:40:16 +0100543 /**
Selim Cinek67b22602014-03-10 15:40:16 +0100544 * Calculate the Z positions for all children based on the number of items in both stacks and
545 * save it in the resultState
Selim Cinek3776fe02016-02-04 13:32:43 -0800546 * @param resultState The result state to update the zTranslation values
Selim Cinek67b22602014-03-10 15:40:16 +0100547 * @param algorithmState The state in which the current pass of the algorithm is currently in
Selim Cinek3776fe02016-02-04 13:32:43 -0800548 * @param ambientState The ambient state of the algorithm
Selim Cinek67b22602014-03-10 15:40:16 +0100549 */
550 private void updateZValuesForState(StackScrollState resultState,
Selim Cinek3776fe02016-02-04 13:32:43 -0800551 StackScrollAlgorithmState algorithmState, AmbientState ambientState) {
Jorim Jaggid4a57442014-04-10 02:45:55 +0200552 int childCount = algorithmState.visibleChildren.size();
Selim Cinek33223572016-02-19 19:32:22 -0800553 float childrenOnTop = 0.0f;
Selim Cinek3776fe02016-02-04 13:32:43 -0800554 for (int i = childCount - 1; i >= 0; i--) {
Selim Cinekdaab6f52017-04-06 16:46:34 -0700555 childrenOnTop = updateChildZValue(i, childrenOnTop,
Muyuan Li87798022016-04-07 17:51:25 -0700556 resultState, algorithmState, ambientState);
557 }
558 }
559
Selim Cinekdaab6f52017-04-06 16:46:34 -0700560 protected float updateChildZValue(int i, float childrenOnTop,
Muyuan Li87798022016-04-07 17:51:25 -0700561 StackScrollState resultState, StackScrollAlgorithmState algorithmState,
562 AmbientState ambientState) {
563 ExpandableView child = algorithmState.visibleChildren.get(i);
Selim Cinekbbcebde2016-11-09 18:28:20 -0800564 ExpandableViewState childViewState = resultState.getViewStateForView(child);
Selim Cinek281c2022016-10-13 19:14:43 -0700565 int zDistanceBetweenElements = ambientState.getZDistanceBetweenElements();
566 float baseZ = ambientState.getBaseZHeight();
Selim Cinek9b9d6e12017-11-30 12:29:47 +0100567 if (child.mustStayOnScreen() && !childViewState.headsUpIsVisible
568 && !ambientState.isDozingAndNotPulsing(child)
Muyuan Li87798022016-04-07 17:51:25 -0700569 && childViewState.yTranslation < ambientState.getTopPadding()
570 + ambientState.getStackTranslation()) {
571 if (childrenOnTop != 0.0f) {
572 childrenOnTop++;
573 } else {
574 float overlap = ambientState.getTopPadding()
575 + ambientState.getStackTranslation() - childViewState.yTranslation;
576 childrenOnTop += Math.min(1.0f, overlap / childViewState.height);
577 }
Selim Cinek281c2022016-10-13 19:14:43 -0700578 childViewState.zTranslation = baseZ
579 + childrenOnTop * zDistanceBetweenElements;
Selim Cinekebf42342017-07-13 15:46:10 +0200580 } else if (i == 0 && ambientState.isAboveShelf(child)) {
Selim Cinekd127d792016-11-01 19:11:41 -0700581 // In case this is a new view that has never been measured before, we don't want to
582 // elevate if we are currently expanded more then the notification
Eliot Courtney5d3d2d02018-01-18 15:59:03 +0900583 int shelfHeight = ambientState.getShelf() == null ? 0 :
584 ambientState.getShelf().getIntrinsicHeight();
Selim Cinekd127d792016-11-01 19:11:41 -0700585 float shelfStart = ambientState.getInnerHeight()
586 - shelfHeight + ambientState.getTopPadding()
587 + ambientState.getStackTranslation();
588 float notificationEnd = childViewState.yTranslation + child.getPinnedHeadsUpHeight()
589 + mPaddingBetweenElements;
590 if (shelfStart > notificationEnd) {
591 childViewState.zTranslation = baseZ;
592 } else {
593 float factor = (notificationEnd - shelfStart) / shelfHeight;
594 factor = Math.min(factor, 1.0f);
595 childViewState.zTranslation = baseZ + factor * zDistanceBetweenElements;
596 }
Muyuan Li87798022016-04-07 17:51:25 -0700597 } else {
Selim Cinek281c2022016-10-13 19:14:43 -0700598 childViewState.zTranslation = baseZ;
Selim Cinek67b22602014-03-10 15:40:16 +0100599 }
Selim Cinek99e9adf2018-03-15 09:17:47 -0700600
601 // We need to scrim the notification more from its surrounding content when we are pinned,
602 // and we therefore elevate it higher.
603 // We can use the headerVisibleAmount for this, since the value nicely goes from 0 to 1 when
604 // expanding after which we have a normal elevation again.
605 childViewState.zTranslation += (1.0f - child.getHeaderVisibleAmount())
606 * mPinnedZTranslationExtra;
Selim Cinekdaab6f52017-04-06 16:46:34 -0700607 return childrenOnTop;
Selim Cinek67b22602014-03-10 15:40:16 +0100608 }
609
Selim Cinek1685e632014-04-08 02:27:49 +0200610 public void setIsExpanded(boolean isExpanded) {
611 this.mIsExpanded = isExpanded;
612 }
613
Selim Cinek281c2022016-10-13 19:14:43 -0700614 public class StackScrollAlgorithmState {
Selim Cinek67b22602014-03-10 15:40:16 +0100615
616 /**
617 * The scroll position of the algorithm
618 */
619 public int scrollY;
620
621 /**
Jorim Jaggid4a57442014-04-10 02:45:55 +0200622 * The children from the host view which are not gone.
623 */
Jorim Jaggibe565df2014-04-28 17:51:23 +0200624 public final ArrayList<ExpandableView> visibleChildren = new ArrayList<ExpandableView>();
Selim Cinek61633a82016-01-25 15:54:10 -0800625
626 /**
Selim Cineka7ed2c12017-01-23 20:47:24 -0800627 * The padding after each child measured in pixels.
Selim Cinek61633a82016-01-25 15:54:10 -0800628 */
Selim Cineka7ed2c12017-01-23 20:47:24 -0800629 public final HashMap<ExpandableView, Float> paddingMap = new HashMap<>();
Selim Cinek2627d722018-01-19 12:16:49 -0800630 private int indexOfExpandingNotification;
Selim Cinek281c2022016-10-13 19:14:43 -0700631
632 public int getPaddingAfterChild(ExpandableView child) {
Selim Cineka7ed2c12017-01-23 20:47:24 -0800633 Float padding = paddingMap.get(child);
634 if (padding == null) {
635 // Should only happen for the last view
636 return mPaddingBetweenElements;
637 }
638 return (int) padding.floatValue();
Selim Cinek281c2022016-10-13 19:14:43 -0700639 }
Selim Cinek2627d722018-01-19 12:16:49 -0800640
641 public int getIndexOfExpandingNotification() {
642 return indexOfExpandingNotification;
643 }
Selim Cinek67b22602014-03-10 15:40:16 +0100644 }
645
646}