From e398505372cf2e19257cf8e3b6cbb20af150caae Mon Sep 17 00:00:00 2001 From: QQ Date: Sat, 14 Nov 2026 14:09:32 +0800 Subject: [PATCH] Import apps/android/plugins/remote-foreground-service/RemoteProcessTextModule.kt --- .../RemoteProcessTextModule.kt | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 apps/android/plugins/remote-foreground-service/RemoteProcessTextModule.kt diff --git a/apps/android/plugins/remote-foreground-service/RemoteProcessTextModule.kt b/apps/android/plugins/remote-foreground-service/RemoteProcessTextModule.kt new file mode 100644 index 0000000..86422f1 --- /dev/null +++ b/apps/android/plugins/remote-foreground-service/RemoteProcessTextModule.kt @@ -0,0 +1,71 @@ +package io.github.liguobao.dshremote + +import android.app.Activity +import android.content.Intent +import com.facebook.react.bridge.ActivityEventListener +import com.facebook.react.bridge.Promise +import com.facebook.react.bridge.ReactApplicationContext +import com.facebook.react.bridge.ReactContextBaseJavaModule +import com.facebook.react.bridge.ReactMethod +import com.facebook.react.modules.core.DeviceEventManagerModule + +class RemoteProcessTextModule( + private val reactContext: ReactApplicationContext +) : ReactContextBaseJavaModule(reactContext), ActivityEventListener { + + private var pendingText: String? = null + + init { + reactContext.addActivityEventListener(this) + } + + override fun getName(): String = "DshProcessText" + + @ReactMethod + fun getInitialText(promise: Promise) { + val currentActivity = reactContext.currentActivity + val fromIntent = extractProcessText(currentActivity?.intent) + if (fromIntent != null) { + // Consume once so it doesn't replay when remounting or switching sessions + currentActivity?.intent?.removeExtra(Intent.EXTRA_PROCESS_TEXT) + } + val text = fromIntent ?: pendingText + pendingText = null + promise.resolve(text) + } + + override fun onNewIntent(intent: Intent) { + try { + val text = extractProcessText(intent) + if (!text.isNullOrEmpty()) { + if (reactContext.hasActiveReactInstance()) { + reactContext + .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java) + .emit("onProcessText", text) + } else { + pendingText = text + } + } + } catch (_: Throwable) { + val text = extractProcessText(intent) + if (!text.isNullOrEmpty()) { + pendingText = text + } + } + } + + override fun onActivityResult(activity: Activity, requestCode: Int, resultCode: Int, data: Intent?) {} + + override fun invalidate() { + super.invalidate() + reactContext.removeActivityEventListener(this) + } + + private fun extractProcessText(intent: Intent?): String? { + if (intent?.action == Intent.ACTION_PROCESS_TEXT) { + val text = intent.getCharSequenceExtra(Intent.EXTRA_PROCESS_TEXT) + return text?.toString() + } + return null + } +}