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

Commit 735666c0 authored by Shai Barack's avatar Shai Barack Committed by Android (Google) Code Review
Browse files

Merge "Remove MessageQueue reflection from TelephonyTest" into main

parents cdf5814b 1e2293be
Loading
Loading
Loading
Loading
+10 −58
Original line number Diff line number Diff line
@@ -175,23 +175,6 @@ public abstract class TelephonyTest {
            new ArrayList<String>(), EmergencyNumber.EMERGENCY_NUMBER_SOURCE_NETWORK_SIGNALING,
            EmergencyNumber.EMERGENCY_CALL_ROUTING_NORMAL);

    private static final Field MESSAGE_QUEUE_FIELD;
    private static final Field MESSAGE_WHEN_FIELD;
    private static final Field MESSAGE_NEXT_FIELD;

    static {
        try {
            MESSAGE_QUEUE_FIELD = MessageQueue.class.getDeclaredField("mMessages");
            MESSAGE_QUEUE_FIELD.setAccessible(true);
            MESSAGE_WHEN_FIELD = Message.class.getDeclaredField("when");
            MESSAGE_WHEN_FIELD.setAccessible(true);
            MESSAGE_NEXT_FIELD = Message.class.getDeclaredField("next");
            MESSAGE_NEXT_FIELD.setAccessible(true);
        } catch (NoSuchFieldException e) {
            throw new RuntimeException("Failed to initialize TelephonyTest", e);
        }
    }

    // Mocked classes
    protected FeatureFlags mFeatureFlags;
    protected GsmCdmaPhone mPhone;
@@ -1348,37 +1331,13 @@ public abstract class TelephonyTest {
        }
    }

    /**
     * @return The longest delay from all the message queues.
     */
    private long getLongestDelay() {
        long delay = 0;
        for (TestableLooper looper : mTestableLoopers) {
            MessageQueue queue = looper.getLooper().getQueue();
            try {
                Message msg = (Message) MESSAGE_QUEUE_FIELD.get(queue);
                while (msg != null) {
                    delay = Math.max(msg.getWhen(), delay);
                    msg = (Message) MESSAGE_NEXT_FIELD.get(msg);
                }
            } catch (IllegalAccessException e) {
                throw new RuntimeException("Access failed in TelephonyTest", e);
            }
        }
        return delay;
    }

    /**
     * @return {@code true} if there are any messages in the queue.
     */
    private boolean messagesExist() {
        for (TestableLooper looper : mTestableLoopers) {
            MessageQueue queue = looper.getLooper().getQueue();
            try {
                Message msg = (Message) MESSAGE_QUEUE_FIELD.get(queue);
                if (msg != null) return true;
            } catch (IllegalAccessException e) {
                throw new RuntimeException("Access failed in TelephonyTest", e);
            if (looper.peekWhen() > 0) {
                return true;
            }
        }
        return false;
@@ -1388,8 +1347,14 @@ public abstract class TelephonyTest {
     * Handle all messages including the delayed messages.
     */
    public void processAllFutureMessages() {
        final long now = SystemClock.uptimeMillis();
        while (messagesExist()) {
            moveTimeForward(getLongestDelay());
            for (TestableLooper looper : mTestableLoopers) {
                long nextDelay = looper.peekWhen() - now;
                if (nextDelay > 0) {
                    looper.moveTimeForward(nextDelay);
                }
            }
            processAllMessages();
        }
    }
@@ -1414,20 +1379,7 @@ public abstract class TelephonyTest {
     */
    public void moveTimeForward(long milliSeconds) {
        for (TestableLooper looper : mTestableLoopers) {
            MessageQueue queue = looper.getLooper().getQueue();
            try {
                Message msg = (Message) MESSAGE_QUEUE_FIELD.get(queue);
                while (msg != null) {
                    long updatedWhen = msg.getWhen() - milliSeconds;
                    if (updatedWhen < 0) {
                        updatedWhen = 0;
                    }
                    MESSAGE_WHEN_FIELD.set(msg, updatedWhen);
                    msg = (Message) MESSAGE_NEXT_FIELD.get(msg);
                }
            } catch (IllegalAccessException e) {
                throw new RuntimeException("Access failed in TelephonyTest", e);
            }
            looper.moveTimeForward(milliSeconds);
        }
    }
}