From 4d647be84860b2b7bb161db69709caf870ef88ca Mon Sep 17 00:00:00 2001 From: Ruslan Shestopalyuk Date: Tue, 30 Jul 2024 05:29:58 -0700 Subject: [PATCH] Kotlinify modules/core interfaces (#45787) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/45787 # Changelog: [Internal] - As in the title. Reviewed By: cortinico Differential Revision: D60377675 fbshipit-source-id: 80670c6a996ea3bbc4adce9733be0e0f81223514 --- ....java => DefaultHardwareBackBtnHandler.kt} | 10 +++---- .../facebook/react/modules/core/JSTimers.java | 21 ------------- .../facebook/react/modules/core/JSTimers.kt | 21 +++++++++++++ ...ecutor.java => JavaScriptTimerExecutor.kt} | 15 +++++----- .../modules/core/PermissionAwareActivity.java | 29 ------------------ .../modules/core/PermissionAwareActivity.kt | 30 +++++++++++++++++++ ...ionListener.java => PermissionListener.kt} | 18 +++++------ .../core/RCTNativeAppEventEmitter.java | 18 ----------- .../modules/core/RCTNativeAppEventEmitter.kt | 17 +++++++++++ .../modules/permissions/PermissionsModule.kt | 2 +- 10 files changed, 89 insertions(+), 92 deletions(-) rename packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/{DefaultHardwareBackBtnHandler.java => DefaultHardwareBackBtnHandler.kt} (74%) delete mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/JSTimers.java create mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/JSTimers.kt rename packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/{JavaScriptTimerExecutor.java => JavaScriptTimerExecutor.kt} (69%) delete mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/PermissionAwareActivity.java create mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/PermissionAwareActivity.kt rename packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/{PermissionListener.java => PermissionListener.kt} (64%) delete mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/RCTNativeAppEventEmitter.java create mode 100644 packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/RCTNativeAppEventEmitter.kt diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/DefaultHardwareBackBtnHandler.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/DefaultHardwareBackBtnHandler.kt similarity index 74% rename from packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/DefaultHardwareBackBtnHandler.java rename to packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/DefaultHardwareBackBtnHandler.kt index 241704e6b51..90c2f234731 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/DefaultHardwareBackBtnHandler.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/DefaultHardwareBackBtnHandler.kt @@ -5,19 +5,17 @@ * LICENSE file in the root directory of this source tree. */ -package com.facebook.react.modules.core; - +package com.facebook.react.modules.core /** - * Interface used by {@link DeviceEventManagerModule} to delegate hardware back button events. It's + * Interface used by [DeviceEventManagerModule] to delegate hardware back button events. It's * suppose to provide a default behavior since it would be triggered in the case when JS side * doesn't want to handle back press events. */ -public interface DefaultHardwareBackBtnHandler { - +public fun interface DefaultHardwareBackBtnHandler { /** * By default, all onBackPress() calls should not execute the default backpress handler and should * instead propagate it to the JS instance. If JS doesn't want to handle the back press itself, it * shall call back into native to invoke this function which should execute the default handler */ - void invokeDefaultOnBackPressed(); + public fun invokeDefaultOnBackPressed() } diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/JSTimers.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/JSTimers.java deleted file mode 100644 index 74455dc1637..00000000000 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/JSTimers.java +++ /dev/null @@ -1,21 +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.modules.core; - -import com.facebook.proguard.annotations.DoNotStrip; -import com.facebook.react.bridge.JavaScriptModule; -import com.facebook.react.bridge.WritableArray; - -@DoNotStrip -public interface JSTimers extends JavaScriptModule { - void callTimers(WritableArray timerIDs); - - void callIdleCallbacks(double frameTime); - - void emitTimeDriftWarning(String warningMessage); -} diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/JSTimers.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/JSTimers.kt new file mode 100644 index 00000000000..47946ca72bd --- /dev/null +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/JSTimers.kt @@ -0,0 +1,21 @@ +/* + * 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.modules.core + +import com.facebook.proguard.annotations.DoNotStrip +import com.facebook.react.bridge.JavaScriptModule +import com.facebook.react.bridge.WritableArray + +@DoNotStrip +public interface JSTimers : JavaScriptModule { + public fun callTimers(timerIDs: WritableArray) + + public fun callIdleCallbacks(frameTime: Double) + + public fun emitTimeDriftWarning(warningMessage: String) +} diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/JavaScriptTimerExecutor.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/JavaScriptTimerExecutor.kt similarity index 69% rename from packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/JavaScriptTimerExecutor.java rename to packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/JavaScriptTimerExecutor.kt index 768d63a9382..413681e657e 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/JavaScriptTimerExecutor.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/JavaScriptTimerExecutor.kt @@ -5,33 +5,32 @@ * LICENSE file in the root directory of this source tree. */ -package com.facebook.react.modules.core; +package com.facebook.react.modules.core -import com.facebook.react.bridge.WritableArray; +import com.facebook.react.bridge.WritableArray -/** An interface used by {@link JavaTimerManager} to access and call JS timers from Java. */ +/** An interface used by [JavaTimerManager] to access and call JS timers from Java. */ public interface JavaScriptTimerExecutor { - /** * Calls the JS callback(s) associated with the timer ID(s). Also unregisters the callback if the * timer isn't recurring (e.g. unregisters for setTimeout, doesn't for setInterval). * * @param timerIDs An array of timer handles to call. Accepts an array as an optimization, to - * avoid unnecessary JNI calls. + * avoid unnecessary JNI calls. */ - void callTimers(WritableArray timerIDs); + public fun callTimers(timerIDs: WritableArray) /** * Invoke the JS callback registered with `requestIdleCallback`. * * @param frameTime The amount of time left in the frame, in ms. */ - void callIdleCallbacks(double frameTime); + public fun callIdleCallbacks(frameTime: Double) /** * Shows a warning message in development when environment times are out of sync. * * @param warningMessage The message to show */ - void emitTimeDriftWarning(String warningMessage); + public fun emitTimeDriftWarning(warningMessage: String) } diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/PermissionAwareActivity.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/PermissionAwareActivity.java deleted file mode 100644 index 21c075c0b8d..00000000000 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/PermissionAwareActivity.java +++ /dev/null @@ -1,29 +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.modules.core; - -import android.app.Activity; - -/** - * Interface used to denote activities that can forward permission requests and call {@link - * PermissionListener}s with the permission request results. - */ -public interface PermissionAwareActivity { - - /** See {@link Activity#checkPermission}. */ - int checkPermission(String permission, int pid, int uid); - - /** See {@link Activity#checkSelfPermission}. */ - int checkSelfPermission(String permission); - - /** See {@link Activity#shouldShowRequestPermissionRationale}. */ - boolean shouldShowRequestPermissionRationale(String permission); - - /** See {@link Activity#requestPermissions}. */ - void requestPermissions(String[] permissions, int requestCode, PermissionListener listener); -} diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/PermissionAwareActivity.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/PermissionAwareActivity.kt new file mode 100644 index 00000000000..f4089724d54 --- /dev/null +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/PermissionAwareActivity.kt @@ -0,0 +1,30 @@ +/* + * 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.modules.core + +/** + * Interface used to denote activities that can forward permission requests and call + * [PermissionListener] with the permission request results. + */ +public interface PermissionAwareActivity { + /** See [Activity.checkPermission]. */ + public fun checkPermission(permission: String, pid: Int, uid: Int): Int + + /** See [Activity.checkSelfPermission]. */ + public fun checkSelfPermission(permission: String): Int + + /** See [Activity.shouldShowRequestPermissionRationale]. */ + public fun shouldShowRequestPermissionRationale(permission: String): Boolean + + /** See [Activity.requestPermissions]. */ + public fun requestPermissions( + permissions: Array, + requestCode: Int, + listener: PermissionListener? + ) +} diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/PermissionListener.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/PermissionListener.kt similarity index 64% rename from packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/PermissionListener.java rename to packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/PermissionListener.kt index 7c93305b56b..881fc979be7 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/PermissionListener.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/PermissionListener.kt @@ -5,21 +5,21 @@ * LICENSE file in the root directory of this source tree. */ -package com.facebook.react.modules.core; - -import android.app.Activity; - +package com.facebook.react.modules.core /** * Interface used by activities to delegate permission request results. Classes implementing this * class will be notified whenever there's a result for a permission request. */ -public interface PermissionListener { - +public fun interface PermissionListener { /** - * Method called whenever there's a result to a permission request. It is forwarded from {@link - * Activity#onRequestPermissionsResult}. + * Method called whenever there's a result to a permission request. It is forwarded from + * [Activity.onRequestPermissionsResult]. * * @return boolean Whether the PermissionListener can be removed. */ - boolean onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults); + public fun onRequestPermissionsResult( + requestCode: Int, + permissions: Array, + grantResults: IntArray + ): Boolean } diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/RCTNativeAppEventEmitter.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/RCTNativeAppEventEmitter.java deleted file mode 100644 index c2cf5d03b1e..00000000000 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/RCTNativeAppEventEmitter.java +++ /dev/null @@ -1,18 +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.modules.core; - -import androidx.annotation.Nullable; -import com.facebook.proguard.annotations.DoNotStrip; -import com.facebook.react.bridge.JavaScriptModule; - -/** Module that handles global application events. */ -@DoNotStrip -public interface RCTNativeAppEventEmitter extends JavaScriptModule { - void emit(String eventName, @Nullable Object data); -} diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/RCTNativeAppEventEmitter.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/RCTNativeAppEventEmitter.kt new file mode 100644 index 00000000000..7cfe0289fcf --- /dev/null +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/RCTNativeAppEventEmitter.kt @@ -0,0 +1,17 @@ +/* + * 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.modules.core + +import com.facebook.proguard.annotations.DoNotStrip +import com.facebook.react.bridge.JavaScriptModule + +/** Module that handles global application events. */ +@DoNotStrip +public fun interface RCTNativeAppEventEmitter : JavaScriptModule { + public fun emit(eventName: String, data: Any?) +} diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/permissions/PermissionsModule.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/permissions/PermissionsModule.kt index c759b8af7cf..508b5ea21d3 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/permissions/PermissionsModule.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/permissions/PermissionsModule.kt @@ -51,7 +51,7 @@ public class PermissionsModule(reactContext: ReactApplicationContext?) : * [permissionAwareActivity.shouldShowRequestPermissionRationale]. */ override public fun shouldShowRequestPermissionRationale( - permission: String?, + permission: String, promise: Promise ): Unit { try {