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
170
171
172
173
package com.swaroop.ClipboardSharer;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Activity;
import android.os.Bundle;
import android.text.ClipboardManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class ClipboardSharerApplicationActivity extends Activity {

static String cachedDeviceClipBoard = "";
static String cachedDesktopClipBoard = "";

static boolean clientThreadAliveToggle = true;
private static Button startSyncButton;
private static EditText editText;

static ClipboardManager clipboardManager;
static ClipboardSharerApplicationActivity currentActivity;

public static ClipboardSharerApplicationActivity getInstance() {
return currentActivity;
}

/**
* Starts the sync process after the user presses the 'Sync button'
*/
private final Button.OnClickListener startSyncButtonOnClick = new Button.OnClickListener() {
public void onClick(View v) {
Thread clientThread = new Thread(new ClientThread());
clientThread.start();
}
};

/**
* Is triggered when the Exit button is clicked. This finishes the activity
*/
private final Button.OnClickListener exitButtonOnClick = new Button.OnClickListener() {
public void onClick(View v) {
clientThreadAliveToggle = false;
currentActivity.finish();
}
};


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

currentActivity = this;
clipboardManager = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);

editText = (EditText) findViewById(R.id.IPAddressInputField);
startSyncButton = (Button) findViewById(R.id.StartSyncButton);

startSyncButton.setOnClickListener(startSyncButtonOnClick);
Button exitAppButton = (Button) findViewById(R.id.ExitSyncClientButton);
exitAppButton.setOnClickListener(exitButtonOnClick);
}

private static class ClientThread implements Runnable {

private static final String CLIPBOARD_SHARER_LOG_TAG = "ClipboardSharer";

public void run() {
String inputIP = editText.getText().toString();
Log.d(CLIPBOARD_SHARER_LOG_TAG, "User entered IP Address = " + inputIP);
InetAddress serverAddress;
try {
while(clientThreadAliveToggle) {
serverAddress = InetAddress.getByName(inputIP);
Socket socket = new Socket(serverAddress, 8686);

// Check if the device has to send any data to server
ObjectOutputStream oos = sendDataToServer(socket);

// Check if the server has sent some data.
ObjectInputStream ois = receiveDataFromServer(socket);

oos.close();
ois.close();
socket.close();

try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}

/**
* Checks if any data has to be sent to the server
* @param socket
* @return
* @throws IOException
*/
private ObjectOutputStream sendDataToServer(Socket socket) throws IOException {

ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());

if(isDeviceClipBoardChanged()) {
Log.d(CLIPBOARD_SHARER_LOG_TAG, "Device clipboard changed, sending update to server " + cachedDeviceClipBoard);
oos.writeObject(cachedDeviceClipBoard);
} else {
Log.d(CLIPBOARD_SHARER_LOG_TAG, "Device clipboard not-changed, sending dummy request");
oos.writeObject("");
}

return oos;
}

/**
* Checks if the server has some data
* @param socket
* @return
* @throws IOException
* @throws ClassNotFoundException
*/
public ObjectInputStream receiveDataFromServer(Socket socket) throws IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());

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

if(!"".equals(receivedStringFromServer)) {
Log.d(CLIPBOARD_SHARER_LOG_TAG, "Received a string from server, Desktop clipboard updated " + receivedStringFromServer);
setDeviceClipBoard(receivedStringFromServer.toString());
cachedDesktopClipBoard = receivedStringFromServer.toString();
}
return ois;
}

public static String getDeviceClipBoard() {
return clipboardManager.getText().toString();
}

public static void setDeviceClipBoard(String updatedClipboard) {
cachedDeviceClipBoard = updatedClipboard;
clipboardManager.setText(updatedClipboard);
}

public static boolean isDeviceClipBoardChanged() {
String currentDeviceClipBoard = getDeviceClipBoard();
if(currentDeviceClipBoard.equals(cachedDeviceClipBoard)) {
return false;
}
cachedDeviceClipBoard = currentDeviceClipBoard;

return true;
}

}


}

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: 5086 bytes, 173 lines
Powered by Google Project Hosting