Skip to content

Commit 80fbbeb

Browse files
fix(terminal): use safe clipboard paste handling
1 parent 1c250e8 commit 80fbbeb

1 file changed

Lines changed: 31 additions & 8 deletions

File tree

‎app/src/main/java/org/randomcoder/udroid/runtime/RuntimeSupervisorService.kt‎

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,14 +81,37 @@ class RuntimeSupervisorService : Service() {
8181
}
8282

8383
override fun onPasteTextFromClipboard(session: TerminalSession) {
84-
val clipboard = getSystemService(ClipboardManager::class.java)
85-
val text =
86-
clipboard.primaryClip
87-
?.getItemAt(0)
88-
?.coerceToText(this@RuntimeSupervisorService)
89-
?.toString()
90-
.orEmpty()
91-
if (text.isNotEmpty()) session.write(text)
84+
try {
85+
val clip = getSystemService(ClipboardManager::class.java).primaryClip
86+
if (clip == null || clip.itemCount == 0) return
87+
88+
val text =
89+
clip
90+
.getItemAt(0)
91+
.coerceToText(this@RuntimeSupervisorService)
92+
?.toString()
93+
.orEmpty()
94+
if (text.isEmpty()) return
95+
96+
// Keep paste semantics in the terminal emulator. It removes
97+
// unsafe control characters, normalizes line endings, and
98+
// wraps input when applications such as nano enable
99+
// bracketed-paste mode.
100+
session.emulator.paste(text)
101+
} catch (error: RuntimeException) {
102+
Log.e("uDroidTerminal", "Could not paste clipboard text", error)
103+
app.journal.append(
104+
component = "terminal",
105+
severity = "error",
106+
event = "clipboard_paste_failed",
107+
message = "Android clipboard paste failed",
108+
bootId = app.runtimeState.current().bootId,
109+
fields =
110+
mapOf(
111+
"error_type" to error.javaClass.simpleName,
112+
),
113+
)
114+
}
92115
}
93116

94117
override fun onBell(session: TerminalSession) = Unit

0 commit comments

Comments
 (0)