Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 1 | /* |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 2 | * Copyright (C) 2015 The Android Open Source Project |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 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.policy; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.content.res.Resources; |
| 21 | import android.database.ContentObserver; |
| 22 | import android.os.Handler; |
| 23 | import android.os.SystemClock; |
| 24 | import android.provider.Settings; |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 25 | import android.support.v4.util.ArraySet; |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 26 | import android.util.ArrayMap; |
| 27 | import android.util.Log; |
| 28 | import android.util.Pools; |
Selim Cinek | 737bff3 | 2015-05-08 16:08:35 -0700 | [diff] [blame] | 29 | import android.view.View; |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 30 | import android.view.ViewTreeObserver; |
| 31 | import android.view.accessibility.AccessibilityEvent; |
| 32 | |
Chris Wren | b659c4f | 2015-06-25 17:12:27 -0400 | [diff] [blame] | 33 | import com.android.internal.logging.MetricsLogger; |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 34 | import com.android.systemui.R; |
Selim Cinek | a59ecc3 | 2015-04-07 10:51:49 -0700 | [diff] [blame] | 35 | import com.android.systemui.statusbar.ExpandableNotificationRow; |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 36 | import com.android.systemui.statusbar.NotificationData; |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 37 | import com.android.systemui.statusbar.notification.VisualStabilityManager; |
Selim Cinek | 83bc783 | 2015-10-22 13:26:54 -0700 | [diff] [blame] | 38 | import com.android.systemui.statusbar.phone.NotificationGroupManager; |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 39 | import com.android.systemui.statusbar.phone.PhoneStatusBar; |
| 40 | |
| 41 | import java.io.FileDescriptor; |
| 42 | import java.io.PrintWriter; |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 43 | import java.util.ArrayList; |
Selim Cinek | 3362c13 | 2016-02-11 15:43:03 -0800 | [diff] [blame] | 44 | import java.util.Collection; |
Selim Cinek | a59ecc3 | 2015-04-07 10:51:49 -0700 | [diff] [blame] | 45 | import java.util.HashMap; |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 46 | import java.util.HashSet; |
| 47 | import java.util.Stack; |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 48 | |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 49 | /** |
| 50 | * A manager which handles heads up notifications which is a special mode where |
| 51 | * they simply peek from the top of the screen. |
| 52 | */ |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 53 | public class HeadsUpManager implements ViewTreeObserver.OnComputeInternalInsetsListener, |
| 54 | VisualStabilityManager.Callback { |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 55 | private static final String TAG = "HeadsUpManager"; |
| 56 | private static final boolean DEBUG = false; |
| 57 | private static final String SETTING_HEADS_UP_SNOOZE_LENGTH_MS = "heads_up_snooze_length_ms"; |
Selim Cinek | 0fccc72 | 2015-07-29 17:04:36 -0700 | [diff] [blame] | 58 | private static final int TAG_CLICKED_NOTIFICATION = R.id.is_clicked_heads_up_tag; |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 59 | |
| 60 | private final int mHeadsUpNotificationDecay; |
| 61 | private final int mMinimumDisplayTime; |
| 62 | |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 63 | private final int mTouchAcceptanceDelay; |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 64 | private final ArrayMap<String, Long> mSnoozedPackages; |
| 65 | private final HashSet<OnHeadsUpChangedListener> mListeners = new HashSet<>(); |
| 66 | private final int mDefaultSnoozeLengthMs; |
| 67 | private final Handler mHandler = new Handler(); |
| 68 | private final Pools.Pool<HeadsUpEntry> mEntryPool = new Pools.Pool<HeadsUpEntry>() { |
| 69 | |
| 70 | private Stack<HeadsUpEntry> mPoolObjects = new Stack<>(); |
| 71 | |
| 72 | @Override |
| 73 | public HeadsUpEntry acquire() { |
| 74 | if (!mPoolObjects.isEmpty()) { |
| 75 | return mPoolObjects.pop(); |
| 76 | } |
| 77 | return new HeadsUpEntry(); |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public boolean release(HeadsUpEntry instance) { |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 82 | instance.reset(); |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 83 | mPoolObjects.push(instance); |
| 84 | return true; |
| 85 | } |
| 86 | }; |
| 87 | |
Selim Cinek | 737bff3 | 2015-05-08 16:08:35 -0700 | [diff] [blame] | 88 | private final View mStatusBarWindowView; |
| 89 | private final int mStatusBarHeight; |
Chris Wren | b659c4f | 2015-06-25 17:12:27 -0400 | [diff] [blame] | 90 | private final Context mContext; |
Selim Cinek | 83bc783 | 2015-10-22 13:26:54 -0700 | [diff] [blame] | 91 | private final NotificationGroupManager mGroupManager; |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 92 | private PhoneStatusBar mBar; |
| 93 | private int mSnoozeLengthMs; |
| 94 | private ContentObserver mSettingsObserver; |
Selim Cinek | a59ecc3 | 2015-04-07 10:51:49 -0700 | [diff] [blame] | 95 | private HashMap<String, HeadsUpEntry> mHeadsUpEntries = new HashMap<>(); |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 96 | private HashSet<String> mSwipedOutKeys = new HashSet<>(); |
| 97 | private int mUser; |
| 98 | private Clock mClock; |
| 99 | private boolean mReleaseOnExpandFinish; |
| 100 | private boolean mTrackingHeadsUp; |
| 101 | private HashSet<NotificationData.Entry> mEntriesToRemoveAfterExpand = new HashSet<>(); |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 102 | private ArraySet<NotificationData.Entry> mEntriesToRemoveWhenReorderingAllowed |
| 103 | = new ArraySet<>(); |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 104 | private boolean mIsExpanded; |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 105 | private boolean mHasPinnedNotification; |
Selim Cinek | a59ecc3 | 2015-04-07 10:51:49 -0700 | [diff] [blame] | 106 | private int[] mTmpTwoArray = new int[2]; |
Selim Cinek | 737bff3 | 2015-05-08 16:08:35 -0700 | [diff] [blame] | 107 | private boolean mHeadsUpGoingAway; |
| 108 | private boolean mWaitingOnCollapseWhenGoingAway; |
| 109 | private boolean mIsObserving; |
Adrian Roos | 1c0ca50 | 2015-10-07 12:20:42 -0700 | [diff] [blame] | 110 | private boolean mRemoteInputActive; |
Selim Cinek | d127d79 | 2016-11-01 19:11:41 -0700 | [diff] [blame] | 111 | private float mExpandedHeight; |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 112 | private VisualStabilityManager mVisualStabilityManager; |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 113 | |
Selim Cinek | 83bc783 | 2015-10-22 13:26:54 -0700 | [diff] [blame] | 114 | public HeadsUpManager(final Context context, View statusBarWindowView, |
| 115 | NotificationGroupManager groupManager) { |
Chris Wren | b659c4f | 2015-06-25 17:12:27 -0400 | [diff] [blame] | 116 | mContext = context; |
| 117 | Resources resources = mContext.getResources(); |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 118 | mTouchAcceptanceDelay = resources.getInteger(R.integer.touch_acceptance_delay); |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 119 | mSnoozedPackages = new ArrayMap<>(); |
| 120 | mDefaultSnoozeLengthMs = resources.getInteger(R.integer.heads_up_default_snooze_length_ms); |
| 121 | mSnoozeLengthMs = mDefaultSnoozeLengthMs; |
| 122 | mMinimumDisplayTime = resources.getInteger(R.integer.heads_up_notification_minimum_time); |
Selim Cinek | e53e6bb | 2015-04-13 16:14:26 -0700 | [diff] [blame] | 123 | mHeadsUpNotificationDecay = resources.getInteger(R.integer.heads_up_notification_decay); |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 124 | mClock = new Clock(); |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 125 | |
| 126 | mSnoozeLengthMs = Settings.Global.getInt(context.getContentResolver(), |
| 127 | SETTING_HEADS_UP_SNOOZE_LENGTH_MS, mDefaultSnoozeLengthMs); |
| 128 | mSettingsObserver = new ContentObserver(mHandler) { |
| 129 | @Override |
| 130 | public void onChange(boolean selfChange) { |
| 131 | final int packageSnoozeLengthMs = Settings.Global.getInt( |
| 132 | context.getContentResolver(), SETTING_HEADS_UP_SNOOZE_LENGTH_MS, -1); |
| 133 | if (packageSnoozeLengthMs > -1 && packageSnoozeLengthMs != mSnoozeLengthMs) { |
| 134 | mSnoozeLengthMs = packageSnoozeLengthMs; |
| 135 | if (DEBUG) Log.v(TAG, "mSnoozeLengthMs = " + mSnoozeLengthMs); |
| 136 | } |
| 137 | } |
| 138 | }; |
| 139 | context.getContentResolver().registerContentObserver( |
| 140 | Settings.Global.getUriFor(SETTING_HEADS_UP_SNOOZE_LENGTH_MS), false, |
| 141 | mSettingsObserver); |
Selim Cinek | 737bff3 | 2015-05-08 16:08:35 -0700 | [diff] [blame] | 142 | mStatusBarWindowView = statusBarWindowView; |
Selim Cinek | 83bc783 | 2015-10-22 13:26:54 -0700 | [diff] [blame] | 143 | mGroupManager = groupManager; |
Selim Cinek | 737bff3 | 2015-05-08 16:08:35 -0700 | [diff] [blame] | 144 | mStatusBarHeight = resources.getDimensionPixelSize( |
| 145 | com.android.internal.R.dimen.status_bar_height); |
| 146 | } |
| 147 | |
| 148 | private void updateTouchableRegionListener() { |
| 149 | boolean shouldObserve = mHasPinnedNotification || mHeadsUpGoingAway |
| 150 | || mWaitingOnCollapseWhenGoingAway; |
| 151 | if (shouldObserve == mIsObserving) { |
| 152 | return; |
| 153 | } |
| 154 | if (shouldObserve) { |
| 155 | mStatusBarWindowView.getViewTreeObserver().addOnComputeInternalInsetsListener(this); |
| 156 | mStatusBarWindowView.requestLayout(); |
| 157 | } else { |
| 158 | mStatusBarWindowView.getViewTreeObserver().removeOnComputeInternalInsetsListener(this); |
| 159 | } |
| 160 | mIsObserving = shouldObserve; |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | public void setBar(PhoneStatusBar bar) { |
| 164 | mBar = bar; |
| 165 | } |
| 166 | |
| 167 | public void addListener(OnHeadsUpChangedListener listener) { |
| 168 | mListeners.add(listener); |
| 169 | } |
| 170 | |
| 171 | public PhoneStatusBar getBar() { |
| 172 | return mBar; |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * Called when posting a new notification to the heads up. |
| 177 | */ |
| 178 | public void showNotification(NotificationData.Entry headsUp) { |
| 179 | if (DEBUG) Log.v(TAG, "showNotification"); |
| 180 | addHeadsUpEntry(headsUp); |
| 181 | updateNotification(headsUp, true); |
| 182 | headsUp.setInterruption(); |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Called when updating or posting a notification to the heads up. |
| 187 | */ |
| 188 | public void updateNotification(NotificationData.Entry headsUp, boolean alert) { |
| 189 | if (DEBUG) Log.v(TAG, "updateNotification"); |
| 190 | |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 191 | headsUp.row.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED); |
| 192 | |
| 193 | if (alert) { |
| 194 | HeadsUpEntry headsUpEntry = mHeadsUpEntries.get(headsUp.key); |
Selim Cinek | 967ed2a | 2016-04-08 18:29:11 -0700 | [diff] [blame] | 195 | if (headsUpEntry == null) { |
| 196 | // the entry was released before this update (i.e by a listener) This can happen |
| 197 | // with the groupmanager |
| 198 | return; |
| 199 | } |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 200 | headsUpEntry.updateEntry(); |
Selim Cinek | 131c1e2 | 2015-05-11 19:04:49 -0700 | [diff] [blame] | 201 | setEntryPinned(headsUpEntry, shouldHeadsUpBecomePinned(headsUp)); |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 202 | } |
| 203 | } |
| 204 | |
| 205 | private void addHeadsUpEntry(NotificationData.Entry entry) { |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 206 | HeadsUpEntry headsUpEntry = mEntryPool.acquire(); |
Selim Cinek | a59ecc3 | 2015-04-07 10:51:49 -0700 | [diff] [blame] | 207 | |
| 208 | // This will also add the entry to the sortedList |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 209 | headsUpEntry.setEntry(entry); |
| 210 | mHeadsUpEntries.put(entry.key, headsUpEntry); |
Selim Cinek | a59ecc3 | 2015-04-07 10:51:49 -0700 | [diff] [blame] | 211 | entry.row.setHeadsUp(true); |
Selim Cinek | 131c1e2 | 2015-05-11 19:04:49 -0700 | [diff] [blame] | 212 | setEntryPinned(headsUpEntry, shouldHeadsUpBecomePinned(entry)); |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 213 | for (OnHeadsUpChangedListener listener : mListeners) { |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 214 | listener.onHeadsUpStateChanged(entry, true); |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 215 | } |
| 216 | entry.row.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED); |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 217 | } |
| 218 | |
Selim Cinek | 131c1e2 | 2015-05-11 19:04:49 -0700 | [diff] [blame] | 219 | private boolean shouldHeadsUpBecomePinned(NotificationData.Entry entry) { |
| 220 | return !mIsExpanded || hasFullScreenIntent(entry); |
| 221 | } |
| 222 | |
| 223 | private boolean hasFullScreenIntent(NotificationData.Entry entry) { |
| 224 | return entry.notification.getNotification().fullScreenIntent != null; |
| 225 | } |
| 226 | |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 227 | private void setEntryPinned(HeadsUpEntry headsUpEntry, boolean isPinned) { |
Selim Cinek | 1f3f544 | 2015-04-10 17:54:46 -0700 | [diff] [blame] | 228 | ExpandableNotificationRow row = headsUpEntry.entry.row; |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 229 | if (row.isPinned() != isPinned) { |
| 230 | row.setPinned(isPinned); |
| 231 | updatePinnedMode(); |
| 232 | for (OnHeadsUpChangedListener listener : mListeners) { |
| 233 | if (isPinned) { |
| 234 | listener.onHeadsUpPinned(row); |
| 235 | } else { |
| 236 | listener.onHeadsUpUnPinned(row); |
Selim Cinek | 1f3f544 | 2015-04-10 17:54:46 -0700 | [diff] [blame] | 237 | } |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 242 | private void removeHeadsUpEntry(NotificationData.Entry entry) { |
| 243 | HeadsUpEntry remove = mHeadsUpEntries.remove(entry.key); |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 244 | entry.row.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED); |
| 245 | entry.row.setHeadsUp(false); |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 246 | setEntryPinned(remove, false /* isPinned */); |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 247 | for (OnHeadsUpChangedListener listener : mListeners) { |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 248 | listener.onHeadsUpStateChanged(entry, false); |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 249 | } |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 250 | mEntryPool.release(remove); |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 251 | } |
| 252 | |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 253 | private void updatePinnedMode() { |
| 254 | boolean hasPinnedNotification = hasPinnedNotificationInternal(); |
| 255 | if (hasPinnedNotification == mHasPinnedNotification) { |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 256 | return; |
| 257 | } |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 258 | mHasPinnedNotification = hasPinnedNotification; |
Chris Wren | 063926b | 2015-10-16 16:24:20 -0400 | [diff] [blame] | 259 | if (mHasPinnedNotification) { |
| 260 | MetricsLogger.count(mContext, "note_peek", 1); |
| 261 | } |
Selim Cinek | 737bff3 | 2015-05-08 16:08:35 -0700 | [diff] [blame] | 262 | updateTouchableRegionListener(); |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 263 | for (OnHeadsUpChangedListener listener : mListeners) { |
John Spurlock | b349af57 | 2015-04-29 12:24:19 -0400 | [diff] [blame] | 264 | listener.onHeadsUpPinnedModeChanged(hasPinnedNotification); |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 265 | } |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * React to the removal of the notification in the heads up. |
| 270 | * |
| 271 | * @return true if the notification was removed and false if it still needs to be kept around |
| 272 | * for a bit since it wasn't shown long enough |
| 273 | */ |
Adrian Roos | c721fcc5 | 2016-04-29 11:28:37 -0700 | [diff] [blame] | 274 | public boolean removeNotification(String key, boolean ignoreEarliestRemovalTime) { |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 275 | if (DEBUG) Log.v(TAG, "remove"); |
Adrian Roos | c721fcc5 | 2016-04-29 11:28:37 -0700 | [diff] [blame] | 276 | if (wasShownLongEnough(key) || ignoreEarliestRemovalTime) { |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 277 | releaseImmediately(key); |
| 278 | return true; |
| 279 | } else { |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 280 | getHeadsUpEntry(key).removeAsSoonAsPossible(); |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 281 | return false; |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | private boolean wasShownLongEnough(String key) { |
| 286 | HeadsUpEntry headsUpEntry = getHeadsUpEntry(key); |
| 287 | HeadsUpEntry topEntry = getTopEntry(); |
| 288 | if (mSwipedOutKeys.contains(key)) { |
| 289 | // We always instantly dismiss views being manually swiped out. |
| 290 | mSwipedOutKeys.remove(key); |
| 291 | return true; |
| 292 | } |
| 293 | if (headsUpEntry != topEntry) { |
| 294 | return true; |
| 295 | } |
| 296 | return headsUpEntry.wasShownLongEnough(); |
| 297 | } |
| 298 | |
| 299 | public boolean isHeadsUp(String key) { |
| 300 | return mHeadsUpEntries.containsKey(key); |
| 301 | } |
| 302 | |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 303 | /** |
| 304 | * Push any current Heads Up notification down into the shade. |
| 305 | */ |
| 306 | public void releaseAllImmediately() { |
| 307 | if (DEBUG) Log.v(TAG, "releaseAllImmediately"); |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 308 | ArrayList<String> keys = new ArrayList<>(mHeadsUpEntries.keySet()); |
| 309 | for (String key : keys) { |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 310 | releaseImmediately(key); |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | public void releaseImmediately(String key) { |
| 315 | HeadsUpEntry headsUpEntry = getHeadsUpEntry(key); |
| 316 | if (headsUpEntry == null) { |
| 317 | return; |
| 318 | } |
| 319 | NotificationData.Entry shadeEntry = headsUpEntry.entry; |
| 320 | removeHeadsUpEntry(shadeEntry); |
| 321 | } |
| 322 | |
| 323 | public boolean isSnoozed(String packageName) { |
| 324 | final String key = snoozeKey(packageName, mUser); |
| 325 | Long snoozedUntil = mSnoozedPackages.get(key); |
| 326 | if (snoozedUntil != null) { |
| 327 | if (snoozedUntil > SystemClock.elapsedRealtime()) { |
| 328 | if (DEBUG) Log.v(TAG, key + " snoozed"); |
| 329 | return true; |
| 330 | } |
| 331 | mSnoozedPackages.remove(packageName); |
| 332 | } |
| 333 | return false; |
| 334 | } |
| 335 | |
| 336 | public void snooze() { |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 337 | for (String key : mHeadsUpEntries.keySet()) { |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 338 | HeadsUpEntry entry = mHeadsUpEntries.get(key); |
| 339 | String packageName = entry.entry.notification.getPackageName(); |
| 340 | mSnoozedPackages.put(snoozeKey(packageName, mUser), |
| 341 | SystemClock.elapsedRealtime() + mSnoozeLengthMs); |
| 342 | } |
| 343 | mReleaseOnExpandFinish = true; |
| 344 | } |
| 345 | |
| 346 | private static String snoozeKey(String packageName, int user) { |
| 347 | return user + "," + packageName; |
| 348 | } |
| 349 | |
| 350 | private HeadsUpEntry getHeadsUpEntry(String key) { |
| 351 | return mHeadsUpEntries.get(key); |
| 352 | } |
| 353 | |
| 354 | public NotificationData.Entry getEntry(String key) { |
| 355 | return mHeadsUpEntries.get(key).entry; |
| 356 | } |
| 357 | |
Selim Cinek | 3362c13 | 2016-02-11 15:43:03 -0800 | [diff] [blame] | 358 | public Collection<HeadsUpEntry> getAllEntries() { |
| 359 | return mHeadsUpEntries.values(); |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | public HeadsUpEntry getTopEntry() { |
Selim Cinek | 3362c13 | 2016-02-11 15:43:03 -0800 | [diff] [blame] | 363 | if (mHeadsUpEntries.isEmpty()) { |
| 364 | return null; |
| 365 | } |
| 366 | HeadsUpEntry topEntry = null; |
| 367 | for (HeadsUpEntry entry: mHeadsUpEntries.values()) { |
| 368 | if (topEntry == null || entry.compareTo(topEntry) == -1) { |
| 369 | topEntry = entry; |
| 370 | } |
| 371 | } |
| 372 | return topEntry; |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | /** |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 376 | * Decides whether a click is invalid for a notification, i.e it has not been shown long enough |
| 377 | * that a user might have consciously clicked on it. |
| 378 | * |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 379 | * @param key the key of the touched notification |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 380 | * @return whether the touch is invalid and should be discarded |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 381 | */ |
| 382 | public boolean shouldSwallowClick(String key) { |
Selim Cinek | 2f6b3fb | 2015-04-20 13:04:47 -0700 | [diff] [blame] | 383 | HeadsUpEntry entry = mHeadsUpEntries.get(key); |
| 384 | if (entry != null && mClock.currentTimeMillis() < entry.postTime) { |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 385 | return true; |
| 386 | } |
| 387 | return false; |
| 388 | } |
| 389 | |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 390 | public void onComputeInternalInsets(ViewTreeObserver.InternalInsetsInfo info) { |
Selim Cinek | 9941539 | 2016-09-09 14:58:41 -0700 | [diff] [blame] | 391 | if (mIsExpanded || mBar.isBouncerShowing()) { |
Selim Cinek | 131c1e2 | 2015-05-11 19:04:49 -0700 | [diff] [blame] | 392 | // The touchable region is always the full area when expanded |
| 393 | return; |
| 394 | } |
Selim Cinek | 737bff3 | 2015-05-08 16:08:35 -0700 | [diff] [blame] | 395 | if (mHasPinnedNotification) { |
Selim Cinek | 3362c13 | 2016-02-11 15:43:03 -0800 | [diff] [blame] | 396 | ExpandableNotificationRow topEntry = getTopEntry().entry.row; |
| 397 | if (topEntry.isChildInGroup()) { |
| 398 | final ExpandableNotificationRow groupSummary |
| 399 | = mGroupManager.getGroupSummary(topEntry.getStatusBarNotification()); |
| 400 | if (groupSummary != null) { |
| 401 | topEntry = groupSummary; |
Selim Cinek | a59ecc3 | 2015-04-07 10:51:49 -0700 | [diff] [blame] | 402 | } |
| 403 | } |
Selim Cinek | 3362c13 | 2016-02-11 15:43:03 -0800 | [diff] [blame] | 404 | topEntry.getLocationOnScreen(mTmpTwoArray); |
| 405 | int minX = mTmpTwoArray[0]; |
| 406 | int maxX = mTmpTwoArray[0] + topEntry.getWidth(); |
| 407 | int maxY = topEntry.getIntrinsicHeight(); |
Selim Cinek | a59ecc3 | 2015-04-07 10:51:49 -0700 | [diff] [blame] | 408 | |
| 409 | info.setTouchableInsets(ViewTreeObserver.InternalInsetsInfo.TOUCHABLE_INSETS_REGION); |
Selim Cinek | 33d4614 | 2016-01-15 18:58:39 -0800 | [diff] [blame] | 410 | info.touchableRegion.set(minX, 0, maxX, maxY); |
Selim Cinek | 737bff3 | 2015-05-08 16:08:35 -0700 | [diff] [blame] | 411 | } else if (mHeadsUpGoingAway || mWaitingOnCollapseWhenGoingAway) { |
| 412 | info.setTouchableInsets(ViewTreeObserver.InternalInsetsInfo.TOUCHABLE_INSETS_REGION); |
| 413 | info.touchableRegion.set(0, 0, mStatusBarWindowView.getWidth(), mStatusBarHeight); |
Selim Cinek | a59ecc3 | 2015-04-07 10:51:49 -0700 | [diff] [blame] | 414 | } |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | public void setUser(int user) { |
| 418 | mUser = user; |
| 419 | } |
| 420 | |
| 421 | public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { |
| 422 | pw.println("HeadsUpManager state:"); |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 423 | pw.print(" mTouchAcceptanceDelay="); pw.println(mTouchAcceptanceDelay); |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 424 | pw.print(" mSnoozeLengthMs="); pw.println(mSnoozeLengthMs); |
| 425 | pw.print(" now="); pw.println(SystemClock.elapsedRealtime()); |
| 426 | pw.print(" mUser="); pw.println(mUser); |
Selim Cinek | 3362c13 | 2016-02-11 15:43:03 -0800 | [diff] [blame] | 427 | for (HeadsUpEntry entry: mHeadsUpEntries.values()) { |
Selim Cinek | a59ecc3 | 2015-04-07 10:51:49 -0700 | [diff] [blame] | 428 | pw.print(" HeadsUpEntry="); pw.println(entry.entry); |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 429 | } |
| 430 | int N = mSnoozedPackages.size(); |
| 431 | pw.println(" snoozed packages: " + N); |
| 432 | for (int i = 0; i < N; i++) { |
| 433 | pw.print(" "); pw.print(mSnoozedPackages.valueAt(i)); |
| 434 | pw.print(", "); pw.println(mSnoozedPackages.keyAt(i)); |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | public boolean hasPinnedHeadsUp() { |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 439 | return mHasPinnedNotification; |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 440 | } |
| 441 | |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 442 | private boolean hasPinnedNotificationInternal() { |
| 443 | for (String key : mHeadsUpEntries.keySet()) { |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 444 | HeadsUpEntry entry = mHeadsUpEntries.get(key); |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 445 | if (entry.entry.row.isPinned()) { |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 446 | return true; |
| 447 | } |
| 448 | } |
| 449 | return false; |
| 450 | } |
| 451 | |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 452 | /** |
| 453 | * Notifies that a notification was swiped out and will be removed. |
| 454 | * |
| 455 | * @param key the notification key |
| 456 | */ |
| 457 | public void addSwipedOutNotification(String key) { |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 458 | mSwipedOutKeys.add(key); |
| 459 | } |
| 460 | |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 461 | public void unpinAll() { |
| 462 | for (String key : mHeadsUpEntries.keySet()) { |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 463 | HeadsUpEntry entry = mHeadsUpEntries.get(key); |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 464 | setEntryPinned(entry, false /* isPinned */); |
Selim Cinek | 31aada4 | 2015-12-18 17:51:15 -0800 | [diff] [blame] | 465 | // maybe it got un sticky |
| 466 | entry.updateEntry(false /* updatePostTime */); |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 467 | } |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 468 | } |
| 469 | |
| 470 | public void onExpandingFinished() { |
| 471 | if (mReleaseOnExpandFinish) { |
| 472 | releaseAllImmediately(); |
| 473 | mReleaseOnExpandFinish = false; |
| 474 | } else { |
| 475 | for (NotificationData.Entry entry : mEntriesToRemoveAfterExpand) { |
Selim Cinek | c9e6834 | 2015-11-06 11:33:20 -0800 | [diff] [blame] | 476 | if (isHeadsUp(entry.key)) { |
| 477 | // Maybe the heads-up was removed already |
| 478 | removeHeadsUpEntry(entry); |
| 479 | } |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 480 | } |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 481 | } |
Selim Cinek | acd0df6 | 2015-07-29 19:09:03 -0700 | [diff] [blame] | 482 | mEntriesToRemoveAfterExpand.clear(); |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | public void setTrackingHeadsUp(boolean trackingHeadsUp) { |
| 486 | mTrackingHeadsUp = trackingHeadsUp; |
| 487 | } |
| 488 | |
Selim Cinek | 31aada4 | 2015-12-18 17:51:15 -0800 | [diff] [blame] | 489 | public boolean isTrackingHeadsUp() { |
| 490 | return mTrackingHeadsUp; |
| 491 | } |
| 492 | |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 493 | public void setIsExpanded(boolean isExpanded) { |
Selim Cinek | 1f3f544 | 2015-04-10 17:54:46 -0700 | [diff] [blame] | 494 | if (isExpanded != mIsExpanded) { |
| 495 | mIsExpanded = isExpanded; |
| 496 | if (isExpanded) { |
Selim Cinek | 737bff3 | 2015-05-08 16:08:35 -0700 | [diff] [blame] | 497 | // make sure our state is sane |
| 498 | mWaitingOnCollapseWhenGoingAway = false; |
| 499 | mHeadsUpGoingAway = false; |
| 500 | updateTouchableRegionListener(); |
Selim Cinek | 1f3f544 | 2015-04-10 17:54:46 -0700 | [diff] [blame] | 501 | } |
| 502 | } |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 503 | } |
| 504 | |
Selim Cinek | 31aada4 | 2015-12-18 17:51:15 -0800 | [diff] [blame] | 505 | /** |
| 506 | * @return the height of the top heads up notification when pinned. This is different from the |
| 507 | * intrinsic height, which also includes whether the notification is system expanded and |
| 508 | * is mainly used when dragging down from a heads up notification. |
| 509 | */ |
| 510 | public int getTopHeadsUpPinnedHeight() { |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 511 | HeadsUpEntry topEntry = getTopEntry(); |
Selim Cinek | 5bc852a | 2015-12-21 12:19:09 -0800 | [diff] [blame] | 512 | if (topEntry == null || topEntry.entry == null) { |
| 513 | return 0; |
| 514 | } |
| 515 | ExpandableNotificationRow row = topEntry.entry.row; |
| 516 | if (row.isChildInGroup()) { |
| 517 | final ExpandableNotificationRow groupSummary |
| 518 | = mGroupManager.getGroupSummary(row.getStatusBarNotification()); |
| 519 | if (groupSummary != null) { |
| 520 | row = groupSummary; |
| 521 | } |
| 522 | } |
Selim Cinek | d127d79 | 2016-11-01 19:11:41 -0700 | [diff] [blame] | 523 | return row.getPinnedHeadsUpHeight(); |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 524 | } |
| 525 | |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 526 | /** |
| 527 | * Compare two entries and decide how they should be ranked. |
| 528 | * |
| 529 | * @return -1 if the first argument should be ranked higher than the second, 1 if the second |
| 530 | * one should be ranked higher and 0 if they are equal. |
| 531 | */ |
Selim Cinek | fbe9a44 | 2015-04-13 16:09:49 -0700 | [diff] [blame] | 532 | public int compare(NotificationData.Entry a, NotificationData.Entry b) { |
| 533 | HeadsUpEntry aEntry = getHeadsUpEntry(a.key); |
| 534 | HeadsUpEntry bEntry = getHeadsUpEntry(b.key); |
| 535 | if (aEntry == null || bEntry == null) { |
| 536 | return aEntry == null ? 1 : -1; |
| 537 | } |
| 538 | return aEntry.compareTo(bEntry); |
| 539 | } |
| 540 | |
Selim Cinek | 737bff3 | 2015-05-08 16:08:35 -0700 | [diff] [blame] | 541 | /** |
| 542 | * Set that we are exiting the headsUp pinned mode, but some notifications might still be |
| 543 | * animating out. This is used to keep the touchable regions in a sane state. |
| 544 | */ |
| 545 | public void setHeadsUpGoingAway(boolean headsUpGoingAway) { |
| 546 | if (headsUpGoingAway != mHeadsUpGoingAway) { |
| 547 | mHeadsUpGoingAway = headsUpGoingAway; |
| 548 | if (!headsUpGoingAway) { |
| 549 | waitForStatusBarLayout(); |
| 550 | } |
| 551 | updateTouchableRegionListener(); |
| 552 | } |
| 553 | } |
| 554 | |
| 555 | /** |
| 556 | * We need to wait on the whole panel to collapse, before we can remove the touchable region |
| 557 | * listener. |
| 558 | */ |
| 559 | private void waitForStatusBarLayout() { |
| 560 | mWaitingOnCollapseWhenGoingAway = true; |
| 561 | mStatusBarWindowView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() { |
| 562 | @Override |
| 563 | public void onLayoutChange(View v, int left, int top, int right, int bottom, |
| 564 | int oldLeft, |
| 565 | int oldTop, int oldRight, int oldBottom) { |
| 566 | if (mStatusBarWindowView.getHeight() <= mStatusBarHeight) { |
| 567 | mStatusBarWindowView.removeOnLayoutChangeListener(this); |
| 568 | mWaitingOnCollapseWhenGoingAway = false; |
| 569 | updateTouchableRegionListener(); |
| 570 | } |
| 571 | } |
| 572 | }); |
| 573 | } |
| 574 | |
Selim Cinek | 0fccc72 | 2015-07-29 17:04:36 -0700 | [diff] [blame] | 575 | public static void setIsClickedNotification(View child, boolean clicked) { |
| 576 | child.setTag(TAG_CLICKED_NOTIFICATION, clicked ? true : null); |
| 577 | } |
| 578 | |
| 579 | public static boolean isClickedHeadsUpNotification(View child) { |
| 580 | Boolean clicked = (Boolean) child.getTag(TAG_CLICKED_NOTIFICATION); |
| 581 | return clicked != null && clicked; |
| 582 | } |
| 583 | |
Adrian Roos | 1c0ca50 | 2015-10-07 12:20:42 -0700 | [diff] [blame] | 584 | public void setRemoteInputActive(NotificationData.Entry entry, boolean remoteInputActive) { |
| 585 | HeadsUpEntry headsUpEntry = mHeadsUpEntries.get(entry.key); |
| 586 | if (headsUpEntry != null && headsUpEntry.remoteInputActive != remoteInputActive) { |
| 587 | headsUpEntry.remoteInputActive = remoteInputActive; |
| 588 | if (remoteInputActive) { |
| 589 | headsUpEntry.removeAutoRemovalCallbacks(); |
| 590 | } else { |
| 591 | headsUpEntry.updateEntry(false /* updatePostTime */); |
| 592 | } |
| 593 | } |
| 594 | } |
| 595 | |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 596 | /** |
Selim Cinek | 31aada4 | 2015-12-18 17:51:15 -0800 | [diff] [blame] | 597 | * Set an entry to be expanded and therefore stick in the heads up area if it's pinned |
| 598 | * until it's collapsed again. |
| 599 | */ |
| 600 | public void setExpanded(NotificationData.Entry entry, boolean expanded) { |
| 601 | HeadsUpEntry headsUpEntry = mHeadsUpEntries.get(entry.key); |
| 602 | if (headsUpEntry != null && headsUpEntry.expanded != expanded) { |
| 603 | headsUpEntry.expanded = expanded; |
| 604 | if (expanded) { |
| 605 | headsUpEntry.removeAutoRemovalCallbacks(); |
| 606 | } else { |
| 607 | headsUpEntry.updateEntry(false /* updatePostTime */); |
| 608 | } |
| 609 | } |
| 610 | } |
| 611 | |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 612 | @Override |
| 613 | public void onReorderingAllowed() { |
| 614 | for (NotificationData.Entry entry : mEntriesToRemoveWhenReorderingAllowed) { |
| 615 | if (isHeadsUp(entry.key)) { |
| 616 | // Maybe the heads-up was removed already |
| 617 | removeHeadsUpEntry(entry); |
| 618 | } |
| 619 | } |
| 620 | mEntriesToRemoveWhenReorderingAllowed.clear(); |
| 621 | } |
| 622 | |
| 623 | public void setVisualStabilityManager(VisualStabilityManager visualStabilityManager) { |
| 624 | mVisualStabilityManager = visualStabilityManager; |
| 625 | } |
| 626 | |
Selim Cinek | 31aada4 | 2015-12-18 17:51:15 -0800 | [diff] [blame] | 627 | /** |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 628 | * This represents a notification and how long it is in a heads up mode. It also manages its |
| 629 | * lifecycle automatically when created. |
| 630 | */ |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 631 | public class HeadsUpEntry implements Comparable<HeadsUpEntry> { |
| 632 | public NotificationData.Entry entry; |
| 633 | public long postTime; |
| 634 | public long earliestRemovaltime; |
| 635 | private Runnable mRemoveHeadsUpRunnable; |
Adrian Roos | 1c0ca50 | 2015-10-07 12:20:42 -0700 | [diff] [blame] | 636 | public boolean remoteInputActive; |
Selim Cinek | 31aada4 | 2015-12-18 17:51:15 -0800 | [diff] [blame] | 637 | public boolean expanded; |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 638 | |
| 639 | public void setEntry(final NotificationData.Entry entry) { |
| 640 | this.entry = entry; |
| 641 | |
| 642 | // The actual post time will be just after the heads-up really slided in |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 643 | postTime = mClock.currentTimeMillis() + mTouchAcceptanceDelay; |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 644 | mRemoveHeadsUpRunnable = new Runnable() { |
| 645 | @Override |
| 646 | public void run() { |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 647 | if (!mVisualStabilityManager.isReorderingAllowed()) { |
| 648 | mEntriesToRemoveWhenReorderingAllowed.add(entry); |
| 649 | mVisualStabilityManager.addReorderingAllowedCallback(HeadsUpManager.this); |
| 650 | } else if (!mTrackingHeadsUp) { |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 651 | removeHeadsUpEntry(entry); |
| 652 | } else { |
| 653 | mEntriesToRemoveAfterExpand.add(entry); |
| 654 | } |
| 655 | } |
| 656 | }; |
| 657 | updateEntry(); |
| 658 | } |
| 659 | |
| 660 | public void updateEntry() { |
Adrian Roos | 1c0ca50 | 2015-10-07 12:20:42 -0700 | [diff] [blame] | 661 | updateEntry(true); |
| 662 | } |
| 663 | |
| 664 | public void updateEntry(boolean updatePostTime) { |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 665 | long currentTime = mClock.currentTimeMillis(); |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 666 | earliestRemovaltime = currentTime + mMinimumDisplayTime; |
Adrian Roos | 1c0ca50 | 2015-10-07 12:20:42 -0700 | [diff] [blame] | 667 | if (updatePostTime) { |
| 668 | postTime = Math.max(postTime, currentTime); |
| 669 | } |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 670 | removeAutoRemovalCallbacks(); |
Selim Cinek | c9e6834 | 2015-11-06 11:33:20 -0800 | [diff] [blame] | 671 | if (mEntriesToRemoveAfterExpand.contains(entry)) { |
| 672 | mEntriesToRemoveAfterExpand.remove(entry); |
| 673 | } |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 674 | if (mEntriesToRemoveWhenReorderingAllowed.contains(entry)) { |
| 675 | mEntriesToRemoveWhenReorderingAllowed.remove(entry); |
| 676 | } |
Selim Cinek | 31aada4 | 2015-12-18 17:51:15 -0800 | [diff] [blame] | 677 | if (!isSticky()) { |
Selim Cinek | 31d9ef7 | 2015-04-15 19:29:49 -0700 | [diff] [blame] | 678 | long finishTime = postTime + mHeadsUpNotificationDecay; |
| 679 | long removeDelay = Math.max(finishTime - currentTime, mMinimumDisplayTime); |
| 680 | mHandler.postDelayed(mRemoveHeadsUpRunnable, removeDelay); |
| 681 | } |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 682 | } |
| 683 | |
Selim Cinek | 31aada4 | 2015-12-18 17:51:15 -0800 | [diff] [blame] | 684 | private boolean isSticky() { |
| 685 | return (entry.row.isPinned() && expanded) |
| 686 | || remoteInputActive || hasFullScreenIntent(entry); |
| 687 | } |
| 688 | |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 689 | @Override |
| 690 | public int compareTo(HeadsUpEntry o) { |
Selim Cinek | 3362c13 | 2016-02-11 15:43:03 -0800 | [diff] [blame] | 691 | boolean isPinned = entry.row.isPinned(); |
| 692 | boolean otherPinned = o.entry.row.isPinned(); |
| 693 | if (isPinned && !otherPinned) { |
| 694 | return -1; |
| 695 | } else if (!isPinned && otherPinned) { |
| 696 | return 1; |
| 697 | } |
Selim Cinek | 2ae7107 | 2015-10-20 16:07:12 -0700 | [diff] [blame] | 698 | boolean selfFullscreen = hasFullScreenIntent(entry); |
| 699 | boolean otherFullscreen = hasFullScreenIntent(o.entry); |
| 700 | if (selfFullscreen && !otherFullscreen) { |
| 701 | return -1; |
| 702 | } else if (!selfFullscreen && otherFullscreen) { |
| 703 | return 1; |
| 704 | } |
Adrian Roos | e211f57 | 2016-04-08 14:24:20 -0700 | [diff] [blame] | 705 | |
| 706 | if (remoteInputActive && !o.remoteInputActive) { |
| 707 | return -1; |
| 708 | } else if (!remoteInputActive && o.remoteInputActive) { |
| 709 | return 1; |
| 710 | } |
| 711 | |
Selim Cinek | a59ecc3 | 2015-04-07 10:51:49 -0700 | [diff] [blame] | 712 | return postTime < o.postTime ? 1 |
Selim Cinek | f87baef | 2015-06-09 15:56:24 -0700 | [diff] [blame] | 713 | : postTime == o.postTime ? entry.key.compareTo(o.entry.key) |
Selim Cinek | a59ecc3 | 2015-04-07 10:51:49 -0700 | [diff] [blame] | 714 | : -1; |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 715 | } |
| 716 | |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 717 | public void removeAutoRemovalCallbacks() { |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 718 | mHandler.removeCallbacks(mRemoveHeadsUpRunnable); |
| 719 | } |
| 720 | |
| 721 | public boolean wasShownLongEnough() { |
| 722 | return earliestRemovaltime < mClock.currentTimeMillis(); |
| 723 | } |
| 724 | |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 725 | public void removeAsSoonAsPossible() { |
| 726 | removeAutoRemovalCallbacks(); |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 727 | mHandler.postDelayed(mRemoveHeadsUpRunnable, |
| 728 | earliestRemovaltime - mClock.currentTimeMillis()); |
| 729 | } |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 730 | |
| 731 | public void reset() { |
| 732 | removeAutoRemovalCallbacks(); |
| 733 | entry = null; |
| 734 | mRemoveHeadsUpRunnable = null; |
Selim Cinek | 31aada4 | 2015-12-18 17:51:15 -0800 | [diff] [blame] | 735 | expanded = false; |
| 736 | remoteInputActive = false; |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 737 | } |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 738 | } |
| 739 | |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 740 | public static class Clock { |
| 741 | public long currentTimeMillis() { |
| 742 | return SystemClock.elapsedRealtime(); |
| 743 | } |
| 744 | } |
| 745 | |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 746 | } |