Changeset View
Changeset View
Standalone View
Standalone View
src/org/kde/kdeconnect/Plugins/ClibpoardPlugin/ClipboardPlugin.java
Show All 22 Lines | |||||
23 | import org.kde.kdeconnect.NetworkPacket; | 23 | import org.kde.kdeconnect.NetworkPacket; | ||
24 | import org.kde.kdeconnect.Plugins.Plugin; | 24 | import org.kde.kdeconnect.Plugins.Plugin; | ||
25 | import org.kde.kdeconnect.Plugins.PluginFactory; | 25 | import org.kde.kdeconnect.Plugins.PluginFactory; | ||
26 | import org.kde.kdeconnect_tp.R; | 26 | import org.kde.kdeconnect_tp.R; | ||
27 | 27 | | |||
28 | @PluginFactory.LoadablePlugin | 28 | @PluginFactory.LoadablePlugin | ||
29 | public class ClipboardPlugin extends Plugin { | 29 | public class ClipboardPlugin extends Plugin { | ||
30 | 30 | | |||
31 | /** | ||||
32 | * Packet containing just clipboard contents, sent when a device updates its clipboard. | ||||
33 | * <p> | ||||
34 | * The body should look like so: | ||||
35 | * { | ||||
36 | * "content": "password" | ||||
37 | * } | ||||
38 | */ | ||||
31 | private final static String PACKET_TYPE_CLIPBOARD = "kdeconnect.clipboard"; | 39 | private final static String PACKET_TYPE_CLIPBOARD = "kdeconnect.clipboard"; | ||
32 | 40 | | |||
41 | /** | ||||
42 | * Packet containing clipboard contents and a timestamp that the contents were last updated, sent | ||||
43 | * on first connection | ||||
44 | * <p> | ||||
45 | * The timestamp is milliseconds since epoch. It can be 0, which indicates that the clipboard | ||||
46 | * update time is currently unknown. | ||||
47 | * <p> | ||||
48 | * The body should look like so: | ||||
49 | * { | ||||
50 | * "timestamp": 542904563213, | ||||
51 | * "content": "password" | ||||
52 | * } | ||||
53 | */ | ||||
54 | private final static String PACKET_TYPE_CLIPBOARD_CONNECT = "kdeconnect.clipboard.connect"; | ||||
55 | | ||||
33 | @Override | 56 | @Override | ||
34 | public String getDisplayName() { | 57 | public String getDisplayName() { | ||
35 | return context.getResources().getString(R.string.pref_plugin_clipboard); | 58 | return context.getResources().getString(R.string.pref_plugin_clipboard); | ||
36 | } | 59 | } | ||
37 | 60 | | |||
38 | @Override | 61 | @Override | ||
39 | public String getDescription() { | 62 | public String getDescription() { | ||
40 | return context.getResources().getString(R.string.pref_plugin_clipboard_desc); | 63 | return context.getResources().getString(R.string.pref_plugin_clipboard_desc); | ||
41 | } | 64 | } | ||
42 | 65 | | |||
43 | @Override | 66 | @Override | ||
44 | public boolean onPacketReceived(NetworkPacket np) { | 67 | public boolean onPacketReceived(NetworkPacket np) { | ||
45 | String content = np.getString("content"); | 68 | String content = np.getString("content"); | ||
69 | switch (np.getType()) { | ||||
70 | case (PACKET_TYPE_CLIPBOARD): | ||||
71 | ClipboardListener.instance(context).setText(content); | ||||
72 | return true; | ||||
73 | case(PACKET_TYPE_CLIPBOARD_CONNECT): | ||||
74 | long packetTime = np.getLong("timestamp"); | ||||
sredman: Spaces | |||||
75 | // If the packetTime is 0, it means the timestamp is unknown (so do nothing). | ||||
albertvaka: If we send the contents onCreate, we probably don't need this. | |||||
76 | if (packetTime == 0 || packetTime < ClipboardListener.instance(context).getUpdateTimestamp()) { | ||||
77 | return false; | ||||
78 | } | ||||
79 | | ||||
46 | ClipboardListener.instance(context).setText(content); | 80 | ClipboardListener.instance(context).setText(content); | ||
47 | return true; | 81 | return true; | ||
48 | } | 82 | } | ||
83 | throw new UnsupportedOperationException("Unknown packet type: " + np.getType()); | ||||
84 | } | ||||
85 | | ||||
86 | private final ClipboardListener.ClipboardObserver observer = this::propagateClipboard; | ||||
49 | 87 | | |||
50 | private final ClipboardListener.ClipboardObserver observer = content -> { | 88 | private void propagateClipboard(String content) { | ||
51 | NetworkPacket np = new NetworkPacket(ClipboardPlugin.PACKET_TYPE_CLIPBOARD); | 89 | NetworkPacket np = new NetworkPacket(ClipboardPlugin.PACKET_TYPE_CLIPBOARD); | ||
52 | np.set("content", content); | 90 | np.set("content", content); | ||
53 | device.sendPacket(np); | 91 | device.sendPacket(np); | ||
It would be nice to write down the packet format, especially since a new field has just appeared. I usually do this in a block comment on the packet type definition (PACKET_TYPE_CLIPBOARD) sredman: It would be nice to write down the packet format, especially since a new field has just… | |||||
54 | }; | 92 | } | ||
93 | | ||||
94 | private void sendConnectPacket() { | ||||
95 | String content = ClipboardListener.instance(context).getCurrentContent(); | ||||
96 | NetworkPacket np = new NetworkPacket(ClipboardPlugin.PACKET_TYPE_CLIPBOARD_CONNECT); | ||||
97 | long timestamp = ClipboardListener.instance(context).getUpdateTimestamp(); | ||||
98 | np.set("timestamp", timestamp); | ||||
99 | np.set("content", content); | ||||
100 | device.sendPacket(np); | ||||
101 | } | ||||
55 | 102 | | |||
56 | @Override | 103 | @Override | ||
57 | public boolean onCreate() { | 104 | public boolean onCreate() { | ||
58 | ClipboardListener.instance(context).registerObserver(observer); | 105 | ClipboardListener.instance(context).registerObserver(observer); | ||
106 | sendConnectPacket(); | ||||
59 | return true; | 107 | return true; | ||
60 | } | 108 | } | ||
61 | 109 | | |||
62 | @Override | 110 | @Override | ||
63 | public void onDestroy() { | 111 | public void onDestroy() { | ||
64 | ClipboardListener.instance(context).removeObserver(observer); | 112 | ClipboardListener.instance(context).removeObserver(observer); | ||
65 | } | 113 | } | ||
66 | 114 | | |||
67 | @Override | 115 | @Override | ||
68 | public String[] getSupportedPacketTypes() { | 116 | public String[] getSupportedPacketTypes() { | ||
69 | return new String[]{PACKET_TYPE_CLIPBOARD}; | 117 | return new String[]{PACKET_TYPE_CLIPBOARD, PACKET_TYPE_CLIPBOARD_CONNECT}; | ||
70 | } | 118 | } | ||
71 | 119 | | |||
72 | @Override | 120 | @Override | ||
73 | public String[] getOutgoingPacketTypes() { | 121 | public String[] getOutgoingPacketTypes() { | ||
74 | return new String[]{PACKET_TYPE_CLIPBOARD}; | 122 | return new String[]{PACKET_TYPE_CLIPBOARD, PACKET_TYPE_CLIPBOARD_CONNECT}; | ||
75 | } | 123 | } | ||
76 | 124 | | |||
77 | 125 | | |||
78 | } | 126 | } |
Spaces