Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.systemui.statusbar.notification; |
| 18 | |
Ned Burns | 9512e0c | 2019-05-30 19:36:04 -0400 | [diff] [blame] | 19 | import android.os.Handler; |
| 20 | import android.os.SystemClock; |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 21 | import android.view.View; |
| 22 | |
Gus Prevas | ab33679 | 2018-11-14 13:52:20 -0500 | [diff] [blame] | 23 | import androidx.collection.ArraySet; |
| 24 | |
Ned Burns | 9512e0c | 2019-05-30 19:36:04 -0400 | [diff] [blame] | 25 | import com.android.systemui.Dumpable; |
Dave Mankoff | 00e8a2f | 2019-12-18 16:59:49 -0500 | [diff] [blame] | 26 | import com.android.systemui.dagger.qualifiers.Main; |
Gus Prevas | 5b9098dc | 2018-12-21 17:07:15 -0500 | [diff] [blame] | 27 | import com.android.systemui.statusbar.NotificationPresenter; |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 28 | import com.android.systemui.statusbar.notification.collection.NotificationEntry; |
Sergey Nikolaienkov | f6ad632 | 2020-02-10 15:46:32 +0100 | [diff] [blame] | 29 | import com.android.systemui.statusbar.notification.dagger.NotificationsModule; |
Rohan Shah | 20790b8 | 2018-07-02 17:21:04 -0700 | [diff] [blame] | 30 | import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow; |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 31 | import com.android.systemui.statusbar.policy.OnHeadsUpChangedListener; |
| 32 | |
Ned Burns | 9512e0c | 2019-05-30 19:36:04 -0400 | [diff] [blame] | 33 | import java.io.FileDescriptor; |
| 34 | import java.io.PrintWriter; |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 35 | import 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 Burns | 9512e0c | 2019-05-30 19:36:04 -0400 | [diff] [blame] | 41 | public class VisualStabilityManager implements OnHeadsUpChangedListener, Dumpable { |
| 42 | |
| 43 | private static final long TEMPORARY_REORDERING_ALLOWED_DURATION = 1000; |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 44 | |
Selim Cinek | ba069ae | 2020-04-01 19:45:16 -0700 | [diff] [blame] | 45 | private final ArrayList<Callback> mReorderingAllowedCallbacks = new ArrayList<>(); |
Selim Cinek | 0260c75 | 2020-05-11 16:03:52 -0700 | [diff] [blame] | 46 | private final ArraySet<Callback> mPersistentReorderingCallbacks = new ArraySet<>(); |
Selim Cinek | ba069ae | 2020-04-01 19:45:16 -0700 | [diff] [blame] | 47 | private final ArrayList<Callback> mGroupChangesAllowedCallbacks = new ArrayList<>(); |
Selim Cinek | 0260c75 | 2020-05-11 16:03:52 -0700 | [diff] [blame] | 48 | private final ArraySet<Callback> mPersistentGroupCallbacks = new ArraySet<>(); |
Ned Burns | 9512e0c | 2019-05-30 19:36:04 -0400 | [diff] [blame] | 49 | private final Handler mHandler; |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 50 | |
| 51 | private boolean mPanelExpanded; |
| 52 | private boolean mScreenOn; |
| 53 | private boolean mReorderingAllowed; |
Selim Cinek | ba069ae | 2020-04-01 19:45:16 -0700 | [diff] [blame] | 54 | private boolean mGroupChangedAllowed; |
Ned Burns | 9512e0c | 2019-05-30 19:36:04 -0400 | [diff] [blame] | 55 | private boolean mIsTemporaryReorderingAllowed; |
| 56 | private long mTemporaryReorderingStart; |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 57 | private VisibilityLocationProvider mVisibilityLocationProvider; |
| 58 | private ArraySet<View> mAllowedReorderViews = new ArraySet<>(); |
Ned Burns | a13b6f1 | 2019-02-11 20:42:58 -0500 | [diff] [blame] | 59 | private ArraySet<NotificationEntry> mLowPriorityReorderingViews = new ArraySet<>(); |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 60 | private ArraySet<View> mAddedChildren = new ArraySet<>(); |
Adrian Roos | 4b820e4 | 2017-01-13 17:40:43 -0800 | [diff] [blame] | 61 | private boolean mPulsing; |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 62 | |
Sergey Nikolaienkov | f6ad632 | 2020-02-10 15:46:32 +0100 | [diff] [blame] | 63 | /** |
| 64 | * Injected constructor. See {@link NotificationsModule}. |
| 65 | */ |
Ned Burns | 9512e0c | 2019-05-30 19:36:04 -0400 | [diff] [blame] | 66 | public VisualStabilityManager( |
Beverly | 95a0802ac | 2020-02-10 15:27:40 -0500 | [diff] [blame] | 67 | NotificationEntryManager notificationEntryManager, |
| 68 | @Main Handler handler) { |
Ned Burns | 9512e0c | 2019-05-30 19:36:04 -0400 | [diff] [blame] | 69 | |
| 70 | mHandler = handler; |
| 71 | |
Gus Prevas | 5b9098dc | 2018-12-21 17:07:15 -0500 | [diff] [blame] | 72 | notificationEntryManager.addNotificationEntryListener(new NotificationEntryListener() { |
| 73 | @Override |
Ned Burns | a13b6f1 | 2019-02-11 20:42:58 -0500 | [diff] [blame] | 74 | public void onPreEntryUpdated(NotificationEntry entry) { |
Beverly | e018f0d | 2019-11-06 14:20:29 -0500 | [diff] [blame] | 75 | final boolean ambientStateHasChanged = |
Ned Burns | 60e9459 | 2019-09-06 14:47:25 -0400 | [diff] [blame] | 76 | entry.isAmbient() != entry.getRow().isLowPriority(); |
Beverly | e018f0d | 2019-11-06 14:20:29 -0500 | [diff] [blame] | 77 | if (ambientStateHasChanged) { |
| 78 | // note: entries are removed in onReorderingFinished |
Ned Burns | a13b6f1 | 2019-02-11 20:42:58 -0500 | [diff] [blame] | 79 | mLowPriorityReorderingViews.add(entry); |
Gus Prevas | 5b9098dc | 2018-12-21 17:07:15 -0500 | [diff] [blame] | 80 | } |
| 81 | } |
| 82 | }); |
| 83 | } |
| 84 | |
| 85 | public void setUpWithPresenter(NotificationPresenter presenter) { |
Jason Monk | 27d01a62 | 2018-12-10 15:57:09 -0500 | [diff] [blame] | 86 | } |
| 87 | |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 88 | /** |
| 89 | * Add a callback to invoke when reordering is allowed again. |
Selim Cinek | 0260c75 | 2020-05-11 16:03:52 -0700 | [diff] [blame] | 90 | * |
| 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 Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 94 | */ |
Selim Cinek | 0260c75 | 2020-05-11 16:03:52 -0700 | [diff] [blame] | 95 | public void addReorderingAllowedCallback(Callback callback, boolean persistent) { |
| 96 | if (persistent) { |
| 97 | mPersistentReorderingCallbacks.add(callback); |
| 98 | } |
Selim Cinek | ba069ae | 2020-04-01 19:45:16 -0700 | [diff] [blame] | 99 | if (mReorderingAllowedCallbacks.contains(callback)) { |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 100 | return; |
| 101 | } |
Selim Cinek | ba069ae | 2020-04-01 19:45:16 -0700 | [diff] [blame] | 102 | mReorderingAllowedCallbacks.add(callback); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Add a callback to invoke when group changes are allowed again. |
Selim Cinek | 0260c75 | 2020-05-11 16:03:52 -0700 | [diff] [blame] | 107 | * |
| 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 Cinek | ba069ae | 2020-04-01 19:45:16 -0700 | [diff] [blame] | 111 | */ |
Selim Cinek | 0260c75 | 2020-05-11 16:03:52 -0700 | [diff] [blame] | 112 | public void addGroupChangesAllowedCallback(Callback callback, boolean persistent) { |
| 113 | if (persistent) { |
| 114 | mPersistentGroupCallbacks.add(callback); |
| 115 | } |
Selim Cinek | ba069ae | 2020-04-01 19:45:16 -0700 | [diff] [blame] | 116 | if (mGroupChangesAllowedCallbacks.contains(callback)) { |
| 117 | return; |
| 118 | } |
| 119 | mGroupChangesAllowedCallbacks.add(callback); |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Set the panel to be expanded. |
| 124 | */ |
| 125 | public void setPanelExpanded(boolean expanded) { |
| 126 | mPanelExpanded = expanded; |
Selim Cinek | ba069ae | 2020-04-01 19:45:16 -0700 | [diff] [blame] | 127 | updateAllowedStates(); |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | /** |
| 131 | * @param screenOn whether the screen is on |
| 132 | */ |
| 133 | public void setScreenOn(boolean screenOn) { |
| 134 | mScreenOn = screenOn; |
Selim Cinek | ba069ae | 2020-04-01 19:45:16 -0700 | [diff] [blame] | 135 | updateAllowedStates(); |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 136 | } |
| 137 | |
Adrian Roos | 4b820e4 | 2017-01-13 17:40:43 -0800 | [diff] [blame] | 138 | /** |
| 139 | * @param pulsing whether we are currently pulsing for ambient display. |
| 140 | */ |
| 141 | public void setPulsing(boolean pulsing) { |
Adrian Roos | b2a8729 | 2017-02-13 15:05:03 +0100 | [diff] [blame] | 142 | if (mPulsing == pulsing) { |
| 143 | return; |
| 144 | } |
Adrian Roos | 4b820e4 | 2017-01-13 17:40:43 -0800 | [diff] [blame] | 145 | mPulsing = pulsing; |
Selim Cinek | ba069ae | 2020-04-01 19:45:16 -0700 | [diff] [blame] | 146 | updateAllowedStates(); |
Adrian Roos | 4b820e4 | 2017-01-13 17:40:43 -0800 | [diff] [blame] | 147 | } |
| 148 | |
Selim Cinek | ba069ae | 2020-04-01 19:45:16 -0700 | [diff] [blame] | 149 | private void updateAllowedStates() { |
Ned Burns | 9512e0c | 2019-05-30 19:36:04 -0400 | [diff] [blame] | 150 | boolean reorderingAllowed = |
| 151 | (!mScreenOn || !mPanelExpanded || mIsTemporaryReorderingAllowed) && !mPulsing; |
| 152 | boolean changedToTrue = reorderingAllowed && !mReorderingAllowed; |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 153 | mReorderingAllowed = reorderingAllowed; |
Ned Burns | 9512e0c | 2019-05-30 19:36:04 -0400 | [diff] [blame] | 154 | if (changedToTrue) { |
Selim Cinek | 0260c75 | 2020-05-11 16:03:52 -0700 | [diff] [blame] | 155 | notifyChangeAllowed(mReorderingAllowedCallbacks, mPersistentReorderingCallbacks); |
Selim Cinek | ba069ae | 2020-04-01 19:45:16 -0700 | [diff] [blame] | 156 | } |
| 157 | boolean groupChangesAllowed = (!mScreenOn || !mPanelExpanded) && !mPulsing; |
| 158 | changedToTrue = groupChangesAllowed && !mGroupChangedAllowed; |
| 159 | mGroupChangedAllowed = groupChangesAllowed; |
| 160 | if (changedToTrue) { |
Selim Cinek | 0260c75 | 2020-05-11 16:03:52 -0700 | [diff] [blame] | 161 | notifyChangeAllowed(mGroupChangesAllowedCallbacks, mPersistentGroupCallbacks); |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 162 | } |
| 163 | } |
| 164 | |
Selim Cinek | 0260c75 | 2020-05-11 16:03:52 -0700 | [diff] [blame] | 165 | private void notifyChangeAllowed(ArrayList<Callback> callbacks, |
| 166 | ArraySet<Callback> persistentCallbacks) { |
Selim Cinek | ba069ae | 2020-04-01 19:45:16 -0700 | [diff] [blame] | 167 | for (int i = 0; i < callbacks.size(); i++) { |
Selim Cinek | 0260c75 | 2020-05-11 16:03:52 -0700 | [diff] [blame] | 168 | Callback callback = callbacks.get(i); |
| 169 | callback.onChangeAllowed(); |
| 170 | if (!persistentCallbacks.contains(callback)) { |
| 171 | callbacks.remove(callback); |
| 172 | i--; |
| 173 | } |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 174 | } |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | /** |
| 178 | * @return whether reordering is currently allowed in general. |
| 179 | */ |
| 180 | public boolean isReorderingAllowed() { |
| 181 | return mReorderingAllowed; |
| 182 | } |
| 183 | |
| 184 | /** |
Selim Cinek | ba069ae | 2020-04-01 19:45:16 -0700 | [diff] [blame] | 185 | * @return whether changes in the grouping should be allowed right now. |
| 186 | */ |
| 187 | public boolean areGroupChangesAllowed() { |
| 188 | return mGroupChangedAllowed; |
| 189 | } |
| 190 | |
| 191 | /** |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 192 | * @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 Burns | a13b6f1 | 2019-02-11 20:42:58 -0500 | [diff] [blame] | 203 | if (mLowPriorityReorderingViews.contains(row.getEntry())) { |
Selim Cinek | 55a3e73 | 2017-05-25 18:30:10 -0700 | [diff] [blame] | 204 | return true; |
| 205 | } |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 206 | if (mAllowedReorderViews.contains(row) |
Evan Laird | 9449285 | 2018-10-25 13:43:01 -0400 | [diff] [blame] | 207 | && !mVisibilityLocationProvider.isInVisibleLocation(row.getEntry())) { |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 208 | 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 Cinek | 55a3e73 | 2017-05-25 18:30:10 -0700 | [diff] [blame] | 221 | mLowPriorityReorderingViews.clear(); |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | @Override |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 225 | public void onHeadsUpStateChanged(NotificationEntry entry, boolean isHeadsUp) { |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 226 | 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 Laird | 9449285 | 2018-10-25 13:43:01 -0400 | [diff] [blame] | 229 | mAllowedReorderViews.add(entry.getRow()); |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 233 | /** |
Ned Burns | 9512e0c | 2019-05-30 19:36:04 -0400 | [diff] [blame] | 234 | * 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 Cinek | ba069ae | 2020-04-01 19:45:16 -0700 | [diff] [blame] | 244 | updateAllowedStates(); |
Ned Burns | 9512e0c | 2019-05-30 19:36:04 -0400 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | private final Runnable mOnTemporaryReorderingExpired = () -> { |
| 248 | mIsTemporaryReorderingAllowed = false; |
Selim Cinek | ba069ae | 2020-04-01 19:45:16 -0700 | [diff] [blame] | 249 | updateAllowedStates(); |
Ned Burns | 9512e0c | 2019-05-30 19:36:04 -0400 | [diff] [blame] | 250 | }; |
| 251 | |
| 252 | /** |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 253 | * 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 Burns | 9512e0c | 2019-05-30 19:36:04 -0400 | [diff] [blame] | 260 | @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 Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 274 | public interface Callback { |
| 275 | /** |
Selim Cinek | ba069ae | 2020-04-01 19:45:16 -0700 | [diff] [blame] | 276 | * Called when changing is allowed again. |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 277 | */ |
Selim Cinek | ba069ae | 2020-04-01 19:45:16 -0700 | [diff] [blame] | 278 | void onChangeAllowed(); |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | } |