blob: 8341c02b6b63e1164e5402afa0ac2cd72954e022 [file] [log] [blame]
Selim Cineka7d4f822016-12-06 14:34:47 -08001/*
2 * Copyright (C) 2016 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
17package com.android.systemui.statusbar.notification;
18
Ned Burns9512e0c2019-05-30 19:36:04 -040019import android.os.Handler;
20import android.os.SystemClock;
Selim Cineka7d4f822016-12-06 14:34:47 -080021import android.view.View;
22
Gus Prevasab336792018-11-14 13:52:20 -050023import androidx.collection.ArraySet;
24
Ned Burns9512e0c2019-05-30 19:36:04 -040025import com.android.systemui.Dumpable;
Dave Mankoff00e8a2f2019-12-18 16:59:49 -050026import com.android.systemui.dagger.qualifiers.Main;
Gus Prevas5b9098dc2018-12-21 17:07:15 -050027import com.android.systemui.statusbar.NotificationPresenter;
Ned Burnsf81c4c42019-01-07 14:10:43 -050028import com.android.systemui.statusbar.notification.collection.NotificationEntry;
Sergey Nikolaienkovf6ad6322020-02-10 15:46:32 +010029import com.android.systemui.statusbar.notification.dagger.NotificationsModule;
Rohan Shah20790b82018-07-02 17:21:04 -070030import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow;
Selim Cineka7d4f822016-12-06 14:34:47 -080031import com.android.systemui.statusbar.policy.OnHeadsUpChangedListener;
32
Ned Burns9512e0c2019-05-30 19:36:04 -040033import java.io.FileDescriptor;
34import java.io.PrintWriter;
Selim Cineka7d4f822016-12-06 14:34:47 -080035import java.util.ArrayList;
36
37/**
38 * A manager that ensures that notifications are visually stable. It will suppress reorderings
39 * and reorder at the right time when they are out of view.
40 */
Ned Burns9512e0c2019-05-30 19:36:04 -040041public class VisualStabilityManager implements OnHeadsUpChangedListener, Dumpable {
42
43 private static final long TEMPORARY_REORDERING_ALLOWED_DURATION = 1000;
Selim Cineka7d4f822016-12-06 14:34:47 -080044
Selim Cinekba069ae2020-04-01 19:45:16 -070045 private final ArrayList<Callback> mReorderingAllowedCallbacks = new ArrayList<>();
Selim Cinek0260c752020-05-11 16:03:52 -070046 private final ArraySet<Callback> mPersistentReorderingCallbacks = new ArraySet<>();
Selim Cinekba069ae2020-04-01 19:45:16 -070047 private final ArrayList<Callback> mGroupChangesAllowedCallbacks = new ArrayList<>();
Selim Cinek0260c752020-05-11 16:03:52 -070048 private final ArraySet<Callback> mPersistentGroupCallbacks = new ArraySet<>();
Ned Burns9512e0c2019-05-30 19:36:04 -040049 private final Handler mHandler;
Selim Cineka7d4f822016-12-06 14:34:47 -080050
51 private boolean mPanelExpanded;
52 private boolean mScreenOn;
53 private boolean mReorderingAllowed;
Selim Cinekba069ae2020-04-01 19:45:16 -070054 private boolean mGroupChangedAllowed;
Ned Burns9512e0c2019-05-30 19:36:04 -040055 private boolean mIsTemporaryReorderingAllowed;
56 private long mTemporaryReorderingStart;
Selim Cineka7d4f822016-12-06 14:34:47 -080057 private VisibilityLocationProvider mVisibilityLocationProvider;
58 private ArraySet<View> mAllowedReorderViews = new ArraySet<>();
Ned Burnsa13b6f12019-02-11 20:42:58 -050059 private ArraySet<NotificationEntry> mLowPriorityReorderingViews = new ArraySet<>();
Selim Cineka7d4f822016-12-06 14:34:47 -080060 private ArraySet<View> mAddedChildren = new ArraySet<>();
Adrian Roos4b820e42017-01-13 17:40:43 -080061 private boolean mPulsing;
Selim Cineka7d4f822016-12-06 14:34:47 -080062
Sergey Nikolaienkovf6ad6322020-02-10 15:46:32 +010063 /**
64 * Injected constructor. See {@link NotificationsModule}.
65 */
Ned Burns9512e0c2019-05-30 19:36:04 -040066 public VisualStabilityManager(
Beverly95a0802ac2020-02-10 15:27:40 -050067 NotificationEntryManager notificationEntryManager,
68 @Main Handler handler) {
Ned Burns9512e0c2019-05-30 19:36:04 -040069
70 mHandler = handler;
71
Gus Prevas5b9098dc2018-12-21 17:07:15 -050072 notificationEntryManager.addNotificationEntryListener(new NotificationEntryListener() {
73 @Override
Ned Burnsa13b6f12019-02-11 20:42:58 -050074 public void onPreEntryUpdated(NotificationEntry entry) {
Beverlye018f0d2019-11-06 14:20:29 -050075 final boolean ambientStateHasChanged =
Ned Burns60e94592019-09-06 14:47:25 -040076 entry.isAmbient() != entry.getRow().isLowPriority();
Beverlye018f0d2019-11-06 14:20:29 -050077 if (ambientStateHasChanged) {
78 // note: entries are removed in onReorderingFinished
Ned Burnsa13b6f12019-02-11 20:42:58 -050079 mLowPriorityReorderingViews.add(entry);
Gus Prevas5b9098dc2018-12-21 17:07:15 -050080 }
81 }
82 });
83 }
84
85 public void setUpWithPresenter(NotificationPresenter presenter) {
Jason Monk27d01a622018-12-10 15:57:09 -050086 }
87
Selim Cineka7d4f822016-12-06 14:34:47 -080088 /**
89 * Add a callback to invoke when reordering is allowed again.
Selim Cinek0260c752020-05-11 16:03:52 -070090 *
91 * @param callback the callback to add
92 * @param persistent {@code true} if this callback should this callback be persisted, otherwise
93 * it will be removed after a single invocation
Selim Cineka7d4f822016-12-06 14:34:47 -080094 */
Selim Cinek0260c752020-05-11 16:03:52 -070095 public void addReorderingAllowedCallback(Callback callback, boolean persistent) {
96 if (persistent) {
97 mPersistentReorderingCallbacks.add(callback);
98 }
Selim Cinekba069ae2020-04-01 19:45:16 -070099 if (mReorderingAllowedCallbacks.contains(callback)) {
Selim Cineka7d4f822016-12-06 14:34:47 -0800100 return;
101 }
Selim Cinekba069ae2020-04-01 19:45:16 -0700102 mReorderingAllowedCallbacks.add(callback);
103 }
104
105 /**
106 * Add a callback to invoke when group changes are allowed again.
Selim Cinek0260c752020-05-11 16:03:52 -0700107 *
108 * @param callback the callback to add
109 * @param persistent {@code true} if this callback should this callback be persisted, otherwise
110 * it will be removed after a single invocation
Selim Cinekba069ae2020-04-01 19:45:16 -0700111 */
Selim Cinek0260c752020-05-11 16:03:52 -0700112 public void addGroupChangesAllowedCallback(Callback callback, boolean persistent) {
113 if (persistent) {
114 mPersistentGroupCallbacks.add(callback);
115 }
Selim Cinekba069ae2020-04-01 19:45:16 -0700116 if (mGroupChangesAllowedCallbacks.contains(callback)) {
117 return;
118 }
119 mGroupChangesAllowedCallbacks.add(callback);
Selim Cineka7d4f822016-12-06 14:34:47 -0800120 }
121
122 /**
123 * Set the panel to be expanded.
124 */
125 public void setPanelExpanded(boolean expanded) {
126 mPanelExpanded = expanded;
Selim Cinekba069ae2020-04-01 19:45:16 -0700127 updateAllowedStates();
Selim Cineka7d4f822016-12-06 14:34:47 -0800128 }
129
130 /**
131 * @param screenOn whether the screen is on
132 */
133 public void setScreenOn(boolean screenOn) {
134 mScreenOn = screenOn;
Selim Cinekba069ae2020-04-01 19:45:16 -0700135 updateAllowedStates();
Selim Cineka7d4f822016-12-06 14:34:47 -0800136 }
137
Adrian Roos4b820e42017-01-13 17:40:43 -0800138 /**
139 * @param pulsing whether we are currently pulsing for ambient display.
140 */
141 public void setPulsing(boolean pulsing) {
Adrian Roosb2a87292017-02-13 15:05:03 +0100142 if (mPulsing == pulsing) {
143 return;
144 }
Adrian Roos4b820e42017-01-13 17:40:43 -0800145 mPulsing = pulsing;
Selim Cinekba069ae2020-04-01 19:45:16 -0700146 updateAllowedStates();
Adrian Roos4b820e42017-01-13 17:40:43 -0800147 }
148
Selim Cinekba069ae2020-04-01 19:45:16 -0700149 private void updateAllowedStates() {
Ned Burns9512e0c2019-05-30 19:36:04 -0400150 boolean reorderingAllowed =
151 (!mScreenOn || !mPanelExpanded || mIsTemporaryReorderingAllowed) && !mPulsing;
152 boolean changedToTrue = reorderingAllowed && !mReorderingAllowed;
Selim Cineka7d4f822016-12-06 14:34:47 -0800153 mReorderingAllowed = reorderingAllowed;
Ned Burns9512e0c2019-05-30 19:36:04 -0400154 if (changedToTrue) {
Selim Cinek0260c752020-05-11 16:03:52 -0700155 notifyChangeAllowed(mReorderingAllowedCallbacks, mPersistentReorderingCallbacks);
Selim Cinekba069ae2020-04-01 19:45:16 -0700156 }
157 boolean groupChangesAllowed = (!mScreenOn || !mPanelExpanded) && !mPulsing;
158 changedToTrue = groupChangesAllowed && !mGroupChangedAllowed;
159 mGroupChangedAllowed = groupChangesAllowed;
160 if (changedToTrue) {
Selim Cinek0260c752020-05-11 16:03:52 -0700161 notifyChangeAllowed(mGroupChangesAllowedCallbacks, mPersistentGroupCallbacks);
Selim Cineka7d4f822016-12-06 14:34:47 -0800162 }
163 }
164
Selim Cinek0260c752020-05-11 16:03:52 -0700165 private void notifyChangeAllowed(ArrayList<Callback> callbacks,
166 ArraySet<Callback> persistentCallbacks) {
Selim Cinekba069ae2020-04-01 19:45:16 -0700167 for (int i = 0; i < callbacks.size(); i++) {
Selim Cinek0260c752020-05-11 16:03:52 -0700168 Callback callback = callbacks.get(i);
169 callback.onChangeAllowed();
170 if (!persistentCallbacks.contains(callback)) {
171 callbacks.remove(callback);
172 i--;
173 }
Selim Cineka7d4f822016-12-06 14:34:47 -0800174 }
Selim Cineka7d4f822016-12-06 14:34:47 -0800175 }
176
177 /**
178 * @return whether reordering is currently allowed in general.
179 */
180 public boolean isReorderingAllowed() {
181 return mReorderingAllowed;
182 }
183
184 /**
Selim Cinekba069ae2020-04-01 19:45:16 -0700185 * @return whether changes in the grouping should be allowed right now.
186 */
187 public boolean areGroupChangesAllowed() {
188 return mGroupChangedAllowed;
189 }
190
191 /**
Selim Cineka7d4f822016-12-06 14:34:47 -0800192 * @return whether a specific notification is allowed to reorder. Certain notifications are
193 * allowed to reorder even if {@link #isReorderingAllowed()} returns false, like newly added
194 * notifications or heads-up notifications that are out of view.
195 */
196 public boolean canReorderNotification(ExpandableNotificationRow row) {
197 if (mReorderingAllowed) {
198 return true;
199 }
200 if (mAddedChildren.contains(row)) {
201 return true;
202 }
Ned Burnsa13b6f12019-02-11 20:42:58 -0500203 if (mLowPriorityReorderingViews.contains(row.getEntry())) {
Selim Cinek55a3e732017-05-25 18:30:10 -0700204 return true;
205 }
Selim Cineka7d4f822016-12-06 14:34:47 -0800206 if (mAllowedReorderViews.contains(row)
Evan Laird94492852018-10-25 13:43:01 -0400207 && !mVisibilityLocationProvider.isInVisibleLocation(row.getEntry())) {
Selim Cineka7d4f822016-12-06 14:34:47 -0800208 return true;
209 }
210 return false;
211 }
212
213 public void setVisibilityLocationProvider(
214 VisibilityLocationProvider visibilityLocationProvider) {
215 mVisibilityLocationProvider = visibilityLocationProvider;
216 }
217
218 public void onReorderingFinished() {
219 mAllowedReorderViews.clear();
220 mAddedChildren.clear();
Selim Cinek55a3e732017-05-25 18:30:10 -0700221 mLowPriorityReorderingViews.clear();
Selim Cineka7d4f822016-12-06 14:34:47 -0800222 }
223
224 @Override
Ned Burnsf81c4c42019-01-07 14:10:43 -0500225 public void onHeadsUpStateChanged(NotificationEntry entry, boolean isHeadsUp) {
Selim Cineka7d4f822016-12-06 14:34:47 -0800226 if (isHeadsUp) {
227 // Heads up notifications should in general be allowed to reorder if they are out of
228 // view and stay at the current location if they aren't.
Evan Laird94492852018-10-25 13:43:01 -0400229 mAllowedReorderViews.add(entry.getRow());
Selim Cineka7d4f822016-12-06 14:34:47 -0800230 }
231 }
232
Selim Cineka7d4f822016-12-06 14:34:47 -0800233 /**
Ned Burns9512e0c2019-05-30 19:36:04 -0400234 * Temporarily allows reordering of the entire shade for a period of 1000ms. Subsequent calls
235 * to this method will extend the timer.
236 */
237 public void temporarilyAllowReordering() {
238 mHandler.removeCallbacks(mOnTemporaryReorderingExpired);
239 mHandler.postDelayed(mOnTemporaryReorderingExpired, TEMPORARY_REORDERING_ALLOWED_DURATION);
240 if (!mIsTemporaryReorderingAllowed) {
241 mTemporaryReorderingStart = SystemClock.elapsedRealtime();
242 }
243 mIsTemporaryReorderingAllowed = true;
Selim Cinekba069ae2020-04-01 19:45:16 -0700244 updateAllowedStates();
Ned Burns9512e0c2019-05-30 19:36:04 -0400245 }
246
247 private final Runnable mOnTemporaryReorderingExpired = () -> {
248 mIsTemporaryReorderingAllowed = false;
Selim Cinekba069ae2020-04-01 19:45:16 -0700249 updateAllowedStates();
Ned Burns9512e0c2019-05-30 19:36:04 -0400250 };
251
252 /**
Selim Cineka7d4f822016-12-06 14:34:47 -0800253 * Notify the visual stability manager that a new view was added and should be allowed to
254 * reorder next time.
255 */
256 public void notifyViewAddition(View view) {
257 mAddedChildren.add(view);
258 }
259
Ned Burns9512e0c2019-05-30 19:36:04 -0400260 @Override
261 public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
262 pw.println("VisualStabilityManager state:");
263 pw.print(" mIsTemporaryReorderingAllowed="); pw.println(mIsTemporaryReorderingAllowed);
264 pw.print(" mTemporaryReorderingStart="); pw.println(mTemporaryReorderingStart);
265
266 long now = SystemClock.elapsedRealtime();
267 pw.print(" Temporary reordering window has been open for ");
268 pw.print(now - (mIsTemporaryReorderingAllowed ? mTemporaryReorderingStart : now));
269 pw.println("ms");
270
271 pw.println();
272 }
273
Selim Cineka7d4f822016-12-06 14:34:47 -0800274 public interface Callback {
275 /**
Selim Cinekba069ae2020-04-01 19:45:16 -0700276 * Called when changing is allowed again.
Selim Cineka7d4f822016-12-06 14:34:47 -0800277 */
Selim Cinekba069ae2020-04-01 19:45:16 -0700278 void onChangeAllowed();
Selim Cineka7d4f822016-12-06 14:34:47 -0800279 }
280
281}