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