Files
react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/UiThreadUtil.java
T
Andres Suarez 3b31e69e28 Tidy up license headers [2/n]
Summary: Changelog: [General] [Fixed] - License header cleanup

Reviewed By: yungsters

Differential Revision: D17952694

fbshipit-source-id: 17c87de7ebb271fa2ac8d00af72a4d1addef8bd0
2019-10-16 10:06:34 -07:00

49 lines
1.6 KiB
Java

/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
package com.facebook.react.bridge;
import android.os.Handler;
import android.os.Looper;
import androidx.annotation.Nullable;
/** Utility for interacting with the UI thread. */
public class UiThreadUtil {
@Nullable private static Handler sMainHandler;
/** @return {@code true} if the current thread is the UI thread. */
public static boolean isOnUiThread() {
return Looper.getMainLooper().getThread() == Thread.currentThread();
}
/** Throws an {@link AssertionException} if the current thread is not the UI thread. */
public static void assertOnUiThread() {
SoftAssertions.assertCondition(isOnUiThread(), "Expected to run on UI thread!");
}
/** Throws an {@link AssertionException} if the current thread is the UI thread. */
public static void assertNotOnUiThread() {
SoftAssertions.assertCondition(!isOnUiThread(), "Expected not to run on UI thread!");
}
/** Runs the given {@code Runnable} on the UI thread. */
public static void runOnUiThread(Runnable runnable) {
runOnUiThread(runnable, 0);
}
/** Runs the given {@code Runnable} on the UI thread with the specified delay. */
public static void runOnUiThread(Runnable runnable, long delayInMs) {
synchronized (UiThreadUtil.class) {
if (sMainHandler == null) {
sMainHandler = new Handler(Looper.getMainLooper());
}
}
sMainHandler.postDelayed(runnable, delayInMs);
}
}