Migrate UiThreadUtil to Kotlin (#50536)

Summary:
This PR aims to migrate UiThreadUtil  from Java to kotlin as part of https://github.com/facebook/react-native/issues/50513

## Changelog:

<!-- Help reviewers and the release process by writing your own changelog entry.

Pick one each for the category and type tags:

[ANDROID|GENERAL|IOS|INTERNAL] [BREAKING|ADDED|CHANGED|DEPRECATED|REMOVED|FIXED|SECURITY] - Message

For more details, see:
https://reactnative.dev/contributing/changelogs-in-pull-requests
-->
[ANDROID][CHANGED]Migrate UiThreadUtil to Kotlin

Pull Request resolved: https://github.com/facebook/react-native/pull/50536

Test Plan: Tested on RN tester with both new and old arch

Reviewed By: cortinico

Differential Revision: D72602041

Pulled By: arushikesarwani94

fbshipit-source-id: 9f45a139805819a21039eb640a0bd1583a3acde9
This commit is contained in:
riteshshukla04 authored and Facebook GitHub Bot committed 2025-04-08 12:11:32 -07:00
1 parent 7b01943337
commit 1033584c20
4 files changed
+81 -85

No files matched your search

@@ -1476,15 +1476,15 @@ public abstract interface class com/facebook/react/bridge/UIManagerProvider {
public abstract fun createUIManager (Lcom/facebook/react/bridge/ReactApplicationContext;)Lcom/facebook/react/bridge/UIManager;
}
public class com/facebook/react/bridge/UiThreadUtil {
public fun <init> ()V
public static fun assertNotOnUiThread ()V
public static fun assertOnUiThread ()V
public static fun getUiThreadHandler ()Landroid/os/Handler;
public static fun isOnUiThread ()Z
public static fun removeOnUiThread (Ljava/lang/Runnable;)V
public static fun runOnUiThread (Ljava/lang/Runnable;)Z
public static fun runOnUiThread (Ljava/lang/Runnable;J)Z
public final class com/facebook/react/bridge/UiThreadUtil {
public static final field INSTANCE Lcom/facebook/react/bridge/UiThreadUtil;
public static final fun assertNotOnUiThread ()V
public static final fun assertOnUiThread ()V
public static final fun getUiThreadHandler ()Landroid/os/Handler;
public static final fun isOnUiThread ()Z
public static final fun removeOnUiThread (Ljava/lang/Runnable;)V
public static final fun runOnUiThread (Ljava/lang/Runnable;)Z
public static final fun runOnUiThread (Ljava/lang/Runnable;J)Z
}
public final class com/facebook/react/bridge/UnexpectedNativeTypeException : java/lang/RuntimeException {
@@ -1,75 +0,0 @@
/*
* Copyright (c) Meta Platforms, Inc. and 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;
import com.facebook.react.common.build.ReactBuildConfig;
/** Utility for interacting with the UI thread. */
public class UiThreadUtil {
private static volatile @Nullable Handler sMainHandler;
public static Handler getUiThreadHandler() {
// Double null-check to avoid unnecessary lock
if (sMainHandler == null) {
synchronized (UiThreadUtil.class) {
if (sMainHandler == null) {
sMainHandler = new Handler(Looper.getMainLooper());
}
}
}
return 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. This is a noop
* in production, and is only meant to run in debug mode! If you need to check for
* incorrect-thread issues in production, duplicate this code and call it elsewhere.
*/
public static void assertOnUiThread() {
if (ReactBuildConfig.DEBUG) {
SoftAssertions.assertCondition(isOnUiThread(), "Expected to run on UI thread!");
}
}
/**
* Throws an {@link AssertionException} if the current thread is the UI thread. This is a noop in
* production, and is only meant to run in debug mode! If you need to check for incorrect-thread
* issues in production, duplicate this code and call it elsewhere.
*/
public static void assertNotOnUiThread() {
if (ReactBuildConfig.DEBUG) {
SoftAssertions.assertCondition(!isOnUiThread(), "Expected not to run on UI thread!");
}
}
/** Runs the given {@code Runnable} on the UI thread. */
public static boolean runOnUiThread(Runnable runnable) {
return getUiThreadHandler().postDelayed(runnable, 0);
}
/** Runs the given {@code Runnable} on the UI thread with the specified delay. */
public static boolean runOnUiThread(Runnable runnable, long delayInMs) {
return getUiThreadHandler().postDelayed(runnable, delayInMs);
}
/** Removes the given {@code Runnable} on the UI thread. */
public static void removeOnUiThread(Runnable runnable) {
getUiThreadHandler().removeCallbacks(runnable);
}
}
@@ -0,0 +1,69 @@
/*
* Copyright (c) Meta Platforms, Inc. and 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 com.facebook.react.common.build.ReactBuildConfig
/** Utility for interacting with the UI thread. */
public object UiThreadUtil {
private val mainHandler: Handler by
lazy(LazyThreadSafetyMode.NONE) { Handler(Looper.getMainLooper()) }
/**
* Returns the handler associated with the main (UI) thread.
*
* @return The handler for the main thread
*/
@JvmStatic public fun getUiThreadHandler(): Handler = mainHandler
/** @return `true` if the current thread is the UI thread. */
@JvmStatic
public fun isOnUiThread(): Boolean = Looper.getMainLooper().thread == Thread.currentThread()
/**
* Throws an [AssertionException] if the current thread is not the UI thread. This is a no-op in
* production and is only meant to run in debug mode! If you need to check for incorrect-thread
* issues in production, duplicate this code and call it elsewhere.
*/
@JvmStatic
public fun assertOnUiThread() {
if (ReactBuildConfig.DEBUG) {
SoftAssertions.assertCondition(isOnUiThread(), "Expected to run on UI thread!")
}
}
/**
* Throws an [AssertionException] if the current thread is the UI thread. This is a noop in
* production, and is only meant to run in debug mode! If you need to check for incorrect-thread
* issues in production, duplicate this code and call it elsewhere.
*/
@JvmStatic
public fun assertNotOnUiThread() {
if (ReactBuildConfig.DEBUG) {
SoftAssertions.assertCondition(!isOnUiThread(), "Expected not to run on UI thread!")
}
}
/** Runs the given [Runnable] on the UI thread. */
@JvmStatic
public fun runOnUiThread(runnable: Runnable): Boolean = mainHandler.postDelayed(runnable, 0)
/** Runs the given [Runnable] on the UI thread after the specified delay. */
@JvmStatic
public fun runOnUiThread(runnable: Runnable, delayInMs: Long): Boolean =
mainHandler.postDelayed(runnable, delayInMs)
/** Removes the given [Runnable] on the UI thread. */
@JvmStatic
public fun removeOnUiThread(runnable: Runnable) {
mainHandler.removeCallbacks(runnable)
}
}
@@ -35,7 +35,9 @@ internal class DebuggingOverlay(context: Context) : View(context) {
for (traceUpdate in traceUpdates) {
val traceUpdateId = traceUpdate.id
if (traceUpdateIdToCleanupRunnableMap.containsKey(traceUpdateId)) {
UiThreadUtil.removeOnUiThread(traceUpdateIdToCleanupRunnableMap[traceUpdateId])
traceUpdateIdToCleanupRunnableMap[traceUpdateId]?.let { runnable ->
UiThreadUtil.removeOnUiThread(runnable)
}
traceUpdateIdToCleanupRunnableMap.remove(traceUpdateId)
}