Files
openless/openless-all/app/android/kotlin/OpenLessAccessibilityCommandReceiver.kt
TRIPandHKLHaoBin 6f9af89583 release: OpenLess 2.0.0-Beta.4+build.20260930 (#1126)
Integrate ASR/Omni fixes, API Route, Android IME and confirmation-based vocabulary learning. Complete settings transitions and macOS single-item credential storage. Synchronize all application versions to 2.0.0-Beta.4+build.20260930.

Integrated PRs: #1116, #1120, #1121, #1123, #1124, #1067 and #1128. Linux #1117 remains independent and is excluded from automatic release.

Validated: full cross-platform CI, 18 UI motion cases, 112 frontend test files, 1245 Core tests (1 ignored) and 572 macOS native tests.

Co-authored-by: HKLHaoBin <we3q@qq.com>
2026-09-30 20:32:53 +08:00

77 lines
3.0 KiB
Kotlin

package com.openless.app
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.os.ResultReceiver
import android.util.Log
class OpenLessAccessibilityCommandReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent?) {
val action = intent?.action ?: return
if (action == ACTION_VOCABULARY) {
OpenLessAccessibilityService.handleVocabularyCommand(intent)
return
}
val receiver = resultReceiver(intent) ?: return
when (action) {
ACTION_PASTE -> {
val pasteText = intent.getStringExtra(EXTRA_PASTE_TEXT)
val result = OpenLessAccessibilityService.performPasteFromCommand(pasteText)
sendResult(receiver, result)
if (result != AccessibilityPasteResult.SUCCESS) {
Log.w(TAG, "paste command failed reason=${result.reason}")
}
}
ACTION_PING -> {
val result =
if (OpenLessAccessibilityService.instance != null) {
AccessibilityPasteResult.SUCCESS
} else {
AccessibilityPasteResult.SERVICE_NOT_CONNECTED
}
sendResult(receiver, result)
}
ACTION_CAPTURE_SELECTED_TEXT -> {
val selectedText = OpenLessAccessibilityService.captureSelectedTextFromCommand()
receiver.send(
if (selectedText != null) {
AccessibilityPasteResult.SUCCESS.code
} else {
AccessibilityPasteResult.SERVICE_NOT_CONNECTED.code
},
Bundle().apply {
putString(EXTRA_SELECTED_TEXT, selectedText.orEmpty())
},
)
}
}
}
private fun sendResult(receiver: ResultReceiver, result: AccessibilityPasteResult) {
receiver.send(
result.code,
Bundle().apply { putString(EXTRA_RESULT_REASON, result.reason) },
)
}
@Suppress("DEPRECATION")
private fun resultReceiver(intent: Intent): ResultReceiver? {
return intent.getParcelableExtra(EXTRA_RESULT_RECEIVER) as? ResultReceiver
}
companion object {
const val ACTION_VOCABULARY = "com.openless.app.accessibility.VOCABULARY"
const val ACTION_PASTE = "com.openless.app.accessibility.PASTE"
const val ACTION_PING = "com.openless.app.accessibility.PING"
const val ACTION_CAPTURE_SELECTED_TEXT =
"com.openless.app.accessibility.CAPTURE_SELECTED_TEXT"
const val EXTRA_RESULT_RECEIVER = "result_receiver"
const val EXTRA_RESULT_REASON = "result_reason"
const val EXTRA_PASTE_TEXT = "paste_text"
const val EXTRA_SELECTED_TEXT = "selected_text"
private const val TAG = "OpenLessA11yCommand"
}
}