Split auto dismissing functionality from heads up

Refactor HeadsUpManager so that the auto dismissing alert functionality
is separate and reusable for future use with ambient pulses.  Add tests
for this new class and also a few for HeadsUpMananger specific logic

First in two-part CL to split ambient pulse logic from heads up logic.
Second CL will add an ambient pulse manager which uses the alert logic
introduced here and involve more of the actual split.

Test: runtest systemui, manual
Change-Id: I08069562e7bea4c8e25674aa35237e1bb8cf4475
diff --git a/packages/SystemUI/src/com/android/systemui/statusbar/policy/HeadsUpManager.java b/packages/SystemUI/src/com/android/systemui/statusbar/policy/HeadsUpManager.java
index 677dd73..d477587 100644
--- a/packages/SystemUI/src/com/android/systemui/statusbar/policy/HeadsUpManager.java
+++ b/packages/SystemUI/src/com/android/systemui/statusbar/policy/HeadsUpManager.java
@@ -21,56 +21,44 @@
 import android.content.Context;
 import android.content.res.Resources;
 import android.database.ContentObserver;
-import android.os.SystemClock;
-import android.os.Handler;
-import android.os.Looper;
 import android.util.ArrayMap;
 import android.provider.Settings;
 import android.util.Log;
-import android.view.accessibility.AccessibilityEvent;
 
 import com.android.internal.logging.MetricsLogger;
 import com.android.systemui.R;
+import com.android.systemui.statusbar.AlertingNotificationManager;
 import com.android.systemui.statusbar.notification.row.ExpandableNotificationRow;
 import com.android.systemui.statusbar.notification.NotificationData;
 
 import java.io.FileDescriptor;
 import java.io.PrintWriter;
-import java.util.Iterator;
-import java.util.stream.Stream;
-import java.util.HashMap;
 import java.util.HashSet;
 
 /**
  * A manager which handles heads up notifications which is a special mode where
  * they simply peek from the top of the screen.
  */
-public class HeadsUpManager {
+public abstract class HeadsUpManager extends AlertingNotificationManager {
     private static final String TAG = "HeadsUpManager";
-    private static final boolean DEBUG = false;
     private static final String SETTING_HEADS_UP_SNOOZE_LENGTH_MS = "heads_up_snooze_length_ms";
 
-    protected final Clock mClock = new Clock();
     protected final HashSet<OnHeadsUpChangedListener> mListeners = new HashSet<>();
-    protected final Handler mHandler = new Handler(Looper.getMainLooper());
 
     protected final Context mContext;
 
-    protected int mHeadsUpNotificationDecay;
-    protected int mMinimumDisplayTime;
     protected int mTouchAcceptanceDelay;
     protected int mSnoozeLengthMs;
     protected boolean mHasPinnedNotification;
     protected int mUser;
 
-    private final HashMap<String, HeadsUpEntry> mHeadsUpEntries = new HashMap<>();
     private final ArrayMap<String, Long> mSnoozedPackages;
 
     public HeadsUpManager(@NonNull final Context context) {
         mContext = context;
         Resources resources = context.getResources();
         mMinimumDisplayTime = resources.getInteger(R.integer.heads_up_notification_minimum_time);
-        mHeadsUpNotificationDecay = resources.getInteger(R.integer.heads_up_notification_decay);
+        mAutoDismissNotificationDecay = resources.getInteger(R.integer.heads_up_notification_decay);
         mTouchAcceptanceDelay = resources.getInteger(R.integer.touch_acceptance_delay);
         mSnoozedPackages = new ArrayMap<>();
         int defaultSnoozeLengthMs =
@@ -85,7 +73,9 @@
                         context.getContentResolver(), SETTING_HEADS_UP_SNOOZE_LENGTH_MS, -1);
                 if (packageSnoozeLengthMs > -1 && packageSnoozeLengthMs != mSnoozeLengthMs) {
                     mSnoozeLengthMs = packageSnoozeLengthMs;
-                    if (DEBUG) Log.v(TAG, "mSnoozeLengthMs = " + mSnoozeLengthMs);
+                    if (Log.isLoggable(TAG, Log.VERBOSE)) {
+                        Log.v(TAG, "mSnoozeLengthMs = " + mSnoozeLengthMs);
+                    }
                 }
             }
         };
@@ -108,49 +98,14 @@
         mListeners.remove(listener);
     }
 
