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