Skip to content

Your Android phone and your Windows PC, working as one. Local-first, end-to-end encrypted, no account.

Android: no "copied" pop-up on the phone for the PC's clipboard

Bee95d7f482e37c
diff --git a/CHANGELOG.md b/CHANGELOG.md
index d4c1599..fcb4006 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -61,6 +61,10 @@ change things that need a step from you, and its notes say so.
- **Phone sound never falls back to the microphone.** On a phone that blocks
capturing what it plays, Elevated recorded the microphone instead and sent
it to the PC as the phone's sound. It now sends no sound.
+- **No "copied" pop-up on the phone for copies made on the PC.** With
+ Elevated on, text from the PC goes on the phone's clipboard without the
+ copy toast or preview Android and One UI show for every copy. Without
+ Elevated, Android doesn't let an app avoid it.
- Android: Phone as webcam no longer crashes the app when its preview
comes back (the same fix covers the QR scanner and Continuity Camera).
- Android: on Hive, the category chips at the top of Settings and the
diff --git a/android/app/src/main/kotlin/app/nectarlink/android/clipboard/PhoneClipboard.kt b/android/app/src/main/kotlin/app/nectarlink/android/clipboard/PhoneClipboard.kt
index d7985eb..60edbdd 100644
--- a/android/app/src/main/kotlin/app/nectarlink/android/clipboard/PhoneClipboard.kt
+++ b/android/app/src/main/kotlin/app/nectarlink/android/clipboard/PhoneClipboard.kt
@@ -159,6 +159,8 @@ object PhoneClipboard {
if (existingText != null && normalizeClipText(existingText) == normalizeClipText(text) && cleanHtml == null) {
return@runCatching true
}
+ // The shell's copy can ask SystemUI not to show it; the app's can't.
+ if (app.nectarlink.android.elevated.Elevated.setClipboard(text, cleanHtml)) return@runCatching true
val clip = if (cleanHtml != null) {
ClipData.newHtmlText("Nectarlink", text, cleanHtml)
} else {
diff --git a/android/app/src/main/kotlin/app/nectarlink/android/elevated/Elevated.kt b/android/app/src/main/kotlin/app/nectarlink/android/elevated/Elevated.kt
index 61fe995..24dc5a3 100644
--- a/android/app/src/main/kotlin/app/nectarlink/android/elevated/Elevated.kt
+++ b/android/app/src/main/kotlin/app/nectarlink/android/elevated/Elevated.kt
@@ -666,6 +666,17 @@ object Elevated {
return send(writer, "C $command")
}
+ /**
+ * Puts a PC's text on the clipboard through the helper, so the phone
+ * doesn't pop up "copied" for a copy made on the PC. False when the
+ * helper isn't running (the app writes it then).
+ */
+ fun setClipboard(text: String, html: String?): Boolean {
+ val writer = input ?: return false
+ val b64 = { value: String -> android.util.Base64.encodeToString(value.toByteArray(Charsets.UTF_8), android.util.Base64.NO_WRAP) }
+ return send(writer, "B ${b64(text)} ${html?.let(b64).orEmpty()}")
+ }
+
/** Turns the physical screen panel off or back on while mirroring. */
fun setScreenOff(off: Boolean): Boolean {
screenOffRequested = off
diff --git a/android/app/src/main/kotlin/app/nectarlink/android/elevated/InputServer.kt b/android/app/src/main/kotlin/app/nectarlink/android/elevated/InputServer.kt
index c1514ea..48da266 100644
--- a/android/app/src/main/kotlin/app/nectarlink/android/elevated/InputServer.kt
+++ b/android/app/src/main/kotlin/app/nectarlink/android/elevated/InputServer.kt
@@ -123,6 +123,34 @@ object InputServer {
@Volatile private var clipListenerProxy: Any? = null
+ /** The clipboard service and its setPrimaryClip, once the watcher found them. */
+ @Volatile private var clipService: Pair<Any, Method>? = null
+
+ /**
+ * Puts a PC's text on the clipboard as the shell, marked to suppress the
+ * clipboard overlay: SystemUI (Samsung's copy toast too) honours that
+ * only for clips the shell sets, so the copy doesn't pop up on the phone.
+ */
+ private fun setClipboard(rest: String) {
+ val (service, set) = clipService ?: return System.err.println("no clipboard to set")
+ runCatching {
+ val parts = rest.split(' ')
+ val decode = { b64: String -> String(android.util.Base64.decode(b64, android.util.Base64.NO_WRAP), Charsets.UTF_8) }
+ val text = decode(parts[0])
+ val html = parts.getOrNull(1)?.takeIf(String::isNotEmpty)?.let(decode)
+ val clip = if (html != null) {
+ android.content.ClipData.newHtmlText("Nectarlink", text, html)
+ } else {
+ android.content.ClipData.newPlainText("Nectarlink", text)
+ }
+ clip.description.extras = android.os.PersistableBundle().apply {
+ putBoolean("com.android.systemui.SUPPRESS_CLIPBOARD_OVERLAY", true)
+ putBoolean("android.content.extra.IS_REMOTE_DEVICE", true)
+ }
+ set.invoke(service, *buildClipboardArgs(set.parameterTypes, null, clip))
+ }.onFailure { System.err.println("can't set the clipboard: $it") }
+ }
+
@android.annotation.SuppressLint("PrivateApi", "DiscouragedPrivateApi")
private fun startClipboardWatcher() {
runCatching {
@@ -131,6 +159,7 @@ object InputServer {
val stub = Class.forName("android.content.IClipboard\$Stub")
val clipboard = stub.getMethod("asInterface", android.os.IBinder::class.java).invoke(null, binder) ?: return
val getMethod = clipboard.javaClass.methods.firstOrNull { it.name == "getPrimaryClip" } ?: return
+ clipboard.javaClass.methods.firstOrNull { it.name == "setPrimaryClip" }?.let { clipService = clipboard to it }
val addMethod = clipboard.javaClass.methods.firstOrNull { it.name == "addPrimaryClipChangedListener" } ?: return
val listenerClass = Class.forName("android.content.IOnPrimaryClipChangedListener")
val listenerBinder = object : android.os.Binder() {
@@ -533,6 +562,7 @@ object InputServer {
}
}
"P" -> if (display < 0) setDisplayPower(rest.trim() != "0")
+ "B" -> if (display < 0) setClipboard(rest)
}
}