My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package com.swaroop.projects;

import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.ServerSocket;
import java.net.Socket;


public class JavaDesktopClipboardAccessServer {

static ServerSocket serverSocket;

static String cachedDesktopClipboard = "";
static String cachedMobileClipboard = "";

/**
* @param args
* @throws IOException
* @throws UnsupportedFlavorException
*/
public static void main(String[] args) throws UnsupportedFlavorException, IOException {

Thread serverThread = new Thread(new ServerThread());
serverThread.start();
}

/**
* Sets the clipboard to the server cache
* @param toUpdateDesktopClipboard
*/
private static void setDesktopClipBoard(String toUpdateDesktopClipboard) {
// Set's a text into the clipboard.
StringSelection stringSelection = new StringSelection(toUpdateDesktopClipboard);
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents( stringSelection, null);
cachedDesktopClipboard = toUpdateDesktopClipboard;
}

/**
* Reads the clipboard and sets it to the cache if changed.
* @return - the clipboard information that's read.
* @throws UnsupportedFlavorException
* @throws IOException
*/
private static String readClipBoard() throws UnsupportedFlavorException, IOException {

// Reads what's in the clipboard.
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
Transferable contents = clipboard.getContents(null);
String clipBoardContent = ((String) contents.getTransferData(DataFlavor.stringFlavor));

System.out.println("Text in the clipboard is " + clipBoardContent);
return clipBoardContent;
}

public static boolean isDesktopClipboardLatest() {
try {
String latestDesktopClipboard = readClipBoard();
boolean isDesktopClipboardAlreadyInSync = cachedDesktopClipboard.equals(latestDesktopClipboard);
System.out.println("Server clipboard is " + (isDesktopClipboardAlreadyInSync ? "not" : "") + " modified" );
return isDesktopClipboardAlreadyInSync;
} catch (UnsupportedFlavorException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return false;
}

public static boolean isMobileClipboardLatest(String mobileClipboardLatest) {
boolean isMobileClipboardAlreadyInSync = cachedMobileClipboard.equals(mobileClipboardLatest);
System.out.println("Mobile clipboard is " + (isMobileClipboardAlreadyInSync ? "not" : "") + " modified" );
return isMobileClipboardAlreadyInSync;
}

private static class ServerThread implements Runnable {

@Override
public void run() {
try {
serverSocket = new ServerSocket(8686);
System.out.println("Listening on port " + serverSocket.getLocalPort());
while(true) {
Socket client = serverSocket.accept();

// Check if data has been sent by the device and receive it
ObjectInputStream ois = readDataFromDevice(client);

// Check if the data has to be sent to the device.
ObjectOutputStream oos = sendDataToDevice(client);

ois.close();
oos.close();
}

} catch (IOException e) {
e.printStackTrace();
} catch (UnsupportedFlavorException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
try {
serverSocket.close();
} catch (IOException e) {
e.printStackTrace();
}
}

}

/**
* Sends the server clipboard if changed
* @param client
* @throws IOException
* @throws UnsupportedFlavorException
*/
private ObjectOutputStream sendDataToDevice(Socket socket)
throws IOException, UnsupportedFlavorException {

// Check if server clipboard has been updated, then send it back to device
ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());

if(!isDesktopClipboardLatest()) {
System.out.println("Appears desktop clipboard has been updated, sending it to device");
String latestDesktopClipboard = readClipBoard();
oos.writeObject(latestDesktopClipboard);
} else {
oos.writeObject("");
}

return oos;
}

/**
* Reads the data that was sent by the Android device
* @param socket
* @throws IOException
* @throws ClassNotFoundException
*/
public static ObjectInputStream readDataFromDevice(Socket socket) throws IOException, ClassNotFoundException {

System.out.println("Synching information");
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());

String message = (String) ois.readObject();

// Mobile clipboard has been sent for updation.
if(!"".equals(message)) {
System.out.println("Mobile sent its clipboard " + message);
setDesktopClipBoard(message);
} else {
System.out.println("Mobile clipboard didn't change, received dummy request");
}

return ois;

}
}
}


Change log

r3 by gnanaswaroop on Jan 9, 2011   Diff
Initializing the cached variables now
causing NLP
Go to: 
Project members, sign in to write a code review

Older revisions

r2 by gnanaswaroop on Jan 9, 2011   Diff
Modified the code to do bi-directional
sync.
TODO
1) Create an icon for the application
2) Remove logging to improve security
...
r1 by gnanaswaroop on Oct 18, 2010   Diff
[No log message]
All revisions of this file

File info

Size: 5323 bytes, 169 lines
Powered by Google Project Hosting