-    /**
-     * Called when posting a new notification to the heads up.
-     */
-    public void showNotification(@NonNull NotificationData.Entry headsUp) {
-        if (DEBUG) Log.v(TAG, "showNotification");
-        addHeadsUpEntry(headsUp);
-        updateNotification(headsUp, true);
-        headsUp.setInterruption();
-    }
-
-    /**
-     * Called when updating or posting a notification to the heads up.
-     */
-    public void updateNotification(@NonNull NotificationData.Entry headsUp, boolean alert) {
-        if (DEBUG) Log.v(TAG, "updateNotification");
-
-        headsUp.row.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
-
-        if (alert) {
-            HeadsUpEntry headsUpEntry = mHeadsUpEntries.get(headsUp.key);
-            if (headsUpEntry == null) {
-                // the entry was released before this update (i.e by a listener) This can happen
-                // with the groupmanager
-                return;
-            }
-            headsUpEntry.updateEntry(true /* updatePostTime */);
-            setEntryPinned(headsUpEntry, shouldHeadsUpBecomePinned(headsUp));
+    public void updateNotification(@NonNull String key, boolean alert) {
+        super.updateNotification(key, alert);
+        AlertEntry alertEntry = getHeadsUpEntry(key);
+        if (alert && alertEntry != null) {
+            setEntryPinned((HeadsUpEntry) alertEntry, shouldHeadsUpBecomePinned(alertEntry.mEntry));
         }
     }
 
-    private void addHeadsUpEntry(@NonNull NotificationData.Entry entry) {
-        HeadsUpEntry headsUpEntry = createHeadsUpEntry();
-        // This will also add the entry to the sortedList
-        headsUpEntry.setEntry(entry);
-        mHeadsUpEntries.put(entry.key, headsUpEntry);
-        entry.row.setHeadsUp(true);
-        setEntryPinned(headsUpEntry, shouldHeadsUpBecomePinned(entry));
-        for (OnHeadsUpChangedListener listener : mListeners) {
-            listener.onHeadsUpStateChanged(entry, true);
-        }
-        entry.row.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
-    }
-
     protected boolean shouldHeadsUpBecomePinned(@NonNull NotificationData.Entry entry) {
         return hasFullScreenIntent(entry);
     }
@@ -161,8 +116,10 @@
 
     protected void setEntryPinned(
             @NonNull HeadsUpManager.HeadsUpEntry headsUpEntry, boolean isPinned) {
-        if (DEBUG) Log.v(TAG, "setEntryPinned: " + isPinned);
-        ExpandableNotificationRow row = headsUpEntry.entry.row;
+        if (Log.isLoggable(TAG, Log.VERBOSE)) {
+            Log.v(TAG, "setEntryPinned: " + isPinned);
+        }
+        ExpandableNotificationRow row = headsUpEntry.mEntry.row;
         if (row.isPinned() != isPinned) {
             row.setPinned(isPinned);
             updatePinnedMode();
@@ -176,20 +133,24 @@
         }
     }
 
-    protected void removeHeadsUpEntry(@NonNull NotificationData.Entry entry) {
-        HeadsUpEntry remove = mHeadsUpEntries.remove(entry.key);
-        onHeadsUpEntryRemoved(remove);
+    @Override
+    protected void onAlertEntryAdded(AlertEntry alertEntry) {
+        NotificationData.Entry entry = alertEntry.mEntry;
+        entry.row.setHeadsUp(true);
+        setEntryPinned((HeadsUpEntry) alertEntry, shouldHeadsUpBecomePinned(entry));
+        for (OnHeadsUpChangedListener listener : mListeners) {
+            listener.onHeadsUpStateChanged(entry, true);
+        }
     }
 
-    protected void onHeadsUpEntryRemoved(@NonNull HeadsUpEntry remove) {
-        NotificationData.Entry entry = remove.entry;
-        entry.row.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
+    @Override
+    protected void onAlertEntryRemoved(AlertEntry alertEntry) {
+        NotificationData.Entry entry = alertEntry.mEntry;
         entry.row.setHeadsUp(false);
-        setEntryPinned(remove, false /* isPinned */);
+        setEntryPinned((HeadsUpEntry) alertEntry, false /* isPinned */);
         for (OnHeadsUpChangedListener listener : mListeners) {
             listener.onHeadsUpStateChanged(entry, false);
         }
-        releaseHeadsUpEntry(remove);
     }
 
     protected void updatePinnedMode() {
@@ -197,7 +158,7 @@
         if (hasPinnedNotification == mHasPinnedNotification) {
             return;
         }
-        if (DEBUG) {
+        if (Log.isLoggable(TAG, Log.VERBOSE)) {
             Log.v(TAG, "Pinned mode changed: " + mHasPinnedNotification + " -> " +
                        hasPinnedNotification);
         }
@@ -211,50 +172,6 @@
     }
 
     /**
-     * React to the removal of the notification in the heads up.
-     *
-     * @return true if the notification was removed and false if it still needs to be kept around
-     * for a bit since it wasn't shown long enough
-     */
-    public boolean removeNotification(@NonNull String key, boolean ignoreEarliestRemovalTime) {
-        if (DEBUG) Log.v(TAG, "removeNotification");
-        releaseImmediately(key);
-        return true;
-    }
-
-    /**
-     * Returns if the given notification is in the Heads Up Notification list or not.
-     */
-    public boolean isHeadsUp(@NonNull String key) {
-        return mHeadsUpEntries.containsKey(key);
-    }
-
-    /**
-     * Pushes any current Heads Up notification down into the shade.
-     */
-    public void releaseAllImmediately() {
-        if (DEBUG) Log.v(TAG, "releaseAllImmediately");
-        Iterator<HeadsUpEntry> iterator = mHeadsUpEntries.values().iterator();
-        while (iterator.hasNext()) {
-            HeadsUpEntry entry = iterator.next();
-            iterator.remove();
-            onHeadsUpEntryRemoved(entry);
-        }
-    }
-
-    /**
-     * Pushes the given Heads Up notification down into the shade.
-     */
-    public void releaseImmediately(@NonNull String key) {
-        HeadsUpEntry headsUpEntry = getHeadsUpEntry(key);
-        if (headsUpEntry == null) {
-            return;
-        }
-        NotificationData.Entry shadeEntry = headsUpEntry.entry;
-        removeHeadsUpEntry(shadeEntry);
-    }
-
-    /**
      * Returns if the given notification is snoozed or not.
      */
     public boolean isSnoozed(@NonNull String packageName) {
@@ -262,7 +179,9 @@
         Long snoozedUntil = mSnoozedPackages.get(key);
         if (snoozedUntil != null) {
             if (snoozedUntil > mClock.currentTimeMillis()) {
-                if (DEBUG) Log.v(TAG, key + " snoozed");
+                if (Log.isLoggable(TAG, Log.VERBOSE)) {
+                    Log.v(TAG, key + " snoozed");
+                }
                 return true;
             }
             mSnoozedPackages.remove(packageName);
@@ -274,9 +193,9 @@
      * Snoozes all current Heads Up Notifications.
      */
     public void snooze() {
-        for (String key : mHeadsUpEntries.keySet()) {
-            HeadsUpEntry entry = mHeadsUpEntries.get(key);
-            String packageName = entry.entry.notification.getPackageName();
+        for (String key : mAlertEntries.keySet()) {
+            AlertEntry entry = getHeadsUpEntry(key);
+            String packageName = entry.mEntry.notification.getPackageName();
             mSnoozedPackages.put(snoozeKey(packageName, mUser),
                     mClock.currentTimeMillis() + mSnoozeLengthMs);
         }
@@ -289,53 +208,27 @@
 
     @Nullable
     protected HeadsUpEntry getHeadsUpEntry(@NonNull String key) {
-        return mHeadsUpEntries.get(key);
+        return (HeadsUpEntry) mAlertEntries.get(key);
     }
 
     /**
-     * Returns the entry of given Heads Up Notification.
-     *
-     * @param key Key of heads up notification
-     */
-    @Nullable
-    public NotificationData.Entry getEntry(@NonNull String key) {
-        HeadsUpEntry entry = mHeadsUpEntries.get(key);
-        return entry != null ? entry.entry : null;
-    }
-
-    /**
-     * Returns the stream of all current Heads Up Notifications.
-     */
-    @NonNull
-    public Stream<NotificationData.Entry> getAllEntries() {
-        return mHeadsUpEntries.values().stream().map(headsUpEntry -> headsUpEntry.entry);
-    }
-
-    /**
-     * Returns the top Heads Up Notification, which appeares to show at first.
+     * Returns the top Heads Up Notification, which appears to show at first.
      */
     @Nullable
     public NotificationData.Entry getTopEntry() {
         HeadsUpEntry topEntry = getTopHeadsUpEntry();
-        return (topEntry != null) ? topEntry.entry : null;
-    }
-
-    /**
-     * Returns if any heads up notification is available or not.
-     */
-    public boolean hasHeadsUpNotifications() {
-        return !mHeadsUpEntries.isEmpty();
+        return (topEntry != null) ? topEntry.mEntry : null;
     }
 
     @Nullable
     protected HeadsUpEntry getTopHeadsUpEntry() {
-        if (mHeadsUpEntries.isEmpty()) {
+        if (mAlertEntries.isEmpty()) {
             return null;
         }
         HeadsUpEntry topEntry = null;
-        for (HeadsUpEntry entry: mHeadsUpEntries.values()) {
+        for (AlertEntry entry: mAlertEntries.values()) {
             if (topEntry == null || entry.compareTo(topEntry) < 0) {
-                topEntry = entry;
+                topEntry = (HeadsUpEntry) entry;
             }
         }
         return topEntry;
@@ -359,8 +252,8 @@
         pw.print("  mSnoozeLengthMs="); pw.println(mSnoozeLengthMs);
         pw.print("  now="); pw.println(mClock.currentTimeMillis());
         pw.print("  mUser="); pw.println(mUser);
-        for (HeadsUpEntry entry: mHeadsUpEntries.values()) {
-            pw.print("  HeadsUpEntry="); pw.println(entry.entry);
+        for (AlertEntry entry: mAlertEntries.values()) {
+            pw.print("  HeadsUpEntry="); pw.println(entry.mEntry);
         }
         int N = mSnoozedPackages.size();
         pw.println("  snoozed packages: " + N);
@@ -378,9 +271,9 @@
     }
 
     private boolean hasPinnedNotificationInternal() {
-        for (String key : mHeadsUpEntries.keySet()) {
-            HeadsUpEntry entry = mHeadsUpEntries.get(key);
-            if (entry.entry.row.isPinned()) {
+        for (String key : mAlertEntries.keySet()) {
+            AlertEntry entry = getHeadsUpEntry(key);
+            if (entry.mEntry.row.isPinned()) {
                 return true;
             }
         }
@@ -392,16 +285,16 @@
      * @param userUnPinned The unpinned action is trigger by user real operation.
      */
     public void unpinAll(boolean userUnPinned) {
-        for (String key : mHeadsUpEntries.keySet()) {
-            HeadsUpEntry entry = mHeadsUpEntries.get(key);
+        for (String key : mAlertEntries.keySet()) {
+            HeadsUpEntry entry = getHeadsUpEntry(key);
             setEntryPinned(entry, false /* isPinned */);
             // maybe it got un sticky
             entry.updateEntry(false /* updatePostTime */);
 
             // when the user unpinned all of HUNs by moving one HUN, all of HUNs should not stay
             // on the screen.
-            if (userUnPinned && entry.entry != null && entry.entry.row != null) {
-                ExpandableNotificationRow row = entry.entry.row;
+            if (userUnPinned && entry.mEntry != null && entry.mEntry.row != null) {
+                ExpandableNotificationRow row = entry.mEntry.row;
                 if (row.mustStayOnScreen()) {
                     row.setHeadsUpIsVisible();
                 }
@@ -425,8 +318,8 @@
      * one should be ranked higher and 0 if they are equal.
      */
     public int compare(@NonNull NotificationData.Entry a, @NonNull NotificationData.Entry b) {
-        HeadsUpEntry aEntry = getHeadsUpEntry(a.key);
-        HeadsUpEntry bEntry = getHeadsUpEntry(b.key);
+        AlertEntry aEntry = getHeadsUpEntry(a.key);
+        AlertEntry bEntry = getHeadsUpEntry(b.key);
         if (aEntry == null || bEntry == null) {
             return aEntry == null ? 1 : -1;
         }
@@ -438,21 +331,18 @@
      * until it's collapsed again.
      */
     public void setExpanded(@NonNull NotificationData.Entry entry, boolean expanded) {
-        HeadsUpManager.HeadsUpEntry headsUpEntry = mHeadsUpEntries.get(entry.key);
+        HeadsUpEntry headsUpEntry = getHeadsUpEntry(entry.key);
         if (headsUpEntry != null && entry.row.isPinned()) {
-            headsUpEntry.expanded(expanded);
+            headsUpEntry.setExpanded(expanded);
         }
     }
 
     @NonNull
-    protected HeadsUpEntry createHeadsUpEntry() {
+    @Override
+    protected HeadsUpEntry createAlertEntry() {
         return new HeadsUpEntry();
     }
 
-    protected void releaseHeadsUpEntry(@NonNull HeadsUpEntry entry) {
-        entry.reset();
-    }
-
     public void onDensityOrFontScaleChanged() {
     }
 
@@ -460,108 +350,58 @@
      * This represents a notification and how long it is in a heads up mode. It also manages its
      * lifecycle automatically when created.
      */
-    protected class HeadsUpEntry implements Comparable<HeadsUpEntry> {
-        @Nullable public NotificationData.Entry entry;
-        public long postTime;
+    protected class HeadsUpEntry extends AlertEntry {
         public boolean remoteInputActive;
-        public long earliestRemovaltime;
-        public boolean expanded;
+        protected boolean expanded;
 
-        @Nullable private Runnable mRemoveHeadsUpRunnable;
-
-        public void setEntry(@Nullable final NotificationData.Entry entry) {
-            setEntry(entry, null);
-        }
-
-        public void setEntry(@Nullable final NotificationData.Entry entry,
-                @Nullable Runnable removeHeadsUpRunnable) {
-            this.entry = entry;
-            this.mRemoveHeadsUpRunnable = removeHeadsUpRunnable;
-
-            // The actual post time will be just after the heads-up really slided in
-            postTime = mClock.currentTimeMillis() + mTouchAcceptanceDelay;
-            updateEntry(true /* updatePostTime */);
-        }
-
-        public void updateEntry(boolean updatePostTime) {
-            if (DEBUG) Log.v(TAG, "updateEntry");
-
-            long currentTime = mClock.currentTimeMillis();
-            earliestRemovaltime = currentTime + mMinimumDisplayTime;
-            if (updatePostTime) {
-                postTime = Math.max(postTime, currentTime);
-            }
-            removeAutoRemovalCallbacks();
-
-            if (!isSticky()) {
-                long finishTime = postTime + mHeadsUpNotificationDecay;
-                long removeDelay = Math.max(finishTime - currentTime, mMinimumDisplayTime);
-                mHandler.postDelayed(mRemoveHeadsUpRunnable, removeDelay);
-            }
-        }
-
-        private boolean isSticky() {
-            return (entry.row.isPinned() && expanded)
-                    || remoteInputActive || hasFullScreenIntent(entry);
+        @Override
+        protected boolean isSticky() {
+            return (mEntry.row.isPinned() && expanded)
+                    || remoteInputActive || hasFullScreenIntent(mEntry);
         }
 
         @Override
-        public int compareTo(@NonNull HeadsUpEntry o) {
-            boolean isPinned = entry.row.isPinned();
-            boolean otherPinned = o.entry.row.isPinned();
+        public int compareTo(@NonNull AlertEntry alertEntry) {
+            HeadsUpEntry headsUpEntry = (HeadsUpEntry) alertEntry;
+            boolean isPinned = mEntry.row.isPinned();
+            boolean otherPinned = headsUpEntry.mEntry.row.isPinned();
             if (isPinned && !otherPinned) {
                 return -1;
             } else if (!isPinned && otherPinned) {
                 return 1;
             }
-            boolean selfFullscreen = hasFullScreenIntent(entry);
-            boolean otherFullscreen = hasFullScreenIntent(o.entry);
+            boolean selfFullscreen = hasFullScreenIntent(mEntry);
+            boolean otherFullscreen = hasFullScreenIntent(headsUpEntry.mEntry);
             if (selfFullscreen && !otherFullscreen) {
                 return -1;
             } else if (!selfFullscreen && otherFullscreen) {
                 return 1;
             }
 
-            if (remoteInputActive && !o.remoteInputActive) {
+            if (remoteInputActive && !headsUpEntry.remoteInputActive) {
                 return -1;
-            } else if (!remoteInputActive && o.remoteInputActive) {
+            } else if (!remoteInputActive && headsUpEntry.remoteInputActive) {
                 return 1;
             }
 
-            return postTime < o.postTime ? 1
-                    : postTime == o.postTime ? entry.key.compareTo(o.entry.key)
-                            : -1;
+            return super.compareTo(headsUpEntry);
         }
 
-        public void expanded(boolean expanded) {
+        public void setExpanded(boolean expanded) {
             this.expanded = expanded;
         }
 
+        @Override
         public void reset() {
-            entry = null;
+            super.reset();
             expanded = false;
             remoteInputActive = false;
-            removeAutoRemovalCallbacks();
-            mRemoveHeadsUpRunnable = null;
         }
 
-        public void removeAutoRemovalCallbacks() {
-            if (mRemoveHeadsUpRunnable != null)
-                mHandler.removeCallbacks(mRemoveHeadsUpRunnable);
-        }
-
-        public void removeAsSoonAsPossible() {
-            if (mRemoveHeadsUpRunnable != null) {
-                removeAutoRemovalCallbacks();
-                mHandler.postDelayed(mRemoveHeadsUpRunnable,
-                        earliestRemovaltime - mClock.currentTimeMillis());
-            }
-        }
-    }
-
-    public static class Clock {
-        public long currentTimeMillis() {
-            return SystemClock.elapsedRealtime();
+        @Override
+        protected long calculatePostTime() {
+            // The actual post time will be just after the heads-up really slided in
+            return super.calculatePostTime() + mTouchAcceptanceDelay;
         }
     }
 }