Skip to content

Commit 446fcd6

Browse files
committed
fix(android): Elevated watches the clipboard only while a device takes the phone's copies (#655)
The helper registered a clipboard listener for the shell at start and read every copy, whether or not any device takes the phone's clipboard. On a Xiaomi with HyperOS, paste then came back empty in every app until Elevated was turned off. The app now tells the helper (W 0 / W 1) whether any paired device has Share my clipboard on, at each connection and when that changes, and the helper adds or removes its listener to match. Signed-off-by: Bee <302257494+thtbee@users.noreply.github.com>
1 parent 705535e commit 446fcd6

4 files changed

Lines changed: 61 additions & 1 deletion

File tree

‎CHANGELOG.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,11 @@ change things that need a step from you, and its notes say so.
594594

595595
### Fixed
596596

597+
- **Copy and paste on the phone works with Elevated on** for phones where it pasted nothing in
598+
every app (a Xiaomi with HyperOS). Elevated only watches the clipboard while some device takes
599+
the phone's copies by itself, so with **Share my clipboard with it** off for every device, it
600+
doesn't touch the clipboard. Thanks to [@golip37](https://github.com/golip37)
601+
([#655](https://github.com/thtbee/nectarlink/issues/655)).
597602
- **Clicking Play on a phone video in Photos more than once downloads it once.** The button shows
598603
that it's fetching and waits. Thanks to [@SolomonNumb1](https://github.com/SolomonNumb1)
599604
([#666](https://github.com/thtbee/nectarlink/pull/666)).

‎android/app/src/main/kotlin/app/nectarlink/android/core/Core.kt‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,16 @@ class Core(context: Context, private val scope: CoroutineScope) : EventListener
456456
app.nectarlink.android.diag.Crashes.noteExits(context)
457457
// Today's count, and a report that couldn't be sent before; off the start path.
458458
currentRunScope.launch(Dispatchers.IO) { runCatching { app.nectarlink.android.diag.Reports.followUp(context) } }
459+
// The Elevated helper watches the clipboard only while some device takes
460+
// this phone's copies by itself (#655).
461+
currentRunScope.launch {
462+
combine(
463+
_state.map { s -> s.devices.map { it.id } }.distinctUntilChanged(),
464+
app.nectarlink.android.clipboard.ClipboardPeers.notShared(context),
465+
) { ids, notShared -> ids.any { it !in notShared } }
466+
.distinctUntilChanged()
467+
.collect { app.nectarlink.android.elevated.Elevated.setClipboardWatch(it) }
468+
}
459469
val initialAwayMode = app.nectarlink.android.ui.Preferences.isAwayModeEnabled(context)
460470
val initialUsbOnly = app.nectarlink.android.ui.Preferences.isUsbOnlyEnabled(context)
461471
val options = NodeOptions(

‎android/app/src/main/kotlin/app/nectarlink/android/elevated/Elevated.kt‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,12 @@ object Elevated {
116116
/** Where the helper listens, and the token it wants, while it runs. */
117117
@Volatile private var helper: Pair<Int, String>? = null
118118
@Volatile private var screenOffRequested = false
119+
120+
/**
121+
* Whether the helper watches the clipboard: only while some device takes
122+
* this phone's copies by itself (#655). It's on until the app says.
123+
*/
124+
@Volatile private var clipboardWatchWanted = true
119125
@Volatile private var serviceClient: NectarlinkServiceClient? = null
120126
@Volatile private var serviceStatus: ServiceStatus? = null
121127

@@ -878,6 +884,7 @@ object Elevated {
878884
input = writer
879885
_state.value = State.Running
880886
if (screenOffRequested) send(writer, "P 0")
887+
send(writer, "W ${if (clipboardWatchWanted) 1 else 0}")
881888
compileIfUpdated()
882889
checkCallAudio()
883890
Thread {
@@ -1460,6 +1467,18 @@ object Elevated {
14601467
return send(writer, "K ${KeyEvent.KEYCODE_WAKEUP}")
14611468
}
14621469

1470+
/**
1471+
* Has the helper watch the clipboard, or stop: it only needs to while some
1472+
* device takes this phone's copies by itself. On some phones (a Xiaomi
1473+
* with HyperOS) its watching left paste empty in every app (#655).
1474+
*/
1475+
fun setClipboardWatch(on: Boolean) {
1476+
if (clipboardWatchWanted == on) return
1477+
clipboardWatchWanted = on
1478+
val writer = input ?: return
1479+
send(writer, "W ${if (on) 1 else 0}")
1480+
}
1481+
14631482
/** Turns the physical screen panel off or back on while mirroring. */
14641483
fun setScreenOff(off: Boolean): Boolean {
14651484
screenOffRequested = off

‎android/app/src/main/kotlin/app/nectarlink/android/elevated/InputServer.kt‎

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,26 @@ object InputServer {
233233

234234
@Volatile private var clipListenerProxy: Any? = null
235235

236+
/** The clipboard service and its listener calls, once the watcher found them. */
237+
@Volatile private var clipWatch: Triple<Any, Method, Method?>? = null
238+
@Volatile private var clipWatching = false
239+
private val clipWatchLock = Any()
240+
241+
/**
242+
* Starts or stops passing the phone's copies to the app (#655): only
243+
* while some device takes them by itself. Stopping unregisters the
244+
* listener, so the helper reads nothing from the clipboard.
245+
*/
246+
private fun watchClipboard(on: Boolean) = synchronized(clipWatchLock) {
247+
if (on == clipWatching) return@synchronized
248+
val (clipboard, add, remove) = clipWatch ?: return@synchronized
249+
val proxy = clipListenerProxy ?: return@synchronized
250+
val method = if (on) add else remove ?: return@synchronized
251+
runCatching { method.invoke(clipboard, *buildClipboardArgs(method.parameterTypes, proxy, null)) }
252+
.onSuccess { clipWatching = on }
253+
.onFailure { System.err.println("can't ${if (on) "start" else "stop"} watching the clipboard: $it") }
254+
}
255+
236256
/** The clipboard service and its setPrimaryClip, once the watcher found them. */
237257
@Volatile private var clipService: Pair<Any, Method>? = null
238258
@Volatile private var clipGet: Method? = null
@@ -318,7 +338,12 @@ object InputServer {
318338
}
319339
proxyRef = proxy
320340
clipListenerProxy = proxy
321-
addMethod.invoke(clipboard, *buildClipboardArgs(addMethod.parameterTypes, proxy, null))
341+
val removeMethod = clipboard.javaClass.methods.firstOrNull { it.name == "removePrimaryClipChangedListener" }
342+
synchronized(clipWatchLock) {
343+
clipWatch = Triple(clipboard, addMethod, removeMethod)
344+
addMethod.invoke(clipboard, *buildClipboardArgs(addMethod.parameterTypes, proxy, null))
345+
clipWatching = true
346+
}
322347
}.onFailure {
323348
System.err.println("clipboard watcher unavailable: $it")
324349
}
@@ -929,6 +954,7 @@ object InputServer {
929954
"P" -> if (display < 0) setDisplayPower(rest.trim() != "0")
930955
"B" -> if (display < 0) setClipboard(rest)
931956
"X" -> if (display < 0) clearClipboard(rest)
957+
"W" -> if (display < 0) watchClipboard(rest.trim() != "0")
932958
}
933959
}
934960

0 commit comments

Comments
 (0)