diff --git a/src/org/kde/kdeconnect/Plugins/ClibpoardPlugin/ClipboardListener.java b/src/org/kde/kdeconnect/Plugins/ClibpoardPlugin/ClipboardListener.java --- a/src/org/kde/kdeconnect/Plugins/ClibpoardPlugin/ClipboardListener.java +++ b/src/org/kde/kdeconnect/Plugins/ClibpoardPlugin/ClipboardListener.java @@ -41,6 +41,7 @@ private final Context context; private String currentContent; + private long updateTimestamp; private ClipboardManager cm = null; private ClipboardManager.OnPrimaryClipChangedListener listener; @@ -91,8 +92,17 @@ }); } + public String getCurrentContent() { + return currentContent; + } + + public long getUpdateTimestamp(){ + return updateTimestamp; + } + @SuppressWarnings("deprecation") public void setText(String text) { + updateTimestamp = System.currentTimeMillis(); currentContent = text; cm.setText(text); } diff --git a/src/org/kde/kdeconnect/Plugins/ClibpoardPlugin/ClipboardPlugin.java b/src/org/kde/kdeconnect/Plugins/ClibpoardPlugin/ClipboardPlugin.java --- a/src/org/kde/kdeconnect/Plugins/ClibpoardPlugin/ClipboardPlugin.java +++ b/src/org/kde/kdeconnect/Plugins/ClibpoardPlugin/ClipboardPlugin.java @@ -28,6 +28,17 @@ @PluginFactory.LoadablePlugin public class ClipboardPlugin extends Plugin { + /** + * Packet containing clipboard contents and a timestamp that the contents were last updated. + *
+ * The timestamp is milliseconds since epoch. It can be 0, which indicates that the clipboard + * update time is currently unknown. + *
+ * The body should look like so: + * { "timestamp": 542904563213, + * "content": "password" + * } + */ private final static String PACKET_TYPE_CLIPBOARD = "kdeconnect.clipboard"; @Override @@ -42,16 +53,41 @@ @Override public boolean onPacketReceived(NetworkPacket np) { + if (np.has("timestamp")) { + long packetTime = np.getLong("timestamp"); + + // If the packetTime is 0, it means the timestamp is unknown (so do nothing). + if (packetTime == 0) { + return false; + } else if (packetTime < ClipboardListener.instance(context).getUpdateTimestamp()) { + propagateClipboard(ClipboardListener.instance(context).getCurrentContent()); + return true; + } + } + String content = np.getString("content"); - ClipboardListener.instance(context).setText(content); - return true; + + if (!content.equals(ClipboardListener.instance(context).getCurrentContent())) { + + ClipboardListener.instance(context).setText(content); + return true; + } else { + return false; + } } - private final ClipboardListener.ClipboardObserver observer = content -> { + private final ClipboardListener.ClipboardObserver observer = this::updateClipboard; + + private void updateClipboard(String content){ + propagateClipboard(content); + } + + private void propagateClipboard(String content){ NetworkPacket np = new NetworkPacket(ClipboardPlugin.PACKET_TYPE_CLIPBOARD); np.set("content", content); + np.set("timestamp", ClipboardListener.instance(context).getUpdateTimestamp()); device.sendPacket(np); - }; + } @Override public boolean onCreate() {