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 | f473681 | 2019-10-18 17:25:50 -0400 | [diff] [blame^] | 26 | import com.android.systemui.dagger.qualifiers.MainHandler; |
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; |
Rohan Shah | 20790b8 | 2018-07-02 17:21:04 -0700 | [diff] [blame] | 29 | import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow; |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 30 | import com.android.systemui.statusbar.policy.OnHeadsUpChangedListener; |
| 31 | |
Ned Burns | 9512e0c | 2019-05-30 19:36:04 -0400 | [diff] [blame] | 32 | import java.io.FileDescriptor; |
| 33 | import java.io.PrintWriter; |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 34 | import java.util.ArrayList; |
| 35 | |
Jason Monk | 27d01a62 | 2018-12-10 15:57:09 -0500 | [diff] [blame] | 36 | import javax.inject.Inject; |
| 37 | import javax.inject.Singleton; |
| 38 | |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 39 | /** |
| 40 | * A manager that ensures that notifications are visually stable. It will suppress reorderings |
| 41 | * and reorder at the right time when they are out of view. |
| 42 | */ |
Jason Monk | 27d01a62 | 2018-12-10 15:57:09 -0500 | [diff] [blame] | 43 | @Singleton |
Ned Burns | 9512e0c | 2019-05-30 19:36:04 -0400 | [diff] [blame] | 44 | public class VisualStabilityManager implements OnHeadsUpChangedListener, Dumpable { |
| 45 | |
| 46 | private static final long TEMPORARY_REORDERING_ALLOWED_DURATION = 1000; |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 47 | |
| 48 | private final ArrayList<Callback> mCallbacks = new ArrayList<>(); |
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 | |
Gus Prevas | 5b9098dc | 2018-12-21 17:07:15 -0500 | [diff] [blame] | 51 | private NotificationPresenter mPresenter; |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 52 | private boolean mPanelExpanded; |
| 53 | private boolean mScreenOn; |
| 54 | private boolean mReorderingAllowed; |
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 | |
Jason Monk | 27d01a62 | 2018-12-10 15:57:09 -0500 | [diff] [blame] | 63 | @Inject |
Ned Burns | 9512e0c | 2019-05-30 19:36:04 -0400 | [diff] [blame] | 64 | public VisualStabilityManager( |
Dave Mankoff | f473681 | 2019-10-18 17:25:50 -0400 | [diff] [blame^] | 65 | NotificationEntryManager notificationEntryManager, @MainHandler Handler handler) { |
Ned Burns | 9512e0c | 2019-05-30 19:36:04 -0400 | [diff] [blame] | 66 | |
| 67 | mHandler = handler; |
| 68 | |
Gus Prevas | 5b9098dc | 2018-12-21 17:07:15 -0500 | [diff] [blame] | 69 | notificationEntryManager.addNotificationEntryListener(new NotificationEntryListener() { |
| 70 | @Override |
Ned Burns | a13b6f1 | 2019-02-11 20:42:58 -0500 | [diff] [blame] | 71 | public void onPreEntryUpdated(NotificationEntry entry) { |
| 72 | final boolean mAmbientStateHasChanged = |
Ned Burns | 60e9459 | 2019-09-06 14:47:25 -0400 | [diff] [blame] | 73 | entry.isAmbient() != entry.getRow().isLowPriority(); |
Ned Burns | a13b6f1 | 2019-02-11 20:42:58 -0500 | [diff] [blame] | 74 | if (mAmbientStateHasChanged) { |
| 75 | mLowPriorityReorderingViews.add(entry); |
Gus Prevas | 5b9098dc | 2018-12-21 17:07:15 -0500 | [diff] [blame] | 76 | } |
| 77 | } |
Ned Burns | a13b6f1 | 2019-02-11 20:42:58 -0500 | [diff] [blame] | 78 | |
| 79 | @Override |
| 80 | public void onPostEntryUpdated(NotificationEntry entry) { |
| 81 | // This line is technically not required as we'll get called as the hierarchy |
| 82 | // manager will call onReorderingFinished() immediately before this. |
| 83 | // TODO: Find a way to make this relationship more explicit |
| 84 | mLowPriorityReorderingViews.remove(entry); |
| 85 | } |
Gus Prevas | 5b9098dc | 2018-12-21 17:07:15 -0500 | [diff] [blame] | 86 | }); |
| 87 | } |
| 88 | |
| 89 | public void setUpWithPresenter(NotificationPresenter presenter) { |
| 90 | mPresenter = presenter; |
Jason Monk | 27d01a62 | 2018-12-10 15:57:09 -0500 | [diff] [blame] | 91 | } |
| 92 | |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 93 | /** |
| 94 | * Add a callback to invoke when reordering is allowed again. |
| 95 | * @param callback |
| 96 | */ |
| 97 | public void addReorderingAllowedCallback(Callback callback) { |
| 98 | if (mCallbacks.contains(callback)) { |
| 99 | return; |
| 100 | } |
| 101 | mCallbacks.add(callback); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Set the panel to be expanded. |
| 106 | */ |
| 107 | public void setPanelExpanded(boolean expanded) { |
| 108 | mPanelExpanded = expanded; |
| 109 | updateReorderingAllowed(); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * @param screenOn whether the screen is on |
| 114 | */ |
| 115 | public void setScreenOn(boolean screenOn) { |
| 116 | mScreenOn = screenOn; |
| 117 | updateReorderingAllowed(); |
| 118 | } |
| 119 | |
Adrian Roos | 4b820e4 | 2017-01-13 17:40:43 -0800 | [diff] [blame] | 120 | /** |
| 121 | * @param pulsing whether we are currently pulsing for ambient display. |
| 122 | */ |
| 123 | public void setPulsing(boolean pulsing) { |
Adrian Roos | b2a8729 | 2017-02-13 15:05:03 +0100 | [diff] [blame] | 124 | if (mPulsing == pulsing) { |
| 125 | return; |
| 126 | } |
Adrian Roos | 4b820e4 | 2017-01-13 17:40:43 -0800 | [diff] [blame] | 127 | mPulsing = pulsing; |
| 128 | updateReorderingAllowed(); |
| 129 | } |
| 130 | |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 131 | private void updateReorderingAllowed() { |
Ned Burns | 9512e0c | 2019-05-30 19:36:04 -0400 | [diff] [blame] | 132 | boolean reorderingAllowed = |
| 133 | (!mScreenOn || !mPanelExpanded || mIsTemporaryReorderingAllowed) && !mPulsing; |
| 134 | boolean changedToTrue = reorderingAllowed && !mReorderingAllowed; |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 135 | mReorderingAllowed = reorderingAllowed; |
Ned Burns | 9512e0c | 2019-05-30 19:36:04 -0400 | [diff] [blame] | 136 | if (changedToTrue) { |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 137 | notifyCallbacks(); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | private void notifyCallbacks() { |
| 142 | for (int i = 0; i < mCallbacks.size(); i++) { |
| 143 | Callback callback = mCallbacks.get(i); |
| 144 | callback.onReorderingAllowed(); |
| 145 | } |
| 146 | mCallbacks.clear(); |
| 147 | } |
| 148 | |
| 149 | /** |
| 150 | * @return whether reordering is currently allowed in general. |
| 151 | */ |
| 152 | public boolean isReorderingAllowed() { |
| 153 | return mReorderingAllowed; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * @return whether a specific notification is allowed to reorder. Certain notifications are |
| 158 | * allowed to reorder even if {@link #isReorderingAllowed()} returns false, like newly added |
| 159 | * notifications or heads-up notifications that are out of view. |
| 160 | */ |
| 161 | public boolean canReorderNotification(ExpandableNotificationRow row) { |
| 162 | if (mReorderingAllowed) { |
| 163 | return true; |
| 164 | } |
| 165 | if (mAddedChildren.contains(row)) { |
| 166 | return true; |
| 167 | } |
Ned Burns | a13b6f1 | 2019-02-11 20:42:58 -0500 | [diff] [blame] | 168 | if (mLowPriorityReorderingViews.contains(row.getEntry())) { |
Selim Cinek | 55a3e73 | 2017-05-25 18:30:10 -0700 | [diff] [blame] | 169 | return true; |
| 170 | } |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 171 | if (mAllowedReorderViews.contains(row) |
Evan Laird | 9449285 | 2018-10-25 13:43:01 -0400 | [diff] [blame] | 172 | && !mVisibilityLocationProvider.isInVisibleLocation(row.getEntry())) { |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 173 | return true; |
| 174 | } |
| 175 | return false; |
| 176 | } |
| 177 | |
| 178 | public void setVisibilityLocationProvider( |
| 179 | VisibilityLocationProvider visibilityLocationProvider) { |
| 180 | mVisibilityLocationProvider = visibilityLocationProvider; |
| 181 | } |
| 182 | |
| 183 | public void onReorderingFinished() { |
| 184 | mAllowedReorderViews.clear(); |
| 185 | mAddedChildren.clear(); |
Selim Cinek | 55a3e73 | 2017-05-25 18:30:10 -0700 | [diff] [blame] | 186 | mLowPriorityReorderingViews.clear(); |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | @Override |
Ned Burns | f81c4c4 | 2019-01-07 14:10:43 -0500 | [diff] [blame] | 190 | public void onHeadsUpStateChanged(NotificationEntry entry, boolean isHeadsUp) { |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 191 | if (isHeadsUp) { |
| 192 | // Heads up notifications should in general be allowed to reorder if they are out of |
| 193 | // view and stay at the current location if they aren't. |
Evan Laird | 9449285 | 2018-10-25 13:43:01 -0400 | [diff] [blame] | 194 | mAllowedReorderViews.add(entry.getRow()); |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 195 | } |
| 196 | } |
| 197 | |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 198 | /** |
Ned Burns | 9512e0c | 2019-05-30 19:36:04 -0400 | [diff] [blame] | 199 | * Temporarily allows reordering of the entire shade for a period of 1000ms. Subsequent calls |
| 200 | * to this method will extend the timer. |
| 201 | */ |
| 202 | public void temporarilyAllowReordering() { |
| 203 | mHandler.removeCallbacks(mOnTemporaryReorderingExpired); |
| 204 | mHandler.postDelayed(mOnTemporaryReorderingExpired, TEMPORARY_REORDERING_ALLOWED_DURATION); |
| 205 | if (!mIsTemporaryReorderingAllowed) { |
| 206 | mTemporaryReorderingStart = SystemClock.elapsedRealtime(); |
| 207 | } |
| 208 | mIsTemporaryReorderingAllowed = true; |
| 209 | updateReorderingAllowed(); |
| 210 | } |
| 211 | |
| 212 | private final Runnable mOnTemporaryReorderingExpired = () -> { |
| 213 | mIsTemporaryReorderingAllowed = false; |
| 214 | updateReorderingAllowed(); |
| 215 | }; |
| 216 | |
| 217 | /** |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 218 | * Notify the visual stability manager that a new view was added and should be allowed to |
| 219 | * reorder next time. |
| 220 | */ |
| 221 | public void notifyViewAddition(View view) { |
| 222 | mAddedChildren.add(view); |
| 223 | } |
| 224 | |
Ned Burns | 9512e0c | 2019-05-30 19:36:04 -0400 | [diff] [blame] | 225 | @Override |
| 226 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 227 | pw.println("VisualStabilityManager state:"); |
| 228 | pw.print(" mIsTemporaryReorderingAllowed="); pw.println(mIsTemporaryReorderingAllowed); |
| 229 | pw.print(" mTemporaryReorderingStart="); pw.println(mTemporaryReorderingStart); |
| 230 | |
| 231 | long now = SystemClock.elapsedRealtime(); |
| 232 | pw.print(" Temporary reordering window has been open for "); |
| 233 | pw.print(now - (mIsTemporaryReorderingAllowed ? mTemporaryReorderingStart : now)); |
| 234 | pw.println("ms"); |
| 235 | |
| 236 | pw.println(); |
| 237 | } |
| 238 | |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 239 | public interface Callback { |
| 240 | /** |
| 241 | * Called when reordering is allowed again. |
| 242 | */ |
| 243 | void onReorderingAllowed(); |
| 244 | } |
| 245 | |
| 246 | } |