From ffc57512b3241d28424cd883985d66ac92698cf3 Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Mon, 25 May 2020 02:58:06 +0200 Subject: [PATCH] Avoid clipboard synchronization loop The Android device listens for clipboard changes to synchronize with the computer clipboard. However, if the change comes from scrcpy (for example via Ctrl+Shift+v), do not notify the change. --- .../src/main/java/com/genymobile/scrcpy/Device.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/server/src/main/java/com/genymobile/scrcpy/Device.java b/server/src/main/java/com/genymobile/scrcpy/Device.java index 1a851b15..ec28bd7c 100644 --- a/server/src/main/java/com/genymobile/scrcpy/Device.java +++ b/server/src/main/java/com/genymobile/scrcpy/Device.java @@ -13,6 +13,8 @@ import android.os.IBinder; import android.view.IRotationWatcher; import android.view.InputEvent; +import java.util.concurrent.atomic.AtomicBoolean; + public final class Device { public static final int POWER_MODE_OFF = SurfaceControl.POWER_MODE_OFF; @@ -31,6 +33,7 @@ public final class Device { private ScreenInfo screenInfo; private RotationListener rotationListener; private ClipboardListener clipboardListener; + private final AtomicBoolean isSettingClipboard = new AtomicBoolean(); /** * Logical display identifier @@ -76,6 +79,10 @@ public final class Device { serviceManager.getClipboardManager().addPrimaryClipChangedListener(new IOnPrimaryClipChangedListener.Stub() { @Override public void dispatchPrimaryClipChanged() { + if (isSettingClipboard.get()) { + // This is a notification for the change we are currently applying, ignore it + return; + } synchronized (Device.this) { if (clipboardListener != null) { String text = getClipboardText(); @@ -181,7 +188,10 @@ public final class Device { } public boolean setClipboardText(String text) { - return serviceManager.getClipboardManager().setText(text); + isSettingClipboard.set(true); + boolean ok = serviceManager.getClipboardManager().setText(text); + isSettingClipboard.set(false); + return ok; } /**