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

Commit 2b763ff2 authored by Christopher Tate's avatar Christopher Tate Committed by The Android Automerger
Browse files

Don't let batch coalescing / rewindowing break batch ordering

Bug 9965704

Change-Id: I41819bd1da16cc61c54938ed4ddd421f15b56fbb
parent b20727bc
Loading
Loading
Loading
Loading
+23 −9
Original line number Diff line number Diff line
@@ -353,15 +353,16 @@ class AlarmManagerService extends IAlarmManager.Stub {
        return (index == 0);
    }

    Batch attemptCoalesceLocked(long whenElapsed, long maxWhen) {
    // Return the index of the matching batch, or -1 if none found.
    int attemptCoalesceLocked(long whenElapsed, long maxWhen) {
        final int N = mAlarmBatches.size();
        for (int i = 0; i < N; i++) {
            Batch b = mAlarmBatches.get(i);
            if (!b.standalone && b.canHold(whenElapsed, maxWhen)) {
                return b;
                return i;
            }
        }
        return null;
        return -1;
    }

    // The RTC clock has moved arbitrarily, so we need to recalculate all the batching
@@ -553,16 +554,23 @@ class AlarmManagerService extends IAlarmManager.Stub {
        removeLocked(operation);

        final boolean reschedule;
        Batch batch = (isStandalone) ? null : attemptCoalesceLocked(whenElapsed, maxWhen);
        if (batch == null) {
            batch = new Batch(a);
        int whichBatch = (isStandalone) ? -1 : attemptCoalesceLocked(whenElapsed, maxWhen);
        if (whichBatch < 0) {
            Batch batch = new Batch(a);
            batch.standalone = isStandalone;
            if (DEBUG_BATCH) {
                Slog.v(TAG, "Starting new alarm batch " + batch);
            }
            reschedule = addBatchLocked(mAlarmBatches, batch);
        } else {
            Batch batch = mAlarmBatches.get(whichBatch);
            reschedule = batch.add(a);
            if (reschedule) {
                // The start time of this batch advanced, so batch ordering may
                // have just been broken.  Move it to where it now belongs.
                mAlarmBatches.remove(whichBatch);
                addBatchLocked(mAlarmBatches, batch);
            }
        }

        if (reschedule) {
@@ -1058,6 +1066,9 @@ class AlarmManagerService extends IAlarmManager.Stub {
                triggerList.clear();

                if ((result & TIME_CHANGED_MASK) != 0) {
                    if (DEBUG_BATCH) {
                        Slog.v(TAG, "Time changed notification from kernel");
                    }
                    remove(mTimeTickSender);
                    rebatchAllAlarms();
                    mClockReceiver.scheduleTimeTickEvent();
@@ -1211,6 +1222,9 @@ class AlarmManagerService extends IAlarmManager.Stub {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(Intent.ACTION_TIME_TICK)) {
                if (DEBUG_BATCH) {
                    Slog.v(TAG, "Received TIME_TICK alarm; rescheduling");
                }
                scheduleTimeTickEvent();
            } else if (intent.getAction().equals(Intent.ACTION_DATE_CHANGED)) {
                // Since the kernel does not keep track of DST, we need to