Selim Cinek | 67b2260 | 2014-03-10 15:40:16 +0100 | [diff] [blame] | 1 | /* |
| 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 Shah | 20790b8 | 2018-07-02 17:21:04 -0700 | [diff] [blame] | 17 | package com.android.systemui.statusbar.notification.stack; |
Selim Cinek | 67b2260 | 2014-03-10 15:40:16 +0100 | [diff] [blame] | 18 | |
| 19 | import android.content.Context; |
Anthony Chen | 9fe1ee7 | 2017-04-07 13:53:37 -0700 | [diff] [blame] | 20 | import android.content.res.Resources; |
Christoph Studer | 6e3eceb | 2014-04-01 18:40:27 +0200 | [diff] [blame] | 21 | import android.util.Log; |
Selim Cinek | 67b2260 | 2014-03-10 15:40:16 +0100 | [diff] [blame] | 22 | import android.view.View; |
| 23 | import android.view.ViewGroup; |
Gus Prevas | e2d6f04 | 2018-10-17 15:25:30 -0400 | [diff] [blame] | 24 | |
Selim Cinek | 67b2260 | 2014-03-10 15:40:16 +0100 | [diff] [blame] | 25 | import com.android.systemui.R; |
Selim Cinek | cde90e5 | 2016-12-22 21:01:49 +0100 | [diff] [blame] | 26 | import com.android.systemui.statusbar.EmptyShadeView; |
Gus Prevas | e2d6f04 | 2018-10-17 15:25:30 -0400 | [diff] [blame] | 27 | import com.android.systemui.statusbar.NotificationShelf; |
| 28 | import com.android.systemui.statusbar.notification.NotificationUtils; |
Rohan Shah | 20790b8 | 2018-07-02 17:21:04 -0700 | [diff] [blame] | 29 | import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow; |
| 30 | import com.android.systemui.statusbar.notification.row.ExpandableView; |
| 31 | import com.android.systemui.statusbar.notification.row.FooterView; |
Selim Cinek | 67b2260 | 2014-03-10 15:40:16 +0100 | [diff] [blame] | 32 | |
Jorim Jaggi | d4a5744 | 2014-04-10 02:45:55 +0200 | [diff] [blame] | 33 | import java.util.ArrayList; |
Selim Cinek | 42357e0 | 2016-02-24 18:48:01 -0800 | [diff] [blame] | 34 | import java.util.HashMap; |
Selim Cinek | b5605e5 | 2015-02-20 18:21:41 +0100 | [diff] [blame] | 35 | import java.util.List; |
Jorim Jaggi | d4a5744 | 2014-04-10 02:45:55 +0200 | [diff] [blame] | 36 | |
Selim Cinek | 67b2260 | 2014-03-10 15:40:16 +0100 | [diff] [blame] | 37 | /** |
Rohan Shah | 20790b8 | 2018-07-02 17:21:04 -0700 | [diff] [blame] | 38 | * The Algorithm of the {@link com.android.systemui.statusbar.notification.stack |
Selim Cinek | 67b2260 | 2014-03-10 15:40:16 +0100 | [diff] [blame] | 39 | * .NotificationStackScrollLayout} which can be queried for {@link com.android.systemui.statusbar |
| 40 | * .stack.StackScrollState} |
| 41 | */ |
| 42 | public class StackScrollAlgorithm { |
| 43 | |
Gus Prevas | 0fa58d6 | 2019-01-11 13:58:40 -0500 | [diff] [blame] | 44 | static final boolean ANCHOR_SCROLLING = false; |
| 45 | |
Christoph Studer | 6e3eceb | 2014-04-01 18:40:27 +0200 | [diff] [blame] | 46 | private static final String LOG_TAG = "StackScrollAlgorithm"; |
Dave Mankoff | a4d195d | 2018-11-16 13:33:27 -0500 | [diff] [blame] | 47 | private final ViewGroup mHostView; |
Christoph Studer | 6e3eceb | 2014-04-01 18:40:27 +0200 | [diff] [blame] | 48 | |
Selim Cinek | 67b2260 | 2014-03-10 15:40:16 +0100 | [diff] [blame] | 49 | private int mPaddingBetweenElements; |
Selim Cinek | 61633a8 | 2016-01-25 15:54:10 -0800 | [diff] [blame] | 50 | private int mIncreasedPaddingBetweenElements; |
Gus Prevas | e2d6f04 | 2018-10-17 15:25:30 -0400 | [diff] [blame] | 51 | private int mGapHeight; |
Selim Cinek | 67b2260 | 2014-03-10 15:40:16 +0100 | [diff] [blame] | 52 | private int mCollapsedSize; |
Selim Cinek | 67b2260 | 2014-03-10 15:40:16 +0100 | [diff] [blame] | 53 | |
Selim Cinek | 67b2260 | 2014-03-10 15:40:16 +0100 | [diff] [blame] | 54 | private StackScrollAlgorithmState mTempAlgorithmState = new StackScrollAlgorithmState(); |
Selim Cinek | 1685e63 | 2014-04-08 02:27:49 +0200 | [diff] [blame] | 55 | private boolean mIsExpanded; |
Anthony Chen | 9fe1ee7 | 2017-04-07 13:53:37 -0700 | [diff] [blame] | 56 | private boolean mClipNotificationScrollToTop; |
Selim Cinek | cafa87f | 2016-10-26 17:00:17 -0700 | [diff] [blame] | 57 | private int mStatusBarHeight; |
Selim Cinek | aa9db1f | 2018-02-27 17:35:47 -0800 | [diff] [blame] | 58 | private float mHeadsUpInset; |
Selim Cinek | 99e9adf | 2018-03-15 09:17:47 -0700 | [diff] [blame] | 59 | private int mPinnedZTranslationExtra; |
Selim Cinek | 67b2260 | 2014-03-10 15:40:16 +0100 | [diff] [blame] | 60 | |
Dave Mankoff | a4d195d | 2018-11-16 13:33:27 -0500 | [diff] [blame] | 61 | public StackScrollAlgorithm(Context context, ViewGroup hostView) { |
| 62 | mHostView = hostView; |
Selim Cinek | af0dc31 | 2015-12-15 17:01:44 -0800 | [diff] [blame] | 63 | initView(context); |
Selim Cinek | 34c0a8d | 2014-05-12 00:01:43 +0200 | [diff] [blame] | 64 | } |
| 65 | |
Selim Cinek | af0dc31 | 2015-12-15 17:01:44 -0800 | [diff] [blame] | 66 | public void initView(Context context) { |
| 67 | initConstants(context); |
Selim Cinek | 34c0a8d | 2014-05-12 00:01:43 +0200 | [diff] [blame] | 68 | } |
| 69 | |
Selim Cinek | 67b2260 | 2014-03-10 15:40:16 +0100 | [diff] [blame] | 70 | private void initConstants(Context context) { |
Anthony Chen | 9fe1ee7 | 2017-04-07 13:53:37 -0700 | [diff] [blame] | 71 | Resources res = context.getResources(); |
| 72 | mPaddingBetweenElements = res.getDimensionPixelSize( |
Selim Cinek | db16737 | 2016-11-17 15:41:17 -0800 | [diff] [blame] | 73 | R.dimen.notification_divider_height); |
Anthony Chen | 9fe1ee7 | 2017-04-07 13:53:37 -0700 | [diff] [blame] | 74 | mIncreasedPaddingBetweenElements = |
| 75 | res.getDimensionPixelSize(R.dimen.notification_divider_height_increased); |
| 76 | mCollapsedSize = res.getDimensionPixelSize(R.dimen.notification_min_height); |
| 77 | mStatusBarHeight = res.getDimensionPixelSize(R.dimen.status_bar_height); |
| 78 | mClipNotificationScrollToTop = res.getBoolean(R.bool.config_clipNotificationScrollToTop); |
Selim Cinek | aa9db1f | 2018-02-27 17:35:47 -0800 | [diff] [blame] | 79 | mHeadsUpInset = mStatusBarHeight + res.getDimensionPixelSize( |
| 80 | R.dimen.heads_up_status_bar_padding); |
Selim Cinek | 99e9adf | 2018-03-15 09:17:47 -0700 | [diff] [blame] | 81 | mPinnedZTranslationExtra = res.getDimensionPixelSize( |
| 82 | R.dimen.heads_up_pinned_elevation); |
Gus Prevas | e2d6f04 | 2018-10-17 15:25:30 -0400 | [diff] [blame] | 83 | mGapHeight = res.getDimensionPixelSize(R.dimen.notification_section_divider_height); |
Jorim Jaggi | d7c1fae | 2014-08-13 18:27:47 +0200 | [diff] [blame] | 84 | } |
Selim Cinek | 67b2260 | 2014-03-10 15:40:16 +0100 | [diff] [blame] | 85 | |
Dave Mankoff | a4d195d | 2018-11-16 13:33:27 -0500 | [diff] [blame] | 86 | /** |
| 87 | * Updates the state of all children in the hostview based on this algorithm. |
| 88 | */ |
| 89 | public void resetViewStates(AmbientState ambientState) { |
Selim Cinek | 67b2260 | 2014-03-10 15:40:16 +0100 | [diff] [blame] | 90 | // The state of the local variables are saved in an algorithmState to easily subdivide it |
| 91 | // into multiple phases. |
| 92 | StackScrollAlgorithmState algorithmState = mTempAlgorithmState; |
| 93 | |
| 94 | // First we reset the view states to their default values. |
Dave Mankoff | a4d195d | 2018-11-16 13:33:27 -0500 | [diff] [blame] | 95 | resetChildViewStates(); |
Selim Cinek | 67b2260 | 2014-03-10 15:40:16 +0100 | [diff] [blame] | 96 | |
Dave Mankoff | a4d195d | 2018-11-16 13:33:27 -0500 | [diff] [blame] | 97 | initAlgorithmState(mHostView, algorithmState, ambientState); |
Selim Cinek | 1408eb5 | 2014-06-02 14:45:38 +0200 | [diff] [blame] | 98 | |
Dave Mankoff | a4d195d | 2018-11-16 13:33:27 -0500 | [diff] [blame] | 99 | updatePositionsForState(algorithmState, ambientState); |
Selim Cinek | 67b2260 | 2014-03-10 15:40:16 +0100 | [diff] [blame] | 100 | |
Dave Mankoff | a4d195d | 2018-11-16 13:33:27 -0500 | [diff] [blame] | 101 | updateZValuesForState(algorithmState, ambientState); |
Selim Cinek | 3776fe0 | 2016-02-04 13:32:43 -0800 | [diff] [blame] | 102 | |
Dave Mankoff | a4d195d | 2018-11-16 13:33:27 -0500 | [diff] [blame] | 103 | updateHeadsUpStates(algorithmState, ambientState); |
Selim Cinek | 5040f2e | 2019-02-14 18:22:42 -0800 | [diff] [blame] | 104 | updatePulsingStates(algorithmState, ambientState); |
Selim Cinek | eb97356 | 2014-05-02 17:07:49 +0200 | [diff] [blame] | 105 | |
Dave Mankoff | a4d195d | 2018-11-16 13:33:27 -0500 | [diff] [blame] | 106 | updateDimmedActivatedHideSensitive(ambientState, algorithmState); |
| 107 | updateClipping(algorithmState, ambientState); |
| 108 | updateSpeedBumpState(algorithmState, ambientState); |
| 109 | updateShelfState(ambientState); |
| 110 | getNotificationChildrenStates(algorithmState, ambientState); |
Selim Cinek | b5605e5 | 2015-02-20 18:21:41 +0100 | [diff] [blame] | 111 | } |
| 112 | |
Dave Mankoff | a4d195d | 2018-11-16 13:33:27 -0500 | [diff] [blame] | 113 | private void resetChildViewStates() { |
| 114 | int numChildren = mHostView.getChildCount(); |
| 115 | for (int i = 0; i < numChildren; i++) { |
| 116 | ExpandableView child = (ExpandableView) mHostView.getChildAt(i); |
| 117 | child.resetViewState(); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | private void getNotificationChildrenStates(StackScrollAlgorithmState algorithmState, |
Selim Cinek | c25989e | 2018-02-16 16:42:14 -0800 | [diff] [blame] | 122 | AmbientState ambientState) { |
Selim Cinek | b5605e5 | 2015-02-20 18:21:41 +0100 | [diff] [blame] | 123 | int childCount = algorithmState.visibleChildren.size(); |
| 124 | for (int i = 0; i < childCount; i++) { |
| 125 | ExpandableView v = algorithmState.visibleChildren.get(i); |
| 126 | if (v instanceof ExpandableNotificationRow) { |
| 127 | ExpandableNotificationRow row = (ExpandableNotificationRow) v; |
Dave Mankoff | a4d195d | 2018-11-16 13:33:27 -0500 | [diff] [blame] | 128 | row.updateChildrenStates(ambientState); |
Selim Cinek | b5605e5 | 2015-02-20 18:21:41 +0100 | [diff] [blame] | 129 | } |
| 130 | } |
Selim Cinek | 3d2b94bf | 2014-07-02 22:12:47 +0200 | [diff] [blame] | 131 | } |
| 132 | |
Dave Mankoff | a4d195d | 2018-11-16 13:33:27 -0500 | [diff] [blame] | 133 | private void updateSpeedBumpState(StackScrollAlgorithmState algorithmState, |
| 134 | AmbientState ambientState) { |
Selim Cinek | 3d2b94bf | 2014-07-02 22:12:47 +0200 | [diff] [blame] | 135 | int childCount = algorithmState.visibleChildren.size(); |
Selim Cinek | db16737 | 2016-11-17 15:41:17 -0800 | [diff] [blame] | 136 | int belowSpeedBump = ambientState.getSpeedBumpIndex(); |
Selim Cinek | 3d2b94bf | 2014-07-02 22:12:47 +0200 | [diff] [blame] | 137 | for (int i = 0; i < childCount; i++) { |
Dave Mankoff | a4d195d | 2018-11-16 13:33:27 -0500 | [diff] [blame] | 138 | ExpandableView child = algorithmState.visibleChildren.get(i); |
| 139 | ExpandableViewState childViewState = child.getViewState(); |
Selim Cinek | 3107cfa | 2014-07-22 15:24:29 +0200 | [diff] [blame] | 140 | |
| 141 | // The speed bump can also be gone, so equality needs to be taken when comparing |
| 142 | // indices. |
Selim Cinek | db16737 | 2016-11-17 15:41:17 -0800 | [diff] [blame] | 143 | childViewState.belowSpeedBump = i >= belowSpeedBump; |
Selim Cinek | 3d2b94bf | 2014-07-02 22:12:47 +0200 | [diff] [blame] | 144 | } |
Selim Cinek | db16737 | 2016-11-17 15:41:17 -0800 | [diff] [blame] | 145 | |
| 146 | } |
Dave Mankoff | a4d195d | 2018-11-16 13:33:27 -0500 | [diff] [blame] | 147 | |
| 148 | private void updateShelfState(AmbientState ambientState) { |
Selim Cinek | 281c202 | 2016-10-13 19:14:43 -0700 | [diff] [blame] | 149 | NotificationShelf shelf = ambientState.getShelf(); |
Eliot Courtney | 5d3d2d0 | 2018-01-18 15:59:03 +0900 | [diff] [blame] | 150 | if (shelf != null) { |
Dave Mankoff | a4d195d | 2018-11-16 13:33:27 -0500 | [diff] [blame] | 151 | shelf.updateState(ambientState); |
Eliot Courtney | 5d3d2d0 | 2018-01-18 15:59:03 +0900 | [diff] [blame] | 152 | } |
Selim Cinek | f54090e | 2014-06-17 17:24:51 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Dave Mankoff | a4d195d | 2018-11-16 13:33:27 -0500 | [diff] [blame] | 155 | private void updateClipping(StackScrollAlgorithmState algorithmState, |
| 156 | AmbientState ambientState) { |
Selim Cinek | 355652a | 2016-12-07 13:32:12 -0800 | [diff] [blame] | 157 | float drawStart = !ambientState.isOnKeyguard() ? ambientState.getTopPadding() |
Selim Cinek | 2627d72 | 2018-01-19 12:16:49 -0800 | [diff] [blame] | 158 | + ambientState.getStackTranslation() + ambientState.getExpandAnimationTopChange() |
| 159 | : 0; |
Selim Cinek | 708a6c1 | 2014-05-28 14:16:02 +0200 | [diff] [blame] | 160 | float previousNotificationEnd = 0; |
| 161 | float previousNotificationStart = 0; |
Selim Cinek | 708a6c1 | 2014-05-28 14:16:02 +0200 | [diff] [blame] | 162 | int childCount = algorithmState.visibleChildren.size(); |
| 163 | for (int i = 0; i < childCount; i++) { |
| 164 | ExpandableView child = algorithmState.visibleChildren.get(i); |
Dave Mankoff | a4d195d | 2018-11-16 13:33:27 -0500 | [diff] [blame] | 165 | ExpandableViewState state = child.getViewState(); |
Selim Cinek | 9b9d6e1 | 2017-11-30 12:29:47 +0100 | [diff] [blame] | 166 | if (!child.mustStayOnScreen() || state.headsUpIsVisible) { |
Selim Cinek | 3776fe0 | 2016-02-04 13:32:43 -0800 | [diff] [blame] | 167 | previousNotificationEnd = Math.max(drawStart, previousNotificationEnd); |
| 168 | previousNotificationStart = Math.max(drawStart, previousNotificationStart); |
| 169 | } |
Selim Cinek | 587cbf3 | 2016-01-19 11:36:18 -0800 | [diff] [blame] | 170 | float newYTranslation = state.yTranslation; |
| 171 | float newHeight = state.height; |
Selim Cinek | 708a6c1 | 2014-05-28 14:16:02 +0200 | [diff] [blame] | 172 | float newNotificationEnd = newYTranslation + newHeight; |
Mady Mellor | 53ac1ef | 2016-06-20 13:11:38 -0700 | [diff] [blame] | 173 | boolean isHeadsUp = (child instanceof ExpandableNotificationRow) |
| 174 | && ((ExpandableNotificationRow) child).isPinned(); |
Anthony Chen | 9fe1ee7 | 2017-04-07 13:53:37 -0700 | [diff] [blame] | 175 | if (mClipNotificationScrollToTop |
| 176 | && !state.inShelf && newYTranslation < previousNotificationEnd |
Jorim Jaggi | 0fdf574 | 2016-06-27 11:50:58 -0700 | [diff] [blame] | 177 | && (!isHeadsUp || ambientState.isShadeExpanded())) { |
Mady Mellor | c128f22 | 2016-04-26 11:42:46 -0700 | [diff] [blame] | 178 | // The previous view is overlapping on top, clip! |
| 179 | float overlapAmount = previousNotificationEnd - newYTranslation; |
| 180 | state.clipTopAmount = (int) overlapAmount; |
Selim Cinek | c5baa3e | 2014-10-29 19:04:19 +0100 | [diff] [blame] | 181 | } else { |
Mady Mellor | c128f22 | 2016-04-26 11:42:46 -0700 | [diff] [blame] | 182 | state.clipTopAmount = 0; |
Selim Cinek | 9c17b77 | 2015-07-07 20:37:09 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Selim Cinek | 708a6c1 | 2014-05-28 14:16:02 +0200 | [diff] [blame] | 185 | if (!child.isTransparent()) { |
| 186 | // Only update the previous values if we are not transparent, |
| 187 | // otherwise we would clip to a transparent view. |
Mady Mellor | c128f22 | 2016-04-26 11:42:46 -0700 | [diff] [blame] | 188 | previousNotificationEnd = newNotificationEnd; |
| 189 | previousNotificationStart = newYTranslation; |
Selim Cinek | 708a6c1 | 2014-05-28 14:16:02 +0200 | [diff] [blame] | 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
Selim Cinek | 9c17b77 | 2015-07-07 20:37:09 -0700 | [diff] [blame] | 194 | public static boolean canChildBeDismissed(View v) { |
Selim Cinek | 9e624e7 | 2016-07-20 13:46:49 -0700 | [diff] [blame] | 195 | if (!(v instanceof ExpandableNotificationRow)) { |
| 196 | return false; |
Selim Cinek | 38d429f | 2016-06-03 11:46:56 -0700 | [diff] [blame] | 197 | } |
Selim Cinek | 9e624e7 | 2016-07-20 13:46:49 -0700 | [diff] [blame] | 198 | ExpandableNotificationRow row = (ExpandableNotificationRow) v; |
Nadia Benbernou | 7a18c81 | 2019-02-08 16:23:10 -0500 | [diff] [blame] | 199 | if (row.isBlockingHelperShowingAndTranslationFinished()) { |
| 200 | return true; |
| 201 | } |
Aaron Heuckroth | 1dd67cb | 2018-06-14 14:28:08 -0400 | [diff] [blame] | 202 | if (row.areGutsExposed() || !row.getEntry().hasFinishedInitialization()) { |
Selim Cinek | 9e624e7 | 2016-07-20 13:46:49 -0700 | [diff] [blame] | 203 | return false; |
| 204 | } |
| 205 | return row.canViewBeDismissed(); |
Selim Cinek | 9c17b77 | 2015-07-07 20:37:09 -0700 | [diff] [blame] | 206 | } |
| 207 | |
Selim Cinek | 708a6c1 | 2014-05-28 14:16:02 +0200 | [diff] [blame] | 208 | /** |
Jorim Jaggi | ae44128 | 2014-08-01 02:45:18 +0200 | [diff] [blame] | 209 | * Updates the dimmed, activated and hiding sensitive states of the children. |
Jorim Jaggi | d552d9d | 2014-05-07 19:41:13 +0200 | [diff] [blame] | 210 | */ |
Jorim Jaggi | ae44128 | 2014-08-01 02:45:18 +0200 | [diff] [blame] | 211 | private void updateDimmedActivatedHideSensitive(AmbientState ambientState, |
Dave Mankoff | a4d195d | 2018-11-16 13:33:27 -0500 | [diff] [blame] | 212 | StackScrollAlgorithmState algorithmState) { |
Jorim Jaggi | d552d9d | 2014-05-07 19:41:13 +0200 | [diff] [blame] | 213 | boolean dimmed = ambientState.isDimmed(); |
Lucas Dupin | 16cfe45 | 2018-02-08 13:14:50 -0800 | [diff] [blame] | 214 | boolean dark = ambientState.isFullyDark(); |
Jorim Jaggi | ae44128 | 2014-08-01 02:45:18 +0200 | [diff] [blame] | 215 | boolean hideSensitive = ambientState.isHideSensitive(); |
Jorim Jaggi | d552d9d | 2014-05-07 19:41:13 +0200 | [diff] [blame] | 216 | View activatedChild = ambientState.getActivatedChild(); |
| 217 | int childCount = algorithmState.visibleChildren.size(); |
| 218 | for (int i = 0; i < childCount; i++) { |
Dave Mankoff | a4d195d | 2018-11-16 13:33:27 -0500 | [diff] [blame] | 219 | ExpandableView child = algorithmState.visibleChildren.get(i); |
| 220 | ExpandableViewState childViewState = child.getViewState(); |
Jorim Jaggi | d552d9d | 2014-05-07 19:41:13 +0200 | [diff] [blame] | 221 | childViewState.dimmed = dimmed; |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 222 | childViewState.dark = dark; |
Jorim Jaggi | ae44128 | 2014-08-01 02:45:18 +0200 | [diff] [blame] | 223 | childViewState.hideSensitive = hideSensitive; |
Selim Cinek | b89de4e | 2014-06-10 10:47:05 +0200 | [diff] [blame] | 224 | boolean isActivatedChild = activatedChild == child; |
Jorim Jaggi | 4538cee | 2014-09-09 15:21:38 +0200 | [diff] [blame] | 225 | if (dimmed && isActivatedChild) { |
Selim Cinek | 281c202 | 2016-10-13 19:14:43 -0700 | [diff] [blame] | 226 | childViewState.zTranslation += 2.0f * ambientState.getZDistanceBetweenElements(); |
Jorim Jaggi | d552d9d | 2014-05-07 19:41:13 +0200 | [diff] [blame] | 227 | } |
| 228 | } |
Selim Cinek | eb97356 | 2014-05-02 17:07:49 +0200 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | /** |
Selim Cinek | 61633a8 | 2016-01-25 15:54:10 -0800 | [diff] [blame] | 232 | * Initialize the algorithm state like updating the visible children. |
Jorim Jaggi | d4a5744 | 2014-04-10 02:45:55 +0200 | [diff] [blame] | 233 | */ |
Dave Mankoff | a4d195d | 2018-11-16 13:33:27 -0500 | [diff] [blame] | 234 | private void initAlgorithmState(ViewGroup hostView, StackScrollAlgorithmState state, |
Selim Cinek | 3776fe0 | 2016-02-04 13:32:43 -0800 | [diff] [blame] | 235 | AmbientState ambientState) { |
Selim Cinek | 3776fe0 | 2016-02-04 13:32:43 -0800 | [diff] [blame] | 236 | float bottomOverScroll = ambientState.getOverScrollAmount(false /* onTop */); |
| 237 | |
| 238 | int scrollY = ambientState.getScrollY(); |
| 239 | |
| 240 | // Due to the overScroller, the stackscroller can have negative scroll state. This is |
| 241 | // already accounted for by the top padding and doesn't need an additional adaption |
| 242 | scrollY = Math.max(0, scrollY); |
| 243 | state.scrollY = (int) (scrollY + bottomOverScroll); |
| 244 | |
Gus Prevas | 0fa58d6 | 2019-01-11 13:58:40 -0500 | [diff] [blame] | 245 | if (ANCHOR_SCROLLING) { |
| 246 | state.anchorViewY = (int) (ambientState.getAnchorViewY() - bottomOverScroll); |
| 247 | } |
| 248 | |
Selim Cinek | 3776fe0 | 2016-02-04 13:32:43 -0800 | [diff] [blame] | 249 | //now init the visible children and update paddings |
Jorim Jaggi | d4a5744 | 2014-04-10 02:45:55 +0200 | [diff] [blame] | 250 | int childCount = hostView.getChildCount(); |
| 251 | state.visibleChildren.clear(); |
| 252 | state.visibleChildren.ensureCapacity(childCount); |
Selim Cinek | a7ed2c1 | 2017-01-23 20:47:24 -0800 | [diff] [blame] | 253 | state.paddingMap.clear(); |
Selim Cinek | b036ca4 | 2015-02-20 15:56:28 +0100 | [diff] [blame] | 254 | int notGoneIndex = 0; |
Selim Cinek | 61633a8 | 2016-01-25 15:54:10 -0800 | [diff] [blame] | 255 | ExpandableView lastView = null; |
Selim Cinek | d96ed40 | 2017-07-28 18:19:03 -0700 | [diff] [blame] | 256 | int firstHiddenIndex = ambientState.isDark() |
| 257 | ? (ambientState.hasPulsingNotifications() ? 1 : 0) |
| 258 | : childCount; |
| 259 | |
| 260 | // The goal here is to fill the padding map, by iterating over how much padding each child |
| 261 | // needs. The map is thereby reused, by first filling it with the padding amount and when |
| 262 | // iterating over it again, it's filled with the actual resolved value. |
| 263 | |
Jorim Jaggi | d4a5744 | 2014-04-10 02:45:55 +0200 | [diff] [blame] | 264 | for (int i = 0; i < childCount; i++) { |
Gus Prevas | 0fa58d6 | 2019-01-11 13:58:40 -0500 | [diff] [blame] | 265 | if (ANCHOR_SCROLLING) { |
| 266 | if (i == ambientState.getAnchorViewIndex()) { |
| 267 | state.anchorViewIndex = state.visibleChildren.size(); |
| 268 | } |
| 269 | } |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 270 | ExpandableView v = (ExpandableView) hostView.getChildAt(i); |
Jorim Jaggi | d4a5744 | 2014-04-10 02:45:55 +0200 | [diff] [blame] | 271 | if (v.getVisibility() != View.GONE) { |
Selim Cinek | 281c202 | 2016-10-13 19:14:43 -0700 | [diff] [blame] | 272 | if (v == ambientState.getShelf()) { |
| 273 | continue; |
| 274 | } |
Selim Cinek | d96ed40 | 2017-07-28 18:19:03 -0700 | [diff] [blame] | 275 | if (i >= firstHiddenIndex) { |
| 276 | // we need normal padding now, to be in sync with what the stack calculates |
| 277 | lastView = null; |
Selim Cinek | d96ed40 | 2017-07-28 18:19:03 -0700 | [diff] [blame] | 278 | } |
Dave Mankoff | a4d195d | 2018-11-16 13:33:27 -0500 | [diff] [blame] | 279 | notGoneIndex = updateNotGoneIndex(state, notGoneIndex, v); |
Selim Cinek | d96ed40 | 2017-07-28 18:19:03 -0700 | [diff] [blame] | 280 | float increasedPadding = v.getIncreasedPaddingAmount(); |
Selim Cinek | 42357e0 | 2016-02-24 18:48:01 -0800 | [diff] [blame] | 281 | if (increasedPadding != 0.0f) { |
Selim Cinek | a7ed2c1 | 2017-01-23 20:47:24 -0800 | [diff] [blame] | 282 | state.paddingMap.put(v, increasedPadding); |
Selim Cinek | 61633a8 | 2016-01-25 15:54:10 -0800 | [diff] [blame] | 283 | if (lastView != null) { |
Selim Cinek | a7ed2c1 | 2017-01-23 20:47:24 -0800 | [diff] [blame] | 284 | Float prevValue = state.paddingMap.get(lastView); |
| 285 | float newValue = getPaddingForValue(increasedPadding); |
| 286 | if (prevValue != null) { |
| 287 | float prevPadding = getPaddingForValue(prevValue); |
| 288 | if (increasedPadding > 0) { |
| 289 | newValue = NotificationUtils.interpolate( |
| 290 | prevPadding, |
| 291 | newValue, |
| 292 | increasedPadding); |
| 293 | } else if (prevValue > 0) { |
| 294 | newValue = NotificationUtils.interpolate( |
| 295 | newValue, |
| 296 | prevPadding, |
| 297 | prevValue); |
| 298 | } |
| 299 | } |
| 300 | state.paddingMap.put(lastView, newValue); |
Selim Cinek | 61633a8 | 2016-01-25 15:54:10 -0800 | [diff] [blame] | 301 | } |
Selim Cinek | a7ed2c1 | 2017-01-23 20:47:24 -0800 | [diff] [blame] | 302 | } else if (lastView != null) { |
Selim Cinek | d96ed40 | 2017-07-28 18:19:03 -0700 | [diff] [blame] | 303 | |
| 304 | // Let's now resolve the value to an actual padding |
Selim Cinek | a7ed2c1 | 2017-01-23 20:47:24 -0800 | [diff] [blame] | 305 | float newValue = getPaddingForValue(state.paddingMap.get(lastView)); |
| 306 | state.paddingMap.put(lastView, newValue); |
Selim Cinek | 61633a8 | 2016-01-25 15:54:10 -0800 | [diff] [blame] | 307 | } |
Selim Cinek | b5605e5 | 2015-02-20 18:21:41 +0100 | [diff] [blame] | 308 | if (v instanceof ExpandableNotificationRow) { |
| 309 | ExpandableNotificationRow row = (ExpandableNotificationRow) v; |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 310 | |
| 311 | // handle the notgoneIndex for the children as well |
Dave Mankoff | a4d195d | 2018-11-16 13:33:27 -0500 | [diff] [blame] | 312 | List<ExpandableNotificationRow> children = row.getNotificationChildren(); |
Selim Cinek | 83bc783 | 2015-10-22 13:26:54 -0700 | [diff] [blame] | 313 | if (row.isSummaryWithChildren() && children != null) { |
Selim Cinek | b5605e5 | 2015-02-20 18:21:41 +0100 | [diff] [blame] | 314 | for (ExpandableNotificationRow childRow : children) { |
| 315 | if (childRow.getVisibility() != View.GONE) { |
Dave Mankoff | a4d195d | 2018-11-16 13:33:27 -0500 | [diff] [blame] | 316 | ExpandableViewState childState = childRow.getViewState(); |
Selim Cinek | b5605e5 | 2015-02-20 18:21:41 +0100 | [diff] [blame] | 317 | childState.notGoneIndex = notGoneIndex; |
| 318 | notGoneIndex++; |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | } |
Selim Cinek | 61633a8 | 2016-01-25 15:54:10 -0800 | [diff] [blame] | 323 | lastView = v; |
Jorim Jaggi | d4a5744 | 2014-04-10 02:45:55 +0200 | [diff] [blame] | 324 | } |
| 325 | } |
Selim Cinek | 2627d72 | 2018-01-19 12:16:49 -0800 | [diff] [blame] | 326 | ExpandableNotificationRow expandingNotification = ambientState.getExpandingNotification(); |
| 327 | state.indexOfExpandingNotification = expandingNotification != null |
Selim Cinek | c25989e | 2018-02-16 16:42:14 -0800 | [diff] [blame] | 328 | ? expandingNotification.isChildInGroup() |
Dave Mankoff | a4d195d | 2018-11-16 13:33:27 -0500 | [diff] [blame] | 329 | ? state.visibleChildren.indexOf(expandingNotification.getNotificationParent()) |
| 330 | : state.visibleChildren.indexOf(expandingNotification) |
Selim Cinek | 2627d72 | 2018-01-19 12:16:49 -0800 | [diff] [blame] | 331 | : -1; |
Jorim Jaggi | d4a5744 | 2014-04-10 02:45:55 +0200 | [diff] [blame] | 332 | } |
| 333 | |
Selim Cinek | a7ed2c1 | 2017-01-23 20:47:24 -0800 | [diff] [blame] | 334 | private float getPaddingForValue(Float increasedPadding) { |
| 335 | if (increasedPadding == null) { |
| 336 | return mPaddingBetweenElements; |
| 337 | } else if (increasedPadding >= 0.0f) { |
| 338 | return NotificationUtils.interpolate( |
| 339 | mPaddingBetweenElements, |
| 340 | mIncreasedPaddingBetweenElements, |
| 341 | increasedPadding); |
| 342 | } else { |
| 343 | return NotificationUtils.interpolate( |
| 344 | 0, |
| 345 | mPaddingBetweenElements, |
| 346 | 1.0f + increasedPadding); |
| 347 | } |
| 348 | } |
| 349 | |
Dave Mankoff | a4d195d | 2018-11-16 13:33:27 -0500 | [diff] [blame] | 350 | private int updateNotGoneIndex(StackScrollAlgorithmState state, int notGoneIndex, |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 351 | ExpandableView v) { |
Dave Mankoff | a4d195d | 2018-11-16 13:33:27 -0500 | [diff] [blame] | 352 | ExpandableViewState viewState = v.getViewState(); |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 353 | viewState.notGoneIndex = notGoneIndex; |
| 354 | state.visibleChildren.add(v); |
| 355 | notGoneIndex++; |
| 356 | return notGoneIndex; |
| 357 | } |
| 358 | |
Jorim Jaggi | d4a5744 | 2014-04-10 02:45:55 +0200 | [diff] [blame] | 359 | /** |
Selim Cinek | 67b2260 | 2014-03-10 15:40:16 +0100 | [diff] [blame] | 360 | * Determine the positions for the views. This is the main part of the algorithm. |
| 361 | * |
Selim Cinek | 67b2260 | 2014-03-10 15:40:16 +0100 | [diff] [blame] | 362 | * @param algorithmState The state in which the current pass of the algorithm is currently in |
Dave Mankoff | a4d195d | 2018-11-16 13:33:27 -0500 | [diff] [blame] | 363 | * @param ambientState The current ambient state |
Selim Cinek | 67b2260 | 2014-03-10 15:40:16 +0100 | [diff] [blame] | 364 | */ |
Dave Mankoff | a4d195d | 2018-11-16 13:33:27 -0500 | [diff] [blame] | 365 | private void updatePositionsForState(StackScrollAlgorithmState algorithmState, |
| 366 | AmbientState ambientState) { |
Selim Cinek | 67b2260 | 2014-03-10 15:40:16 +0100 | [diff] [blame] | 367 | |
Gus Prevas | 0fa58d6 | 2019-01-11 13:58:40 -0500 | [diff] [blame] | 368 | if (ANCHOR_SCROLLING) { |
| 369 | float currentYPosition = algorithmState.anchorViewY; |
| 370 | int childCount = algorithmState.visibleChildren.size(); |
| 371 | for (int i = algorithmState.anchorViewIndex; i < childCount; i++) { |
| 372 | if (i > algorithmState.anchorViewIndex && ambientState.beginsNewSection(i)) { |
| 373 | currentYPosition += mGapHeight; |
| 374 | } |
| 375 | currentYPosition = updateChild(i, algorithmState, ambientState, currentYPosition, |
| 376 | false /* reverse */); |
| 377 | } |
| 378 | currentYPosition = algorithmState.anchorViewY; |
| 379 | for (int i = algorithmState.anchorViewIndex - 1; i >= 0; i--) { |
| 380 | if (ambientState.beginsNewSection(i + 1)) { |
| 381 | currentYPosition -= mGapHeight; |
| 382 | } |
| 383 | currentYPosition = updateChild(i, algorithmState, ambientState, currentYPosition, |
| 384 | true /* reverse */); |
| 385 | } |
| 386 | } else { |
| 387 | // The y coordinate of the current child. |
| 388 | float currentYPosition = -algorithmState.scrollY; |
| 389 | int childCount = algorithmState.visibleChildren.size(); |
| 390 | for (int i = 0; i < childCount; i++) { |
| 391 | if (ambientState.beginsNewSection(i)) { |
| 392 | currentYPosition += mGapHeight; |
| 393 | } |
| 394 | currentYPosition = updateChild(i, algorithmState, ambientState, currentYPosition, |
| 395 | false /* reverse */); |
| 396 | } |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 397 | } |
| 398 | } |
| 399 | |
Gus Prevas | 0fa58d6 | 2019-01-11 13:58:40 -0500 | [diff] [blame] | 400 | /** |
| 401 | * Populates the {@link ExpandableViewState} for a single child. |
| 402 | * |
| 403 | * @param i The index of the child in |
| 404 | * {@link StackScrollAlgorithmState#visibleChildren}. |
| 405 | * @param algorithmState The overall output state of the algorithm. |
| 406 | * @param ambientState The input state provided to the algorithm. |
| 407 | * @param currentYPosition The Y position of the current pass of the algorithm. For a forward |
| 408 | * pass, this should be the top of the child; for a reverse pass, the |
| 409 | * bottom of the child. |
| 410 | * @param reverse Whether we're laying out children in the reverse direction (Y |
| 411 | * positions |
| 412 | * decreasing) instead of the forward direction (Y positions |
| 413 | * increasing). |
| 414 | * @return The Y position after laying out the child. This will be the {@code currentYPosition} |
| 415 | * for the next call to this method, after adjusting for any gaps between children. |
| 416 | */ |
Dave Mankoff | a4d195d | 2018-11-16 13:33:27 -0500 | [diff] [blame] | 417 | protected float updateChild( |
| 418 | int i, |
| 419 | StackScrollAlgorithmState algorithmState, |
| 420 | AmbientState ambientState, |
Gus Prevas | 0fa58d6 | 2019-01-11 13:58:40 -0500 | [diff] [blame] | 421 | float currentYPosition, |
| 422 | boolean reverse) { |
Muyuan Li | 8779802 | 2016-04-07 17:51:25 -0700 | [diff] [blame] | 423 | ExpandableView child = algorithmState.visibleChildren.get(i); |
Dave Mankoff | a4d195d | 2018-11-16 13:33:27 -0500 | [diff] [blame] | 424 | ExpandableViewState childViewState = child.getViewState(); |
Selim Cinek | bbcebde | 2016-11-09 18:28:20 -0800 | [diff] [blame] | 425 | childViewState.location = ExpandableViewState.LOCATION_UNKNOWN; |
Muyuan Li | 8779802 | 2016-04-07 17:51:25 -0700 | [diff] [blame] | 426 | int paddingAfterChild = getPaddingAfterChild(algorithmState, child); |
| 427 | int childHeight = getMaxAllowedChildHeight(child); |
Gus Prevas | 0fa58d6 | 2019-01-11 13:58:40 -0500 | [diff] [blame] | 428 | if (reverse) { |
| 429 | childViewState.yTranslation = currentYPosition - (childHeight + paddingAfterChild); |
| 430 | if (currentYPosition <= 0) { |
| 431 | childViewState.location = ExpandableViewState.LOCATION_HIDDEN_TOP; |
| 432 | } |
| 433 | } else { |
| 434 | childViewState.yTranslation = currentYPosition; |
Gus Prevas | e2d6f04 | 2018-10-17 15:25:30 -0400 | [diff] [blame] | 435 | } |
Julia Reynolds | ed1c9af | 2018-03-21 15:21:09 -0400 | [diff] [blame] | 436 | boolean isFooterView = child instanceof FooterView; |
Selim Cinek | cde90e5 | 2016-12-22 21:01:49 +0100 | [diff] [blame] | 437 | boolean isEmptyShadeView = child instanceof EmptyShadeView; |
Muyuan Li | 8779802 | 2016-04-07 17:51:25 -0700 | [diff] [blame] | 438 | |
Selim Cinek | db16737 | 2016-11-17 15:41:17 -0800 | [diff] [blame] | 439 | childViewState.location = ExpandableViewState.LOCATION_MAIN_AREA; |
Selim Cinek | 9b9d6e1 | 2017-11-30 12:29:47 +0100 | [diff] [blame] | 440 | float inset = ambientState.getTopPadding() + ambientState.getStackTranslation(); |
Selim Cinek | c25989e | 2018-02-16 16:42:14 -0800 | [diff] [blame] | 441 | if (i <= algorithmState.getIndexOfExpandingNotification()) { |
Selim Cinek | 2627d72 | 2018-01-19 12:16:49 -0800 | [diff] [blame] | 442 | inset += ambientState.getExpandAnimationTopChange(); |
| 443 | } |
Selim Cinek | 9b9d6e1 | 2017-11-30 12:29:47 +0100 | [diff] [blame] | 444 | if (child.mustStayOnScreen() && childViewState.yTranslation >= 0) { |
| 445 | // Even if we're not scrolled away we're in view and we're also not in the |
| 446 | // shelf. We can relax the constraints and let us scroll off the top! |
| 447 | float end = childViewState.yTranslation + childViewState.height + inset; |
| 448 | childViewState.headsUpIsVisible = end < ambientState.getMaxHeadsUpTranslation(); |
| 449 | } |
Julia Reynolds | ed1c9af | 2018-03-21 15:21:09 -0400 | [diff] [blame] | 450 | if (isFooterView) { |
Selim Cinek | db16737 | 2016-11-17 15:41:17 -0800 | [diff] [blame] | 451 | childViewState.yTranslation = Math.min(childViewState.yTranslation, |
| 452 | ambientState.getInnerHeight() - childHeight); |
Selim Cinek | cde90e5 | 2016-12-22 21:01:49 +0100 | [diff] [blame] | 453 | } else if (isEmptyShadeView) { |
| 454 | childViewState.yTranslation = ambientState.getInnerHeight() - childHeight |
| 455 | + ambientState.getStackTranslation() * 0.25f; |
Muyuan Li | 8779802 | 2016-04-07 17:51:25 -0700 | [diff] [blame] | 456 | } else { |
Selim Cinek | 2627d72 | 2018-01-19 12:16:49 -0800 | [diff] [blame] | 457 | clampPositionToShelf(child, childViewState, ambientState); |
Muyuan Li | 8779802 | 2016-04-07 17:51:25 -0700 | [diff] [blame] | 458 | } |
| 459 | |
Gus Prevas | 0fa58d6 | 2019-01-11 13:58:40 -0500 | [diff] [blame] | 460 | if (reverse) { |
| 461 | currentYPosition = childViewState.yTranslation; |
| 462 | } else { |
| 463 | currentYPosition = childViewState.yTranslation + childHeight + paddingAfterChild; |
| 464 | if (currentYPosition <= 0) { |
| 465 | childViewState.location = ExpandableViewState.LOCATION_HIDDEN_TOP; |
| 466 | } |
Muyuan Li | 8779802 | 2016-04-07 17:51:25 -0700 | [diff] [blame] | 467 | } |
Selim Cinek | bbcebde | 2016-11-09 18:28:20 -0800 | [diff] [blame] | 468 | if (childViewState.location == ExpandableViewState.LOCATION_UNKNOWN) { |
Muyuan Li | 8779802 | 2016-04-07 17:51:25 -0700 | [diff] [blame] | 469 | Log.wtf(LOG_TAG, "Failed to assign location for child " + i); |
| 470 | } |
| 471 | |
Selim Cinek | 9b9d6e1 | 2017-11-30 12:29:47 +0100 | [diff] [blame] | 472 | childViewState.yTranslation += inset; |
Muyuan Li | 8779802 | 2016-04-07 17:51:25 -0700 | [diff] [blame] | 473 | return currentYPosition; |
| 474 | } |
| 475 | |
| 476 | protected int getPaddingAfterChild(StackScrollAlgorithmState algorithmState, |
Selim Cinek | 61633a8 | 2016-01-25 15:54:10 -0800 | [diff] [blame] | 477 | ExpandableView child) { |
Selim Cinek | 281c202 | 2016-10-13 19:14:43 -0700 | [diff] [blame] | 478 | return algorithmState.getPaddingAfterChild(child); |
Selim Cinek | 61633a8 | 2016-01-25 15:54:10 -0800 | [diff] [blame] | 479 | } |
| 480 | |
Selim Cinek | 5040f2e | 2019-02-14 18:22:42 -0800 | [diff] [blame] | 481 | private void updatePulsingStates(StackScrollAlgorithmState algorithmState, |
| 482 | AmbientState ambientState) { |
| 483 | int childCount = algorithmState.visibleChildren.size(); |
| 484 | for (int i = 0; i < childCount; i++) { |
| 485 | View child = algorithmState.visibleChildren.get(i); |
| 486 | if (!(child instanceof ExpandableNotificationRow)) { |
| 487 | continue; |
| 488 | } |
| 489 | ExpandableNotificationRow row = (ExpandableNotificationRow) child; |
Selim Cinek | 459aee3 | 2019-02-20 11:18:56 -0800 | [diff] [blame] | 490 | if (!row.showingAmbientPulsing() || (i == 0 && ambientState.isPulseExpanding())) { |
Selim Cinek | 5040f2e | 2019-02-14 18:22:42 -0800 | [diff] [blame] | 491 | continue; |
| 492 | } |
| 493 | ExpandableViewState viewState = row.getViewState(); |
| 494 | viewState.hidden = false; |
| 495 | } |
| 496 | } |
| 497 | |
Dave Mankoff | a4d195d | 2018-11-16 13:33:27 -0500 | [diff] [blame] | 498 | private void updateHeadsUpStates(StackScrollAlgorithmState algorithmState, |
| 499 | AmbientState ambientState) { |
Selim Cinek | a4baaa3 | 2015-04-20 14:27:54 -0700 | [diff] [blame] | 500 | int childCount = algorithmState.visibleChildren.size(); |
| 501 | ExpandableNotificationRow topHeadsUpEntry = null; |
| 502 | for (int i = 0; i < childCount; i++) { |
| 503 | View child = algorithmState.visibleChildren.get(i); |
| 504 | if (!(child instanceof ExpandableNotificationRow)) { |
| 505 | break; |
| 506 | } |
| 507 | ExpandableNotificationRow row = (ExpandableNotificationRow) child; |
| 508 | if (!row.isHeadsUp()) { |
| 509 | break; |
Selim Cinek | a4baaa3 | 2015-04-20 14:27:54 -0700 | [diff] [blame] | 510 | } |
Dave Mankoff | a4d195d | 2018-11-16 13:33:27 -0500 | [diff] [blame] | 511 | ExpandableViewState childState = row.getViewState(); |
Selim Cinek | 9b9d6e1 | 2017-11-30 12:29:47 +0100 | [diff] [blame] | 512 | if (topHeadsUpEntry == null && row.mustStayOnScreen() && !childState.headsUpIsVisible) { |
Selim Cinek | 3776fe0 | 2016-02-04 13:32:43 -0800 | [diff] [blame] | 513 | topHeadsUpEntry = row; |
Selim Cinek | bbcebde | 2016-11-09 18:28:20 -0800 | [diff] [blame] | 514 | childState.location = ExpandableViewState.LOCATION_FIRST_HUN; |
Selim Cinek | 3776fe0 | 2016-02-04 13:32:43 -0800 | [diff] [blame] | 515 | } |
Selim Cinek | 1f3f544 | 2015-04-10 17:54:46 -0700 | [diff] [blame] | 516 | boolean isTopEntry = topHeadsUpEntry == row; |
Selim Cinek | 3776fe0 | 2016-02-04 13:32:43 -0800 | [diff] [blame] | 517 | float unmodifiedEndLocation = childState.yTranslation + childState.height; |
Selim Cinek | 131c1e2 | 2015-05-11 19:04:49 -0700 | [diff] [blame] | 518 | if (mIsExpanded) { |
Selim Cinek | 9b9d6e1 | 2017-11-30 12:29:47 +0100 | [diff] [blame] | 519 | if (row.mustStayOnScreen() && !childState.headsUpIsVisible) { |
| 520 | // Ensure that the heads up is always visible even when scrolled off |
| 521 | clampHunToTop(ambientState, row, childState); |
Selim Cinek | 459aee3 | 2019-02-20 11:18:56 -0800 | [diff] [blame] | 522 | if (i == 0 && row.isAboveShelf()) { |
Selim Cinek | 9b9d6e1 | 2017-11-30 12:29:47 +0100 | [diff] [blame] | 523 | // the first hun can't get off screen. |
| 524 | clampHunToMaxTranslation(ambientState, row, childState); |
| 525 | childState.hidden = false; |
| 526 | } |
Selim Cinek | d127d79 | 2016-11-01 19:11:41 -0700 | [diff] [blame] | 527 | } |
Selim Cinek | 131c1e2 | 2015-05-11 19:04:49 -0700 | [diff] [blame] | 528 | } |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 529 | if (row.isPinned()) { |
Selim Cinek | aa9db1f | 2018-02-27 17:35:47 -0800 | [diff] [blame] | 530 | childState.yTranslation = Math.max(childState.yTranslation, mHeadsUpInset); |
Selim Cinek | 31aada4 | 2015-12-18 17:51:15 -0800 | [diff] [blame] | 531 | childState.height = Math.max(row.getIntrinsicHeight(), childState.height); |
Selim Cinek | cafa87f | 2016-10-26 17:00:17 -0700 | [diff] [blame] | 532 | childState.hidden = false; |
Dave Mankoff | a4d195d | 2018-11-16 13:33:27 -0500 | [diff] [blame] | 533 | ExpandableViewState topState = |
| 534 | topHeadsUpEntry == null ? null : topHeadsUpEntry.getViewState(); |
Selim Cinek | 61cfd4b | 2017-12-08 12:42:36 -0800 | [diff] [blame] | 535 | if (topState != null && !isTopEntry && (!mIsExpanded |
Selim Cinek | 3776fe0 | 2016-02-04 13:32:43 -0800 | [diff] [blame] | 536 | || unmodifiedEndLocation < topState.yTranslation + topState.height)) { |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 537 | // Ensure that a headsUp doesn't vertically extend further than the heads-up at |
| 538 | // the top most z-position |
Selim Cinek | 31aada4 | 2015-12-18 17:51:15 -0800 | [diff] [blame] | 539 | childState.height = row.getIntrinsicHeight(); |
Selim Cinek | e53e6bb | 2015-04-13 16:14:26 -0700 | [diff] [blame] | 540 | childState.yTranslation = topState.yTranslation + topState.height |
| 541 | - childState.height; |
Selim Cinek | 1f3f544 | 2015-04-10 17:54:46 -0700 | [diff] [blame] | 542 | } |
felkachang | 529bfe6 | 2018-07-04 12:51:44 +0800 | [diff] [blame] | 543 | |
| 544 | // heads up notification show and this row is the top entry of heads up |
| 545 | // notifications. i.e. this row should be the only one row that has input field |
| 546 | // To check if the row need to do translation according to scroll Y |
| 547 | // heads up show full of row's content and any scroll y indicate that the |
| 548 | // translationY need to move up the HUN. |
Gus Prevas | 0fa58d6 | 2019-01-11 13:58:40 -0500 | [diff] [blame] | 549 | // TODO: fix this check for anchor scrolling. |
felkachang | 529bfe6 | 2018-07-04 12:51:44 +0800 | [diff] [blame] | 550 | if (!mIsExpanded && isTopEntry && ambientState.getScrollY() > 0) { |
| 551 | childState.yTranslation -= ambientState.getScrollY(); |
| 552 | } |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 553 | } |
Selim Cinek | cafa87f | 2016-10-26 17:00:17 -0700 | [diff] [blame] | 554 | if (row.isHeadsUpAnimatingAway()) { |
| 555 | childState.hidden = false; |
| 556 | } |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 557 | } |
Selim Cinek | 67b2260 | 2014-03-10 15:40:16 +0100 | [diff] [blame] | 558 | } |
| 559 | |
Selim Cinek | 3776fe0 | 2016-02-04 13:32:43 -0800 | [diff] [blame] | 560 | private void clampHunToTop(AmbientState ambientState, ExpandableNotificationRow row, |
Selim Cinek | bbcebde | 2016-11-09 18:28:20 -0800 | [diff] [blame] | 561 | ExpandableViewState childState) { |
Selim Cinek | 3776fe0 | 2016-02-04 13:32:43 -0800 | [diff] [blame] | 562 | float newTranslation = Math.max(ambientState.getTopPadding() |
| 563 | + ambientState.getStackTranslation(), childState.yTranslation); |
| 564 | childState.height = (int) Math.max(childState.height - (newTranslation |
Selim Cinek | 567e845 | 2016-03-24 10:54:56 -0700 | [diff] [blame] | 565 | - childState.yTranslation), row.getCollapsedHeight()); |
Selim Cinek | 3776fe0 | 2016-02-04 13:32:43 -0800 | [diff] [blame] | 566 | childState.yTranslation = newTranslation; |
| 567 | } |
| 568 | |
| 569 | private void clampHunToMaxTranslation(AmbientState ambientState, ExpandableNotificationRow row, |
Selim Cinek | bbcebde | 2016-11-09 18:28:20 -0800 | [diff] [blame] | 570 | ExpandableViewState childState) { |
Selim Cinek | 3776fe0 | 2016-02-04 13:32:43 -0800 | [diff] [blame] | 571 | float newTranslation; |
Selim Cinek | 7e0f948 | 2017-05-22 20:00:56 -0700 | [diff] [blame] | 572 | float maxHeadsUpTranslation = ambientState.getMaxHeadsUpTranslation(); |
| 573 | float maxShelfPosition = ambientState.getInnerHeight() + ambientState.getTopPadding() |
| 574 | + ambientState.getStackTranslation(); |
| 575 | maxHeadsUpTranslation = Math.min(maxHeadsUpTranslation, maxShelfPosition); |
| 576 | float bottomPosition = maxHeadsUpTranslation - row.getCollapsedHeight(); |
Selim Cinek | 3776fe0 | 2016-02-04 13:32:43 -0800 | [diff] [blame] | 577 | newTranslation = Math.min(childState.yTranslation, bottomPosition); |
Selim Cinek | 7e0f948 | 2017-05-22 20:00:56 -0700 | [diff] [blame] | 578 | childState.height = (int) Math.min(childState.height, maxHeadsUpTranslation |
| 579 | - newTranslation); |
Selim Cinek | 3776fe0 | 2016-02-04 13:32:43 -0800 | [diff] [blame] | 580 | childState.yTranslation = newTranslation; |
Selim Cinek | 343e6e2 | 2014-04-11 21:23:30 +0200 | [diff] [blame] | 581 | } |
| 582 | |
| 583 | /** |
Selim Cinek | 281c202 | 2016-10-13 19:14:43 -0700 | [diff] [blame] | 584 | * Clamp the height of the child down such that its end is at most on the beginning of |
| 585 | * the shelf. |
| 586 | * |
Selim Cinek | 281c202 | 2016-10-13 19:14:43 -0700 | [diff] [blame] | 587 | * @param childViewState the view state of the child |
Dave Mankoff | a4d195d | 2018-11-16 13:33:27 -0500 | [diff] [blame] | 588 | * @param ambientState the ambient state |
Selim Cinek | 281c202 | 2016-10-13 19:14:43 -0700 | [diff] [blame] | 589 | */ |
Selim Cinek | 2627d72 | 2018-01-19 12:16:49 -0800 | [diff] [blame] | 590 | private void clampPositionToShelf(ExpandableView child, |
| 591 | ExpandableViewState childViewState, |
Selim Cinek | 281c202 | 2016-10-13 19:14:43 -0700 | [diff] [blame] | 592 | AmbientState ambientState) { |
Eliot Courtney | 5d3d2d0 | 2018-01-18 15:59:03 +0900 | [diff] [blame] | 593 | if (ambientState.getShelf() == null) { |
| 594 | return; |
| 595 | } |
| 596 | |
Selim Cinek | a686b2c | 2016-10-26 13:58:27 -0700 | [diff] [blame] | 597 | int shelfStart = ambientState.getInnerHeight() |
| 598 | - ambientState.getShelf().getIntrinsicHeight(); |
shawnlin | 5be1f7c | 2018-05-21 20:50:54 +0800 | [diff] [blame] | 599 | if (ambientState.isAppearing() && !child.isAboveShelf()) { |
| 600 | // Don't show none heads-up notifications while in appearing phase. |
| 601 | childViewState.yTranslation = Math.max(childViewState.yTranslation, shelfStart); |
| 602 | } |
Selim Cinek | 281c202 | 2016-10-13 19:14:43 -0700 | [diff] [blame] | 603 | childViewState.yTranslation = Math.min(childViewState.yTranslation, shelfStart); |
Selim Cinek | c383fd0 | 2016-10-21 15:31:26 -0700 | [diff] [blame] | 604 | if (childViewState.yTranslation >= shelfStart) { |
Selim Cinek | c25989e | 2018-02-16 16:42:14 -0800 | [diff] [blame] | 605 | childViewState.hidden = !child.isExpandAnimationRunning() && !child.hasExpandingChild(); |
Selim Cinek | eccb5de | 2016-10-28 15:04:05 -0700 | [diff] [blame] | 606 | childViewState.inShelf = true; |
Selim Cinek | 9b9d6e1 | 2017-11-30 12:29:47 +0100 | [diff] [blame] | 607 | childViewState.headsUpIsVisible = false; |
Selim Cinek | c383fd0 | 2016-10-21 15:31:26 -0700 | [diff] [blame] | 608 | } |
Selim Cinek | 281c202 | 2016-10-13 19:14:43 -0700 | [diff] [blame] | 609 | } |
| 610 | |
Muyuan Li | 8779802 | 2016-04-07 17:51:25 -0700 | [diff] [blame] | 611 | protected int getMaxAllowedChildHeight(View child) { |
Selim Cinek | 31aada4 | 2015-12-18 17:51:15 -0800 | [diff] [blame] | 612 | if (child instanceof ExpandableView) { |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 613 | ExpandableView expandableView = (ExpandableView) child; |
Selim Cinek | 8d5727f | 2015-04-28 19:17:32 -0700 | [diff] [blame] | 614 | return expandableView.getIntrinsicHeight(); |
Selim Cinek | 1685e63 | 2014-04-08 02:27:49 +0200 | [diff] [blame] | 615 | } |
Dave Mankoff | a4d195d | 2018-11-16 13:33:27 -0500 | [diff] [blame] | 616 | return child == null ? mCollapsedSize : child.getHeight(); |
Selim Cinek | 1685e63 | 2014-04-08 02:27:49 +0200 | [diff] [blame] | 617 | } |
| 618 | |
Selim Cinek | 67b2260 | 2014-03-10 15:40:16 +0100 | [diff] [blame] | 619 | /** |
Selim Cinek | 67b2260 | 2014-03-10 15:40:16 +0100 | [diff] [blame] | 620 | * Calculate the Z positions for all children based on the number of items in both stacks and |
| 621 | * save it in the resultState |
Dave Mankoff | a4d195d | 2018-11-16 13:33:27 -0500 | [diff] [blame] | 622 | * |
Selim Cinek | 67b2260 | 2014-03-10 15:40:16 +0100 | [diff] [blame] | 623 | * @param algorithmState The state in which the current pass of the algorithm is currently in |
Dave Mankoff | a4d195d | 2018-11-16 13:33:27 -0500 | [diff] [blame] | 624 | * @param ambientState The ambient state of the algorithm |
Selim Cinek | 67b2260 | 2014-03-10 15:40:16 +0100 | [diff] [blame] | 625 | */ |
Dave Mankoff | a4d195d | 2018-11-16 13:33:27 -0500 | [diff] [blame] | 626 | private void updateZValuesForState(StackScrollAlgorithmState algorithmState, |
| 627 | AmbientState ambientState) { |
Jorim Jaggi | d4a5744 | 2014-04-10 02:45:55 +0200 | [diff] [blame] | 628 | int childCount = algorithmState.visibleChildren.size(); |
Selim Cinek | 3322357 | 2016-02-19 19:32:22 -0800 | [diff] [blame] | 629 | float childrenOnTop = 0.0f; |
Selim Cinek | 3776fe0 | 2016-02-04 13:32:43 -0800 | [diff] [blame] | 630 | for (int i = childCount - 1; i >= 0; i--) { |
Selim Cinek | daab6f5 | 2017-04-06 16:46:34 -0700 | [diff] [blame] | 631 | childrenOnTop = updateChildZValue(i, childrenOnTop, |
Dave Mankoff | a4d195d | 2018-11-16 13:33:27 -0500 | [diff] [blame] | 632 | algorithmState, ambientState); |
Muyuan Li | 8779802 | 2016-04-07 17:51:25 -0700 | [diff] [blame] | 633 | } |
| 634 | } |
| 635 | |
Selim Cinek | daab6f5 | 2017-04-06 16:46:34 -0700 | [diff] [blame] | 636 | protected float updateChildZValue(int i, float childrenOnTop, |
Dave Mankoff | a4d195d | 2018-11-16 13:33:27 -0500 | [diff] [blame] | 637 | StackScrollAlgorithmState algorithmState, |
Muyuan Li | 8779802 | 2016-04-07 17:51:25 -0700 | [diff] [blame] | 638 | AmbientState ambientState) { |
| 639 | ExpandableView child = algorithmState.visibleChildren.get(i); |
Dave Mankoff | a4d195d | 2018-11-16 13:33:27 -0500 | [diff] [blame] | 640 | ExpandableViewState childViewState = child.getViewState(); |
Selim Cinek | 281c202 | 2016-10-13 19:14:43 -0700 | [diff] [blame] | 641 | int zDistanceBetweenElements = ambientState.getZDistanceBetweenElements(); |
| 642 | float baseZ = ambientState.getBaseZHeight(); |
Selim Cinek | 9b9d6e1 | 2017-11-30 12:29:47 +0100 | [diff] [blame] | 643 | if (child.mustStayOnScreen() && !childViewState.headsUpIsVisible |
| 644 | && !ambientState.isDozingAndNotPulsing(child) |
Muyuan Li | 8779802 | 2016-04-07 17:51:25 -0700 | [diff] [blame] | 645 | && childViewState.yTranslation < ambientState.getTopPadding() |
| 646 | + ambientState.getStackTranslation()) { |
| 647 | if (childrenOnTop != 0.0f) { |
| 648 | childrenOnTop++; |
| 649 | } else { |
| 650 | float overlap = ambientState.getTopPadding() |
| 651 | + ambientState.getStackTranslation() - childViewState.yTranslation; |
| 652 | childrenOnTop += Math.min(1.0f, overlap / childViewState.height); |
| 653 | } |
Selim Cinek | 281c202 | 2016-10-13 19:14:43 -0700 | [diff] [blame] | 654 | childViewState.zTranslation = baseZ |
| 655 | + childrenOnTop * zDistanceBetweenElements; |
Selim Cinek | 459aee3 | 2019-02-20 11:18:56 -0800 | [diff] [blame] | 656 | } else if (i == 0 && child.isAboveShelf()) { |
Selim Cinek | d127d79 | 2016-11-01 19:11:41 -0700 | [diff] [blame] | 657 | // In case this is a new view that has never been measured before, we don't want to |
| 658 | // elevate if we are currently expanded more then the notification |
Eliot Courtney | 5d3d2d0 | 2018-01-18 15:59:03 +0900 | [diff] [blame] | 659 | int shelfHeight = ambientState.getShelf() == null ? 0 : |
| 660 | ambientState.getShelf().getIntrinsicHeight(); |
Selim Cinek | d127d79 | 2016-11-01 19:11:41 -0700 | [diff] [blame] | 661 | float shelfStart = ambientState.getInnerHeight() |
| 662 | - shelfHeight + ambientState.getTopPadding() |
| 663 | + ambientState.getStackTranslation(); |
| 664 | float notificationEnd = childViewState.yTranslation + child.getPinnedHeadsUpHeight() |
| 665 | + mPaddingBetweenElements; |
| 666 | if (shelfStart > notificationEnd) { |
| 667 | childViewState.zTranslation = baseZ; |
| 668 | } else { |
| 669 | float factor = (notificationEnd - shelfStart) / shelfHeight; |
| 670 | factor = Math.min(factor, 1.0f); |
| 671 | childViewState.zTranslation = baseZ + factor * zDistanceBetweenElements; |
| 672 | } |
Muyuan Li | 8779802 | 2016-04-07 17:51:25 -0700 | [diff] [blame] | 673 | } else { |
Selim Cinek | 281c202 | 2016-10-13 19:14:43 -0700 | [diff] [blame] | 674 | childViewState.zTranslation = baseZ; |
Selim Cinek | 67b2260 | 2014-03-10 15:40:16 +0100 | [diff] [blame] | 675 | } |
Selim Cinek | 99e9adf | 2018-03-15 09:17:47 -0700 | [diff] [blame] | 676 | |
| 677 | // We need to scrim the notification more from its surrounding content when we are pinned, |
| 678 | // and we therefore elevate it higher. |
| 679 | // We can use the headerVisibleAmount for this, since the value nicely goes from 0 to 1 when |
| 680 | // expanding after which we have a normal elevation again. |
| 681 | childViewState.zTranslation += (1.0f - child.getHeaderVisibleAmount()) |
| 682 | * mPinnedZTranslationExtra; |
Selim Cinek | daab6f5 | 2017-04-06 16:46:34 -0700 | [diff] [blame] | 683 | return childrenOnTop; |
Selim Cinek | 67b2260 | 2014-03-10 15:40:16 +0100 | [diff] [blame] | 684 | } |
| 685 | |
Selim Cinek | 1685e63 | 2014-04-08 02:27:49 +0200 | [diff] [blame] | 686 | public void setIsExpanded(boolean isExpanded) { |
| 687 | this.mIsExpanded = isExpanded; |
| 688 | } |
| 689 | |
Selim Cinek | 281c202 | 2016-10-13 19:14:43 -0700 | [diff] [blame] | 690 | public class StackScrollAlgorithmState { |
Selim Cinek | 67b2260 | 2014-03-10 15:40:16 +0100 | [diff] [blame] | 691 | |
| 692 | /** |
Gus Prevas | 0fa58d6 | 2019-01-11 13:58:40 -0500 | [diff] [blame] | 693 | * The scroll position of the algorithm (absolute scrolling). |
Selim Cinek | 67b2260 | 2014-03-10 15:40:16 +0100 | [diff] [blame] | 694 | */ |
| 695 | public int scrollY; |
| 696 | |
Gus Prevas | 0fa58d6 | 2019-01-11 13:58:40 -0500 | [diff] [blame] | 697 | /** The index of the anchor view (anchor scrolling). */ |
| 698 | public int anchorViewIndex; |
| 699 | |
| 700 | /** |
| 701 | * The Y position, relative to the top of the screen, of the anchor view (anchor scrolling). |
| 702 | */ |
| 703 | public int anchorViewY; |
| 704 | |
Selim Cinek | 67b2260 | 2014-03-10 15:40:16 +0100 | [diff] [blame] | 705 | /** |
Jorim Jaggi | d4a5744 | 2014-04-10 02:45:55 +0200 | [diff] [blame] | 706 | * The children from the host view which are not gone. |
| 707 | */ |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 708 | public final ArrayList<ExpandableView> visibleChildren = new ArrayList<ExpandableView>(); |
Selim Cinek | 61633a8 | 2016-01-25 15:54:10 -0800 | [diff] [blame] | 709 | |
| 710 | /** |
Selim Cinek | a7ed2c1 | 2017-01-23 20:47:24 -0800 | [diff] [blame] | 711 | * The padding after each child measured in pixels. |
Selim Cinek | 61633a8 | 2016-01-25 15:54:10 -0800 | [diff] [blame] | 712 | */ |
Selim Cinek | a7ed2c1 | 2017-01-23 20:47:24 -0800 | [diff] [blame] | 713 | public final HashMap<ExpandableView, Float> paddingMap = new HashMap<>(); |
Selim Cinek | 2627d72 | 2018-01-19 12:16:49 -0800 | [diff] [blame] | 714 | private int indexOfExpandingNotification; |
Selim Cinek | 281c202 | 2016-10-13 19:14:43 -0700 | [diff] [blame] | 715 | |
| 716 | public int getPaddingAfterChild(ExpandableView child) { |
Selim Cinek | a7ed2c1 | 2017-01-23 20:47:24 -0800 | [diff] [blame] | 717 | Float padding = paddingMap.get(child); |
| 718 | if (padding == null) { |
| 719 | // Should only happen for the last view |
| 720 | return mPaddingBetweenElements; |
| 721 | } |
| 722 | return (int) padding.floatValue(); |
Selim Cinek | 281c202 | 2016-10-13 19:14:43 -0700 | [diff] [blame] | 723 | } |
Selim Cinek | 2627d72 | 2018-01-19 12:16:49 -0800 | [diff] [blame] | 724 | |
| 725 | public int getIndexOfExpandingNotification() { |
| 726 | return indexOfExpandingNotification; |
| 727 | } |
Selim Cinek | 67b2260 | 2014-03-10 15:40:16 +0100 | [diff] [blame] | 728 | } |
| 729 | |
| 730 | } |