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

Commit 5dfaf336 authored by LuK1337's avatar LuK1337 Committed by Łukasz Patron
Browse files

core: Move notification timeout code to NotificationAttentionHelper

14 QRP3 enabled refactor_attention_helper flag.

Fixes: https://gitlab.com/LineageOS/issues/android/-/issues/7458
Change-Id: I2bbcd8d16587fe38eebf24eea159d968c5256ede
parent f9c33143
Loading
Loading
Loading
Loading
+36 −1
Original line number Diff line number Diff line
@@ -50,6 +50,7 @@ import android.media.IRingtonePlayer;
import android.net.Uri;
import android.os.Binder;
import android.os.RemoteException;
import android.os.SystemClock;
import android.os.SystemProperties;
import android.os.UserHandle;
import android.os.UserManager;
@@ -58,9 +59,11 @@ import android.provider.Settings;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.ArrayMap;
import android.util.Log;
import android.util.Pair;
import android.util.Slog;
import android.util.TimeUtils;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityManager;

@@ -158,6 +161,7 @@ public final class NotificationAttentionHelper {
    private final AudioAttributes mInCallNotificationAudioAttributes;
    private final float mInCallNotificationVolume;
    private Binder mCallNotificationToken = null;
    private final ArrayMap<String, Long> mLastSoundTimestamps = new ArrayMap<>();
    private LineageNotificationLights mLineageNotificationLights;

    // Settings flags
@@ -430,7 +434,8 @@ public final class NotificationAttentionHelper {
                boolean vibrateOnly =
                        hasValidVibrate && mNotificationCooldownVibrateUnlocked && mUserPresent;
                boolean hasAudibleAlert = hasValidSound || hasValidVibrate;
                if (hasAudibleAlert && !shouldMuteNotificationLocked(record, signals)) {
                if (hasAudibleAlert && !shouldMuteNotificationLocked(record, signals)
                        && !isInSoundTimeoutPeriod(record)) {
                    if (!sentAccessibilityEvent) {
                        sendAccessibilityEvent(record);
                        sentAccessibilityEvent = true;
@@ -521,6 +526,10 @@ public final class NotificationAttentionHelper {
                }
            }
        }
        if (buzz || beep) {
            mLastSoundTimestamps.put(generateLastSoundTimeoutKey(record),
                    SystemClock.elapsedRealtime());
        }
        final int buzzBeepBlinkLoggingCode =
                (buzz ? 1 : 0) | (beep ? 2 : 0) | (blink ? 4 : 0) | getPoliteBit(record);
        if (buzzBeepBlinkLoggingCode > 0) {
@@ -541,6 +550,24 @@ public final class NotificationAttentionHelper {
        return buzzBeepBlinkLoggingCode;
    }

    private boolean isInSoundTimeoutPeriod(NotificationRecord record) {
        long timeoutMillis = mNMP.getNotificationSoundTimeout(
                record.getSbn().getPackageName(), record.getSbn().getUid());
        if (timeoutMillis == 0) {
            return false;
        }

        Long value = mLastSoundTimestamps.get(generateLastSoundTimeoutKey(record));
        if (value == null) {
            return false;
        }
        return SystemClock.elapsedRealtime() - value < timeoutMillis;
    }

    private String generateLastSoundTimeoutKey(NotificationRecord record) {
        return record.getSbn().getPackageName() + "|" + record.getSbn().getUid();
    }

    private int getPoliteBit(final NotificationRecord record) {
        switch (getPolitenessState(record)) {
            case PolitenessStrategy.POLITE_STATE_POLITE:
@@ -1099,6 +1126,14 @@ public final class NotificationAttentionHelper {
        pw.print(prefix);
        pw.println("  mNotificationPulseEnabled=" + mNotificationPulseEnabled);

        long now = SystemClock.elapsedRealtime();
        pw.println("\n  Last notification sound timestamps:");
        for (Map.Entry<String, Long> entry : mLastSoundTimestamps.entrySet()) {
            pw.print("    " + entry.getKey() + " -> ");
            TimeUtils.formatDuration(entry.getValue(), now, pw);
            pw.println(" ago");
        }

        int N = mLights.size();
        if (N > 0) {
            pw.print(prefix);
+1 −0
Original line number Diff line number Diff line
@@ -25,4 +25,5 @@ import android.annotation.Nullable;
interface NotificationManagerPrivate {
    @Nullable
    NotificationRecord getNotificationByKey(String key);
    long getNotificationSoundTimeout(String pkg, int uid);
}
+12 −37
Original line number Diff line number Diff line
@@ -309,7 +309,6 @@ import android.util.Slog;
import android.util.SparseArray;
import android.util.SparseBooleanArray;
import android.util.StatsEvent;
import android.util.TimeUtils;
import android.util.Xml;
import android.util.proto.ProtoOutputStream;
import android.view.Display;
@@ -688,7 +687,6 @@ public class NotificationManagerService extends SystemService {
    @GuardedBy("mToastQueue")
    private final Set<Integer> mToastRateLimitingDisabledUids = new ArraySet<>();
    final ArrayMap<String, NotificationRecord> mSummaryByGroupKey = new ArrayMap<>();
    final ArrayMap<String, Long> mLastSoundTimestamps = new ArrayMap<>();
    // True if the toast that's on top of the queue is being shown at the moment.
    @GuardedBy("mToastQueue")
@@ -1788,10 +1786,18 @@ public class NotificationManagerService extends SystemService {
    };
    NotificationManagerPrivate mNotificationManagerPrivate = key -> {
    NotificationManagerPrivate mNotificationManagerPrivate = new NotificationManagerPrivate() {
        @Override
        public NotificationRecord getNotificationByKey(String key) {
            synchronized (mNotificationLock) {
                return mNotificationsByKey.get(key);
            }
        }
        @Override
        public long getNotificationSoundTimeout(String pkg, int uid) {
            return mPreferencesHelper.getNotificationSoundTimeout(pkg, uid);
        }
    };
    @VisibleForTesting
@@ -7165,14 +7171,6 @@ public class NotificationManagerService extends SystemService {
                pw.println("\n  Usage Stats:");
                mUsageStats.dump(pw, "    ", filter);
            }
            long now = SystemClock.elapsedRealtime();
            pw.println("\n  Last notification sound timestamps:");
            for (Map.Entry<String, Long> entry : mLastSoundTimestamps.entrySet()) {
                pw.print("    " + entry.getKey() + " -> ");
                TimeUtils.formatDuration(entry.getValue(), now, pw);
                pw.println(" ago");
            }
        }
    }
@@ -9215,8 +9213,7 @@ public class NotificationManagerService extends SystemService {
                }
                hasValidVibrate = vibration != null;
                boolean hasAudibleAlert = hasValidSound || hasValidVibrate;
                if (hasAudibleAlert && !shouldMuteNotificationLocked(record)
                        && !isInSoundTimeoutPeriod(record)) {
                if (hasAudibleAlert && !shouldMuteNotificationLocked(record)) {
                    if (!sentAccessibilityEvent) {
                        sendAccessibilityEvent(record);
                        sentAccessibilityEvent = true;
@@ -9287,10 +9284,6 @@ public class NotificationManagerService extends SystemService {
        } else if (wasShowLights) {
            updateLightsLocked();
        }
        if (buzz || beep) {
            mLastSoundTimestamps.put(generateLastSoundTimeoutKey(record),
                    SystemClock.elapsedRealtime());
        }
        final int buzzBeepBlink = (buzz ? 1 : 0) | (beep ? 2 : 0) | (blink ? 4 : 0);
        if (buzzBeepBlink > 0) {
            // Ignore summary updates because we don't display most of the information.
@@ -9321,24 +9314,6 @@ public class NotificationManagerService extends SystemService {
        return buzzBeepBlink;
    }
    private boolean isInSoundTimeoutPeriod(NotificationRecord record) {
        long timeoutMillis = mPreferencesHelper.getNotificationSoundTimeout(
                record.getSbn().getPackageName(), record.getSbn().getUid());
        if (timeoutMillis == 0) {
            return false;
        }
        Long value = mLastSoundTimestamps.get(generateLastSoundTimeoutKey(record));
        if (value == null) {
            return false;
        }
        return SystemClock.elapsedRealtime() - value < timeoutMillis;
    }
    private String generateLastSoundTimeoutKey(NotificationRecord record) {
        return record.getSbn().getPackageName() + "|" + record.getSbn().getUid();
    }
    @GuardedBy("mNotificationLock")
    boolean canShowLightsLocked(final NotificationRecord record, boolean aboveThreshold) {
        // device lacks light