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<>(); |
| 46 | private final ArrayList<Callback> mGroupChangesAllowedCallbacks = new ArrayList<>(); |
Ned Burns | 9512e0c | 2019-05-30 19:36:04 -0400 | [diff] [blame] | 47 | private final Handler mHandler; |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 48 | |
| 49 | private boolean mPanelExpanded; |
| 50 | private boolean mScreenOn; |
| 51 | private boolean mReorderingAllowed; |
Selim Cinek | ba069ae | 2020-04-01 19:45:16 -0700 | [diff] [blame] | 52 | private boolean mGroupChangedAllowed; |
Ned Burns | 9512e0c | 2019-05-30 19:36:04 -0400 | [diff] [blame] | 53 | private boolean mIsTemporaryReorderingAllowed; |
| 54 | private long mTemporaryReorderingStart; |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 55 | private VisibilityLocationProvider mVisibilityLocationProvider; |
| 56 | private ArraySet<View> mAllowedReorderViews = new ArraySet<>(); |
Ned Burns | a13b6f1 | 2019-02-11 20:42:58 -0500 | [diff] [blame] | 57 | private ArraySet<NotificationEntry> mLowPriorityReorderingViews = new ArraySet<>(); |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 58 | private ArraySet<View> mAddedChildren = new ArraySet<>(); |
Adrian Roos | 4b820e4 | 2017-01-13 17:40:43 -0800 | [diff] [blame] | 59 | private boolean mPulsing; |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 60 | |
Sergey Nikolaienkov | f6ad632 | 2020-02-10 15:46:32 +0100 | [diff] [blame] | 61 | /** |
| 62 | * Injected constructor. See {@link NotificationsModule}. |
| 63 | */ |
Ned Burns | 9512e0c | 2019-05-30 19:36:04 -0400 | [diff] [blame] | 64 | public VisualStabilityManager( |
Beverly | 95a0802ac | 2020-02-10 15:27:40 -0500 | [diff] [blame] | 65 | NotificationEntryManager notificationEntryManager, |
| 66 | @Main Handler handler) { |
Ned Burns | 9512e0c | 2019-05-30 19:36:04 -0400 | [diff] [blame] | 67 | |
| 68 | mHandler = handler; |
| 69 | |
Gus Prevas | 5b9098dc | 2018-12-21 17:07:15 -0500 | [diff] [blame] | 70 | notificationEntryManager.addNotificationEntryListener(new NotificationEntryListener() { |
| 71 | @Override |
Ned Burns | a13b6f1 | 2019-02-11 20:42:58 -0500 | [diff] [blame] | 72 | public void onPreEntryUpdated(NotificationEntry entry) { |
Beverly | e018f0d | 2019-11-06 14:20:29 -0500 | [diff] [blame] | 73 | final boolean ambientStateHasChanged = |
Ned Burns | 60e9459 | 2019-09-06 14:47:25 -0400 | [diff] [blame] | 74 | entry.isAmbient() != entry.getRow().isLowPriority(); |
Beverly | e018f0d | 2019-11-06 14:20:29 -0500 | [diff] [blame] | 75 | if (ambientStateHasChanged) { |
| 76 | // note: entries are removed in onReorderingFinished |
Ned Burns | a13b6f1 | 2019-02-11 20:42:58 -0500 | [diff] [blame] | 77 | mLowPriorityReorderingViews.add(entry); |
Gus Prevas | 5b9098dc | 2018-12-21 17:07:15 -0500 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | }); |
| 81 | } |
| 82 | |
| 83 | public void setUpWithPresenter(NotificationPresenter presenter) { |
Jason Monk | 27d01a62 | 2018-12-10 15:57:09 -0500 | [diff] [blame] | 84 | } |
| 85 | |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 86 | /** |
| 87 | * Add a callback to invoke when reordering is allowed again. |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 88 | */ |
| 89 | public void addReorderingAllowedCallback(Callback callback) { |
Selim Cinek | ba069ae | 2020-04-01 19:45:16 -0700 | [diff] [blame] | 90 | if (mReorderingAllowedCallbacks.contains(callback)) { |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 91 | return; |
| 92 | } |
Selim Cinek | ba069ae | 2020-04-01 19:45:16 -0700 | [diff] [blame] | 93 | mReorderingAllowedCallbacks.add(callback); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Add a callback to invoke when group changes are allowed again. |
| 98 | */ |
| 99 | public void addGroupChangesAllowedCallback(Callback callback) { |
| 100 | if (mGroupChangesAllowedCallbacks.contains(callback)) { |
| 101 | return; |
| 102 | } |
| 103 | mGroupChangesAllowedCallbacks.add(callback); |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Set the panel to be expanded. |
| 108 | */ |
| 109 | public void setPanelExpanded(boolean expanded) { |
| 110 | mPanelExpanded = expanded; |
Selim Cinek | ba069ae | 2020-04-01 19:45:16 -0700 | [diff] [blame] | 111 | updateAllowedStates(); |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | /** |
| 115 | * @param screenOn whether the screen is on |
| 116 | */ |
| 117 | public void setScreenOn(boolean screenOn) { |
| 118 | mScreenOn = screenOn; |
Selim Cinek | ba069ae | 2020-04-01 19:45:16 -0700 | [diff] [blame] | 119 | updateAllowedStates(); |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 120 | } |
| 121 | |
Adrian Roos | 4b820e4 | 2017-01-13 17:40:43 -0800 | [diff] [blame] | 122 | /** |
| 123 | * @param pulsing whether we are currently pulsing for ambient display. |
| 124 | */ |
| 125 | public void setPulsing(boolean pulsing) { |
Adrian Roos | b2a8729 | 2017-02-13 15:05:03 +0100 | [diff] [blame] | 126 | if (mPulsing == pulsing) { |
| 127 | return; |
| 128 | } |
Adrian Roos | 4b820e4 | 2017-01-13 17:40:43 -0800 | [diff] [blame] | 129 | mPulsing = pulsing; |
Selim Cinek | ba069ae | 2020-04-01 19:45:16 -0700 | [diff] [blame] | 130 | updateAllowedStates(); |
Adrian Roos | 4b820e4 | 2017-01-13 17:40:43 -0800 | [diff] [blame] | 131 | } |
| 132 | |
Selim Cinek | ba069ae | 2020-04-01 19:45:16 -0700 | [diff] [blame] | 133 | private void updateAllowedStates() { |
Ned Burns | 9512e0c | 2019-05-30 19:36:04 -0400 | [diff] [blame] | 134 | boolean reorderingAllowed = |
| 135 | (!mScreenOn || !mPanelExpanded || mIsTemporaryReorderingAllowed) && !mPulsing; |
| 136 | boolean changedToTrue = reorderingAllowed && !mReorderingAllowed; |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 137 | mReorderingAllowed = reorderingAllowed; |
Ned Burns | 9512e0c | 2019-05-30 19:36:04 -0400 | [diff] [blame] | 138 | if (changedToTrue) { |
Selim Cinek | ba069ae | 2020-04-01 19:45:16 -0700 | [diff] [blame] | 139 | notifyChangeAllowed(mReorderingAllowedCallbacks); |
| 140 | } |
| 141 | boolean groupChangesAllowed = (!mScreenOn || !mPanelExpanded) && !mPulsing; |
| 142 | changedToTrue = groupChangesAllowed && !mGroupChangedAllowed; |
| 143 | mGroupChangedAllowed = groupChangesAllowed; |
| 144 | if (changedToTrue) { |
| 145 | notifyChangeAllowed(mGroupChangesAllowedCallbacks); |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
Selim Cinek | ba069ae | 2020-04-01 19:45:16 -0700 | [diff] [blame] | 149 | private void notifyChangeAllowed(ArrayList<Callback> callbacks) { |
| 150 | for (int i = 0; i < callbacks.size(); i++) { |
| 151 | callbacks.get(i).onChangeAllowed(); |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 152 | } |
Selim Cinek | ba069ae | 2020-04-01 19:45:16 -0700 | [diff] [blame] | 153 | callbacks.clear(); |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | /** |
| 157 | * @return whether reordering is currently allowed in general. |
| 158 | */ |
| 159 | public boolean isReorderingAllowed() { |
| 160 | return mReorderingAllowed; |
| 161 | } |
| 162 | |
| 163 | /** |
Selim Cinek | ba069ae | 2020-04-01 19:45:16 -0700 | [diff] [blame] | 164 | * @return whether changes in the grouping should be allowed right now. |
| 165 | */ |
| 166 | public boolean areGroupChangesAllowed() { |
| 167 | return mGroupChangedAllowed; |
| 168 | } |
| 169 | |
| 170 | /** |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 171 | * @return whether a specific notification is allowed to reorder. Certain notifications are |
| 172 | * allowed to reorder even if {@link #isReorderingAllowed()} returns false, like newly added |
| 173 | * notifications or heads-up notifications that are out of view. |
| 174 | */ |
| 175 | public boolean canReorderNotification(ExpandableNotificationRow row) { |
| 176 | if (mReorderingAllowed) { |
| 177 | return true; |
| 178 | } |
| 179 | if (mAddedChildren.contains(row)) { |
| 180 | return true; |
| 181 | } |
Ned Burns | a13b6f1 | 2019-02-11 20:42:58 -0500 | [diff] [blame] | 182 | if (mLowPriorityReorderingViews.contains(row.getEntry())) { |
Selim Cinek | 55a3e73 | 2017-05-25 18:30:10 -0700 | [diff] [blame] | 183 | return true; |
| 184 | } |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 185 | if (mAllowedReorderViews.contains(row) |
Evan Laird | 9449285 | 2018-10-25 13:43:01 -0400 | [diff] [blame] | 186 | && !mVisibilityLocationProvider.isInVisibleLocation(row.getEntry())) { |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 187 | return true; |
| 188 | } |
| 189 | return false; |
| 190 | } |
| 191 | |
| 192 | public void setVisibilityLocationProvider( |
| 193 | VisibilityLocationProvider visibilityLocationProvider) { |
| 194 | mVisibilityLocationProvider = visibilityLocationProvider; |
| 195 | } |
| 196 | |
| 197 | public void onReorderingFinished() { |
| 198 | mAllowedReorderViews.clear(); |
| 199 | mAddedChildren.clear(); |
Selim Cinek | 55a3e73 | 2017-05-25 18:30:10 -0700 | [diff] [blame] | 200 | mLowPriorityReorderingViews.clear(); |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | @Override |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 204 | public void onHeadsUpStateChanged(NotificationEntry entry, boolean isHeadsUp) { |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 205 | if (isHeadsUp) { |
| 206 | // Heads up notifications should in general be allowed to reorder if they are out of |
| 207 | // view and stay at the current location if they aren't. |
Evan Laird | 9449285 | 2018-10-25 13:43:01 -0400 | [diff] [blame] | 208 | mAllowedReorderViews.add(entry.getRow()); |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 209 | } |
| 210 | } |
| 211 | |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 212 | /** |
Ned Burns | 9512e0c | 2019-05-30 19:36:04 -0400 | [diff] [blame] | 213 | * Temporarily allows reordering of the entire shade for a period of 1000ms. Subsequent calls |
| 214 | * to this method will extend the timer. |
| 215 | */ |
| 216 | public void temporarilyAllowReordering() { |
| 217 | mHandler.removeCallbacks(mOnTemporaryReorderingExpired); |
| 218 | mHandler.postDelayed(mOnTemporaryReorderingExpired, TEMPORARY_REORDERING_ALLOWED_DURATION); |
| 219 | if (!mIsTemporaryReorderingAllowed) { |
| 220 | mTemporaryReorderingStart = SystemClock.elapsedRealtime(); |
| 221 | } |
| 222 | mIsTemporaryReorderingAllowed = true; |
Selim Cinek | ba069ae | 2020-04-01 19:45:16 -0700 | [diff] [blame] | 223 | updateAllowedStates(); |
Ned Burns | 9512e0c | 2019-05-30 19:36:04 -0400 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | private final Runnable mOnTemporaryReorderingExpired = () -> { |
| 227 | mIsTemporaryReorderingAllowed = false; |
Selim Cinek | ba069ae | 2020-04-01 19:45:16 -0700 | [diff] [blame] | 228 | updateAllowedStates(); |
Ned Burns | 9512e0c | 2019-05-30 19:36:04 -0400 | [diff] [blame] | 229 | }; |
| 230 | |
| 231 | /** |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 232 | * Notify the visual stability manager that a new view was added and should be allowed to |
| 233 | * reorder next time. |
| 234 | */ |
| 235 | public void notifyViewAddition(View view) { |
| 236 | mAddedChildren.add(view); |
| 237 | } |
| 238 | |
Ned Burns | 9512e0c | 2019-05-30 19:36:04 -0400 | [diff] [blame] | 239 | @Override |
| 240 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 241 | pw.println("VisualStabilityManager state:"); |
| 242 | pw.print(" mIsTemporaryReorderingAllowed="); pw.println(mIsTemporaryReorderingAllowed); |
| 243 | pw.print(" mTemporaryReorderingStart="); pw.println(mTemporaryReorderingStart); |
| 244 | |
| 245 | long now = SystemClock.elapsedRealtime(); |
| 246 | pw.print(" Temporary reordering window has been open for "); |
| 247 | pw.print(now - (mIsTemporaryReorderingAllowed ? mTemporaryReorderingStart : now)); |
| 248 | pw.println("ms"); |
| 249 | |
| 250 | pw.println(); |
| 251 | } |
| 252 | |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 253 | public interface Callback { |
| 254 | /** |
Selim Cinek | ba069ae | 2020-04-01 19:45:16 -0700 | [diff] [blame] | 255 | * Called when changing is allowed again. |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 256 | */ |
Selim Cinek | ba069ae | 2020-04-01 19:45:16 -0700 | [diff] [blame] | 257 | void onChangeAllowed(); |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | } |