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