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

Commit 2f77941d authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Add synchronization to ttlhelper" into main

parents a92cc5cc ae80bb06
Loading
Loading
Loading
Loading
+40 −23
Original line number Diff line number Diff line
@@ -54,13 +54,17 @@ public class TimeToLiveHelper {
    private final AlarmManager mAm;

    @VisibleForTesting
    @GuardedBy("mLock")
    final TreeSet<Pair<Long, String>> mKeys;
    final Object mLock = new Object();

    public TimeToLiveHelper(NotificationManagerPrivate nm, Context context) {
        mContext = context;
        mNm = nm;
        mAm = context.getSystemService(AlarmManager.class);
        synchronized (mLock) {
            mKeys = new TreeSet<>((left, right) -> Long.compare(left.first, right.first));
        }

        IntentFilter timeoutFilter = new IntentFilter(ACTION);
        timeoutFilter.addDataScheme(SCHEME_TIMEOUT);
@@ -73,8 +77,10 @@ public class TimeToLiveHelper {
    }

    void dump(PrintWriter pw, String indent) {
        synchronized (mLock) {
            pw.println(indent + "mKeys " + mKeys);
        }
    }

    private @NonNull PendingIntent getAlarmPendingIntent(String nextKey, int flags) {
        flags |= PendingIntent.FLAG_IMMUTABLE;
@@ -93,6 +99,7 @@ public class TimeToLiveHelper {

    @VisibleForTesting
    void scheduleTimeoutLocked(NotificationRecord record, long currentTime) {
        synchronized (mLock) {
            removeMatchingEntry(record.getKey());

            final long timeoutAfter = currentTime + record.getNotification().getTimeoutAfter();
@@ -111,12 +118,16 @@ public class TimeToLiveHelper {
                }
            }
        }
    }

    @VisibleForTesting
    void cancelScheduledTimeoutLocked(NotificationRecord record) {
        synchronized (mLock) {
            removeMatchingEntry(record.getKey());
        }
    }

    @GuardedBy("mLock")
    private void removeMatchingEntry(String key) {
        if (!mKeys.isEmpty() && key.equals(mKeys.first().second)) {
            // cancel the first alarm, remove the first entry, maybe schedule the alarm for the new
@@ -139,11 +150,13 @@ public class TimeToLiveHelper {
        }
    }

    @GuardedBy("mLock")
    private void cancelFirstAlarm() {
        final PendingIntent pi = getAlarmPendingIntent(mKeys.first().second, FLAG_CANCEL_CURRENT);
        mAm.cancel(pi);
    }

    @GuardedBy("mLock")
    private void maybeScheduleFirstAlarm() {
        if (!mKeys.isEmpty()) {
            final PendingIntent piNewFirst = getAlarmPendingIntent(mKeys.first().second,
@@ -162,13 +175,17 @@ public class TimeToLiveHelper {
                return;
            }
            if (ACTION.equals(action)) {
                String timeoutKey = null;
                synchronized (mLock) {
                    Pair<Long, String> earliest = mKeys.first();
                    String key = intent.getStringExtra(EXTRA_KEY);
                    if (!earliest.second.equals(key)) {
                        Slog.wtf(TAG, "Alarm triggered but wasn't the earliest we were tracking");
                    }
                    removeMatchingEntry(key);
                mNm.timeoutNotification(earliest.second);
                    timeoutKey = earliest.second;
                }
                mNm.timeoutNotification(timeoutKey);
            }
        }
    };