Selim Cinek | 29aab96 | 2018-02-27 17:05:45 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License |
| 15 | */ |
| 16 | |
Rohan Shah | 20790b8 | 2018-07-02 17:21:04 -0700 | [diff] [blame] | 17 | package com.android.systemui.statusbar.notification.stack; |
Selim Cinek | 29aab96 | 2018-02-27 17:05:45 -0800 | [diff] [blame] | 18 | |
Dave Mankoff | a4d195d | 2018-11-16 13:33:27 -0500 | [diff] [blame] | 19 | import static com.android.systemui.statusbar.notification.stack.NotificationStackScrollLayout.NUM_SECTIONS; |
Selim Cinek | 29aab96 | 2018-02-27 17:05:45 -0800 | [diff] [blame] | 20 | |
Lucas Dupin | 00be88f | 2019-01-03 17:50:52 -0800 | [diff] [blame] | 21 | import com.android.systemui.statusbar.AmbientPulseManager; |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 22 | import com.android.systemui.statusbar.notification.collection.NotificationEntry; |
Rohan Shah | 20790b8 | 2018-07-02 17:21:04 -0700 | [diff] [blame] | 23 | import com.android.systemui.statusbar.notification.row.ActivatableNotificationView; |
| 24 | import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow; |
Dave Mankoff | a4d195d | 2018-11-16 13:33:27 -0500 | [diff] [blame] | 25 | import com.android.systemui.statusbar.notification.row.ExpandableView; |
Selim Cinek | 29aab96 | 2018-02-27 17:05:45 -0800 | [diff] [blame] | 26 | import com.android.systemui.statusbar.policy.OnHeadsUpChangedListener; |
| 27 | |
| 28 | import java.util.HashSet; |
| 29 | |
Lucas Dupin | 00be88f | 2019-01-03 17:50:52 -0800 | [diff] [blame] | 30 | import javax.inject.Inject; |
| 31 | import javax.inject.Singleton; |
| 32 | |
Selim Cinek | 29aab96 | 2018-02-27 17:05:45 -0800 | [diff] [blame] | 33 | /** |
| 34 | * A class that manages the roundness for notification views |
| 35 | */ |
Lucas Dupin | 00be88f | 2019-01-03 17:50:52 -0800 | [diff] [blame] | 36 | @Singleton |
| 37 | class NotificationRoundnessManager implements OnHeadsUpChangedListener, |
| 38 | AmbientPulseManager.OnAmbientChangedListener { |
Selim Cinek | 29aab96 | 2018-02-27 17:05:45 -0800 | [diff] [blame] | 39 | |
Lucas Dupin | 00be88f | 2019-01-03 17:50:52 -0800 | [diff] [blame] | 40 | private final ActivatableNotificationView[] mFirstInSectionViews; |
| 41 | private final ActivatableNotificationView[] mLastInSectionViews; |
| 42 | private final ActivatableNotificationView[] mTmpFirstInSectionViews; |
| 43 | private final ActivatableNotificationView[] mTmpLastInSectionViews; |
Selim Cinek | 29aab96 | 2018-02-27 17:05:45 -0800 | [diff] [blame] | 44 | private boolean mExpanded; |
Dave Mankoff | a4d195d | 2018-11-16 13:33:27 -0500 | [diff] [blame] | 45 | private HashSet<ExpandableView> mAnimatedChildren; |
Selim Cinek | 29aab96 | 2018-02-27 17:05:45 -0800 | [diff] [blame] | 46 | private Runnable mRoundingChangedCallback; |
Selim Cinek | aa9db1f | 2018-02-27 17:35:47 -0800 | [diff] [blame] | 47 | private ExpandableNotificationRow mTrackedHeadsUp; |
Lucas Dupin | 00be88f | 2019-01-03 17:50:52 -0800 | [diff] [blame] | 48 | private ActivatableNotificationView mTrackedAmbient; |
Selim Cinek | aa9db1f | 2018-02-27 17:35:47 -0800 | [diff] [blame] | 49 | private float mAppearFraction; |
Selim Cinek | 29aab96 | 2018-02-27 17:05:45 -0800 | [diff] [blame] | 50 | |
Lucas Dupin | 00be88f | 2019-01-03 17:50:52 -0800 | [diff] [blame] | 51 | @Inject |
| 52 | NotificationRoundnessManager(AmbientPulseManager ambientPulseManager) { |
Gus Prevas | e2d6f04 | 2018-10-17 15:25:30 -0400 | [diff] [blame] | 53 | mFirstInSectionViews = new ActivatableNotificationView[NUM_SECTIONS]; |
| 54 | mLastInSectionViews = new ActivatableNotificationView[NUM_SECTIONS]; |
| 55 | mTmpFirstInSectionViews = new ActivatableNotificationView[NUM_SECTIONS]; |
| 56 | mTmpLastInSectionViews = new ActivatableNotificationView[NUM_SECTIONS]; |
Lucas Dupin | 00be88f | 2019-01-03 17:50:52 -0800 | [diff] [blame] | 57 | ambientPulseManager.addListener(this); |
Gus Prevas | e2d6f04 | 2018-10-17 15:25:30 -0400 | [diff] [blame] | 58 | } |
| 59 | |
Selim Cinek | 29aab96 | 2018-02-27 17:05:45 -0800 | [diff] [blame] | 60 | @Override |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 61 | public void onHeadsUpPinned(NotificationEntry headsUp) { |
Evan Laird | 9449285 | 2018-10-25 13:43:01 -0400 | [diff] [blame] | 62 | updateView(headsUp.getRow(), false /* animate */); |
Selim Cinek | 29aab96 | 2018-02-27 17:05:45 -0800 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | @Override |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 66 | public void onHeadsUpUnPinned(NotificationEntry headsUp) { |
Evan Laird | 9449285 | 2018-10-25 13:43:01 -0400 | [diff] [blame] | 67 | updateView(headsUp.getRow(), true /* animate */); |
Selim Cinek | 29aab96 | 2018-02-27 17:05:45 -0800 | [diff] [blame] | 68 | } |
| 69 | |
Selim Cinek | 8875de1 | 2018-03-22 10:14:32 -0700 | [diff] [blame] | 70 | public void onHeadsupAnimatingAwayChanged(ExpandableNotificationRow row, |
| 71 | boolean isAnimatingAway) { |
Gus Prevas | e2d6f04 | 2018-10-17 15:25:30 -0400 | [diff] [blame] | 72 | updateView(row, false /* animate */); |
Selim Cinek | 8875de1 | 2018-03-22 10:14:32 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Lucas Dupin | 00be88f | 2019-01-03 17:50:52 -0800 | [diff] [blame] | 75 | @Override |
| 76 | public void onAmbientStateChanged(NotificationEntry entry, boolean isPulsing) { |
| 77 | ActivatableNotificationView row = entry.getRow(); |
| 78 | if (isPulsing) { |
| 79 | mTrackedAmbient = row; |
| 80 | } else if (mTrackedAmbient == row) { |
| 81 | mTrackedAmbient = null; |
| 82 | } |
| 83 | updateView(row, false /* animate */); |
| 84 | } |
| 85 | |
Gus Prevas | e2d6f04 | 2018-10-17 15:25:30 -0400 | [diff] [blame] | 86 | private void updateView(ActivatableNotificationView view, boolean animate) { |
| 87 | boolean changed = updateViewWithoutCallback(view, animate); |
| 88 | if (changed) { |
Selim Cinek | 29aab96 | 2018-02-27 17:05:45 -0800 | [diff] [blame] | 89 | mRoundingChangedCallback.run(); |
| 90 | } |
| 91 | } |
| 92 | |
Gus Prevas | e2d6f04 | 2018-10-17 15:25:30 -0400 | [diff] [blame] | 93 | private boolean updateViewWithoutCallback(ActivatableNotificationView view, |
| 94 | boolean animate) { |
| 95 | float topRoundness = getRoundness(view, true /* top */); |
| 96 | float bottomRoundness = getRoundness(view, false /* top */); |
| 97 | boolean topChanged = view.setTopRoundness(topRoundness, animate); |
| 98 | boolean bottomChanged = view.setBottomRoundness(bottomRoundness, animate); |
| 99 | boolean firstInSection = isFirstInSection(view, false /* exclude first section */); |
| 100 | boolean lastInSection = isLastInSection(view, false /* exclude last section */); |
| 101 | view.setFirstInSection(firstInSection); |
| 102 | view.setLastInSection(lastInSection); |
| 103 | return (firstInSection || lastInSection) && (topChanged || bottomChanged); |
| 104 | } |
| 105 | |
| 106 | private boolean isFirstInSection(ActivatableNotificationView view, |
| 107 | boolean includeFirstSection) { |
| 108 | int numNonEmptySections = 0; |
| 109 | for (int i = 0; i < mFirstInSectionViews.length; i++) { |
| 110 | if (view == mFirstInSectionViews[i]) { |
| 111 | return includeFirstSection || numNonEmptySections > 0; |
| 112 | } |
| 113 | if (mFirstInSectionViews[i] != null) { |
| 114 | numNonEmptySections++; |
| 115 | } |
| 116 | } |
| 117 | return false; |
| 118 | } |
| 119 | |
| 120 | private boolean isLastInSection(ActivatableNotificationView view, boolean includeLastSection) { |
| 121 | int numNonEmptySections = 0; |
| 122 | for (int i = mLastInSectionViews.length - 1; i >= 0; i--) { |
| 123 | if (view == mLastInSectionViews[i]) { |
| 124 | return includeLastSection || numNonEmptySections > 0; |
| 125 | } |
| 126 | if (mLastInSectionViews[i] != null) { |
| 127 | numNonEmptySections++; |
| 128 | } |
| 129 | } |
| 130 | return false; |
| 131 | } |
| 132 | |
Selim Cinek | 29aab96 | 2018-02-27 17:05:45 -0800 | [diff] [blame] | 133 | private float getRoundness(ActivatableNotificationView view, boolean top) { |
| 134 | if ((view.isPinned() || view.isHeadsUpAnimatingAway()) && !mExpanded) { |
| 135 | return 1.0f; |
| 136 | } |
Gus Prevas | e2d6f04 | 2018-10-17 15:25:30 -0400 | [diff] [blame] | 137 | if (isFirstInSection(view, true /* include first section */) && top) { |
Selim Cinek | 29aab96 | 2018-02-27 17:05:45 -0800 | [diff] [blame] | 138 | return 1.0f; |
| 139 | } |
Gus Prevas | e2d6f04 | 2018-10-17 15:25:30 -0400 | [diff] [blame] | 140 | if (isLastInSection(view, true /* include last section */) && !top) { |
Selim Cinek | 29aab96 | 2018-02-27 17:05:45 -0800 | [diff] [blame] | 141 | return 1.0f; |
| 142 | } |
Selim Cinek | aa9db1f | 2018-02-27 17:35:47 -0800 | [diff] [blame] | 143 | if (view == mTrackedHeadsUp && mAppearFraction <= 0.0f) { |
| 144 | // If we're pushing up on a headsup the appear fraction is < 0 and it needs to still be |
| 145 | // rounded. |
| 146 | return 1.0f; |
| 147 | } |
Lucas Dupin | 00be88f | 2019-01-03 17:50:52 -0800 | [diff] [blame] | 148 | if (view == mTrackedAmbient) { |
| 149 | return 1.0f; |
| 150 | } |
Selim Cinek | 29aab96 | 2018-02-27 17:05:45 -0800 | [diff] [blame] | 151 | return 0.0f; |
| 152 | } |
| 153 | |
Selim Cinek | aa9db1f | 2018-02-27 17:35:47 -0800 | [diff] [blame] | 154 | public void setExpanded(float expandedHeight, float appearFraction) { |
| 155 | mExpanded = expandedHeight != 0.0f; |
| 156 | mAppearFraction = appearFraction; |
| 157 | if (mTrackedHeadsUp != null) { |
Gus Prevas | e2d6f04 | 2018-10-17 15:25:30 -0400 | [diff] [blame] | 158 | updateView(mTrackedHeadsUp, true); |
Selim Cinek | aa9db1f | 2018-02-27 17:35:47 -0800 | [diff] [blame] | 159 | } |
Selim Cinek | 29aab96 | 2018-02-27 17:05:45 -0800 | [diff] [blame] | 160 | } |
| 161 | |
Gus Prevas | e2d6f04 | 2018-10-17 15:25:30 -0400 | [diff] [blame] | 162 | public void updateRoundedChildren(NotificationSection[] sections) { |
| 163 | boolean anyChanged = false; |
| 164 | for (int i = 0; i < NUM_SECTIONS; i++) { |
| 165 | mTmpFirstInSectionViews[i] = mFirstInSectionViews[i]; |
| 166 | mTmpLastInSectionViews[i] = mLastInSectionViews[i]; |
| 167 | mFirstInSectionViews[i] = sections[i].getFirstVisibleChild(); |
| 168 | mLastInSectionViews[i] = sections[i].getLastVisibleChild(); |
Selim Cinek | 29aab96 | 2018-02-27 17:05:45 -0800 | [diff] [blame] | 169 | } |
Gus Prevas | e2d6f04 | 2018-10-17 15:25:30 -0400 | [diff] [blame] | 170 | anyChanged |= handleRemovedOldViews(sections, mTmpFirstInSectionViews, true); |
| 171 | anyChanged |= handleRemovedOldViews(sections, mTmpLastInSectionViews, false); |
| 172 | anyChanged |= handleAddedNewViews(sections, mTmpFirstInSectionViews, true); |
| 173 | anyChanged |= handleAddedNewViews(sections, mTmpLastInSectionViews, false); |
| 174 | if (anyChanged) { |
| 175 | mRoundingChangedCallback.run(); |
Selim Cinek | 29aab96 | 2018-02-27 17:05:45 -0800 | [diff] [blame] | 176 | } |
Gus Prevas | e2d6f04 | 2018-10-17 15:25:30 -0400 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | private boolean handleRemovedOldViews(NotificationSection[] sections, |
| 180 | ActivatableNotificationView[] oldViews, boolean first) { |
| 181 | boolean anyChanged = false; |
| 182 | for (ActivatableNotificationView oldView : oldViews) { |
| 183 | if (oldView != null) { |
| 184 | boolean isStillPresent = false; |
| 185 | boolean adjacentSectionChanged = false; |
| 186 | for (NotificationSection section : sections) { |
| 187 | ActivatableNotificationView newView = |
| 188 | (first ? section.getFirstVisibleChild() |
| 189 | : section.getLastVisibleChild()); |
| 190 | if (newView == oldView) { |
| 191 | isStillPresent = true; |
| 192 | if (oldView.isFirstInSection() != isFirstInSection(oldView, |
| 193 | false /* exclude first section */) |
| 194 | || oldView.isLastInSection() != isLastInSection(oldView, |
| 195 | false /* exclude last section */)) { |
| 196 | adjacentSectionChanged = true; |
| 197 | } |
| 198 | break; |
| 199 | } |
| 200 | } |
| 201 | if (!isStillPresent || adjacentSectionChanged) { |
| 202 | anyChanged = true; |
| 203 | if (!oldView.isRemoved()) { |
| 204 | updateViewWithoutCallback(oldView, oldView.isShown()); |
| 205 | } |
| 206 | } |
| 207 | } |
Selim Cinek | 29aab96 | 2018-02-27 17:05:45 -0800 | [diff] [blame] | 208 | } |
Gus Prevas | e2d6f04 | 2018-10-17 15:25:30 -0400 | [diff] [blame] | 209 | return anyChanged; |
| 210 | } |
| 211 | |
| 212 | private boolean handleAddedNewViews(NotificationSection[] sections, |
| 213 | ActivatableNotificationView[] oldViews, boolean first) { |
| 214 | boolean anyChanged = false; |
| 215 | for (NotificationSection section : sections) { |
| 216 | ActivatableNotificationView newView = |
| 217 | (first ? section.getFirstVisibleChild() : section.getLastVisibleChild()); |
| 218 | if (newView != null) { |
| 219 | boolean wasAlreadyPresent = false; |
| 220 | for (ActivatableNotificationView oldView : oldViews) { |
| 221 | if (oldView == newView) { |
| 222 | wasAlreadyPresent = true; |
| 223 | break; |
| 224 | } |
| 225 | } |
| 226 | if (!wasAlreadyPresent) { |
| 227 | anyChanged = true; |
| 228 | updateViewWithoutCallback(newView, |
| 229 | newView.isShown() && !mAnimatedChildren.contains(newView)); |
| 230 | } |
| 231 | } |
Selim Cinek | 29aab96 | 2018-02-27 17:05:45 -0800 | [diff] [blame] | 232 | } |
Gus Prevas | e2d6f04 | 2018-10-17 15:25:30 -0400 | [diff] [blame] | 233 | return anyChanged; |
Selim Cinek | 29aab96 | 2018-02-27 17:05:45 -0800 | [diff] [blame] | 234 | } |
| 235 | |
Dave Mankoff | a4d195d | 2018-11-16 13:33:27 -0500 | [diff] [blame] | 236 | public void setAnimatedChildren(HashSet<ExpandableView> animatedChildren) { |
Selim Cinek | 29aab96 | 2018-02-27 17:05:45 -0800 | [diff] [blame] | 237 | mAnimatedChildren = animatedChildren; |
| 238 | } |
| 239 | |
| 240 | public void setOnRoundingChangedCallback(Runnable roundingChangedCallback) { |
| 241 | mRoundingChangedCallback = roundingChangedCallback; |
| 242 | } |
Selim Cinek | aa9db1f | 2018-02-27 17:35:47 -0800 | [diff] [blame] | 243 | |
| 244 | public void setTrackingHeadsUp(ExpandableNotificationRow row) { |
| 245 | mTrackedHeadsUp = row; |
| 246 | } |
Selim Cinek | 29aab96 | 2018-02-27 17:05:45 -0800 | [diff] [blame] | 247 | } |