diff --git a/packages/react-native/ReactAndroid/api/ReactAndroid.api b/packages/react-native/ReactAndroid/api/ReactAndroid.api index 180ff9fe81476d..ddf91f007b5e3b 100644 --- a/packages/react-native/ReactAndroid/api/ReactAndroid.api +++ b/packages/react-native/ReactAndroid/api/ReactAndroid.api @@ -3041,7 +3041,7 @@ public class com/facebook/react/modules/camera/ImageStoreManager : com/facebook/ public fun getBase64ForTag (Ljava/lang/String;Lcom/facebook/react/bridge/Callback;Lcom/facebook/react/bridge/Callback;)V } -public class com/facebook/react/modules/clipboard/ClipboardModule : com/facebook/fbreact/specs/NativeClipboardSpec { +public final class com/facebook/react/modules/clipboard/ClipboardModule : com/facebook/fbreact/specs/NativeClipboardSpec { public fun (Lcom/facebook/react/bridge/ReactApplicationContext;)V public fun getString (Lcom/facebook/react/bridge/Promise;)V public fun setString (Ljava/lang/String;)V diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/clipboard/ClipboardModule.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/clipboard/ClipboardModule.java deleted file mode 100644 index 8491ac3091dc08..00000000000000 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/clipboard/ClipboardModule.java +++ /dev/null @@ -1,53 +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.clipboard; - -import android.content.ClipData; -import android.content.ClipboardManager; -import com.facebook.fbreact.specs.NativeClipboardSpec; -import com.facebook.react.bridge.Promise; -import com.facebook.react.bridge.ReactApplicationContext; -import com.facebook.react.module.annotations.ReactModule; - -/** A module that allows JS to get/set clipboard contents. */ -@ReactModule(name = NativeClipboardSpec.NAME) -public class ClipboardModule extends NativeClipboardSpec { - - public ClipboardModule(ReactApplicationContext context) { - super(context); - } - - private ClipboardManager getClipboardService() { - return (ClipboardManager) - getReactApplicationContext() - .getSystemService(getReactApplicationContext().CLIPBOARD_SERVICE); - } - - @Override - public void getString(Promise promise) { - try { - ClipboardManager clipboard = getClipboardService(); - ClipData clipData = clipboard.getPrimaryClip(); - if (clipData != null && clipData.getItemCount() >= 1) { - ClipData.Item firstItem = clipboard.getPrimaryClip().getItemAt(0); - promise.resolve("" + firstItem.getText()); - } else { - promise.resolve(""); - } - } catch (Exception e) { - promise.reject(e); - } - } - - @Override - public void setString(String text) { - ClipData clipdata = ClipData.newPlainText(null, text); - ClipboardManager clipboard = getClipboardService(); - clipboard.setPrimaryClip(clipdata); - } -} diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/clipboard/ClipboardModule.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/clipboard/ClipboardModule.kt new file mode 100644 index 00000000000000..f0f21574a144b0 --- /dev/null +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/clipboard/ClipboardModule.kt @@ -0,0 +1,45 @@ +/* + * 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.clipboard + +import android.content.ClipData +import android.content.ClipboardManager +import com.facebook.fbreact.specs.NativeClipboardSpec +import com.facebook.react.bridge.Promise +import com.facebook.react.bridge.ReactApplicationContext +import com.facebook.react.module.annotations.ReactModule + +/** A module that allows JS to get/set clipboard contents. */ +@ReactModule(name = NativeClipboardSpec.NAME) +public class ClipboardModule(context: ReactApplicationContext) : NativeClipboardSpec(context) { + + private val clipboardService: ClipboardManager + get() = + getReactApplicationContext().getSystemService(ReactApplicationContext.CLIPBOARD_SERVICE) + as ClipboardManager + + public override fun getString(promise: Promise) { + try { + val clipboard = clipboardService + val clipData = clipboard.primaryClip + if (clipData != null && clipData.itemCount >= 1) { + val firstItem = clipData.getItemAt(0) + promise.resolve("${firstItem.text}") + } else { + promise.resolve("") + } + } catch (e: Exception) { + promise.reject(e) + } + } + + public override fun setString(text: String?) { + val clipdata: ClipData = ClipData.newPlainText(null, text) + clipboardService.setPrimaryClip(clipdata) + } +}