1 /*
2  * Copyright 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package androidx.camera.testing.impl;
18 
19 import android.os.Handler;
20 import android.os.Looper;
21 import android.os.MessageQueue;
22 
23 import androidx.camera.testing.impl.compat.LooperCompat;
24 
25 import org.jspecify.annotations.NonNull;
26 
27 import java.util.concurrent.Semaphore;
28 
29 /** Utility functions for {@link Handler} */
30 public final class HandlerUtil {
31     /**
32      * Wait for the {@link Looper} of the given {@link Handler} to idle.
33      *
34      * @throws InterruptedException if thread for the handler is interrupted while waiting
35      * @throws RuntimeException if unable to obtain the {@link MessageQueue} for the {@link
36      * Handler}.
37      */
waitForLooperToIdle(@onNull Handler handler)38     public static void waitForLooperToIdle(@NonNull Handler handler) throws InterruptedException {
39         final Looper looper = handler.getLooper();
40         final Semaphore semaphore = new Semaphore(0);
41 
42         // Post a message that will add the idle handler. This will ensure the handler is not
43         // already idle before setting the idle handler, causing the idle handler to never be
44         // called.
45         handler.post(() -> {
46             MessageQueue messageQueue = LooperCompat.getQueue(looper);
47             messageQueue.addIdleHandler(() -> {
48                 semaphore.release();
49                 return false;
50             });
51         });
52 
53         // Wait for idle
54         semaphore.acquire();
55     }
56 
HandlerUtil()57     private HandlerUtil() {
58     }
59 }
60