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

Commit b6500a01 authored by Mark Fasheh's avatar Mark Fasheh
Browse files

MessageQueue: extract compareMessages to Message.java

Test: atest MessageQueueTest
Flag: EXEMPT refactor
Bug: 421623328
Change-Id: I07ae62e30ab6129399c31733aa40ae1ba3538088
parent 14dd4f8a
Loading
Loading
Loading
Loading
+2 −18
Original line number Diff line number Diff line
@@ -288,28 +288,12 @@ public final class MessageQueue {
    static final class EnqueueOrder implements Comparator<Message> {
        @Override
        public int compare(Message m1, Message m2) {
            return compareMessages(m1, m2);
            return Message.compareMessages(m1, m2);
        }
    }

    private static final EnqueueOrder sEnqueueOrder = new EnqueueOrder();

    static int compareMessages(@NonNull Message m1, @NonNull Message m2) {
        // Primary queue order is by when.
        // Messages with an earlier when should come first in the queue.
        final long whenDiff = m1.when - m2.when;
        if (whenDiff > 0) return 1;
        if (whenDiff < 0) return -1;

        // Secondary queue order is by insert sequence.
        // If two messages were inserted with the same `when`, the one inserted
        // first should come first in the queue.
        final long insertSeqDiff = m1.insertSeq - m2.insertSeq;
        if (insertSeqDiff > 0) return 1;
        if (insertSeqDiff < 0) return -1;

        return 0;
    }

    private static boolean isBarrier(Message msg) {
        return msg != null && msg.target == null;
@@ -805,7 +789,7 @@ public final class MessageQueue {
                if (msg == null) {
                    earliest = asyncMsg;
                } else if (asyncMsg != null) {
                    if (compareMessages(msg, asyncMsg) > 0) {
                    if (Message.compareMessages(msg, asyncMsg) > 0) {
                        earliest = asyncMsg;
                    }
                }
+18 −0
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@

package android.os;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.compat.annotation.UnsupportedAppUsage;
import android.util.TimeUtils;
@@ -556,6 +557,23 @@ public final class Message implements Parcelable {
    public Message() {
    }

    /*package*/ static int compareMessages(@NonNull Message m1, @NonNull Message m2) {
        // Primary queue order is by when.
        // Messages with an earlier when should come first in the queue.
        final long whenDiff = m1.when - m2.when;
        if (whenDiff > 0) return 1;
        if (whenDiff < 0) return -1;

        // Secondary queue order is by insert sequence.
        // If two messages were inserted with the same `when`, the one inserted
        // first should come first in the queue.
        final long insertSeqDiff = m1.insertSeq - m2.insertSeq;
        if (insertSeqDiff > 0) return 1;
        if (insertSeqDiff < 0) return -1;

        return 0;
    }

    @Override
    public String toString() {
        return toString(SystemClock.uptimeMillis());