blob: 422184699952734d79908688b860d024b6a8d3a4 [file] [log] [blame]
Selim Cinek29aab962018-02-27 17:05:45 -08001/*
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 Shah20790b82018-07-02 17:21:04 -070017package com.android.systemui.statusbar.notification.stack;
Selim Cinek29aab962018-02-27 17:05:45 -080018
Dave Mankoffa4d195d2018-11-16 13:33:27 -050019import static com.android.systemui.statusbar.notification.stack.NotificationStackScrollLayout.NUM_SECTIONS;
Selim Cinek29aab962018-02-27 17:05:45 -080020
Selim Cinekc7e4cb52019-06-20 15:41:45 -070021
22import android.util.MathUtils;
23
Ned Burnsf81c4c42019-01-07 14:10:43 -050024import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Rohan Shah20790b82018-07-02 17:21:04 -070025import com.android.systemui.statusbar.notification.row.ActivatableNotificationView;
26import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow;
Dave Mankoffa4d195d2018-11-16 13:33:27 -050027import com.android.systemui.statusbar.notification.row.ExpandableView;
Selim Cinekc7e4cb52019-06-20 15:41:45 -070028import com.android.systemui.statusbar.phone.KeyguardBypassController;
Selim Cinek29aab962018-02-27 17:05:45 -080029import com.android.systemui.statusbar.policy.OnHeadsUpChangedListener;
30
31import java.util.HashSet;
32
Lucas Dupin00be88f2019-01-03 17:50:52 -080033import javax.inject.Inject;
34import javax.inject.Singleton;
35
Selim Cinek29aab962018-02-27 17:05:45 -080036/**
37 * A class that manages the roundness for notification views
38 */
Lucas Dupin00be88f2019-01-03 17:50:52 -080039@Singleton
Selim Cinekc7e4cb52019-06-20 15:41:45 -070040public class NotificationRoundnessManager implements OnHeadsUpChangedListener {
Selim Cinek29aab962018-02-27 17:05:45 -080041
Lucas Dupin00be88f2019-01-03 17:50:52 -080042 private final ActivatableNotificationView[] mFirstInSectionViews;
43 private final ActivatableNotificationView[] mLastInSectionViews;
44 private final ActivatableNotificationView[] mTmpFirstInSectionViews;
45 private final ActivatableNotificationView[] mTmpLastInSectionViews;
Selim Cinekc7e4cb52019-06-20 15:41:45 -070046 private final KeyguardBypassController mBypassController;
Selim Cinek29aab962018-02-27 17:05:45 -080047 private boolean mExpanded;
Dave Mankoffa4d195d2018-11-16 13:33:27 -050048 private HashSet<ExpandableView> mAnimatedChildren;
Selim Cinek29aab962018-02-27 17:05:45 -080049 private Runnable mRoundingChangedCallback;
Selim Cinekaa9db1f2018-02-27 17:35:47 -080050 private ExpandableNotificationRow mTrackedHeadsUp;
51 private float mAppearFraction;
Selim Cinek29aab962018-02-27 17:05:45 -080052
Lucas Dupin00be88f2019-01-03 17:50:52 -080053 @Inject
Selim Cinekc7e4cb52019-06-20 15:41:45 -070054 NotificationRoundnessManager(KeyguardBypassController keyguardBypassController) {
Gus Prevase2d6f042018-10-17 15:25:30 -040055 mFirstInSectionViews = new ActivatableNotificationView[NUM_SECTIONS];
56 mLastInSectionViews = new ActivatableNotificationView[NUM_SECTIONS];
57 mTmpFirstInSectionViews = new ActivatableNotificationView[NUM_SECTIONS];
58 mTmpLastInSectionViews = new ActivatableNotificationView[NUM_SECTIONS];
Selim Cinekc7e4cb52019-06-20 15:41:45 -070059 mBypassController = keyguardBypassController;
Gus Prevase2d6f042018-10-17 15:25:30 -040060 }
61
Selim Cinek29aab962018-02-27 17:05:45 -080062 @Override
Ned Burnsf81c4c42019-01-07 14:10:43 -050063 public void onHeadsUpPinned(NotificationEntry headsUp) {
Evan Laird94492852018-10-25 13:43:01 -040064 updateView(headsUp.getRow(), false /* animate */);
Selim Cinek29aab962018-02-27 17:05:45 -080065 }
66
67 @Override
Ned Burnsf81c4c42019-01-07 14:10:43 -050068 public void onHeadsUpUnPinned(NotificationEntry headsUp) {
Evan Laird94492852018-10-25 13:43:01 -040069 updateView(headsUp.getRow(), true /* animate */);
Selim Cinek29aab962018-02-27 17:05:45 -080070 }
71
Selim Cinek8875de12018-03-22 10:14:32 -070072 public void onHeadsupAnimatingAwayChanged(ExpandableNotificationRow row,
73 boolean isAnimatingAway) {
Gus Prevase2d6f042018-10-17 15:25:30 -040074 updateView(row, false /* animate */);
Selim Cinek8875de12018-03-22 10:14:32 -070075 }
76
Lucas Dupin00be88f2019-01-03 17:50:52 -080077 @Override
Selim Cinekc3fec682019-06-06 18:11:07 -070078 public void onHeadsUpStateChanged(NotificationEntry entry, boolean isHeadsUp) {
79 updateView(entry.getRow(), false /* animate */);
Lucas Dupin00be88f2019-01-03 17:50:52 -080080 }
81
Gus Prevase2d6f042018-10-17 15:25:30 -040082 private void updateView(ActivatableNotificationView view, boolean animate) {
83 boolean changed = updateViewWithoutCallback(view, animate);
84 if (changed) {
Selim Cinek29aab962018-02-27 17:05:45 -080085 mRoundingChangedCallback.run();
86 }
87 }
88
Gus Prevase2d6f042018-10-17 15:25:30 -040089 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 Cinek29aab962018-02-27 17:05:45 -0800129 private float getRoundness(ActivatableNotificationView view, boolean top) {
130 if ((view.isPinned() || view.isHeadsUpAnimatingAway()) && !mExpanded) {
131 return 1.0f;
132 }
Gus Prevase2d6f042018-10-17 15:25:30 -0400133 if (isFirstInSection(view, true /* include first section */) && top) {
Selim Cinek29aab962018-02-27 17:05:45 -0800134 return 1.0f;
135 }
Gus Prevase2d6f042018-10-17 15:25:30 -0400136 if (isLastInSection(view, true /* include last section */) && !top) {
Selim Cinek29aab962018-02-27 17:05:45 -0800137 return 1.0f;
138 }
Selim Cinekc7e4cb52019-06-20 15:41:45 -0700139 if (view == mTrackedHeadsUp) {
Selim Cinekaa9db1f2018-02-27 17:35:47 -0800140 // If we're pushing up on a headsup the appear fraction is < 0 and it needs to still be
141 // rounded.
Selim Cinekc7e4cb52019-06-20 15:41:45 -0700142 return MathUtils.saturate(1.0f - mAppearFraction);
143 }
144 if (view.showingPulsing() && !mBypassController.getBypassEnabled()) {
Selim Cinekaa9db1f2018-02-27 17:35:47 -0800145 return 1.0f;
146 }
Selim Cinek29aab962018-02-27 17:05:45 -0800147 return 0.0f;
148 }
149
Selim Cinekaa9db1f2018-02-27 17:35:47 -0800150 public void setExpanded(float expandedHeight, float appearFraction) {
151 mExpanded = expandedHeight != 0.0f;
152 mAppearFraction = appearFraction;
153 if (mTrackedHeadsUp != null) {
Gus Prevase2d6f042018-10-17 15:25:30 -0400154 updateView(mTrackedHeadsUp, true);
Selim Cinekaa9db1f2018-02-27 17:35:47 -0800155 }
Selim Cinek29aab962018-02-27 17:05:45 -0800156 }
157
Gus Prevase2d6f042018-10-17 15:25:30 -0400158 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 Cinek29aab962018-02-27 17:05:45 -0800165 }
Gus Prevase2d6f042018-10-17 15:25:30 -0400166 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 Cinek29aab962018-02-27 17:05:45 -0800172 }
Gus Prevase2d6f042018-10-17 15:25:30 -0400173 }
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 Cinek29aab962018-02-27 17:05:45 -0800204 }
Gus Prevase2d6f042018-10-17 15:25:30 -0400205 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 Cinek29aab962018-02-27 17:05:45 -0800228 }
Gus Prevase2d6f042018-10-17 15:25:30 -0400229 return anyChanged;
Selim Cinek29aab962018-02-27 17:05:45 -0800230 }
231
Dave Mankoffa4d195d2018-11-16 13:33:27 -0500232 public void setAnimatedChildren(HashSet<ExpandableView> animatedChildren) {
Selim Cinek29aab962018-02-27 17:05:45 -0800233 mAnimatedChildren = animatedChildren;
234 }
235
236 public void setOnRoundingChangedCallback(Runnable roundingChangedCallback) {
237 mRoundingChangedCallback = roundingChangedCallback;
238 }
Selim Cinekaa9db1f2018-02-27 17:35:47 -0800239
240 public void setTrackingHeadsUp(ExpandableNotificationRow row) {
Selim Cinekc7e4cb52019-06-20 15:41:45 -0700241 ExpandableNotificationRow previous = mTrackedHeadsUp;
Selim Cinekaa9db1f2018-02-27 17:35:47 -0800242 mTrackedHeadsUp = row;
Selim Cinekc7e4cb52019-06-20 15:41:45 -0700243 if (previous != null) {
244 updateView(previous, true /* animate */);
245 }
Selim Cinekaa9db1f2018-02-27 17:35:47 -0800246 }
Selim Cinek29aab962018-02-27 17:05:45 -0800247}