Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f9b99ec4 authored by Lyn's avatar Lyn
Browse files

Abstract AlertEntry into HeadsUpEntry

Bug: 319721648
Test: SystemUITests
Test: use heads up notifications normally, no regressions
Flag: none
Change-Id: Ie9a4079617157f6de0b310f3766d8c3db90a2070
parent 82798460
Loading
Loading
Loading
Loading
+15 −113
Original line number Diff line number Diff line
@@ -43,7 +43,7 @@ public abstract class AlertingNotificationManager {
    protected int mMinimumDisplayTime;
    protected int mStickyForSomeTimeAutoDismissTime;
    protected int mAutoDismissTime;
    private DelayableExecutor mExecutor;
    protected DelayableExecutor mExecutor;

    public AlertingNotificationManager(HeadsUpManagerLogger logger,
            SystemClock systemClock, @Main DelayableExecutor executor) {
@@ -100,55 +100,14 @@ public abstract class AlertingNotificationManager {
    public abstract long getEarliestRemovalTime(String key);

    protected class AlertEntry implements Comparable<AlertEntry> {
        @Nullable public NotificationEntry mEntry;
        public long mPostTime;
        public long mEarliestRemovalTime;

        @Nullable protected Runnable mRemoveAlertRunnable;
        @Nullable private Runnable mCancelRemoveAlertRunnable;

        public void setEntry(@NonNull final NotificationEntry entry) {
            setEntry(entry, () -> removeAlertEntry(entry.getKey()));
        }
        public void setEntry(@NonNull final NotificationEntry entry) {}

        public void setEntry(@NonNull final NotificationEntry entry,
                @Nullable Runnable removeAlertRunnable) {
            mEntry = entry;
            mRemoveAlertRunnable = removeAlertRunnable;

            mPostTime = calculatePostTime();
            updateEntry(true /* updatePostTime */, "setEntry");
        }

        /**
         * Updates an entry's removal time.
         * @param updatePostTime whether or not to refresh the post time
         */
        public void updateEntry(boolean updatePostTime, @Nullable String reason) {
            mLogger.logUpdateEntry(mEntry, updatePostTime, reason);

            final long now = mSystemClock.elapsedRealtime();
            mEarliestRemovalTime = now + mMinimumDisplayTime;

            if (updatePostTime) {
                mPostTime = Math.max(mPostTime, now);
            }

            if (isSticky()) {
                removeAutoRemovalCallbacks("updateEntry (sticky)");
                return;
            }
                @Nullable Runnable removeAlertRunnable) {}

            final long finishTime = calculateFinishTime();
            final long timeLeft = Math.max(finishTime - now, mMinimumDisplayTime);
            scheduleAutoRemovalCallback(timeLeft, "updateEntry (not sticky)");
        }
        public void updateEntry(boolean updatePostTime, @Nullable String reason) { }

        /**
         * Whether or not the notification is "sticky" i.e. should stay on screen regardless
         * of the timer (forever) and should be removed externally.
         * @return true if the notification is sticky
         */
        public boolean isSticky() {
            // This implementation is overridden by HeadsUpManager HeadsUpEntry #isSticky
            return false;
@@ -159,90 +118,33 @@ public abstract class AlertingNotificationManager {
            return false;
        }

        /**
         * Whether the notification has befen on screen long enough and can be removed.
         * @return true if the notification has been on screen long enough
         */
        public boolean wasShownLongEnough() {
            return mEarliestRemovalTime < mSystemClock.elapsedRealtime();
            // This implementation is overridden by HeadsUpManager HeadsUpEntry #wasShownLongEnough
            return false;
        }

        @Override
        public int compareTo(@NonNull AlertEntry alertEntry) {
            return (mPostTime < alertEntry.mPostTime)
                    ? 1 : ((mPostTime == alertEntry.mPostTime)
                            ? mEntry.getKey().compareTo(alertEntry.mEntry.getKey()) : -1);
        }

        public void reset() {
            removeAutoRemovalCallbacks("reset()");
            mEntry = null;
            mRemoveAlertRunnable = null;
        }

        /**
         * Clear any pending removal runnables.
         */
        public void removeAutoRemovalCallbacks(@Nullable String reason) {
            final boolean removed = removeAutoRemovalCallbackInternal();

            if (removed) {
                mLogger.logAutoRemoveCanceled(mEntry, reason);
            }
        }

        private void scheduleAutoRemovalCallback(long delayMillis, @NonNull String reason) {
            if (mRemoveAlertRunnable == null) {
                Log.wtf(TAG, "scheduleAutoRemovalCallback with no callback set");
                return;
            // This implementation is overridden by HeadsUpManager HeadsUpEntry #compareTo
            return -1;
        }

            final boolean removed = removeAutoRemovalCallbackInternal();

            if (removed) {
                mLogger.logAutoRemoveRescheduled(mEntry, delayMillis, reason);
            } else {
                mLogger.logAutoRemoveScheduled(mEntry, delayMillis, reason);
            }


            mCancelRemoveAlertRunnable = mExecutor.executeDelayed(mRemoveAlertRunnable,
                    delayMillis);
        }
        public void reset() {}

        private boolean removeAutoRemovalCallbackInternal() {
            final boolean scheduled = (mCancelRemoveAlertRunnable != null);
        public void removeAutoRemovalCallbacks(@Nullable String reason) {}

            if (scheduled) {
                mCancelRemoveAlertRunnable.run();
                mCancelRemoveAlertRunnable = null;
            }
        public void scheduleAutoRemovalCallback(long delayMillis, @NonNull String reason) {}

            return scheduled;
        public boolean removeAutoRemovalCallbackInternal() {
            return false;
        }

        /**
         * Remove the alert at the earliest allowed removal time.
         */
        public void removeAsSoonAsPossible() {
            if (mRemoveAlertRunnable != null) {
                final long timeLeft = mEarliestRemovalTime - mSystemClock.elapsedRealtime();
                scheduleAutoRemovalCallback(timeLeft, "removeAsSoonAsPossible");
            }
        }
        public void removeAsSoonAsPossible() {}

        /**
         * Calculate what the post time of a notification is at some current time.
         * @return the post time
         */
        protected long calculatePostTime() {
            return mSystemClock.elapsedRealtime();
            return 0;
        }

        /**
         * @return When the notification should auto-dismiss itself, based on
         * {@link SystemClock#elapsedRealtime()}
         */
        protected long calculateFinishTime() {
            // Overridden by HeadsUpManager HeadsUpEntry #calculateFinishTime
            return 0;
+131 −3
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@ import android.database.ContentObserver;
import android.os.Handler;
import android.util.ArrayMap;
import android.util.ArraySet;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityManager;

@@ -644,6 +645,59 @@ public abstract class BaseHeadsUpManager extends AlertingNotificationManager imp
        protected boolean mExpanded;
        protected boolean mWasUnpinned;

        @Nullable public NotificationEntry mEntry;
        public long mPostTime;
        public long mEarliestRemovalTime;

        @Nullable protected Runnable mRemoveAlertRunnable;

        @Nullable private Runnable mCancelRemoveAlertRunnable;

        @Override
        public void setEntry(@NonNull final NotificationEntry entry) {
            setEntry(entry, () -> removeAlertEntry(entry.getKey()));
        }

        @Override
        public void setEntry(@NonNull final NotificationEntry entry,
                @Nullable Runnable removeAlertRunnable) {
            mEntry = entry;
            mRemoveAlertRunnable = removeAlertRunnable;

            mPostTime = calculatePostTime();
            updateEntry(true /* updatePostTime */, "setEntry");
        }

        /**
         * Updates an entry's removal time.
         * @param updatePostTime whether or not to refresh the post time
         */
        @Override
        public void updateEntry(boolean updatePostTime, @Nullable String reason) {
            mLogger.logUpdateEntry(mEntry, updatePostTime, reason);

            final long now = mSystemClock.elapsedRealtime();
            mEarliestRemovalTime = now + mMinimumDisplayTime;

            if (updatePostTime) {
                mPostTime = Math.max(mPostTime, now);
            }

            if (isSticky()) {
                removeAutoRemovalCallbacks("updateEntry (sticky)");
                return;
            }

            final long finishTime = calculateFinishTime();
            final long timeLeft = Math.max(finishTime - now, mMinimumDisplayTime);
            scheduleAutoRemovalCallback(timeLeft, "updateEntry (not sticky)");
        }

        /**
         * Whether or not the notification is "sticky" i.e. should stay on screen regardless
         * of the timer (forever) and should be removed externally.
         * @return true if the notification is sticky
         */
        @Override
        public boolean isSticky() {
            return (mEntry.isRowPinned() && mExpanded)
@@ -656,6 +710,15 @@ public abstract class BaseHeadsUpManager extends AlertingNotificationManager imp
            return mEntry.isStickyAndNotDemoted();
        }

        /**
         * Whether the notification has befen on screen long enough and can be removed.
         * @return true if the notification has been on screen long enough
         */
        @Override
        public boolean wasShownLongEnough() {
            return mEarliestRemovalTime < mSystemClock.elapsedRealtime();
        }

        @Override
        public int compareTo(@NonNull AlertEntry alertEntry) {
            HeadsUpEntry headsUpEntry = (HeadsUpEntry) alertEntry;
@@ -689,7 +752,13 @@ public abstract class BaseHeadsUpManager extends AlertingNotificationManager imp
                return 1;
            }

            return super.compareTo(headsUpEntry);
            if (mPostTime > alertEntry.mPostTime) {
                return -1;
            } else if (mPostTime == alertEntry.mPostTime) {
                return mEntry.getKey().compareTo(alertEntry.mEntry.getKey());
            } else {
                return 1;
            }
        }

        public void setExpanded(boolean expanded) {
@@ -698,15 +767,74 @@ public abstract class BaseHeadsUpManager extends AlertingNotificationManager imp

        @Override
        public void reset() {
            super.reset();
            removeAutoRemovalCallbacks("reset()");
            mEntry = null;
            mRemoveAlertRunnable = null;
            mExpanded = false;
            mRemoteInputActive = false;
        }

        /**
         * Clear any pending removal runnables.
         */
        @Override
        public void removeAutoRemovalCallbacks(@Nullable String reason) {
            final boolean removed = removeAutoRemovalCallbackInternal();

            if (removed) {
                mLogger.logAutoRemoveCanceled(mEntry, reason);
            }
        }

        @Override
        public void scheduleAutoRemovalCallback(long delayMillis, @NonNull String reason) {
            if (mRemoveAlertRunnable == null) {
                Log.wtf(TAG, "scheduleAutoRemovalCallback with no callback set");
                return;
            }

            final boolean removed = removeAutoRemovalCallbackInternal();

            if (removed) {
                mLogger.logAutoRemoveRescheduled(mEntry, delayMillis, reason);
            } else {
                mLogger.logAutoRemoveScheduled(mEntry, delayMillis, reason);
            }

            mCancelRemoveAlertRunnable = mExecutor.executeDelayed(mRemoveAlertRunnable,
                    delayMillis);
        }

        @Override
        public boolean removeAutoRemovalCallbackInternal() {
            final boolean scheduled = (mCancelRemoveAlertRunnable != null);

            if (scheduled) {
                mCancelRemoveAlertRunnable.run();
                mCancelRemoveAlertRunnable = null;
            }

            return scheduled;
        }

        /**
         * Remove the alert at the earliest allowed removal time.
         */
        public void removeAsSoonAsPossible() {
            if (mRemoveAlertRunnable != null) {
                final long timeLeft = mEarliestRemovalTime - mSystemClock.elapsedRealtime();
                scheduleAutoRemovalCallback(timeLeft, "removeAsSoonAsPossible");
            }
        }

        /**
         * Calculate what the post time of a notification is at some current time.
         * @return the post time
         */
        @Override
        protected long calculatePostTime() {
            // The actual post time will be just after the heads-up really slided in
            return super.calculatePostTime() + mTouchAcceptanceDelay;
            return mSystemClock.elapsedRealtime() + mTouchAcceptanceDelay;
        }

        /**