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 | |
| 17 | package com.android.systemui.statusbar.stack; |
| 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; |
| 24 | import com.android.systemui.R; |
Selim Cinek | c9c640f | 2016-11-14 17:27:19 -0800 | [diff] [blame] | 25 | import com.android.systemui.statusbar.DismissView; |
Selim Cinek | cde90e5 | 2016-12-22 21:01:49 +0100 | [diff] [blame] | 26 | import com.android.systemui.statusbar.EmptyShadeView; |
Selim Cinek | 1685e63 | 2014-04-08 02:27:49 +0200 | [diff] [blame] | 27 | import com.android.systemui.statusbar.ExpandableNotificationRow; |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 28 | import com.android.systemui.statusbar.ExpandableView; |
Selim Cinek | 281c202 | 2016-10-13 19:14:43 -0700 | [diff] [blame] | 29 | import com.android.systemui.statusbar.NotificationShelf; |
Selim Cinek | 42357e0 | 2016-02-24 18:48:01 -0800 | [diff] [blame] | 30 | import com.android.systemui.statusbar.notification.NotificationUtils; |
Selim Cinek | 67b2260 | 2014-03-10 15:40:16 +0100 | [diff] [blame] | 31 | |
Jorim Jaggi | d4a5744 | 2014-04-10 02:45:55 +0200 | [diff] [blame] | 32 | import java.util.ArrayList; |
Selim Cinek | 42357e0 | 2016-02-24 18:48:01 -0800 | [diff] [blame] | 33 | import java.util.HashMap; |
Selim Cinek | b5605e5 | 2015-02-20 18:21:41 +0100 | [diff] [blame] | 34 | import java.util.List; |
Jorim Jaggi | d4a5744 | 2014-04-10 02:45:55 +0200 | [diff] [blame] | 35 | |
Selim Cinek | 67b2260 | 2014-03-10 15:40:16 +0100 | [diff] [blame] | 36 | /** |
| 37 | * The Algorithm of the {@link com.android.systemui.statusbar.stack |
| 38 | * .NotificationStackScrollLayout} which can be queried for {@link com.android.systemui.statusbar |
| 39 | * .stack.StackScrollState} |
| 40 | */ |
| 41 | public class StackScrollAlgorithm { |
| 42 | |
Christoph Studer | 6e3eceb | 2014-04-01 18:40:27 +0200 | [diff] [blame] | 43 | private static final String LOG_TAG = "StackScrollAlgorithm"; |
| 44 | |
Selim Cinek | 67b2260 | 2014-03-10 15:40:16 +0100 | [diff] [blame] | 45 | private int mPaddingBetweenElements; |
Selim Cinek | 61633a8 | 2016-01-25 15:54:10 -0800 | [diff] [blame] | 46 | private int mIncreasedPaddingBetweenElements; |
Selim Cinek | 67b2260 | 2014-03-10 15:40:16 +0100 | [diff] [blame] | 47 | private int mCollapsedSize; |
Selim Cinek | 67b2260 | 2014-03-10 15:40:16 +0100 | [diff] [blame] | 48 | |
Selim Cinek | 67b2260 | 2014-03-10 15:40:16 +0100 | [diff] [blame] | 49 | private StackScrollAlgorithmState mTempAlgorithmState = new StackScrollAlgorithmState(); |
Selim Cinek | 1685e63 | 2014-04-08 02:27:49 +0200 | [diff] [blame] | 50 | private boolean mIsExpanded; |
Anthony Chen | 9fe1ee7 | 2017-04-07 13:53:37 -0700 | [diff] [blame] | 51 | private boolean mClipNotificationScrollToTop; |
Selim Cinek | cafa87f | 2016-10-26 17:00:17 -0700 | [diff] [blame] | 52 | private int mStatusBarHeight; |
Selim Cinek | 67b2260 | 2014-03-10 15:40:16 +0100 | [diff] [blame] | 53 | |
| 54 | public StackScrollAlgorithm(Context context) { |
Selim Cinek | af0dc31 | 2015-12-15 17:01:44 -0800 | [diff] [blame] | 55 | initView(context); |
Selim Cinek | 34c0a8d | 2014-05-12 00:01:43 +0200 | [diff] [blame] | 56 | } |
| 57 | |
Selim Cinek | af0dc31 | 2015-12-15 17:01:44 -0800 | [diff] [blame] | 58 | public void initView(Context context) { |
| 59 | initConstants(context); |
Selim Cinek | 34c0a8d | 2014-05-12 00:01:43 +0200 | [diff] [blame] | 60 | } |
| 61 | |
Selim Cinek | 67b2260 | 2014-03-10 15:40:16 +0100 | [diff] [blame] | 62 | private void initConstants(Context context) { |
Anthony Chen | 9fe1ee7 | 2017-04-07 13:53:37 -0700 | [diff] [blame] | 63 | Resources res = context.getResources(); |
| 64 | mPaddingBetweenElements = res.getDimensionPixelSize( |
Selim Cinek | db16737 | 2016-11-17 15:41:17 -0800 | [diff] [blame] | 65 | R.dimen.notification_divider_height); |
Anthony Chen | 9fe1ee7 | 2017-04-07 13:53:37 -0700 | [diff] [blame] | 66 | mIncreasedPaddingBetweenElements = |
| 67 | res.getDimensionPixelSize(R.dimen.notification_divider_height_increased); |
| 68 | mCollapsedSize = res.getDimensionPixelSize(R.dimen.notification_min_height); |
| 69 | mStatusBarHeight = res.getDimensionPixelSize(R.dimen.status_bar_height); |
| 70 | mClipNotificationScrollToTop = res.getBoolean(R.bool.config_clipNotificationScrollToTop); |
Jorim Jaggi | d7c1fae | 2014-08-13 18:27:47 +0200 | [diff] [blame] | 71 | } |
Selim Cinek | 67b2260 | 2014-03-10 15:40:16 +0100 | [diff] [blame] | 72 | |
Jorim Jaggi | d552d9d | 2014-05-07 19:41:13 +0200 | [diff] [blame] | 73 | public void getStackScrollState(AmbientState ambientState, StackScrollState resultState) { |
Selim Cinek | 67b2260 | 2014-03-10 15:40:16 +0100 | [diff] [blame] | 74 | // The state of the local variables are saved in an algorithmState to easily subdivide it |
| 75 | // into multiple phases. |
| 76 | StackScrollAlgorithmState algorithmState = mTempAlgorithmState; |
| 77 | |
| 78 | // First we reset the view states to their default values. |
| 79 | resultState.resetViewStates(); |
| 80 | |
Selim Cinek | 3776fe0 | 2016-02-04 13:32:43 -0800 | [diff] [blame] | 81 | initAlgorithmState(resultState, algorithmState, ambientState); |
Selim Cinek | 1408eb5 | 2014-06-02 14:45:38 +0200 | [diff] [blame] | 82 | |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 83 | updatePositionsForState(resultState, algorithmState, ambientState); |
Selim Cinek | 67b2260 | 2014-03-10 15:40:16 +0100 | [diff] [blame] | 84 | |
Selim Cinek | 3776fe0 | 2016-02-04 13:32:43 -0800 | [diff] [blame] | 85 | updateZValuesForState(resultState, algorithmState, ambientState); |
| 86 | |
| 87 | updateHeadsUpStates(resultState, algorithmState, ambientState); |
Selim Cinek | eb97356 | 2014-05-02 17:07:49 +0200 | [diff] [blame] | 88 | |
Jorim Jaggi | d552d9d | 2014-05-07 19:41:13 +0200 | [diff] [blame] | 89 | handleDraggedViews(ambientState, resultState, algorithmState); |
Jorim Jaggi | ae44128 | 2014-08-01 02:45:18 +0200 | [diff] [blame] | 90 | updateDimmedActivatedHideSensitive(ambientState, resultState, algorithmState); |
Selim Cinek | a59ecc3 | 2015-04-07 10:51:49 -0700 | [diff] [blame] | 91 | updateClipping(resultState, algorithmState, ambientState); |
Selim Cinek | db16737 | 2016-11-17 15:41:17 -0800 | [diff] [blame] | 92 | updateSpeedBumpState(resultState, algorithmState, ambientState); |
| 93 | updateShelfState(resultState, ambientState); |
Selim Cinek | b5605e5 | 2015-02-20 18:21:41 +0100 | [diff] [blame] | 94 | getNotificationChildrenStates(resultState, algorithmState); |
| 95 | } |
| 96 | |
| 97 | private void getNotificationChildrenStates(StackScrollState resultState, |
| 98 | StackScrollAlgorithmState algorithmState) { |
| 99 | int childCount = algorithmState.visibleChildren.size(); |
| 100 | for (int i = 0; i < childCount; i++) { |
| 101 | ExpandableView v = algorithmState.visibleChildren.get(i); |
| 102 | if (v instanceof ExpandableNotificationRow) { |
| 103 | ExpandableNotificationRow row = (ExpandableNotificationRow) v; |
| 104 | row.getChildrenStates(resultState); |
| 105 | } |
| 106 | } |
Selim Cinek | 3d2b94bf | 2014-07-02 22:12:47 +0200 | [diff] [blame] | 107 | } |
| 108 | |
Selim Cinek | db16737 | 2016-11-17 15:41:17 -0800 | [diff] [blame] | 109 | private void updateSpeedBumpState(StackScrollState resultState, |
Selim Cinek | 281c202 | 2016-10-13 19:14:43 -0700 | [diff] [blame] | 110 | StackScrollAlgorithmState algorithmState, AmbientState ambientState) { |
Selim Cinek | 3d2b94bf | 2014-07-02 22:12:47 +0200 | [diff] [blame] | 111 | int childCount = algorithmState.visibleChildren.size(); |
Selim Cinek | db16737 | 2016-11-17 15:41:17 -0800 | [diff] [blame] | 112 | int belowSpeedBump = ambientState.getSpeedBumpIndex(); |
Selim Cinek | 3d2b94bf | 2014-07-02 22:12:47 +0200 | [diff] [blame] | 113 | for (int i = 0; i < childCount; i++) { |
| 114 | View child = algorithmState.visibleChildren.get(i); |
Selim Cinek | bbcebde | 2016-11-09 18:28:20 -0800 | [diff] [blame] | 115 | ExpandableViewState childViewState = resultState.getViewStateForView(child); |
Selim Cinek | 3107cfa | 2014-07-22 15:24:29 +0200 | [diff] [blame] | 116 | |
| 117 | // The speed bump can also be gone, so equality needs to be taken when comparing |
| 118 | // indices. |
Selim Cinek | db16737 | 2016-11-17 15:41:17 -0800 | [diff] [blame] | 119 | childViewState.belowSpeedBump = i >= belowSpeedBump; |
Selim Cinek | 3d2b94bf | 2014-07-02 22:12:47 +0200 | [diff] [blame] | 120 | } |
Selim Cinek | db16737 | 2016-11-17 15:41:17 -0800 | [diff] [blame] | 121 | |
| 122 | } |
| 123 | private void updateShelfState(StackScrollState resultState, AmbientState ambientState) { |
Selim Cinek | 281c202 | 2016-10-13 19:14:43 -0700 | [diff] [blame] | 124 | NotificationShelf shelf = ambientState.getShelf(); |
Eliot Courtney | 5d3d2d0 | 2018-01-18 15:59:03 +0900 | [diff] [blame] | 125 | if (shelf != null) { |
| 126 | shelf.updateState(resultState, ambientState); |
| 127 | } |
Selim Cinek | f54090e | 2014-06-17 17:24:51 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Selim Cinek | 708a6c1 | 2014-05-28 14:16:02 +0200 | [diff] [blame] | 130 | private void updateClipping(StackScrollState resultState, |
Selim Cinek | a59ecc3 | 2015-04-07 10:51:49 -0700 | [diff] [blame] | 131 | StackScrollAlgorithmState algorithmState, AmbientState ambientState) { |
Selim Cinek | 355652a | 2016-12-07 13:32:12 -0800 | [diff] [blame] | 132 | float drawStart = !ambientState.isOnKeyguard() ? ambientState.getTopPadding() |
Selim Cinek | 2627d72 | 2018-01-19 12:16:49 -0800 | [diff] [blame] | 133 | + ambientState.getStackTranslation() + ambientState.getExpandAnimationTopChange() |
| 134 | : 0; |
Selim Cinek | 708a6c1 | 2014-05-28 14:16:02 +0200 | [diff] [blame] | 135 | float previousNotificationEnd = 0; |
| 136 | float previousNotificationStart = 0; |
Selim Cinek | 708a6c1 | 2014-05-28 14:16:02 +0200 | [diff] [blame] | 137 | int childCount = algorithmState.visibleChildren.size(); |
| 138 | for (int i = 0; i < childCount; i++) { |
| 139 | ExpandableView child = algorithmState.visibleChildren.get(i); |
Selim Cinek | bbcebde | 2016-11-09 18:28:20 -0800 | [diff] [blame] | 140 | ExpandableViewState state = resultState.getViewStateForView(child); |
Selim Cinek | 9b9d6e1 | 2017-11-30 12:29:47 +0100 | [diff] [blame] | 141 | if (!child.mustStayOnScreen() || state.headsUpIsVisible) { |
Selim Cinek | 3776fe0 | 2016-02-04 13:32:43 -0800 | [diff] [blame] | 142 | previousNotificationEnd = Math.max(drawStart, previousNotificationEnd); |
| 143 | previousNotificationStart = Math.max(drawStart, previousNotificationStart); |
| 144 | } |
Selim Cinek | 587cbf3 | 2016-01-19 11:36:18 -0800 | [diff] [blame] | 145 | float newYTranslation = state.yTranslation; |
| 146 | float newHeight = state.height; |
Selim Cinek | 708a6c1 | 2014-05-28 14:16:02 +0200 | [diff] [blame] | 147 | float newNotificationEnd = newYTranslation + newHeight; |
Mady Mellor | 53ac1ef | 2016-06-20 13:11:38 -0700 | [diff] [blame] | 148 | boolean isHeadsUp = (child instanceof ExpandableNotificationRow) |
| 149 | && ((ExpandableNotificationRow) child).isPinned(); |
Anthony Chen | 9fe1ee7 | 2017-04-07 13:53:37 -0700 | [diff] [blame] | 150 | if (mClipNotificationScrollToTop |
| 151 | && !state.inShelf && newYTranslation < previousNotificationEnd |
Jorim Jaggi | 0fdf574 | 2016-06-27 11:50:58 -0700 | [diff] [blame] | 152 | && (!isHeadsUp || ambientState.isShadeExpanded())) { |
Mady Mellor | c128f22 | 2016-04-26 11:42:46 -0700 | [diff] [blame] | 153 | // The previous view is overlapping on top, clip! |
| 154 | float overlapAmount = previousNotificationEnd - newYTranslation; |
| 155 | state.clipTopAmount = (int) overlapAmount; |
Selim Cinek | c5baa3e | 2014-10-29 19:04:19 +0100 | [diff] [blame] | 156 | } else { |
Mady Mellor | c128f22 | 2016-04-26 11:42:46 -0700 | [diff] [blame] | 157 | state.clipTopAmount = 0; |
Selim Cinek | 9c17b77 | 2015-07-07 20:37:09 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Selim Cinek | 708a6c1 | 2014-05-28 14:16:02 +0200 | [diff] [blame] | 160 | if (!child.isTransparent()) { |
| 161 | // Only update the previous values if we are not transparent, |
| 162 | // otherwise we would clip to a transparent view. |
Mady Mellor | c128f22 | 2016-04-26 11:42:46 -0700 | [diff] [blame] | 163 | previousNotificationEnd = newNotificationEnd; |
| 164 | previousNotificationStart = newYTranslation; |
Selim Cinek | 708a6c1 | 2014-05-28 14:16:02 +0200 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | } |
| 168 | |
Selim Cinek | 9c17b77 | 2015-07-07 20:37:09 -0700 | [diff] [blame] | 169 | public static boolean canChildBeDismissed(View v) { |
Selim Cinek | 9e624e7 | 2016-07-20 13:46:49 -0700 | [diff] [blame] | 170 | if (!(v instanceof ExpandableNotificationRow)) { |
| 171 | return false; |
Selim Cinek | 38d429f | 2016-06-03 11:46:56 -0700 | [diff] [blame] | 172 | } |
Selim Cinek | 9e624e7 | 2016-07-20 13:46:49 -0700 | [diff] [blame] | 173 | ExpandableNotificationRow row = (ExpandableNotificationRow) v; |
| 174 | if (row.areGutsExposed()) { |
| 175 | return false; |
| 176 | } |
| 177 | return row.canViewBeDismissed(); |
Selim Cinek | 9c17b77 | 2015-07-07 20:37:09 -0700 | [diff] [blame] | 178 | } |
| 179 | |
Selim Cinek | 708a6c1 | 2014-05-28 14:16:02 +0200 | [diff] [blame] | 180 | /** |
Jorim Jaggi | ae44128 | 2014-08-01 02:45:18 +0200 | [diff] [blame] | 181 | * Updates the dimmed, activated and hiding sensitive states of the children. |
Jorim Jaggi | d552d9d | 2014-05-07 19:41:13 +0200 | [diff] [blame] | 182 | */ |
Jorim Jaggi | ae44128 | 2014-08-01 02:45:18 +0200 | [diff] [blame] | 183 | private void updateDimmedActivatedHideSensitive(AmbientState ambientState, |
| 184 | StackScrollState resultState, StackScrollAlgorithmState algorithmState) { |
Jorim Jaggi | d552d9d | 2014-05-07 19:41:13 +0200 | [diff] [blame] | 185 | boolean dimmed = ambientState.isDimmed(); |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 186 | boolean dark = ambientState.isDark(); |
Jorim Jaggi | ae44128 | 2014-08-01 02:45:18 +0200 | [diff] [blame] | 187 | boolean hideSensitive = ambientState.isHideSensitive(); |
Jorim Jaggi | d552d9d | 2014-05-07 19:41:13 +0200 | [diff] [blame] | 188 | View activatedChild = ambientState.getActivatedChild(); |
| 189 | int childCount = algorithmState.visibleChildren.size(); |
| 190 | for (int i = 0; i < childCount; i++) { |
| 191 | View child = algorithmState.visibleChildren.get(i); |
Selim Cinek | bbcebde | 2016-11-09 18:28:20 -0800 | [diff] [blame] | 192 | ExpandableViewState childViewState = resultState.getViewStateForView(child); |
Jorim Jaggi | d552d9d | 2014-05-07 19:41:13 +0200 | [diff] [blame] | 193 | childViewState.dimmed = dimmed; |
John Spurlock | bf37099 | 2014-06-17 13:58:31 -0400 | [diff] [blame] | 194 | childViewState.dark = dark; |
Jorim Jaggi | ae44128 | 2014-08-01 02:45:18 +0200 | [diff] [blame] | 195 | childViewState.hideSensitive = hideSensitive; |
Selim Cinek | b89de4e | 2014-06-10 10:47:05 +0200 | [diff] [blame] | 196 | boolean isActivatedChild = activatedChild == child; |
Jorim Jaggi | 4538cee | 2014-09-09 15:21:38 +0200 | [diff] [blame] | 197 | if (dimmed && isActivatedChild) { |
Selim Cinek | 281c202 | 2016-10-13 19:14:43 -0700 | [diff] [blame] | 198 | childViewState.zTranslation += 2.0f * ambientState.getZDistanceBetweenElements(); |
Jorim Jaggi | d552d9d | 2014-05-07 19:41:13 +0200 | [diff] [blame] | 199 | } |
| 200 | } |
Selim Cinek | eb97356 | 2014-05-02 17:07:49 +0200 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Handle the special state when views are being dragged |
| 205 | */ |
Jorim Jaggi | d552d9d | 2014-05-07 19:41:13 +0200 | [diff] [blame] | 206 | private void handleDraggedViews(AmbientState ambientState, StackScrollState resultState, |
Selim Cinek | eb97356 | 2014-05-02 17:07:49 +0200 | [diff] [blame] | 207 | StackScrollAlgorithmState algorithmState) { |
Jorim Jaggi | d552d9d | 2014-05-07 19:41:13 +0200 | [diff] [blame] | 208 | ArrayList<View> draggedViews = ambientState.getDraggedViews(); |
| 209 | for (View draggedView : draggedViews) { |
Selim Cinek | eb97356 | 2014-05-02 17:07:49 +0200 | [diff] [blame] | 210 | int childIndex = algorithmState.visibleChildren.indexOf(draggedView); |
| 211 | if (childIndex >= 0 && childIndex < algorithmState.visibleChildren.size() - 1) { |
| 212 | View nextChild = algorithmState.visibleChildren.get(childIndex + 1); |
Jorim Jaggi | d552d9d | 2014-05-07 19:41:13 +0200 | [diff] [blame] | 213 | if (!draggedViews.contains(nextChild)) { |
Selim Cinek | eb97356 | 2014-05-02 17:07:49 +0200 | [diff] [blame] | 214 | // only if the view is not dragged itself we modify its state to be fully |
| 215 | // visible |
Selim Cinek | bbcebde | 2016-11-09 18:28:20 -0800 | [diff] [blame] | 216 | ExpandableViewState viewState = resultState.getViewStateForView( |
Selim Cinek | eb97356 | 2014-05-02 17:07:49 +0200 | [diff] [blame] | 217 | nextChild); |
| 218 | // The child below the dragged one must be fully visible |
Selim Cinek | 131c1e2 | 2015-05-11 19:04:49 -0700 | [diff] [blame] | 219 | if (ambientState.isShadeExpanded()) { |
Selim Cinek | 277a8aa | 2016-01-22 12:12:37 -0800 | [diff] [blame] | 220 | viewState.shadowAlpha = 1; |
| 221 | viewState.hidden = false; |
Selim Cinek | a59ecc3 | 2015-04-07 10:51:49 -0700 | [diff] [blame] | 222 | } |
Selim Cinek | eb97356 | 2014-05-02 17:07:49 +0200 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | // Lets set the alpha to the one it currently has, as its currently being dragged |
Selim Cinek | bbcebde | 2016-11-09 18:28:20 -0800 | [diff] [blame] | 226 | ExpandableViewState viewState = resultState.getViewStateForView(draggedView); |
Selim Cinek | eb97356 | 2014-05-02 17:07:49 +0200 | [diff] [blame] | 227 | // The dragged child should keep the set alpha |
| 228 | viewState.alpha = draggedView.getAlpha(); |
| 229 | } |
| 230 | } |
Selim Cinek | 343e6e2 | 2014-04-11 21:23:30 +0200 | [diff] [blame] | 231 | } |
Selim Cinek | 67b2260 | 2014-03-10 15:40:16 +0100 | [diff] [blame] | 232 | |
Selim Cinek | 343e6e2 | 2014-04-11 21:23:30 +0200 | [diff] [blame] | 233 | /** |
Selim Cinek | 61633a8 | 2016-01-25 15:54:10 -0800 | [diff] [blame] | 234 | * Initialize the algorithm state like updating the visible children. |
Jorim Jaggi | d4a5744 | 2014-04-10 02:45:55 +0200 | [diff] [blame] | 235 | */ |
Selim Cinek | 3776fe0 | 2016-02-04 13:32:43 -0800 | [diff] [blame] | 236 | private void initAlgorithmState(StackScrollState resultState, StackScrollAlgorithmState state, |
| 237 | AmbientState ambientState) { |
Selim Cinek | 3776fe0 | 2016-02-04 13:32:43 -0800 | [diff] [blame] | 238 | float bottomOverScroll = ambientState.getOverScrollAmount(false /* onTop */); |
| 239 | |
| 240 | int scrollY = ambientState.getScrollY(); |
| 241 | |
| 242 | // Due to the overScroller, the stackscroller can have negative scroll state. This is |
| 243 | // already accounted for by the top padding and doesn't need an additional adaption |
| 244 | scrollY = Math.max(0, scrollY); |
| 245 | state.scrollY = (int) (scrollY + bottomOverScroll); |
| 246 | |
| 247 | //now init the visible children and update paddings |
Jorim Jaggi | d4a5744 | 2014-04-10 02:45:55 +0200 | [diff] [blame] | 248 | ViewGroup hostView = resultState.getHostView(); |
| 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++) { |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 264 | ExpandableView v = (ExpandableView) hostView.getChildAt(i); |
Jorim Jaggi | d4a5744 | 2014-04-10 02:45:55 +0200 | [diff] [blame] | 265 | if (v.getVisibility() != View.GONE) { |
Selim Cinek | 281c202 | 2016-10-13 19:14:43 -0700 | [diff] [blame] | 266 | if (v == ambientState.getShelf()) { |
| 267 | continue; |
| 268 | } |
Selim Cinek | d96ed40 | 2017-07-28 18:19:03 -0700 | [diff] [blame] | 269 | if (i >= firstHiddenIndex) { |
| 270 | // we need normal padding now, to be in sync with what the stack calculates |
| 271 | lastView = null; |
| 272 | ExpandableViewState viewState = resultState.getViewStateForView(v); |
| 273 | viewState.hidden = true; |
| 274 | } |
Selim Cinek | a4baaa3 | 2015-04-20 14:27:54 -0700 | [diff] [blame] | 275 | notGoneIndex = updateNotGoneIndex(resultState, state, notGoneIndex, v); |
Selim Cinek | d96ed40 | 2017-07-28 18:19:03 -0700 | [diff] [blame] | 276 | float increasedPadding = v.getIncreasedPaddingAmount(); |
Selim Cinek | 42357e0 | 2016-02-24 18:48:01 -0800 | [diff] [blame] | 277 | if (increasedPadding != 0.0f) { |
Selim Cinek | a7ed2c1 | 2017-01-23 20:47:24 -0800 | [diff] [blame] | 278 | state.paddingMap.put(v, increasedPadding); |
Selim Cinek | 61633a8 | 2016-01-25 15:54:10 -0800 | [diff] [blame] | 279 | if (lastView != null) { |
Selim Cinek | a7ed2c1 | 2017-01-23 20:47:24 -0800 | [diff] [blame] | 280 | Float prevValue = state.paddingMap.get(lastView); |
| 281 | float newValue = getPaddingForValue(increasedPadding); |
| 282 | if (prevValue != null) { |
| 283 | float prevPadding = getPaddingForValue(prevValue); |
| 284 | if (increasedPadding > 0) { |
| 285 | newValue = NotificationUtils.interpolate( |
| 286 | prevPadding, |
| 287 | newValue, |
| 288 | increasedPadding); |
| 289 | } else if (prevValue > 0) { |
| 290 | newValue = NotificationUtils.interpolate( |
| 291 | newValue, |
| 292 | prevPadding, |
| 293 | prevValue); |
| 294 | } |
| 295 | } |
| 296 | state.paddingMap.put(lastView, newValue); |
Selim Cinek | 61633a8 | 2016-01-25 15:54:10 -0800 | [diff] [blame] | 297 | } |
Selim Cinek | a7ed2c1 | 2017-01-23 20:47:24 -0800 | [diff] [blame] | 298 | } else if (lastView != null) { |
Selim Cinek | d96ed40 | 2017-07-28 18:19:03 -0700 | [diff] [blame] | 299 | |
| 300 | // Let's now resolve the value to an actual padding |
Selim Cinek | a7ed2c1 | 2017-01-23 20:47:24 -0800 | [diff] [blame] | 301 | float newValue = getPaddingForValue(state.paddingMap.get(lastView)); |
| 302 | state.paddingMap.put(lastView, newValue); |
Selim Cinek | 61633a8 | 2016-01-25 15:54:10 -0800 | [diff] [blame] | 303 | } |
Selim Cinek | b5605e5 | 2015-02-20 18:21:41 +0100 | [diff] [blame] | 304 | if (v instanceof ExpandableNotificationRow) { |
| 305 | ExpandableNotificationRow row = (ExpandableNotificationRow) v; |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 306 | |
| 307 | // handle the notgoneIndex for the children as well |
Selim Cinek | b5605e5 | 2015-02-20 18:21:41 +0100 | [diff] [blame] | 308 | List<ExpandableNotificationRow> children = |
| 309 | row.getNotificationChildren(); |
Selim Cinek | 83bc783 | 2015-10-22 13:26:54 -0700 | [diff] [blame] | 310 | if (row.isSummaryWithChildren() && children != null) { |
Selim Cinek | b5605e5 | 2015-02-20 18:21:41 +0100 | [diff] [blame] | 311 | for (ExpandableNotificationRow childRow : children) { |
| 312 | if (childRow.getVisibility() != View.GONE) { |
Selim Cinek | bbcebde | 2016-11-09 18:28:20 -0800 | [diff] [blame] | 313 | ExpandableViewState childState |
Selim Cinek | b5605e5 | 2015-02-20 18:21:41 +0100 | [diff] [blame] | 314 | = resultState.getViewStateForView(childRow); |
| 315 | childState.notGoneIndex = notGoneIndex; |
| 316 | notGoneIndex++; |
| 317 | } |
| 318 | } |
| 319 | } |
| 320 | } |
Selim Cinek | 61633a8 | 2016-01-25 15:54:10 -0800 | [diff] [blame] | 321 | lastView = v; |
Jorim Jaggi | d4a5744 | 2014-04-10 02:45:55 +0200 | [diff] [blame] | 322 | } |
| 323 | } |
Selim Cinek | 2627d72 | 2018-01-19 12:16:49 -0800 | [diff] [blame] | 324 | ExpandableNotificationRow expandingNotification = ambientState.getExpandingNotification(); |
| 325 | state.indexOfExpandingNotification = expandingNotification != null |
| 326 | ? state.visibleChildren.indexOf(expandingNotification) |
| 327 | : -1; |
Jorim Jaggi | d4a5744 | 2014-04-10 02:45:55 +0200 | [diff] [blame] | 328 | } |
| 329 | |
Selim Cinek | a7ed2c1 | 2017-01-23 20:47:24 -0800 | [diff] [blame] | 330 | private float getPaddingForValue(Float increasedPadding) { |
| 331 | if (increasedPadding == null) { |
| 332 | return mPaddingBetweenElements; |
| 333 | } else if (increasedPadding >= 0.0f) { |
| 334 | return NotificationUtils.interpolate( |
| 335 | mPaddingBetweenElements, |
| 336 | mIncreasedPaddingBetweenElements, |
| 337 | increasedPadding); |
| 338 | } else { |
| 339 | return NotificationUtils.interpolate( |
| 340 | 0, |
| 341 | mPaddingBetweenElements, |
| 342 | 1.0f + increasedPadding); |
| 343 | } |
| 344 | } |
| 345 | |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 346 | private int updateNotGoneIndex(StackScrollState resultState, |
| 347 | StackScrollAlgorithmState state, int notGoneIndex, |
| 348 | ExpandableView v) { |
Selim Cinek | bbcebde | 2016-11-09 18:28:20 -0800 | [diff] [blame] | 349 | ExpandableViewState viewState = resultState.getViewStateForView(v); |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 350 | viewState.notGoneIndex = notGoneIndex; |
| 351 | state.visibleChildren.add(v); |
| 352 | notGoneIndex++; |
| 353 | return notGoneIndex; |
| 354 | } |
| 355 | |
Jorim Jaggi | d4a5744 | 2014-04-10 02:45:55 +0200 | [diff] [blame] | 356 | /** |
Selim Cinek | 67b2260 | 2014-03-10 15:40:16 +0100 | [diff] [blame] | 357 | * Determine the positions for the views. This is the main part of the algorithm. |
| 358 | * |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 359 | * @param resultState The result state to update if a change to the properties of a child occurs |
Selim Cinek | 67b2260 | 2014-03-10 15:40:16 +0100 | [diff] [blame] | 360 | * @param algorithmState The state in which the current pass of the algorithm is currently in |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 361 | * @param ambientState The current ambient state |
Selim Cinek | 67b2260 | 2014-03-10 15:40:16 +0100 | [diff] [blame] | 362 | */ |
| 363 | private void updatePositionsForState(StackScrollState resultState, |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 364 | StackScrollAlgorithmState algorithmState, AmbientState ambientState) { |
Selim Cinek | 67b2260 | 2014-03-10 15:40:16 +0100 | [diff] [blame] | 365 | |
Selim Cinek | 67b2260 | 2014-03-10 15:40:16 +0100 | [diff] [blame] | 366 | // The y coordinate of the current child. |
Selim Cinek | 3776fe0 | 2016-02-04 13:32:43 -0800 | [diff] [blame] | 367 | float currentYPosition = -algorithmState.scrollY; |
Jorim Jaggi | d4a5744 | 2014-04-10 02:45:55 +0200 | [diff] [blame] | 368 | int childCount = algorithmState.visibleChildren.size(); |
Selim Cinek | 67b2260 | 2014-03-10 15:40:16 +0100 | [diff] [blame] | 369 | for (int i = 0; i < childCount; i++) { |
Muyuan Li | 8779802 | 2016-04-07 17:51:25 -0700 | [diff] [blame] | 370 | currentYPosition = updateChild(i, resultState, algorithmState, ambientState, |
Selim Cinek | db16737 | 2016-11-17 15:41:17 -0800 | [diff] [blame] | 371 | currentYPosition); |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 372 | } |
| 373 | } |
| 374 | |
Muyuan Li | 8779802 | 2016-04-07 17:51:25 -0700 | [diff] [blame] | 375 | protected float updateChild(int i, StackScrollState resultState, |
| 376 | StackScrollAlgorithmState algorithmState, AmbientState ambientState, |
Selim Cinek | db16737 | 2016-11-17 15:41:17 -0800 | [diff] [blame] | 377 | float currentYPosition) { |
Muyuan Li | 8779802 | 2016-04-07 17:51:25 -0700 | [diff] [blame] | 378 | ExpandableView child = algorithmState.visibleChildren.get(i); |
Selim Cinek | bbcebde | 2016-11-09 18:28:20 -0800 | [diff] [blame] | 379 | ExpandableViewState childViewState = resultState.getViewStateForView(child); |
| 380 | childViewState.location = ExpandableViewState.LOCATION_UNKNOWN; |
Muyuan Li | 8779802 | 2016-04-07 17:51:25 -0700 | [diff] [blame] | 381 | int paddingAfterChild = getPaddingAfterChild(algorithmState, child); |
| 382 | int childHeight = getMaxAllowedChildHeight(child); |
Muyuan Li | 8779802 | 2016-04-07 17:51:25 -0700 | [diff] [blame] | 383 | childViewState.yTranslation = currentYPosition; |
Selim Cinek | c9c640f | 2016-11-14 17:27:19 -0800 | [diff] [blame] | 384 | boolean isDismissView = child instanceof DismissView; |
Selim Cinek | cde90e5 | 2016-12-22 21:01:49 +0100 | [diff] [blame] | 385 | boolean isEmptyShadeView = child instanceof EmptyShadeView; |
Muyuan Li | 8779802 | 2016-04-07 17:51:25 -0700 | [diff] [blame] | 386 | |
Selim Cinek | db16737 | 2016-11-17 15:41:17 -0800 | [diff] [blame] | 387 | childViewState.location = ExpandableViewState.LOCATION_MAIN_AREA; |
Selim Cinek | 9b9d6e1 | 2017-11-30 12:29:47 +0100 | [diff] [blame] | 388 | float inset = ambientState.getTopPadding() + ambientState.getStackTranslation(); |
Selim Cinek | 2627d72 | 2018-01-19 12:16:49 -0800 | [diff] [blame] | 389 | if (i < algorithmState.getIndexOfExpandingNotification()) { |
| 390 | inset += ambientState.getExpandAnimationTopChange(); |
| 391 | } |
Selim Cinek | 9b9d6e1 | 2017-11-30 12:29:47 +0100 | [diff] [blame] | 392 | if (child.mustStayOnScreen() && childViewState.yTranslation >= 0) { |
| 393 | // Even if we're not scrolled away we're in view and we're also not in the |
| 394 | // shelf. We can relax the constraints and let us scroll off the top! |
| 395 | float end = childViewState.yTranslation + childViewState.height + inset; |
| 396 | childViewState.headsUpIsVisible = end < ambientState.getMaxHeadsUpTranslation(); |
| 397 | } |
Selim Cinek | db16737 | 2016-11-17 15:41:17 -0800 | [diff] [blame] | 398 | if (isDismissView) { |
| 399 | childViewState.yTranslation = Math.min(childViewState.yTranslation, |
| 400 | ambientState.getInnerHeight() - childHeight); |
Selim Cinek | cde90e5 | 2016-12-22 21:01:49 +0100 | [diff] [blame] | 401 | } else if (isEmptyShadeView) { |
| 402 | childViewState.yTranslation = ambientState.getInnerHeight() - childHeight |
| 403 | + ambientState.getStackTranslation() * 0.25f; |
Muyuan Li | 8779802 | 2016-04-07 17:51:25 -0700 | [diff] [blame] | 404 | } else { |
Selim Cinek | 2627d72 | 2018-01-19 12:16:49 -0800 | [diff] [blame] | 405 | clampPositionToShelf(child, childViewState, ambientState); |
Muyuan Li | 8779802 | 2016-04-07 17:51:25 -0700 | [diff] [blame] | 406 | } |
| 407 | |
Muyuan Li | 8779802 | 2016-04-07 17:51:25 -0700 | [diff] [blame] | 408 | currentYPosition = childViewState.yTranslation + childHeight + paddingAfterChild; |
| 409 | if (currentYPosition <= 0) { |
Selim Cinek | bbcebde | 2016-11-09 18:28:20 -0800 | [diff] [blame] | 410 | childViewState.location = ExpandableViewState.LOCATION_HIDDEN_TOP; |
Muyuan Li | 8779802 | 2016-04-07 17:51:25 -0700 | [diff] [blame] | 411 | } |
Selim Cinek | bbcebde | 2016-11-09 18:28:20 -0800 | [diff] [blame] | 412 | if (childViewState.location == ExpandableViewState.LOCATION_UNKNOWN) { |
Muyuan Li | 8779802 | 2016-04-07 17:51:25 -0700 | [diff] [blame] | 413 | Log.wtf(LOG_TAG, "Failed to assign location for child " + i); |
| 414 | } |
| 415 | |
Selim Cinek | 9b9d6e1 | 2017-11-30 12:29:47 +0100 | [diff] [blame] | 416 | childViewState.yTranslation += inset; |
Muyuan Li | 8779802 | 2016-04-07 17:51:25 -0700 | [diff] [blame] | 417 | return currentYPosition; |
| 418 | } |
| 419 | |
| 420 | protected int getPaddingAfterChild(StackScrollAlgorithmState algorithmState, |
Selim Cinek | 61633a8 | 2016-01-25 15:54:10 -0800 | [diff] [blame] | 421 | ExpandableView child) { |
Selim Cinek | 281c202 | 2016-10-13 19:14:43 -0700 | [diff] [blame] | 422 | return algorithmState.getPaddingAfterChild(child); |
Selim Cinek | 61633a8 | 2016-01-25 15:54:10 -0800 | [diff] [blame] | 423 | } |
| 424 | |
Selim Cinek | a4baaa3 | 2015-04-20 14:27:54 -0700 | [diff] [blame] | 425 | private void updateHeadsUpStates(StackScrollState resultState, |
| 426 | StackScrollAlgorithmState algorithmState, AmbientState ambientState) { |
| 427 | int childCount = algorithmState.visibleChildren.size(); |
| 428 | ExpandableNotificationRow topHeadsUpEntry = null; |
| 429 | for (int i = 0; i < childCount; i++) { |
| 430 | View child = algorithmState.visibleChildren.get(i); |
| 431 | if (!(child instanceof ExpandableNotificationRow)) { |
| 432 | break; |
| 433 | } |
| 434 | ExpandableNotificationRow row = (ExpandableNotificationRow) child; |
| 435 | if (!row.isHeadsUp()) { |
| 436 | break; |
Selim Cinek | a4baaa3 | 2015-04-20 14:27:54 -0700 | [diff] [blame] | 437 | } |
Selim Cinek | bbcebde | 2016-11-09 18:28:20 -0800 | [diff] [blame] | 438 | ExpandableViewState childState = resultState.getViewStateForView(row); |
Selim Cinek | 9b9d6e1 | 2017-11-30 12:29:47 +0100 | [diff] [blame] | 439 | if (topHeadsUpEntry == null && row.mustStayOnScreen() && !childState.headsUpIsVisible) { |
Selim Cinek | 3776fe0 | 2016-02-04 13:32:43 -0800 | [diff] [blame] | 440 | topHeadsUpEntry = row; |
Selim Cinek | bbcebde | 2016-11-09 18:28:20 -0800 | [diff] [blame] | 441 | childState.location = ExpandableViewState.LOCATION_FIRST_HUN; |
Selim Cinek | 3776fe0 | 2016-02-04 13:32:43 -0800 | [diff] [blame] | 442 | } |
Selim Cinek | 1f3f544 | 2015-04-10 17:54:46 -0700 | [diff] [blame] | 443 | boolean isTopEntry = topHeadsUpEntry == row; |
Selim Cinek | 3776fe0 | 2016-02-04 13:32:43 -0800 | [diff] [blame] | 444 | float unmodifiedEndLocation = childState.yTranslation + childState.height; |
Selim Cinek | 131c1e2 | 2015-05-11 19:04:49 -0700 | [diff] [blame] | 445 | if (mIsExpanded) { |
Selim Cinek | 9b9d6e1 | 2017-11-30 12:29:47 +0100 | [diff] [blame] | 446 | if (row.mustStayOnScreen() && !childState.headsUpIsVisible) { |
| 447 | // Ensure that the heads up is always visible even when scrolled off |
| 448 | clampHunToTop(ambientState, row, childState); |
| 449 | if (i == 0 && ambientState.isAboveShelf(row)) { |
| 450 | // the first hun can't get off screen. |
| 451 | clampHunToMaxTranslation(ambientState, row, childState); |
| 452 | childState.hidden = false; |
| 453 | } |
Selim Cinek | d127d79 | 2016-11-01 19:11:41 -0700 | [diff] [blame] | 454 | } |
Selim Cinek | 131c1e2 | 2015-05-11 19:04:49 -0700 | [diff] [blame] | 455 | } |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 456 | if (row.isPinned()) { |
Selim Cinek | 33d4614 | 2016-01-15 18:58:39 -0800 | [diff] [blame] | 457 | childState.yTranslation = Math.max(childState.yTranslation, 0); |
Selim Cinek | 31aada4 | 2015-12-18 17:51:15 -0800 | [diff] [blame] | 458 | childState.height = Math.max(row.getIntrinsicHeight(), childState.height); |
Selim Cinek | cafa87f | 2016-10-26 17:00:17 -0700 | [diff] [blame] | 459 | childState.hidden = false; |
Selim Cinek | bbcebde | 2016-11-09 18:28:20 -0800 | [diff] [blame] | 460 | ExpandableViewState topState = resultState.getViewStateForView(topHeadsUpEntry); |
Selim Cinek | 61cfd4b | 2017-12-08 12:42:36 -0800 | [diff] [blame] | 461 | if (topState != null && !isTopEntry && (!mIsExpanded |
Selim Cinek | 3776fe0 | 2016-02-04 13:32:43 -0800 | [diff] [blame] | 462 | || unmodifiedEndLocation < topState.yTranslation + topState.height)) { |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 463 | // Ensure that a headsUp doesn't vertically extend further than the heads-up at |
| 464 | // the top most z-position |
Selim Cinek | 31aada4 | 2015-12-18 17:51:15 -0800 | [diff] [blame] | 465 | childState.height = row.getIntrinsicHeight(); |
Selim Cinek | e53e6bb | 2015-04-13 16:14:26 -0700 | [diff] [blame] | 466 | childState.yTranslation = topState.yTranslation + topState.height |
| 467 | - childState.height; |
Selim Cinek | 1f3f544 | 2015-04-10 17:54:46 -0700 | [diff] [blame] | 468 | } |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 469 | } |
Selim Cinek | cafa87f | 2016-10-26 17:00:17 -0700 | [diff] [blame] | 470 | if (row.isHeadsUpAnimatingAway()) { |
| 471 | childState.hidden = false; |
| 472 | } |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 473 | } |
Selim Cinek | 67b2260 | 2014-03-10 15:40:16 +0100 | [diff] [blame] | 474 | } |
| 475 | |
Selim Cinek | 3776fe0 | 2016-02-04 13:32:43 -0800 | [diff] [blame] | 476 | private void clampHunToTop(AmbientState ambientState, ExpandableNotificationRow row, |
Selim Cinek | bbcebde | 2016-11-09 18:28:20 -0800 | [diff] [blame] | 477 | ExpandableViewState childState) { |
Selim Cinek | 3776fe0 | 2016-02-04 13:32:43 -0800 | [diff] [blame] | 478 | float newTranslation = Math.max(ambientState.getTopPadding() |
| 479 | + ambientState.getStackTranslation(), childState.yTranslation); |
| 480 | childState.height = (int) Math.max(childState.height - (newTranslation |
Selim Cinek | 567e845 | 2016-03-24 10:54:56 -0700 | [diff] [blame] | 481 | - childState.yTranslation), row.getCollapsedHeight()); |
Selim Cinek | 3776fe0 | 2016-02-04 13:32:43 -0800 | [diff] [blame] | 482 | childState.yTranslation = newTranslation; |
| 483 | } |
| 484 | |
| 485 | private void clampHunToMaxTranslation(AmbientState ambientState, ExpandableNotificationRow row, |
Selim Cinek | bbcebde | 2016-11-09 18:28:20 -0800 | [diff] [blame] | 486 | ExpandableViewState childState) { |
Selim Cinek | 3776fe0 | 2016-02-04 13:32:43 -0800 | [diff] [blame] | 487 | float newTranslation; |
Selim Cinek | 7e0f948 | 2017-05-22 20:00:56 -0700 | [diff] [blame] | 488 | float maxHeadsUpTranslation = ambientState.getMaxHeadsUpTranslation(); |
| 489 | float maxShelfPosition = ambientState.getInnerHeight() + ambientState.getTopPadding() |
| 490 | + ambientState.getStackTranslation(); |
| 491 | maxHeadsUpTranslation = Math.min(maxHeadsUpTranslation, maxShelfPosition); |
| 492 | float bottomPosition = maxHeadsUpTranslation - row.getCollapsedHeight(); |
Selim Cinek | 3776fe0 | 2016-02-04 13:32:43 -0800 | [diff] [blame] | 493 | newTranslation = Math.min(childState.yTranslation, bottomPosition); |
Selim Cinek | 7e0f948 | 2017-05-22 20:00:56 -0700 | [diff] [blame] | 494 | childState.height = (int) Math.min(childState.height, maxHeadsUpTranslation |
| 495 | - newTranslation); |
Selim Cinek | 3776fe0 | 2016-02-04 13:32:43 -0800 | [diff] [blame] | 496 | childState.yTranslation = newTranslation; |
Selim Cinek | 343e6e2 | 2014-04-11 21:23:30 +0200 | [diff] [blame] | 497 | } |
| 498 | |
| 499 | /** |
Selim Cinek | 281c202 | 2016-10-13 19:14:43 -0700 | [diff] [blame] | 500 | * Clamp the height of the child down such that its end is at most on the beginning of |
| 501 | * the shelf. |
| 502 | * |
Selim Cinek | 2627d72 | 2018-01-19 12:16:49 -0800 | [diff] [blame] | 503 | * @param child |
Selim Cinek | 281c202 | 2016-10-13 19:14:43 -0700 | [diff] [blame] | 504 | * @param childViewState the view state of the child |
| 505 | * @param ambientState the ambient state |
| 506 | */ |
Selim Cinek | 2627d72 | 2018-01-19 12:16:49 -0800 | [diff] [blame] | 507 | private void clampPositionToShelf(ExpandableView child, |
| 508 | ExpandableViewState childViewState, |
Selim Cinek | 281c202 | 2016-10-13 19:14:43 -0700 | [diff] [blame] | 509 | AmbientState ambientState) { |
Eliot Courtney | 5d3d2d0 | 2018-01-18 15:59:03 +0900 | [diff] [blame] | 510 | if (ambientState.getShelf() == null) { |
| 511 | return; |
| 512 | } |
| 513 | |
Selim Cinek | a686b2c | 2016-10-26 13:58:27 -0700 | [diff] [blame] | 514 | int shelfStart = ambientState.getInnerHeight() |
| 515 | - ambientState.getShelf().getIntrinsicHeight(); |
Selim Cinek | 281c202 | 2016-10-13 19:14:43 -0700 | [diff] [blame] | 516 | childViewState.yTranslation = Math.min(childViewState.yTranslation, shelfStart); |
Selim Cinek | c383fd0 | 2016-10-21 15:31:26 -0700 | [diff] [blame] | 517 | if (childViewState.yTranslation >= shelfStart) { |
Selim Cinek | 2627d72 | 2018-01-19 12:16:49 -0800 | [diff] [blame] | 518 | childViewState.hidden = !child.isExpandAnimationRunning(); |
Selim Cinek | eccb5de | 2016-10-28 15:04:05 -0700 | [diff] [blame] | 519 | childViewState.inShelf = true; |
Selim Cinek | 9b9d6e1 | 2017-11-30 12:29:47 +0100 | [diff] [blame] | 520 | childViewState.headsUpIsVisible = false; |
Selim Cinek | c383fd0 | 2016-10-21 15:31:26 -0700 | [diff] [blame] | 521 | } |
Selim Cinek | cafa87f | 2016-10-26 17:00:17 -0700 | [diff] [blame] | 522 | if (!ambientState.isShadeExpanded()) { |
| 523 | childViewState.height = (int) (mStatusBarHeight - childViewState.yTranslation); |
| 524 | } |
Selim Cinek | 281c202 | 2016-10-13 19:14:43 -0700 | [diff] [blame] | 525 | } |
| 526 | |
Muyuan Li | 8779802 | 2016-04-07 17:51:25 -0700 | [diff] [blame] | 527 | protected int getMaxAllowedChildHeight(View child) { |
Selim Cinek | 31aada4 | 2015-12-18 17:51:15 -0800 | [diff] [blame] | 528 | if (child instanceof ExpandableView) { |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 529 | ExpandableView expandableView = (ExpandableView) child; |
Selim Cinek | 8d5727f | 2015-04-28 19:17:32 -0700 | [diff] [blame] | 530 | return expandableView.getIntrinsicHeight(); |
Selim Cinek | 1685e63 | 2014-04-08 02:27:49 +0200 | [diff] [blame] | 531 | } |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 532 | return child == null? mCollapsedSize : child.getHeight(); |
Selim Cinek | 1685e63 | 2014-04-08 02:27:49 +0200 | [diff] [blame] | 533 | } |
| 534 | |
Selim Cinek | 67b2260 | 2014-03-10 15:40:16 +0100 | [diff] [blame] | 535 | /** |
Selim Cinek | 67b2260 | 2014-03-10 15:40:16 +0100 | [diff] [blame] | 536 | * Calculate the Z positions for all children based on the number of items in both stacks and |
| 537 | * save it in the resultState |
Selim Cinek | 3776fe0 | 2016-02-04 13:32:43 -0800 | [diff] [blame] | 538 | * @param resultState The result state to update the zTranslation values |
Selim Cinek | 67b2260 | 2014-03-10 15:40:16 +0100 | [diff] [blame] | 539 | * @param algorithmState The state in which the current pass of the algorithm is currently in |
Selim Cinek | 3776fe0 | 2016-02-04 13:32:43 -0800 | [diff] [blame] | 540 | * @param ambientState The ambient state of the algorithm |
Selim Cinek | 67b2260 | 2014-03-10 15:40:16 +0100 | [diff] [blame] | 541 | */ |
| 542 | private void updateZValuesForState(StackScrollState resultState, |
Selim Cinek | 3776fe0 | 2016-02-04 13:32:43 -0800 | [diff] [blame] | 543 | StackScrollAlgorithmState algorithmState, AmbientState ambientState) { |
Jorim Jaggi | d4a5744 | 2014-04-10 02:45:55 +0200 | [diff] [blame] | 544 | int childCount = algorithmState.visibleChildren.size(); |
Selim Cinek | 3322357 | 2016-02-19 19:32:22 -0800 | [diff] [blame] | 545 | float childrenOnTop = 0.0f; |
Selim Cinek | 3776fe0 | 2016-02-04 13:32:43 -0800 | [diff] [blame] | 546 | for (int i = childCount - 1; i >= 0; i--) { |
Selim Cinek | daab6f5 | 2017-04-06 16:46:34 -0700 | [diff] [blame] | 547 | childrenOnTop = updateChildZValue(i, childrenOnTop, |
Muyuan Li | 8779802 | 2016-04-07 17:51:25 -0700 | [diff] [blame] | 548 | resultState, algorithmState, ambientState); |
| 549 | } |
| 550 | } |
| 551 | |
Selim Cinek | daab6f5 | 2017-04-06 16:46:34 -0700 | [diff] [blame] | 552 | protected float updateChildZValue(int i, float childrenOnTop, |
Muyuan Li | 8779802 | 2016-04-07 17:51:25 -0700 | [diff] [blame] | 553 | StackScrollState resultState, StackScrollAlgorithmState algorithmState, |
| 554 | AmbientState ambientState) { |
| 555 | ExpandableView child = algorithmState.visibleChildren.get(i); |
Selim Cinek | bbcebde | 2016-11-09 18:28:20 -0800 | [diff] [blame] | 556 | ExpandableViewState childViewState = resultState.getViewStateForView(child); |
Selim Cinek | 281c202 | 2016-10-13 19:14:43 -0700 | [diff] [blame] | 557 | int zDistanceBetweenElements = ambientState.getZDistanceBetweenElements(); |
| 558 | float baseZ = ambientState.getBaseZHeight(); |
Selim Cinek | 9b9d6e1 | 2017-11-30 12:29:47 +0100 | [diff] [blame] | 559 | if (child.mustStayOnScreen() && !childViewState.headsUpIsVisible |
| 560 | && !ambientState.isDozingAndNotPulsing(child) |
Muyuan Li | 8779802 | 2016-04-07 17:51:25 -0700 | [diff] [blame] | 561 | && childViewState.yTranslation < ambientState.getTopPadding() |
| 562 | + ambientState.getStackTranslation()) { |
| 563 | if (childrenOnTop != 0.0f) { |
| 564 | childrenOnTop++; |
| 565 | } else { |
| 566 | float overlap = ambientState.getTopPadding() |
| 567 | + ambientState.getStackTranslation() - childViewState.yTranslation; |
| 568 | childrenOnTop += Math.min(1.0f, overlap / childViewState.height); |
| 569 | } |
Selim Cinek | 281c202 | 2016-10-13 19:14:43 -0700 | [diff] [blame] | 570 | childViewState.zTranslation = baseZ |
| 571 | + childrenOnTop * zDistanceBetweenElements; |
Selim Cinek | ebf4234 | 2017-07-13 15:46:10 +0200 | [diff] [blame] | 572 | } else if (i == 0 && ambientState.isAboveShelf(child)) { |
Selim Cinek | d127d79 | 2016-11-01 19:11:41 -0700 | [diff] [blame] | 573 | // In case this is a new view that has never been measured before, we don't want to |
| 574 | // elevate if we are currently expanded more then the notification |
Eliot Courtney | 5d3d2d0 | 2018-01-18 15:59:03 +0900 | [diff] [blame] | 575 | int shelfHeight = ambientState.getShelf() == null ? 0 : |
| 576 | ambientState.getShelf().getIntrinsicHeight(); |
Selim Cinek | d127d79 | 2016-11-01 19:11:41 -0700 | [diff] [blame] | 577 | float shelfStart = ambientState.getInnerHeight() |
| 578 | - shelfHeight + ambientState.getTopPadding() |
| 579 | + ambientState.getStackTranslation(); |
| 580 | float notificationEnd = childViewState.yTranslation + child.getPinnedHeadsUpHeight() |
| 581 | + mPaddingBetweenElements; |
| 582 | if (shelfStart > notificationEnd) { |
| 583 | childViewState.zTranslation = baseZ; |
| 584 | } else { |
| 585 | float factor = (notificationEnd - shelfStart) / shelfHeight; |
| 586 | factor = Math.min(factor, 1.0f); |
| 587 | childViewState.zTranslation = baseZ + factor * zDistanceBetweenElements; |
| 588 | } |
Muyuan Li | 8779802 | 2016-04-07 17:51:25 -0700 | [diff] [blame] | 589 | } else { |
Selim Cinek | 281c202 | 2016-10-13 19:14:43 -0700 | [diff] [blame] | 590 | childViewState.zTranslation = baseZ; |
Selim Cinek | 67b2260 | 2014-03-10 15:40:16 +0100 | [diff] [blame] | 591 | } |
Selim Cinek | daab6f5 | 2017-04-06 16:46:34 -0700 | [diff] [blame] | 592 | return childrenOnTop; |
Selim Cinek | 67b2260 | 2014-03-10 15:40:16 +0100 | [diff] [blame] | 593 | } |
| 594 | |
Selim Cinek | 1685e63 | 2014-04-08 02:27:49 +0200 | [diff] [blame] | 595 | public void setIsExpanded(boolean isExpanded) { |
| 596 | this.mIsExpanded = isExpanded; |
| 597 | } |
| 598 | |
Selim Cinek | 281c202 | 2016-10-13 19:14:43 -0700 | [diff] [blame] | 599 | public class StackScrollAlgorithmState { |
Selim Cinek | 67b2260 | 2014-03-10 15:40:16 +0100 | [diff] [blame] | 600 | |
| 601 | /** |
| 602 | * The scroll position of the algorithm |
| 603 | */ |
| 604 | public int scrollY; |
| 605 | |
| 606 | /** |
Jorim Jaggi | d4a5744 | 2014-04-10 02:45:55 +0200 | [diff] [blame] | 607 | * The children from the host view which are not gone. |
| 608 | */ |
Jorim Jaggi | be565df | 2014-04-28 17:51:23 +0200 | [diff] [blame] | 609 | public final ArrayList<ExpandableView> visibleChildren = new ArrayList<ExpandableView>(); |
Selim Cinek | 61633a8 | 2016-01-25 15:54:10 -0800 | [diff] [blame] | 610 | |
| 611 | /** |
Selim Cinek | a7ed2c1 | 2017-01-23 20:47:24 -0800 | [diff] [blame] | 612 | * The padding after each child measured in pixels. |
Selim Cinek | 61633a8 | 2016-01-25 15:54:10 -0800 | [diff] [blame] | 613 | */ |
Selim Cinek | a7ed2c1 | 2017-01-23 20:47:24 -0800 | [diff] [blame] | 614 | public final HashMap<ExpandableView, Float> paddingMap = new HashMap<>(); |
Selim Cinek | 2627d72 | 2018-01-19 12:16:49 -0800 | [diff] [blame] | 615 | private int indexOfExpandingNotification; |
Selim Cinek | 281c202 | 2016-10-13 19:14:43 -0700 | [diff] [blame] | 616 | |
| 617 | public int getPaddingAfterChild(ExpandableView child) { |
Selim Cinek | a7ed2c1 | 2017-01-23 20:47:24 -0800 | [diff] [blame] | 618 | Float padding = paddingMap.get(child); |
| 619 | if (padding == null) { |
| 620 | // Should only happen for the last view |
| 621 | return mPaddingBetweenElements; |
| 622 | } |
| 623 | return (int) padding.floatValue(); |
Selim Cinek | 281c202 | 2016-10-13 19:14:43 -0700 | [diff] [blame] | 624 | } |
Selim Cinek | 2627d72 | 2018-01-19 12:16:49 -0800 | [diff] [blame] | 625 | |
| 626 | public int getIndexOfExpandingNotification() { |
| 627 | return indexOfExpandingNotification; |
| 628 | } |
Selim Cinek | 67b2260 | 2014-03-10 15:40:16 +0100 | [diff] [blame] | 629 | } |
| 630 | |
| 631 | } |