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 | |
yoshiki iguchi | 4e30e76 | 2018-02-06 12:09:23 +0900 | [diff] [blame] | 19 | import android.annotation.NonNull; |
| 20 | import android.annotation.Nullable; |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 21 | import android.content.Context; |
| 22 | import android.content.res.Resources; |
| 23 | import android.database.ContentObserver; |
yoshiki iguchi | 4e30e76 | 2018-02-06 12:09:23 +0900 | [diff] [blame] | 24 | import android.os.SystemClock; |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 25 | import android.os.Handler; |
Selim Cinek | 5cf1d05 | 2017-06-01 17:36:46 -0700 | [diff] [blame] | 26 | import android.os.Looper; |
yoshiki iguchi | 0adf6a6 | 2018-02-01 13:46:26 +0900 | [diff] [blame] | 27 | import android.util.ArrayMap; |
yoshiki iguchi | 4e30e76 | 2018-02-06 12:09:23 +0900 | [diff] [blame] | 28 | import android.provider.Settings; |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 29 | import android.util.Log; |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 30 | import android.view.accessibility.AccessibilityEvent; |
| 31 | |
Chris Wren | b659c4f | 2015-06-25 17:12:27 -0400 | [diff] [blame] | 32 | import com.android.internal.logging.MetricsLogger; |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 33 | import com.android.systemui.R; |
Selim Cinek | a59ecc3 | 2015-04-07 10:51:49 -0700 | [diff] [blame] | 34 | import com.android.systemui.statusbar.ExpandableNotificationRow; |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 35 | import com.android.systemui.statusbar.NotificationData; |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 36 | |
| 37 | import java.io.FileDescriptor; |
| 38 | import java.io.PrintWriter; |
yoshiki iguchi | 4e30e76 | 2018-02-06 12:09:23 +0900 | [diff] [blame] | 39 | import java.util.Iterator; |
| 40 | import java.util.stream.Stream; |
Selim Cinek | a59ecc3 | 2015-04-07 10:51:49 -0700 | [diff] [blame] | 41 | import java.util.HashMap; |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 42 | import java.util.HashSet; |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 43 | |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 44 | /** |
| 45 | * A manager which handles heads up notifications which is a special mode where |
| 46 | * they simply peek from the top of the screen. |
| 47 | */ |
yoshiki iguchi | 4e30e76 | 2018-02-06 12:09:23 +0900 | [diff] [blame] | 48 | public class HeadsUpManager { |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 49 | private static final String TAG = "HeadsUpManager"; |
| 50 | private static final boolean DEBUG = false; |
| 51 | private static final String SETTING_HEADS_UP_SNOOZE_LENGTH_MS = "heads_up_snooze_length_ms"; |
| 52 | |
yoshiki iguchi | 4e30e76 | 2018-02-06 12:09:23 +0900 | [diff] [blame] | 53 | protected final Clock mClock = new Clock(); |
| 54 | protected final HashSet<OnHeadsUpChangedListener> mListeners = new HashSet<>(); |
| 55 | protected final Handler mHandler = new Handler(Looper.getMainLooper()); |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 56 | |
yoshiki iguchi | 4e30e76 | 2018-02-06 12:09:23 +0900 | [diff] [blame] | 57 | protected final Context mContext; |
| 58 | |
| 59 | protected int mHeadsUpNotificationDecay; |
| 60 | protected int mMinimumDisplayTime; |
| 61 | protected int mTouchAcceptanceDelay; |
| 62 | protected int mSnoozeLengthMs; |
| 63 | protected boolean mHasPinnedNotification; |
| 64 | protected int mUser; |
| 65 | |
| 66 | private final HashMap<String, HeadsUpEntry> mHeadsUpEntries = new HashMap<>(); |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 67 | private final ArrayMap<String, Long> mSnoozedPackages; |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 68 | |
yoshiki iguchi | 4e30e76 | 2018-02-06 12:09:23 +0900 | [diff] [blame] | 69 | public HeadsUpManager(@NonNull final Context context) { |
Chris Wren | b659c4f | 2015-06-25 17:12:27 -0400 | [diff] [blame] | 70 | mContext = context; |
yoshiki iguchi | 4e30e76 | 2018-02-06 12:09:23 +0900 | [diff] [blame] | 71 | Resources resources = context.getResources(); |
yoshiki iguchi | 0adf6a6 | 2018-02-01 13:46:26 +0900 | [diff] [blame] | 72 | mMinimumDisplayTime = resources.getInteger(R.integer.heads_up_notification_minimum_time); |
| 73 | mHeadsUpNotificationDecay = resources.getInteger(R.integer.heads_up_notification_decay); |
yoshiki iguchi | 4e30e76 | 2018-02-06 12:09:23 +0900 | [diff] [blame] | 74 | mTouchAcceptanceDelay = resources.getInteger(R.integer.touch_acceptance_delay); |
| 75 | mSnoozedPackages = new ArrayMap<>(); |
| 76 | int defaultSnoozeLengthMs = |
| 77 | resources.getInteger(R.integer.heads_up_default_snooze_length_ms); |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 78 | |
| 79 | mSnoozeLengthMs = Settings.Global.getInt(context.getContentResolver(), |
yoshiki iguchi | 4e30e76 | 2018-02-06 12:09:23 +0900 | [diff] [blame] | 80 | SETTING_HEADS_UP_SNOOZE_LENGTH_MS, defaultSnoozeLengthMs); |
| 81 | ContentObserver settingsObserver = new ContentObserver(mHandler) { |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 82 | @Override |
| 83 | public void onChange(boolean selfChange) { |
| 84 | final int packageSnoozeLengthMs = Settings.Global.getInt( |
| 85 | context.getContentResolver(), SETTING_HEADS_UP_SNOOZE_LENGTH_MS, -1); |
| 86 | if (packageSnoozeLengthMs > -1 && packageSnoozeLengthMs != mSnoozeLengthMs) { |
| 87 | mSnoozeLengthMs = packageSnoozeLengthMs; |
| 88 | if (DEBUG) Log.v(TAG, "mSnoozeLengthMs = " + mSnoozeLengthMs); |
| 89 | } |
| 90 | } |
| 91 | }; |
| 92 | context.getContentResolver().registerContentObserver( |
| 93 | Settings.Global.getUriFor(SETTING_HEADS_UP_SNOOZE_LENGTH_MS), false, |
yoshiki iguchi | 4e30e76 | 2018-02-06 12:09:23 +0900 | [diff] [blame] | 94 | settingsObserver); |
Selim Cinek | 737bff3 | 2015-05-08 16:08:35 -0700 | [diff] [blame] | 95 | } |
| 96 | |
yoshiki iguchi | 4e30e76 | 2018-02-06 12:09:23 +0900 | [diff] [blame] | 97 | /** |
| 98 | * Adds an OnHeadUpChangedListener to observe events. |
| 99 | */ |
| 100 | public void addListener(@NonNull OnHeadsUpChangedListener listener) { |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 101 | mListeners.add(listener); |
| 102 | } |
| 103 | |
yoshiki iguchi | 4e30e76 | 2018-02-06 12:09:23 +0900 | [diff] [blame] | 104 | /** |
| 105 | * Removes the OnHeadUpChangedListener from the observer list. |
| 106 | */ |
| 107 | public void removeListener(@NonNull OnHeadsUpChangedListener listener) { |
Jason Monk | 7ebba0b | 2017-04-20 14:10:20 -0400 | [diff] [blame] | 108 | mListeners.remove(listener); |
| 109 | } |
| 110 | |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 111 | /** |
| 112 | * Called when posting a new notification to the heads up. |
| 113 | */ |
yoshiki iguchi | 4e30e76 | 2018-02-06 12:09:23 +0900 | [diff] [blame] | 114 | public void showNotification(@NonNull NotificationData.Entry headsUp) { |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 115 | if (DEBUG) Log.v(TAG, "showNotification"); |
| 116 | addHeadsUpEntry(headsUp); |
| 117 | updateNotification(headsUp, true); |
| 118 | headsUp.setInterruption(); |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Called when updating or posting a notification to the heads up. |
| 123 | */ |
yoshiki iguchi | 4e30e76 | 2018-02-06 12:09:23 +0900 | [diff] [blame] | 124 | public void updateNotification(@NonNull NotificationData.Entry headsUp, boolean alert) { |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 125 | if (DEBUG) Log.v(TAG, "updateNotification"); |
| 126 | |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 127 | headsUp.row.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED); |
| 128 | |
| 129 | if (alert) { |
| 130 | HeadsUpEntry headsUpEntry = mHeadsUpEntries.get(headsUp.key); |
Selim Cinek | 967ed2a | 2016-04-08 18:29:11 -0700 | [diff] [blame] | 131 | if (headsUpEntry == null) { |
| 132 | // the entry was released before this update (i.e by a listener) This can happen |
| 133 | // with the groupmanager |
| 134 | return; |
| 135 | } |
yoshiki iguchi | 4e30e76 | 2018-02-06 12:09:23 +0900 | [diff] [blame] | 136 | headsUpEntry.updateEntry(true /* updatePostTime */); |
Selim Cinek | 131c1e2 | 2015-05-11 19:04:49 -0700 | [diff] [blame] | 137 | setEntryPinned(headsUpEntry, shouldHeadsUpBecomePinned(headsUp)); |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
yoshiki iguchi | 4e30e76 | 2018-02-06 12:09:23 +0900 | [diff] [blame] | 141 | private void addHeadsUpEntry(@NonNull NotificationData.Entry entry) { |
| 142 | HeadsUpEntry headsUpEntry = createHeadsUpEntry(); |
Selim Cinek | a59ecc3 | 2015-04-07 10:51:49 -0700 | [diff] [blame] | 143 | // This will also add the entry to the sortedList |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 144 | headsUpEntry.setEntry(entry); |
| 145 | mHeadsUpEntries.put(entry.key, headsUpEntry); |
Selim Cinek | a59ecc3 | 2015-04-07 10:51:49 -0700 | [diff] [blame] | 146 | entry.row.setHeadsUp(true); |
Selim Cinek | 131c1e2 | 2015-05-11 19:04:49 -0700 | [diff] [blame] | 147 | setEntryPinned(headsUpEntry, shouldHeadsUpBecomePinned(entry)); |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 148 | for (OnHeadsUpChangedListener listener : mListeners) { |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 149 | listener.onHeadsUpStateChanged(entry, true); |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 150 | } |
| 151 | entry.row.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED); |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 152 | } |
| 153 | |
yoshiki iguchi | 4e30e76 | 2018-02-06 12:09:23 +0900 | [diff] [blame] | 154 | protected boolean shouldHeadsUpBecomePinned(@NonNull NotificationData.Entry entry) { |
| 155 | return hasFullScreenIntent(entry); |
Selim Cinek | 131c1e2 | 2015-05-11 19:04:49 -0700 | [diff] [blame] | 156 | } |
| 157 | |
yoshiki iguchi | 4e30e76 | 2018-02-06 12:09:23 +0900 | [diff] [blame] | 158 | protected boolean hasFullScreenIntent(@NonNull NotificationData.Entry entry) { |
Selim Cinek | 131c1e2 | 2015-05-11 19:04:49 -0700 | [diff] [blame] | 159 | return entry.notification.getNotification().fullScreenIntent != null; |
| 160 | } |
| 161 | |
yoshiki iguchi | 4e30e76 | 2018-02-06 12:09:23 +0900 | [diff] [blame] | 162 | protected void setEntryPinned( |
| 163 | @NonNull HeadsUpManager.HeadsUpEntry headsUpEntry, boolean isPinned) { |
| 164 | if (DEBUG) Log.v(TAG, "setEntryPinned: " + isPinned); |
Selim Cinek | 1f3f544 | 2015-04-10 17:54:46 -0700 | [diff] [blame] | 165 | ExpandableNotificationRow row = headsUpEntry.entry.row; |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 166 | if (row.isPinned() != isPinned) { |
| 167 | row.setPinned(isPinned); |
| 168 | updatePinnedMode(); |
| 169 | for (OnHeadsUpChangedListener listener : mListeners) { |
| 170 | if (isPinned) { |
| 171 | listener.onHeadsUpPinned(row); |
| 172 | } else { |
| 173 | listener.onHeadsUpUnPinned(row); |
Selim Cinek | 1f3f544 | 2015-04-10 17:54:46 -0700 | [diff] [blame] | 174 | } |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
yoshiki iguchi | 4e30e76 | 2018-02-06 12:09:23 +0900 | [diff] [blame] | 179 | protected void removeHeadsUpEntry(@NonNull NotificationData.Entry entry) { |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 180 | HeadsUpEntry remove = mHeadsUpEntries.remove(entry.key); |
yoshiki iguchi | 4e30e76 | 2018-02-06 12:09:23 +0900 | [diff] [blame] | 181 | onHeadsUpEntryRemoved(remove); |
| 182 | } |
| 183 | |
| 184 | protected void onHeadsUpEntryRemoved(@NonNull HeadsUpEntry remove) { |
| 185 | NotificationData.Entry entry = remove.entry; |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 186 | entry.row.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED); |
| 187 | entry.row.setHeadsUp(false); |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 188 | setEntryPinned(remove, false /* isPinned */); |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 189 | for (OnHeadsUpChangedListener listener : mListeners) { |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 190 | listener.onHeadsUpStateChanged(entry, false); |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 191 | } |
yoshiki iguchi | 4e30e76 | 2018-02-06 12:09:23 +0900 | [diff] [blame] | 192 | releaseHeadsUpEntry(remove); |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 193 | } |
| 194 | |
yoshiki iguchi | 4e30e76 | 2018-02-06 12:09:23 +0900 | [diff] [blame] | 195 | protected void updatePinnedMode() { |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 196 | boolean hasPinnedNotification = hasPinnedNotificationInternal(); |
| 197 | if (hasPinnedNotification == mHasPinnedNotification) { |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 198 | return; |
| 199 | } |
yoshiki iguchi | 4e30e76 | 2018-02-06 12:09:23 +0900 | [diff] [blame] | 200 | if (DEBUG) { |
| 201 | Log.v(TAG, "Pinned mode changed: " + mHasPinnedNotification + " -> " + |
| 202 | hasPinnedNotification); |
| 203 | } |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 204 | mHasPinnedNotification = hasPinnedNotification; |
Chris Wren | 063926b | 2015-10-16 16:24:20 -0400 | [diff] [blame] | 205 | if (mHasPinnedNotification) { |
| 206 | MetricsLogger.count(mContext, "note_peek", 1); |
| 207 | } |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 208 | for (OnHeadsUpChangedListener listener : mListeners) { |
John Spurlock | b349af57 | 2015-04-29 12:24:19 -0400 | [diff] [blame] | 209 | listener.onHeadsUpPinnedModeChanged(hasPinnedNotification); |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 210 | } |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * React to the removal of the notification in the heads up. |
| 215 | * |
| 216 | * @return true if the notification was removed and false if it still needs to be kept around |
| 217 | * for a bit since it wasn't shown long enough |
| 218 | */ |
yoshiki iguchi | 4e30e76 | 2018-02-06 12:09:23 +0900 | [diff] [blame] | 219 | public boolean removeNotification(@NonNull String key, boolean ignoreEarliestRemovalTime) { |
| 220 | if (DEBUG) Log.v(TAG, "removeNotification"); |
| 221 | releaseImmediately(key); |
| 222 | return true; |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 223 | } |
| 224 | |
yoshiki iguchi | 4e30e76 | 2018-02-06 12:09:23 +0900 | [diff] [blame] | 225 | /** |
| 226 | * Returns if the given notification is in the Heads Up Notification list or not. |
| 227 | */ |
| 228 | public boolean isHeadsUp(@NonNull String key) { |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 229 | return mHeadsUpEntries.containsKey(key); |
| 230 | } |
| 231 | |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 232 | /** |
yoshiki iguchi | 4e30e76 | 2018-02-06 12:09:23 +0900 | [diff] [blame] | 233 | * Pushes any current Heads Up notification down into the shade. |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 234 | */ |
| 235 | public void releaseAllImmediately() { |
| 236 | if (DEBUG) Log.v(TAG, "releaseAllImmediately"); |
yoshiki iguchi | 4e30e76 | 2018-02-06 12:09:23 +0900 | [diff] [blame] | 237 | Iterator<HeadsUpEntry> iterator = mHeadsUpEntries.values().iterator(); |
| 238 | while (iterator.hasNext()) { |
| 239 | HeadsUpEntry entry = iterator.next(); |
| 240 | iterator.remove(); |
| 241 | onHeadsUpEntryRemoved(entry); |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 242 | } |
| 243 | } |
| 244 | |
yoshiki iguchi | 4e30e76 | 2018-02-06 12:09:23 +0900 | [diff] [blame] | 245 | /** |
| 246 | * Pushes the given Heads Up notification down into the shade. |
| 247 | */ |
| 248 | public void releaseImmediately(@NonNull String key) { |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 249 | HeadsUpEntry headsUpEntry = getHeadsUpEntry(key); |
| 250 | if (headsUpEntry == null) { |
| 251 | return; |
| 252 | } |
| 253 | NotificationData.Entry shadeEntry = headsUpEntry.entry; |
| 254 | removeHeadsUpEntry(shadeEntry); |
| 255 | } |
| 256 | |
yoshiki iguchi | 4e30e76 | 2018-02-06 12:09:23 +0900 | [diff] [blame] | 257 | /** |
| 258 | * Returns if the given notification is snoozed or not. |
| 259 | */ |
| 260 | public boolean isSnoozed(@NonNull String packageName) { |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 261 | final String key = snoozeKey(packageName, mUser); |
| 262 | Long snoozedUntil = mSnoozedPackages.get(key); |
| 263 | if (snoozedUntil != null) { |
yoshiki iguchi | 4e30e76 | 2018-02-06 12:09:23 +0900 | [diff] [blame] | 264 | if (snoozedUntil > mClock.currentTimeMillis()) { |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 265 | if (DEBUG) Log.v(TAG, key + " snoozed"); |
| 266 | return true; |
| 267 | } |
| 268 | mSnoozedPackages.remove(packageName); |
| 269 | } |
| 270 | return false; |
| 271 | } |
| 272 | |
yoshiki iguchi | 4e30e76 | 2018-02-06 12:09:23 +0900 | [diff] [blame] | 273 | /** |
| 274 | * Snoozes all current Heads Up Notifications. |
| 275 | */ |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 276 | public void snooze() { |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 277 | for (String key : mHeadsUpEntries.keySet()) { |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 278 | HeadsUpEntry entry = mHeadsUpEntries.get(key); |
| 279 | String packageName = entry.entry.notification.getPackageName(); |
| 280 | mSnoozedPackages.put(snoozeKey(packageName, mUser), |
yoshiki iguchi | 4e30e76 | 2018-02-06 12:09:23 +0900 | [diff] [blame] | 281 | mClock.currentTimeMillis() + mSnoozeLengthMs); |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 282 | } |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 283 | } |
| 284 | |
yoshiki iguchi | 4e30e76 | 2018-02-06 12:09:23 +0900 | [diff] [blame] | 285 | @NonNull |
| 286 | private static String snoozeKey(@NonNull String packageName, int user) { |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 287 | return user + "," + packageName; |
| 288 | } |
| 289 | |
yoshiki iguchi | 4e30e76 | 2018-02-06 12:09:23 +0900 | [diff] [blame] | 290 | @Nullable |
| 291 | protected HeadsUpEntry getHeadsUpEntry(@NonNull String key) { |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 292 | return mHeadsUpEntries.get(key); |
| 293 | } |
| 294 | |
yoshiki iguchi | 4e30e76 | 2018-02-06 12:09:23 +0900 | [diff] [blame] | 295 | /** |
| 296 | * Returns the entry of given Heads Up Notification. |
| 297 | * |
| 298 | * @param key Key of heads up notification |
| 299 | */ |
| 300 | @Nullable |
| 301 | public NotificationData.Entry getEntry(@NonNull String key) { |
| 302 | HeadsUpEntry entry = mHeadsUpEntries.get(key); |
| 303 | return entry != null ? entry.entry : null; |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 304 | } |
| 305 | |
yoshiki iguchi | 4e30e76 | 2018-02-06 12:09:23 +0900 | [diff] [blame] | 306 | /** |
| 307 | * Returns the stream of all current Heads Up Notifications. |
| 308 | */ |
| 309 | @NonNull |
| 310 | public Stream<NotificationData.Entry> getAllEntries() { |
| 311 | return mHeadsUpEntries.values().stream().map(headsUpEntry -> headsUpEntry.entry); |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 312 | } |
| 313 | |
yoshiki iguchi | 4e30e76 | 2018-02-06 12:09:23 +0900 | [diff] [blame] | 314 | /** |
| 315 | * Returns the top Heads Up Notification, which appeares to show at first. |
| 316 | */ |
| 317 | @Nullable |
| 318 | public NotificationData.Entry getTopEntry() { |
| 319 | HeadsUpEntry topEntry = getTopHeadsUpEntry(); |
| 320 | return (topEntry != null) ? topEntry.entry : null; |
| 321 | } |
| 322 | |
| 323 | /** |
| 324 | * Returns if any heads up notification is available or not. |
| 325 | */ |
| 326 | public boolean hasHeadsUpNotifications() { |
| 327 | return !mHeadsUpEntries.isEmpty(); |
| 328 | } |
| 329 | |
| 330 | @Nullable |
| 331 | protected HeadsUpEntry getTopHeadsUpEntry() { |
Selim Cinek | 3362c13 | 2016-02-11 15:43:03 -0800 | [diff] [blame] | 332 | if (mHeadsUpEntries.isEmpty()) { |
| 333 | return null; |
| 334 | } |
| 335 | HeadsUpEntry topEntry = null; |
| 336 | for (HeadsUpEntry entry: mHeadsUpEntries.values()) { |
yoshiki iguchi | 4e30e76 | 2018-02-06 12:09:23 +0900 | [diff] [blame] | 337 | if (topEntry == null || entry.compareTo(topEntry) < 0) { |
Selim Cinek | 3362c13 | 2016-02-11 15:43:03 -0800 | [diff] [blame] | 338 | topEntry = entry; |
| 339 | } |
| 340 | } |
| 341 | return topEntry; |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 342 | } |
| 343 | |
| 344 | /** |
yoshiki iguchi | 4e30e76 | 2018-02-06 12:09:23 +0900 | [diff] [blame] | 345 | * Sets the current user. |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 346 | */ |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 347 | public void setUser(int user) { |
| 348 | mUser = user; |
| 349 | } |
| 350 | |
yoshiki iguchi | 4e30e76 | 2018-02-06 12:09:23 +0900 | [diff] [blame] | 351 | public void dump(@NonNull FileDescriptor fd, @NonNull PrintWriter pw, @NonNull String[] args) { |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 352 | pw.println("HeadsUpManager state:"); |
yoshiki iguchi | 4e30e76 | 2018-02-06 12:09:23 +0900 | [diff] [blame] | 353 | dumpInternal(fd, pw, args); |
| 354 | } |
| 355 | |
| 356 | protected void dumpInternal( |
| 357 | @NonNull FileDescriptor fd, @NonNull PrintWriter pw, @NonNull String[] args) { |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 358 | pw.print(" mTouchAcceptanceDelay="); pw.println(mTouchAcceptanceDelay); |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 359 | pw.print(" mSnoozeLengthMs="); pw.println(mSnoozeLengthMs); |
yoshiki iguchi | 4e30e76 | 2018-02-06 12:09:23 +0900 | [diff] [blame] | 360 | pw.print(" now="); pw.println(mClock.currentTimeMillis()); |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 361 | pw.print(" mUser="); pw.println(mUser); |
Selim Cinek | 3362c13 | 2016-02-11 15:43:03 -0800 | [diff] [blame] | 362 | for (HeadsUpEntry entry: mHeadsUpEntries.values()) { |
Selim Cinek | a59ecc3 | 2015-04-07 10:51:49 -0700 | [diff] [blame] | 363 | pw.print(" HeadsUpEntry="); pw.println(entry.entry); |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 364 | } |
| 365 | int N = mSnoozedPackages.size(); |
| 366 | pw.println(" snoozed packages: " + N); |
| 367 | for (int i = 0; i < N; i++) { |
| 368 | pw.print(" "); pw.print(mSnoozedPackages.valueAt(i)); |
| 369 | pw.print(", "); pw.println(mSnoozedPackages.keyAt(i)); |
| 370 | } |
| 371 | } |
| 372 | |
yoshiki iguchi | 4e30e76 | 2018-02-06 12:09:23 +0900 | [diff] [blame] | 373 | /** |
| 374 | * Returns if there are any pinned Heads Up Notifications or not. |
| 375 | */ |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 376 | public boolean hasPinnedHeadsUp() { |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 377 | return mHasPinnedNotification; |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 378 | } |
| 379 | |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 380 | private boolean hasPinnedNotificationInternal() { |
| 381 | for (String key : mHeadsUpEntries.keySet()) { |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 382 | HeadsUpEntry entry = mHeadsUpEntries.get(key); |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 383 | if (entry.entry.row.isPinned()) { |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 384 | return true; |
| 385 | } |
| 386 | } |
| 387 | return false; |
| 388 | } |
| 389 | |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 390 | /** |
yoshiki iguchi | 4e30e76 | 2018-02-06 12:09:23 +0900 | [diff] [blame] | 391 | * Unpins all pinned Heads Up Notifications. |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 392 | */ |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 393 | public void unpinAll() { |
| 394 | for (String key : mHeadsUpEntries.keySet()) { |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 395 | HeadsUpEntry entry = mHeadsUpEntries.get(key); |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 396 | setEntryPinned(entry, false /* isPinned */); |
Selim Cinek | 31aada4 | 2015-12-18 17:51:15 -0800 | [diff] [blame] | 397 | // maybe it got un sticky |
| 398 | entry.updateEntry(false /* updatePostTime */); |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 399 | } |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 400 | } |
| 401 | |
yoshiki iguchi | 0adf6a6 | 2018-02-01 13:46:26 +0900 | [diff] [blame] | 402 | /** |
yoshiki iguchi | 4e30e76 | 2018-02-06 12:09:23 +0900 | [diff] [blame] | 403 | * Returns the value of the tracking-heads-up flag. See the doc of {@code setTrackingHeadsUp} as |
| 404 | * well. |
yoshiki iguchi | 0adf6a6 | 2018-02-01 13:46:26 +0900 | [diff] [blame] | 405 | */ |
yoshiki iguchi | 4e30e76 | 2018-02-06 12:09:23 +0900 | [diff] [blame] | 406 | public boolean isTrackingHeadsUp() { |
| 407 | // Might be implemented in subclass. |
| 408 | return false; |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 409 | } |
| 410 | |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 411 | /** |
| 412 | * Compare two entries and decide how they should be ranked. |
| 413 | * |
| 414 | * @return -1 if the first argument should be ranked higher than the second, 1 if the second |
| 415 | * one should be ranked higher and 0 if they are equal. |
| 416 | */ |
yoshiki iguchi | 4e30e76 | 2018-02-06 12:09:23 +0900 | [diff] [blame] | 417 | public int compare(@NonNull NotificationData.Entry a, @NonNull NotificationData.Entry b) { |
Selim Cinek | fbe9a44 | 2015-04-13 16:09:49 -0700 | [diff] [blame] | 418 | HeadsUpEntry aEntry = getHeadsUpEntry(a.key); |
| 419 | HeadsUpEntry bEntry = getHeadsUpEntry(b.key); |
| 420 | if (aEntry == null || bEntry == null) { |
| 421 | return aEntry == null ? 1 : -1; |
| 422 | } |
| 423 | return aEntry.compareTo(bEntry); |
| 424 | } |
| 425 | |
Selim Cinek | 737bff3 | 2015-05-08 16:08:35 -0700 | [diff] [blame] | 426 | /** |
Selim Cinek | 31aada4 | 2015-12-18 17:51:15 -0800 | [diff] [blame] | 427 | * Set an entry to be expanded and therefore stick in the heads up area if it's pinned |
| 428 | * until it's collapsed again. |
| 429 | */ |
yoshiki iguchi | 4e30e76 | 2018-02-06 12:09:23 +0900 | [diff] [blame] | 430 | public void setExpanded(@NonNull NotificationData.Entry entry, boolean expanded) { |
| 431 | HeadsUpManager.HeadsUpEntry headsUpEntry = mHeadsUpEntries.get(entry.key); |
| 432 | if (headsUpEntry != null && entry.row.isPinned()) { |
| 433 | headsUpEntry.expanded(expanded); |
Selim Cinek | 31aada4 | 2015-12-18 17:51:15 -0800 | [diff] [blame] | 434 | } |
| 435 | } |
| 436 | |
yoshiki iguchi | 4e30e76 | 2018-02-06 12:09:23 +0900 | [diff] [blame] | 437 | @NonNull |
| 438 | protected HeadsUpEntry createHeadsUpEntry() { |
| 439 | return new HeadsUpEntry(); |
Selim Cinek | a7d4f82 | 2016-12-06 14:34:47 -0800 | [diff] [blame] | 440 | } |
| 441 | |
yoshiki iguchi | 4e30e76 | 2018-02-06 12:09:23 +0900 | [diff] [blame] | 442 | protected void releaseHeadsUpEntry(@NonNull HeadsUpEntry entry) { |
| 443 | entry.reset(); |
Selim Cinek | 01ee2cd5 | 2016-12-21 18:23:11 +0100 | [diff] [blame] | 444 | } |
| 445 | |
Selim Cinek | 31aada4 | 2015-12-18 17:51:15 -0800 | [diff] [blame] | 446 | /** |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 447 | * This represents a notification and how long it is in a heads up mode. It also manages its |
| 448 | * lifecycle automatically when created. |
| 449 | */ |
yoshiki iguchi | 4e30e76 | 2018-02-06 12:09:23 +0900 | [diff] [blame] | 450 | protected class HeadsUpEntry implements Comparable<HeadsUpEntry> { |
| 451 | @Nullable public NotificationData.Entry entry; |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 452 | public long postTime; |
yoshiki iguchi | 0adf6a6 | 2018-02-01 13:46:26 +0900 | [diff] [blame] | 453 | public boolean remoteInputActive; |
yoshiki iguchi | 4e30e76 | 2018-02-06 12:09:23 +0900 | [diff] [blame] | 454 | public long earliestRemovaltime; |
Selim Cinek | 31aada4 | 2015-12-18 17:51:15 -0800 | [diff] [blame] | 455 | public boolean expanded; |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 456 | |
yoshiki iguchi | 4e30e76 | 2018-02-06 12:09:23 +0900 | [diff] [blame] | 457 | @Nullable private Runnable mRemoveHeadsUpRunnable; |
| 458 | |
| 459 | public void setEntry(@Nullable final NotificationData.Entry entry) { |
| 460 | setEntry(entry, null); |
| 461 | } |
| 462 | |
| 463 | public void setEntry(@Nullable final NotificationData.Entry entry, |
| 464 | @Nullable Runnable removeHeadsUpRunnable) { |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 465 | this.entry = entry; |
yoshiki iguchi | 4e30e76 | 2018-02-06 12:09:23 +0900 | [diff] [blame] | 466 | this.mRemoveHeadsUpRunnable = removeHeadsUpRunnable; |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 467 | |
| 468 | // 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] | 469 | postTime = mClock.currentTimeMillis() + mTouchAcceptanceDelay; |
yoshiki iguchi | 4e30e76 | 2018-02-06 12:09:23 +0900 | [diff] [blame] | 470 | updateEntry(true /* updatePostTime */); |
Adrian Roos | 1c0ca50 | 2015-10-07 12:20:42 -0700 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | public void updateEntry(boolean updatePostTime) { |
yoshiki iguchi | 4e30e76 | 2018-02-06 12:09:23 +0900 | [diff] [blame] | 474 | if (DEBUG) Log.v(TAG, "updateEntry"); |
| 475 | |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 476 | long currentTime = mClock.currentTimeMillis(); |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 477 | earliestRemovaltime = currentTime + mMinimumDisplayTime; |
Adrian Roos | 1c0ca50 | 2015-10-07 12:20:42 -0700 | [diff] [blame] | 478 | if (updatePostTime) { |
| 479 | postTime = Math.max(postTime, currentTime); |
| 480 | } |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 481 | removeAutoRemovalCallbacks(); |
yoshiki iguchi | 4e30e76 | 2018-02-06 12:09:23 +0900 | [diff] [blame] | 482 | |
Selim Cinek | 31aada4 | 2015-12-18 17:51:15 -0800 | [diff] [blame] | 483 | if (!isSticky()) { |
Selim Cinek | 31d9ef7 | 2015-04-15 19:29:49 -0700 | [diff] [blame] | 484 | long finishTime = postTime + mHeadsUpNotificationDecay; |
| 485 | long removeDelay = Math.max(finishTime - currentTime, mMinimumDisplayTime); |
| 486 | mHandler.postDelayed(mRemoveHeadsUpRunnable, removeDelay); |
| 487 | } |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 488 | } |
| 489 | |
Selim Cinek | 31aada4 | 2015-12-18 17:51:15 -0800 | [diff] [blame] | 490 | private boolean isSticky() { |
| 491 | return (entry.row.isPinned() && expanded) |
| 492 | || remoteInputActive || hasFullScreenIntent(entry); |
| 493 | } |
| 494 | |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 495 | @Override |
yoshiki iguchi | 4e30e76 | 2018-02-06 12:09:23 +0900 | [diff] [blame] | 496 | public int compareTo(@NonNull HeadsUpEntry o) { |
Selim Cinek | 3362c13 | 2016-02-11 15:43:03 -0800 | [diff] [blame] | 497 | boolean isPinned = entry.row.isPinned(); |
| 498 | boolean otherPinned = o.entry.row.isPinned(); |
| 499 | if (isPinned && !otherPinned) { |
| 500 | return -1; |
| 501 | } else if (!isPinned && otherPinned) { |
| 502 | return 1; |
| 503 | } |
Selim Cinek | 2ae7107 | 2015-10-20 16:07:12 -0700 | [diff] [blame] | 504 | boolean selfFullscreen = hasFullScreenIntent(entry); |
| 505 | boolean otherFullscreen = hasFullScreenIntent(o.entry); |
| 506 | if (selfFullscreen && !otherFullscreen) { |
| 507 | return -1; |
| 508 | } else if (!selfFullscreen && otherFullscreen) { |
| 509 | return 1; |
| 510 | } |
Adrian Roos | e211f57 | 2016-04-08 14:24:20 -0700 | [diff] [blame] | 511 | |
| 512 | if (remoteInputActive && !o.remoteInputActive) { |
| 513 | return -1; |
| 514 | } else if (!remoteInputActive && o.remoteInputActive) { |
| 515 | return 1; |
| 516 | } |
| 517 | |
Selim Cinek | a59ecc3 | 2015-04-07 10:51:49 -0700 | [diff] [blame] | 518 | return postTime < o.postTime ? 1 |
Selim Cinek | f87baef | 2015-06-09 15:56:24 -0700 | [diff] [blame] | 519 | : postTime == o.postTime ? entry.key.compareTo(o.entry.key) |
Selim Cinek | a59ecc3 | 2015-04-07 10:51:49 -0700 | [diff] [blame] | 520 | : -1; |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 521 | } |
| 522 | |
yoshiki iguchi | 4e30e76 | 2018-02-06 12:09:23 +0900 | [diff] [blame] | 523 | public void expanded(boolean expanded) { |
| 524 | this.expanded = expanded; |
yoshiki iguchi | 0adf6a6 | 2018-02-01 13:46:26 +0900 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | public void reset() { |
yoshiki iguchi | 0adf6a6 | 2018-02-01 13:46:26 +0900 | [diff] [blame] | 528 | entry = null; |
yoshiki iguchi | 0adf6a6 | 2018-02-01 13:46:26 +0900 | [diff] [blame] | 529 | expanded = false; |
| 530 | remoteInputActive = false; |
yoshiki iguchi | 4e30e76 | 2018-02-06 12:09:23 +0900 | [diff] [blame] | 531 | removeAutoRemovalCallbacks(); |
| 532 | mRemoveHeadsUpRunnable = null; |
| 533 | } |
| 534 | |
| 535 | public void removeAutoRemovalCallbacks() { |
| 536 | if (mRemoveHeadsUpRunnable != null) |
| 537 | mHandler.removeCallbacks(mRemoveHeadsUpRunnable); |
| 538 | } |
| 539 | |
| 540 | public void removeAsSoonAsPossible() { |
| 541 | if (mRemoveHeadsUpRunnable != null) { |
| 542 | removeAutoRemovalCallbacks(); |
| 543 | mHandler.postDelayed(mRemoveHeadsUpRunnable, |
| 544 | earliestRemovaltime - mClock.currentTimeMillis()); |
| 545 | } |
Selim Cinek | 684a442 | 2015-04-15 16:18:39 -0700 | [diff] [blame] | 546 | } |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 547 | } |
| 548 | |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 549 | public static class Clock { |
| 550 | public long currentTimeMillis() { |
| 551 | return SystemClock.elapsedRealtime(); |
| 552 | } |
| 553 | } |
Selim Cinek | b8f09cf | 2015-03-16 17:09:28 -0700 | [diff] [blame] | 554 | } |