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

Commit 31ad9f46 authored by Makoto Onuki's avatar Makoto Onuki
Browse files

Fix waitForLooperDone()

We need to post an empty message to ensure the idle handler is always
triggered.

Bug: 292141694
Flag: TEST_ONLY
Test: $ANDROID_BUILD_TOP/frameworks/base/ravenwood/scripts/run-ravenwood-tests.sh -r
Change-Id: I2698bee7c807db2ec8a0eef607fdde5e75164914
parent 5a90495a
Loading
Loading
Loading
Loading
+18 −3
Original line number Diff line number Diff line
@@ -22,8 +22,11 @@ import android.os.Looper;
import android.os.MessageQueue;
import android.util.Log;

import com.android.internal.annotations.GuardedBy;
import com.android.ravenwood.common.SneakyThrow;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
@@ -38,15 +41,24 @@ public class RavenwoodUtils {

    private static final int DEFAULT_TIMEOUT_SECONDS = 10;

    private class MainHandlerHolder {
        static Handler sMainHandler = new Handler(Looper.getMainLooper());
    @GuardedBy("sHandlers")
    private static final Map<Looper, Handler> sHandlers = new HashMap<>();

    /**
     * Return a handler for any looper.
     */
    @NonNull
    private static Handler getHandler(@NonNull Looper looper) {
        synchronized (sHandlers) {
            return sHandlers.computeIfAbsent(looper, (l) -> new Handler(l));
        }
    }

    /**
     * Returns the main thread handler.
     */
    public static Handler getMainHandler() {
        return MainHandlerHolder.sMainHandler;
        return getHandler(Looper.getMainLooper());
    }

    /**
@@ -123,6 +135,9 @@ public class RavenwoodUtils {
    public static void waitForLooperDone(Looper looper) {
        var idler = new Idler();
        looper.getQueue().addIdleHandler(idler);
        // Wake up the queue, if sleeping.
        getHandler(looper).post(() -> {});

        idler.waitForIdle();

        sPendingExceptionThrower.run();