blob: 2ce6df27558894ae8ba5038102ea8737df5a5e51 [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()
133 + ambientState.getStackTranslation() : 0;
Selim Cinek708a6c12014-05-28 14:16:02 +0200134 float previousNotificationEnd = 0;
135 float previousNotificationStart = 0;
Selim Cinek708a6c12014-05-28 14:16:02 +0200136 int childCount = algorithmState.visibleChildren.size();
137 for (int i = 0; i < childCount; i++) {
138 ExpandableView child = algorithmState.visibleChildren.get(i);
Selim Cinekbbcebde2016-11-09 18:28:20 -0800139 ExpandableViewState state = resultState.getViewStateForView(child);
Selim Cinek9b9d6e12017-11-30 12:29:47 +0100140 if (!child.mustStayOnScreen() || state.headsUpIsVisible) {
Selim Cinek3776fe02016-02-04 13:32:43 -0800141 previousNotificationEnd = Math.max(drawStart, previousNotificationEnd);
142 previousNotificationStart = Math.max(drawStart, previousNotificationStart);
143 }
Selim Cinek587cbf32016-01-19 11:36:18 -0800144 float newYTranslation = state.yTranslation;
145 float newHeight = state.height;
Selim Cinek708a6c12014-05-28 14:16:02 +0200146 float newNotificationEnd = newYTranslation + newHeight;
Mady Mellor53ac1ef2016-06-20 13:11:38 -0700147 boolean isHeadsUp = (child instanceof ExpandableNotificationRow)
148 && ((ExpandableNotificationRow) child).isPinned();
Anthony Chen9fe1ee72017-04-07 13:53:37 -0700149 if (mClipNotificationScrollToTop
150 && !state.inShelf && newYTranslation < previousNotificationEnd
Jorim Jaggi0fdf5742016-06-27 11:50:58 -0700151 && (!isHeadsUp || ambientState.isShadeExpanded())) {
Mady Mellorc128f222016-04-26 11:42:46 -0700152 // The previous view is overlapping on top, clip!
153 float overlapAmount = previousNotificationEnd - newYTranslation;
154 state.clipTopAmount = (int) overlapAmount;
Selim Cinekc5baa3e2014-10-29 19:04:19 +0100155 } else {
Mady Mellorc128f222016-04-26 11:42:46 -0700156 state.clipTopAmount = 0;
Selim Cinek9c17b772015-07-07 20:37:09 -0700157 }
158
Selim Cinek708a6c12014-05-28 14:16:02 +0200159 if (!child.isTransparent()) {
160 // Only update the previous values if we are not transparent,
161 // otherwise we would clip to a transparent view.
Mady Mellorc128f222016-04-26 11:42:46 -0700162 previousNotificationEnd = newNotificationEnd;
163 previousNotificationStart = newYTranslation;
Selim Cinek708a6c12014-05-28 14:16:02 +0200164 }
165 }
166 }
167
Selim Cinek9c17b772015-07-07 20:37:09 -0700168 public static boolean canChildBeDismissed(View v) {
Selim Cinek9e624e72016-07-20 13:46:49 -0700169 if (!(v instanceof ExpandableNotificationRow)) {
170 return false;
Selim Cinek38d429f2016-06-03 11:46:56 -0700171 }
Selim Cinek9e624e72016-07-20 13:46:49 -0700172 ExpandableNotificationRow row = (ExpandableNotificationRow) v;
173 if (row.areGutsExposed()) {
174 return false;
175 }
176 return row.canViewBeDismissed();
Selim Cinek9c17b772015-07-07 20:37:09 -0700177 }
178
Selim Cinek708a6c12014-05-28 14:16:02 +0200179 /**
Jorim Jaggiae441282014-08-01 02:45:18 +0200180 * Updates the dimmed, activated and hiding sensitive states of the children.
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200181 */
Jorim Jaggiae441282014-08-01 02:45:18 +0200182 private void updateDimmedActivatedHideSensitive(AmbientState ambientState,
183 StackScrollState resultState, StackScrollAlgorithmState algorithmState) {
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200184 boolean dimmed = ambientState.isDimmed();
John Spurlockbf370992014-06-17 13:58:31 -0400185 boolean dark = ambientState.isDark();
Jorim Jaggiae441282014-08-01 02:45:18 +0200186 boolean hideSensitive = ambientState.isHideSensitive();
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200187 View activatedChild = ambientState.getActivatedChild();
188 int childCount = algorithmState.visibleChildren.size();
189 for (int i = 0; i < childCount; i++) {
190 View child = algorithmState.visibleChildren.get(i);
Selim Cinekbbcebde2016-11-09 18:28:20 -0800191 ExpandableViewState childViewState = resultState.getViewStateForView(child);
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200192 childViewState.dimmed = dimmed;
John Spurlockbf370992014-06-17 13:58:31 -0400193 childViewState.dark = dark;
Jorim Jaggiae441282014-08-01 02:45:18 +0200194 childViewState.hideSensitive = hideSensitive;
Selim Cinekb89de4e2014-06-10 10:47:05 +0200195 boolean isActivatedChild = activatedChild == child;
Jorim Jaggi4538cee2014-09-09 15:21:38 +0200196 if (dimmed && isActivatedChild) {
Selim Cinek281c2022016-10-13 19:14:43 -0700197 childViewState.zTranslation += 2.0f * ambientState.getZDistanceBetweenElements();
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200198 }
199 }
Selim Cinekeb973562014-05-02 17:07:49 +0200200 }
201
202 /**
203 * Handle the special state when views are being dragged
204 */
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200205 private void handleDraggedViews(AmbientState ambientState, StackScrollState resultState,
Selim Cinekeb973562014-05-02 17:07:49 +0200206 StackScrollAlgorithmState algorithmState) {
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200207 ArrayList<View> draggedViews = ambientState.getDraggedViews();
208 for (View draggedView : draggedViews) {
Selim Cinekeb973562014-05-02 17:07:49 +0200209 int childIndex = algorithmState.visibleChildren.indexOf(draggedView);
210 if (childIndex >= 0 && childIndex < algorithmState.visibleChildren.size() - 1) {
211 View nextChild = algorithmState.visibleChildren.get(childIndex + 1);
Jorim Jaggid552d9d2014-05-07 19:41:13 +0200212 if (!draggedViews.contains(nextChild)) {
Selim Cinekeb973562014-05-02 17:07:49 +0200213 // only if the view is not dragged itself we modify its state to be fully
214 // visible
Selim Cinekbbcebde2016-11-09 18:28:20 -0800215 ExpandableViewState viewState = resultState.getViewStateForView(
Selim Cinekeb973562014-05-02 17:07:49 +0200216 nextChild);
217 // The child below the dragged one must be fully visible
Selim Cinek131c1e22015-05-11 19:04:49 -0700218 if (ambientState.isShadeExpanded()) {
Selim Cinek277a8aa2016-01-22 12:12:37 -0800219 viewState.shadowAlpha = 1;
220 viewState.hidden = false;
Selim Cineka59ecc32015-04-07 10:51:49 -0700221 }
Selim Cinekeb973562014-05-02 17:07:49 +0200222 }
223
224 // Lets set the alpha to the one it currently has, as its currently being dragged
Selim Cinekbbcebde2016-11-09 18:28:20 -0800225 ExpandableViewState viewState = resultState.getViewStateForView(draggedView);
Selim Cinekeb973562014-05-02 17:07:49 +0200226 // The dragged child should keep the set alpha
227 viewState.alpha = draggedView.getAlpha();
228 }
229 }
Selim Cinek343e6e22014-04-11 21:23:30 +0200230 }
Selim Cinek67b22602014-03-10 15:40:16 +0100231
Selim Cinek343e6e22014-04-11 21:23:30 +0200232 /**
Selim Cinek61633a82016-01-25 15:54:10 -0800233 * Initialize the algorithm state like updating the visible children.
Jorim Jaggid4a57442014-04-10 02:45:55 +0200234 */
Selim Cinek3776fe02016-02-04 13:32:43 -0800235 private void initAlgorithmState(StackScrollState resultState, StackScrollAlgorithmState state,
236 AmbientState ambientState) {
Selim Cinek3776fe02016-02-04 13:32:43 -0800237 float bottomOverScroll = ambientState.getOverScrollAmount(false /* onTop */);
238
239 int scrollY = ambientState.getScrollY();
240
241 // Due to the overScroller, the stackscroller can have negative scroll state. This is
242 // already accounted for by the top padding and doesn't need an additional adaption
243 scrollY = Math.max(0, scrollY);
244 state.scrollY = (int) (scrollY + bottomOverScroll);
245
246 //now init the visible children and update paddings
Jorim Jaggid4a57442014-04-10 02:45:55 +0200247 ViewGroup hostView = resultState.getHostView();
248 int childCount = hostView.getChildCount();
249 state.visibleChildren.clear();
250 state.visibleChildren.ensureCapacity(childCount);
Selim Cineka7ed2c12017-01-23 20:47:24 -0800251 state.paddingMap.clear();
Selim Cinekb036ca42015-02-20 15:56:28 +0100252 int notGoneIndex = 0;
Selim Cinek61633a82016-01-25 15:54:10 -0800253 ExpandableView lastView = null;
Selim Cinekd96ed402017-07-28 18:19:03 -0700254 int firstHiddenIndex = ambientState.isDark()
255 ? (ambientState.hasPulsingNotifications() ? 1 : 0)
256 : childCount;
257
258 // The goal here is to fill the padding map, by iterating over how much padding each child
259 // needs. The map is thereby reused, by first filling it with the padding amount and when
260 // iterating over it again, it's filled with the actual resolved value.
261
Jorim Jaggid4a57442014-04-10 02:45:55 +0200262 for (int i = 0; i < childCount; i++) {
Jorim Jaggibe565df2014-04-28 17:51:23 +0200263 ExpandableView v = (ExpandableView) hostView.getChildAt(i);
Jorim Jaggid4a57442014-04-10 02:45:55 +0200264 if (v.getVisibility() != View.GONE) {
Selim Cinek281c2022016-10-13 19:14:43 -0700265 if (v == ambientState.getShelf()) {
266 continue;
267 }
Selim Cinekd96ed402017-07-28 18:19:03 -0700268 if (i >= firstHiddenIndex) {
269 // we need normal padding now, to be in sync with what the stack calculates
270 lastView = null;
271 ExpandableViewState viewState = resultState.getViewStateForView(v);
272 viewState.hidden = true;
273 }
Selim Cineka4baaa32015-04-20 14:27:54 -0700274 notGoneIndex = updateNotGoneIndex(resultState, state, notGoneIndex, v);
Selim Cinekd96ed402017-07-28 18:19:03 -0700275 float increasedPadding = v.getIncreasedPaddingAmount();
Selim Cinek42357e02016-02-24 18:48:01 -0800276 if (increasedPadding != 0.0f) {
Selim Cineka7ed2c12017-01-23 20:47:24 -0800277 state.paddingMap.put(v, increasedPadding);
Selim Cinek61633a82016-01-25 15:54:10 -0800278 if (lastView != null) {
Selim Cineka7ed2c12017-01-23 20:47:24 -0800279 Float prevValue = state.paddingMap.get(lastView);
280 float newValue = getPaddingForValue(increasedPadding);
281 if (prevValue != null) {
282 float prevPadding = getPaddingForValue(prevValue);
283 if (increasedPadding > 0) {
284 newValue = NotificationUtils.interpolate(
285 prevPadding,
286 newValue,
287 increasedPadding);
288 } else if (prevValue > 0) {
289 newValue = NotificationUtils.interpolate(
290 newValue,
291 prevPadding,
292 prevValue);
293 }
294 }
295 state.paddingMap.put(lastView, newValue);
Selim Cinek61633a82016-01-25 15:54:10 -0800296 }
Selim Cineka7ed2c12017-01-23 20:47:24 -0800297 } else if (lastView != null) {
Selim Cinekd96ed402017-07-28 18:19:03 -0700298
299 // Let's now resolve the value to an actual padding
Selim Cineka7ed2c12017-01-23 20:47:24 -0800300 float newValue = getPaddingForValue(state.paddingMap.get(lastView));
301 state.paddingMap.put(lastView, newValue);
Selim Cinek61633a82016-01-25 15:54:10 -0800302 }
Selim Cinekb5605e52015-02-20 18:21:41 +0100303 if (v instanceof ExpandableNotificationRow) {
304 ExpandableNotificationRow row = (ExpandableNotificationRow) v;
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700305
306 // handle the notgoneIndex for the children as well
Selim Cinekb5605e52015-02-20 18:21:41 +0100307 List<ExpandableNotificationRow> children =
308 row.getNotificationChildren();
Selim Cinek83bc7832015-10-22 13:26:54 -0700309 if (row.isSummaryWithChildren() && children != null) {
Selim Cinekb5605e52015-02-20 18:21:41 +0100310 for (ExpandableNotificationRow childRow : children) {
311 if (childRow.getVisibility() != View.GONE) {
Selim Cinekbbcebde2016-11-09 18:28:20 -0800312 ExpandableViewState childState
Selim Cinekb5605e52015-02-20 18:21:41 +0100313 = resultState.getViewStateForView(childRow);
314 childState.notGoneIndex = notGoneIndex;
315 notGoneIndex++;
316 }
317 }
318 }
319 }
Selim Cinek61633a82016-01-25 15:54:10 -0800320 lastView = v;
Jorim Jaggid4a57442014-04-10 02:45:55 +0200321 }
322 }
323 }
324
Selim Cineka7ed2c12017-01-23 20:47:24 -0800325 private float getPaddingForValue(Float increasedPadding) {
326 if (increasedPadding == null) {
327 return mPaddingBetweenElements;
328 } else if (increasedPadding >= 0.0f) {
329 return NotificationUtils.interpolate(
330 mPaddingBetweenElements,
331 mIncreasedPaddingBetweenElements,
332 increasedPadding);
333 } else {
334 return NotificationUtils.interpolate(
335 0,
336 mPaddingBetweenElements,
337 1.0f + increasedPadding);
338 }
339 }
340
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700341 private int updateNotGoneIndex(StackScrollState resultState,
342 StackScrollAlgorithmState state, int notGoneIndex,
343 ExpandableView v) {
Selim Cinekbbcebde2016-11-09 18:28:20 -0800344 ExpandableViewState viewState = resultState.getViewStateForView(v);
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700345 viewState.notGoneIndex = notGoneIndex;
346 state.visibleChildren.add(v);
347 notGoneIndex++;
348 return notGoneIndex;
349 }
350
Jorim Jaggid4a57442014-04-10 02:45:55 +0200351 /**
Selim Cinek67b22602014-03-10 15:40:16 +0100352 * Determine the positions for the views. This is the main part of the algorithm.
353 *
Selim Cinek684a4422015-04-15 16:18:39 -0700354 * @param resultState The result state to update if a change to the properties of a child occurs
Selim Cinek67b22602014-03-10 15:40:16 +0100355 * @param algorithmState The state in which the current pass of the algorithm is currently in
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700356 * @param ambientState The current ambient state
Selim Cinek67b22602014-03-10 15:40:16 +0100357 */
358 private void updatePositionsForState(StackScrollState resultState,
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700359 StackScrollAlgorithmState algorithmState, AmbientState ambientState) {
Selim Cinek67b22602014-03-10 15:40:16 +0100360
Selim Cinek67b22602014-03-10 15:40:16 +0100361 // The y coordinate of the current child.
Selim Cinek3776fe02016-02-04 13:32:43 -0800362 float currentYPosition = -algorithmState.scrollY;
Jorim Jaggid4a57442014-04-10 02:45:55 +0200363 int childCount = algorithmState.visibleChildren.size();
Selim Cinek67b22602014-03-10 15:40:16 +0100364 for (int i = 0; i < childCount; i++) {
Muyuan Li87798022016-04-07 17:51:25 -0700365 currentYPosition = updateChild(i, resultState, algorithmState, ambientState,
Selim Cinekdb167372016-11-17 15:41:17 -0800366 currentYPosition);
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700367 }
368 }
369
Muyuan Li87798022016-04-07 17:51:25 -0700370 protected float updateChild(int i, StackScrollState resultState,
371 StackScrollAlgorithmState algorithmState, AmbientState ambientState,
Selim Cinekdb167372016-11-17 15:41:17 -0800372 float currentYPosition) {
Muyuan Li87798022016-04-07 17:51:25 -0700373 ExpandableView child = algorithmState.visibleChildren.get(i);
Selim Cinekbbcebde2016-11-09 18:28:20 -0800374 ExpandableViewState childViewState = resultState.getViewStateForView(child);
375 childViewState.location = ExpandableViewState.LOCATION_UNKNOWN;
Muyuan Li87798022016-04-07 17:51:25 -0700376 int paddingAfterChild = getPaddingAfterChild(algorithmState, child);
377 int childHeight = getMaxAllowedChildHeight(child);
Muyuan Li87798022016-04-07 17:51:25 -0700378 childViewState.yTranslation = currentYPosition;
Selim Cinekc9c640f2016-11-14 17:27:19 -0800379 boolean isDismissView = child instanceof DismissView;
Selim Cinekcde90e52016-12-22 21:01:49 +0100380 boolean isEmptyShadeView = child instanceof EmptyShadeView;
Muyuan Li87798022016-04-07 17:51:25 -0700381
Selim Cinekdb167372016-11-17 15:41:17 -0800382 childViewState.location = ExpandableViewState.LOCATION_MAIN_AREA;
Selim Cinek9b9d6e12017-11-30 12:29:47 +0100383 float inset = ambientState.getTopPadding() + ambientState.getStackTranslation();
384 if (child.mustStayOnScreen() && childViewState.yTranslation >= 0) {
385 // Even if we're not scrolled away we're in view and we're also not in the
386 // shelf. We can relax the constraints and let us scroll off the top!
387 float end = childViewState.yTranslation + childViewState.height + inset;
388 childViewState.headsUpIsVisible = end < ambientState.getMaxHeadsUpTranslation();
389 }
Selim Cinekdb167372016-11-17 15:41:17 -0800390 if (isDismissView) {
391 childViewState.yTranslation = Math.min(childViewState.yTranslation,
392 ambientState.getInnerHeight() - childHeight);
Selim Cinekcde90e52016-12-22 21:01:49 +0100393 } else if (isEmptyShadeView) {
394 childViewState.yTranslation = ambientState.getInnerHeight() - childHeight
395 + ambientState.getStackTranslation() * 0.25f;
Muyuan Li87798022016-04-07 17:51:25 -0700396 } else {
Selim Cinekdb167372016-11-17 15:41:17 -0800397 clampPositionToShelf(childViewState, ambientState);
Muyuan Li87798022016-04-07 17:51:25 -0700398 }
399
Muyuan Li87798022016-04-07 17:51:25 -0700400 currentYPosition = childViewState.yTranslation + childHeight + paddingAfterChild;
401 if (currentYPosition <= 0) {
Selim Cinekbbcebde2016-11-09 18:28:20 -0800402 childViewState.location = ExpandableViewState.LOCATION_HIDDEN_TOP;
Muyuan Li87798022016-04-07 17:51:25 -0700403 }
Selim Cinekbbcebde2016-11-09 18:28:20 -0800404 if (childViewState.location == ExpandableViewState.LOCATION_UNKNOWN) {
Muyuan Li87798022016-04-07 17:51:25 -0700405 Log.wtf(LOG_TAG, "Failed to assign location for child " + i);
406 }
407
Selim Cinek9b9d6e12017-11-30 12:29:47 +0100408 childViewState.yTranslation += inset;
Muyuan Li87798022016-04-07 17:51:25 -0700409 return currentYPosition;
410 }
411
412 protected int getPaddingAfterChild(StackScrollAlgorithmState algorithmState,
Selim Cinek61633a82016-01-25 15:54:10 -0800413 ExpandableView child) {
Selim Cinek281c2022016-10-13 19:14:43 -0700414 return algorithmState.getPaddingAfterChild(child);
Selim Cinek61633a82016-01-25 15:54:10 -0800415 }
416
Selim Cineka4baaa32015-04-20 14:27:54 -0700417 private void updateHeadsUpStates(StackScrollState resultState,
418 StackScrollAlgorithmState algorithmState, AmbientState ambientState) {
419 int childCount = algorithmState.visibleChildren.size();
420 ExpandableNotificationRow topHeadsUpEntry = null;
421 for (int i = 0; i < childCount; i++) {
422 View child = algorithmState.visibleChildren.get(i);
423 if (!(child instanceof ExpandableNotificationRow)) {
424 break;
425 }
426 ExpandableNotificationRow row = (ExpandableNotificationRow) child;
427 if (!row.isHeadsUp()) {
428 break;
Selim Cineka4baaa32015-04-20 14:27:54 -0700429 }
Selim Cinekbbcebde2016-11-09 18:28:20 -0800430 ExpandableViewState childState = resultState.getViewStateForView(row);
Selim Cinek9b9d6e12017-11-30 12:29:47 +0100431 if (topHeadsUpEntry == null && row.mustStayOnScreen() && !childState.headsUpIsVisible) {
Selim Cinek3776fe02016-02-04 13:32:43 -0800432 topHeadsUpEntry = row;
Selim Cinekbbcebde2016-11-09 18:28:20 -0800433 childState.location = ExpandableViewState.LOCATION_FIRST_HUN;
Selim Cinek3776fe02016-02-04 13:32:43 -0800434 }
Selim Cinek1f3f5442015-04-10 17:54:46 -0700435 boolean isTopEntry = topHeadsUpEntry == row;
Selim Cinek3776fe02016-02-04 13:32:43 -0800436 float unmodifiedEndLocation = childState.yTranslation + childState.height;
Selim Cinek131c1e22015-05-11 19:04:49 -0700437 if (mIsExpanded) {
Selim Cinek9b9d6e12017-11-30 12:29:47 +0100438 if (row.mustStayOnScreen() && !childState.headsUpIsVisible) {
439 // Ensure that the heads up is always visible even when scrolled off
440 clampHunToTop(ambientState, row, childState);
441 if (i == 0 && ambientState.isAboveShelf(row)) {
442 // the first hun can't get off screen.
443 clampHunToMaxTranslation(ambientState, row, childState);
444 childState.hidden = false;
445 }
Selim Cinekd127d792016-11-01 19:11:41 -0700446 }
Selim Cinek131c1e22015-05-11 19:04:49 -0700447 }
Selim Cinek684a4422015-04-15 16:18:39 -0700448 if (row.isPinned()) {
Selim Cinek33d46142016-01-15 18:58:39 -0800449 childState.yTranslation = Math.max(childState.yTranslation, 0);
Selim Cinek31aada42015-12-18 17:51:15 -0800450 childState.height = Math.max(row.getIntrinsicHeight(), childState.height);
Selim Cinekcafa87f2016-10-26 17:00:17 -0700451 childState.hidden = false;
Selim Cinekbbcebde2016-11-09 18:28:20 -0800452 ExpandableViewState topState = resultState.getViewStateForView(topHeadsUpEntry);
Selim Cinek61cfd4b2017-12-08 12:42:36 -0800453 if (topState != null && !isTopEntry && (!mIsExpanded
Selim Cinek3776fe02016-02-04 13:32:43 -0800454 || unmodifiedEndLocation < topState.yTranslation + topState.height)) {
Selim Cinek684a4422015-04-15 16:18:39 -0700455 // Ensure that a headsUp doesn't vertically extend further than the heads-up at
456 // the top most z-position
Selim Cinek31aada42015-12-18 17:51:15 -0800457 childState.height = row.getIntrinsicHeight();
Selim Cineke53e6bb2015-04-13 16:14:26 -0700458 childState.yTranslation = topState.yTranslation + topState.height
459 - childState.height;
Selim Cinek1f3f5442015-04-10 17:54:46 -0700460 }
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700461 }
Selim Cinekcafa87f2016-10-26 17:00:17 -0700462 if (row.isHeadsUpAnimatingAway()) {
463 childState.hidden = false;
464 }
Selim Cinekb8f09cf2015-03-16 17:09:28 -0700465 }
Selim Cinek67b22602014-03-10 15:40:16 +0100466 }
467
Selim Cinek3776fe02016-02-04 13:32:43 -0800468 private void clampHunToTop(AmbientState ambientState, ExpandableNotificationRow row,
Selim Cinekbbcebde2016-11-09 18:28:20 -0800469 ExpandableViewState childState) {
Selim Cinek3776fe02016-02-04 13:32:43 -0800470 float newTranslation = Math.max(ambientState.getTopPadding()
471 + ambientState.getStackTranslation(), childState.yTranslation);
472 childState.height = (int) Math.max(childState.height - (newTranslation
Selim Cinek567e8452016-03-24 10:54:56 -0700473 - childState.yTranslation), row.getCollapsedHeight());
Selim Cinek3776fe02016-02-04 13:32:43 -0800474 childState.yTranslation = newTranslation;
475 }
476
477 private void clampHunToMaxTranslation(AmbientState ambientState, ExpandableNotificationRow row,
Selim Cinekbbcebde2016-11-09 18:28:20 -0800478 ExpandableViewState childState) {
Selim Cinek3776fe02016-02-04 13:32:43 -0800479 float newTranslation;
Selim Cinek7e0f9482017-05-22 20:00:56 -0700480 float maxHeadsUpTranslation = ambientState.getMaxHeadsUpTranslation();
481 float maxShelfPosition = ambientState.getInnerHeight() + ambientState.getTopPadding()
482 + ambientState.getStackTranslation();
483 maxHeadsUpTranslation = Math.min(maxHeadsUpTranslation, maxShelfPosition);
484 float bottomPosition = maxHeadsUpTranslation - row.getCollapsedHeight();
Selim Cinek3776fe02016-02-04 13:32:43 -0800485 newTranslation = Math.min(childState.yTranslation, bottomPosition);
Selim Cinek7e0f9482017-05-22 20:00:56 -0700486 childState.height = (int) Math.min(childState.height, maxHeadsUpTranslation
487 - newTranslation);
Selim Cinek3776fe02016-02-04 13:32:43 -0800488 childState.yTranslation = newTranslation;
Selim Cinek343e6e22014-04-11 21:23:30 +0200489 }
490
491 /**
Selim Cinek281c2022016-10-13 19:14:43 -0700492 * Clamp the height of the child down such that its end is at most on the beginning of
493 * the shelf.
494 *
Selim Cinek281c2022016-10-13 19:14:43 -0700495 * @param childViewState the view state of the child
496 * @param ambientState the ambient state
497 */
Selim Cineka686b2c2016-10-26 13:58:27 -0700498 private void clampPositionToShelf(ExpandableViewState childViewState,
Selim Cinek281c2022016-10-13 19:14:43 -0700499 AmbientState ambientState) {
Eliot Courtney5d3d2d02018-01-18 15:59:03 +0900500 if (ambientState.getShelf() == null) {
501 return;
502 }
503
Selim Cineka686b2c2016-10-26 13:58:27 -0700504 int shelfStart = ambientState.getInnerHeight()
505 - ambientState.getShelf().getIntrinsicHeight();
Selim Cinek281c2022016-10-13 19:14:43 -0700506 childViewState.yTranslation = Math.min(childViewState.yTranslation, shelfStart);
Selim Cinekc383fd02016-10-21 15:31:26 -0700507 if (childViewState.yTranslation >= shelfStart) {
508 childViewState.hidden = true;
Selim Cinekeccb5de2016-10-28 15:04:05 -0700509 childViewState.inShelf = true;
Selim Cinek9b9d6e12017-11-30 12:29:47 +0100510 childViewState.headsUpIsVisible = false;
Selim Cinekc383fd02016-10-21 15:31:26 -0700511 }
Selim Cinekcafa87f2016-10-26 17:00:17 -0700512 if (!ambientState.isShadeExpanded()) {
513 childViewState.height = (int) (mStatusBarHeight - childViewState.yTranslation);
514 }
Selim Cinek281c2022016-10-13 19:14:43 -0700515 }
516
Muyuan Li87798022016-04-07 17:51:25 -0700517 protected int getMaxAllowedChildHeight(View child) {
Selim Cinek31aada42015-12-18 17:51:15 -0800518 if (child instanceof ExpandableView) {
Jorim Jaggibe565df2014-04-28 17:51:23 +0200519 ExpandableView expandableView = (ExpandableView) child;
Selim Cinek8d5727f2015-04-28 19:17:32 -0700520 return expandableView.getIntrinsicHeight();
Selim Cinek1685e632014-04-08 02:27:49 +0200521 }
Jorim Jaggibe565df2014-04-28 17:51:23 +0200522 return child == null? mCollapsedSize : child.getHeight();
Selim Cinek1685e632014-04-08 02:27:49 +0200523 }
524
Selim Cinek67b22602014-03-10 15:40:16 +0100525 /**
Selim Cinek67b22602014-03-10 15:40:16 +0100526 * Calculate the Z positions for all children based on the number of items in both stacks and
527 * save it in the resultState
Selim Cinek3776fe02016-02-04 13:32:43 -0800528 * @param resultState The result state to update the zTranslation values
Selim Cinek67b22602014-03-10 15:40:16 +0100529 * @param algorithmState The state in which the current pass of the algorithm is currently in
Selim Cinek3776fe02016-02-04 13:32:43 -0800530 * @param ambientState The ambient state of the algorithm
Selim Cinek67b22602014-03-10 15:40:16 +0100531 */
532 private void updateZValuesForState(StackScrollState resultState,
Selim Cinek3776fe02016-02-04 13:32:43 -0800533 StackScrollAlgorithmState algorithmState, AmbientState ambientState) {
Jorim Jaggid4a57442014-04-10 02:45:55 +0200534 int childCount = algorithmState.visibleChildren.size();
Selim Cinek33223572016-02-19 19:32:22 -0800535 float childrenOnTop = 0.0f;
Selim Cinek3776fe02016-02-04 13:32:43 -0800536 for (int i = childCount - 1; i >= 0; i--) {
Selim Cinekdaab6f52017-04-06 16:46:34 -0700537 childrenOnTop = updateChildZValue(i, childrenOnTop,
Muyuan Li87798022016-04-07 17:51:25 -0700538 resultState, algorithmState, ambientState);
539 }
540 }
541
Selim Cinekdaab6f52017-04-06 16:46:34 -0700542 protected float updateChildZValue(int i, float childrenOnTop,
Muyuan Li87798022016-04-07 17:51:25 -0700543 StackScrollState resultState, StackScrollAlgorithmState algorithmState,
544 AmbientState ambientState) {
545 ExpandableView child = algorithmState.visibleChildren.get(i);
Selim Cinekbbcebde2016-11-09 18:28:20 -0800546 ExpandableViewState childViewState = resultState.getViewStateForView(child);
Selim Cinek281c2022016-10-13 19:14:43 -0700547 int zDistanceBetweenElements = ambientState.getZDistanceBetweenElements();
548 float baseZ = ambientState.getBaseZHeight();
Selim Cinek9b9d6e12017-11-30 12:29:47 +0100549 if (child.mustStayOnScreen() && !childViewState.headsUpIsVisible
550 && !ambientState.isDozingAndNotPulsing(child)
Muyuan Li87798022016-04-07 17:51:25 -0700551 && childViewState.yTranslation < ambientState.getTopPadding()
552 + ambientState.getStackTranslation()) {
553 if (childrenOnTop != 0.0f) {
554 childrenOnTop++;
555 } else {
556 float overlap = ambientState.getTopPadding()
557 + ambientState.getStackTranslation() - childViewState.yTranslation;
558 childrenOnTop += Math.min(1.0f, overlap / childViewState.height);
559 }
Selim Cinek281c2022016-10-13 19:14:43 -0700560 childViewState.zTranslation = baseZ
561 + childrenOnTop * zDistanceBetweenElements;
Selim Cinekebf42342017-07-13 15:46:10 +0200562 } else if (i == 0 && ambientState.isAboveShelf(child)) {
Selim Cinekd127d792016-11-01 19:11:41 -0700563 // In case this is a new view that has never been measured before, we don't want to
564 // elevate if we are currently expanded more then the notification
Eliot Courtney5d3d2d02018-01-18 15:59:03 +0900565 int shelfHeight = ambientState.getShelf() == null ? 0 :
566 ambientState.getShelf().getIntrinsicHeight();
Selim Cinekd127d792016-11-01 19:11:41 -0700567 float shelfStart = ambientState.getInnerHeight()
568 - shelfHeight + ambientState.getTopPadding()
569 + ambientState.getStackTranslation();
570 float notificationEnd = childViewState.yTranslation + child.getPinnedHeadsUpHeight()
571 + mPaddingBetweenElements;
572 if (shelfStart > notificationEnd) {
573 childViewState.zTranslation = baseZ;
574 } else {
575 float factor = (notificationEnd - shelfStart) / shelfHeight;
576 factor = Math.min(factor, 1.0f);
577 childViewState.zTranslation = baseZ + factor * zDistanceBetweenElements;
578 }
Muyuan Li87798022016-04-07 17:51:25 -0700579 } else {
Selim Cinek281c2022016-10-13 19:14:43 -0700580 childViewState.zTranslation = baseZ;
Selim Cinek67b22602014-03-10 15:40:16 +0100581 }
Selim Cinekdaab6f52017-04-06 16:46:34 -0700582 return childrenOnTop;
Selim Cinek67b22602014-03-10 15:40:16 +0100583 }
584
Selim Cinek1685e632014-04-08 02:27:49 +0200585 public void setIsExpanded(boolean isExpanded) {
586 this.mIsExpanded = isExpanded;
587 }
588
Selim Cinek281c2022016-10-13 19:14:43 -0700589 public class StackScrollAlgorithmState {
Selim Cinek67b22602014-03-10 15:40:16 +0100590
591 /**
592 * The scroll position of the algorithm
593 */
594 public int scrollY;
595
596 /**
Jorim Jaggid4a57442014-04-10 02:45:55 +0200597 * The children from the host view which are not gone.
598 */
Jorim Jaggibe565df2014-04-28 17:51:23 +0200599 public final ArrayList<ExpandableView> visibleChildren = new ArrayList<ExpandableView>();
Selim Cinek61633a82016-01-25 15:54:10 -0800600
601 /**
Selim Cineka7ed2c12017-01-23 20:47:24 -0800602 * The padding after each child measured in pixels.
Selim Cinek61633a82016-01-25 15:54:10 -0800603 */
Selim Cineka7ed2c12017-01-23 20:47:24 -0800604 public final HashMap<ExpandableView, Float> paddingMap = new HashMap<>();
Selim Cinek281c2022016-10-13 19:14:43 -0700605
606 public int getPaddingAfterChild(ExpandableView child) {
Selim Cineka7ed2c12017-01-23 20:47:24 -0800607 Float padding = paddingMap.get(child);
608 if (padding == null) {
609 // Should only happen for the last view
610 return mPaddingBetweenElements;
611 }
612 return (int) padding.floatValue();
Selim Cinek281c2022016-10-13 19:14:43 -0700613 }
Selim Cinek67b22602014-03-10 15:40:16 +0100614 }
615
616}