Mercurial > 510ConnectbotMonitor
annotate src/com/five_ten_sg/connectbot/monitor/MonitorService.java @ 13:5bf6d84cc5b8 stable-1.0.1
add showurl command
author | Carl Byington <carl@five-ten-sg.com> |
---|---|
date | Tue, 24 Jun 2014 07:58:13 -0700 |
parents | 992dca085fa2 |
children | a481d8fb5571 |
rev | line source |
---|---|
0 | 1 package com.five_ten_sg.connectbot.monitor; |
2 | |
3 import java.io.IOException; | |
4 import java.io.InputStream; | |
5 import java.io.OutputStream; | |
6 import java.net.ServerSocket; | |
7 import java.net.Socket; | |
5 | 8 import java.util.HashMap; |
0 | 9 import java.util.concurrent.ArrayBlockingQueue; |
10 import java.util.concurrent.BlockingQueue; | |
11 import java.util.concurrent.ConcurrentHashMap; | |
12 import java.util.Locale; | |
13 | |
14 import android.app.Activity; | |
15 import android.app.Service; | |
16 import android.content.Context; | |
17 import android.content.Intent; | |
18 import android.content.ServiceConnection; | |
19 import android.net.wifi.WifiManager.WifiLock; | |
20 import android.net.wifi.WifiManager; | |
21 import android.os.Binder; | |
22 import android.os.Bundle; | |
23 import android.os.Handler; | |
24 import android.os.IBinder; | |
25 import android.os.Message; | |
26 import android.os.PowerManager; | |
27 import android.speech.tts.TextToSpeech; | |
28 import android.speech.tts.TextToSpeech.OnInitListener; | |
29 import android.util.Log; | |
30 import android.widget.TextView; | |
31 | |
32 public class MonitorService extends Service implements OnInitListener { | |
33 public final static String TAG = "ConnectBot.MonitorService"; | |
34 | |
35 public static final char MONITOR_CMD_INIT = 0; | |
36 public static final char MONITOR_CMD_ACTIVATE = 1; | |
2 | 37 public static final char MONITOR_CMD_KEYSTATE = 2; |
0 | 38 public static final char MONITOR_CMD_CURSORMOVE = 3; |
39 public static final char MONITOR_CMD_SCREENCHANGE = 4; | |
40 public static final char MONITOR_CMD_FIELDVALUE = 5; | |
41 public static final char MONITOR_CMD_SETFIELD = 5; | |
42 public static final char MONITOR_CMD_GETFIELD = 6; | |
43 public static final char MONITOR_CMD_SCREENWATCH = 7; | |
2 | 44 public static final char MONITOR_CMD_DEPRESS = 8; |
13 | 45 public static final char MONITOR_CMD_SHOWURL = 9; |
0 | 46 |
47 public static final int MONITORPORT = 6000; | |
3
2be5bca648ab
switch to static functions
Carl Byington <carl@five-ten-sg.com>
parents:
2
diff
changeset
|
48 public static ConcurrentHashMap<Integer,CommunicationThread> clients = new ConcurrentHashMap<Integer,CommunicationThread>(); |
2be5bca648ab
switch to static functions
Carl Byington <carl@five-ten-sg.com>
parents:
2
diff
changeset
|
49 public static int currentConnection = -1; |
0 | 50 |
51 private boolean speech = false; | |
52 private TextToSpeech talker = null; | |
5 | 53 private BlockingQueue<String> talkerQueue = null; |
0 | 54 public Handler handler = null; |
55 private ServerSocket serverSocket; | |
56 private Thread serverThread = null; | |
57 private WifiManager.WifiLock wifiLock; | |
58 private PowerManager.WakeLock wakeLock; | |
59 final private IBinder binder = new MonitorBinder(); | |
60 | |
61 | |
62 public class MonitorBinder extends Binder { | |
63 public MonitorService getService() { | |
64 return MonitorService.this; | |
65 } | |
66 } | |
67 | |
68 @Override | |
69 public void onInit(int status) { | |
70 if (status == TextToSpeech.SUCCESS) { | |
71 talker.setLanguage(Locale.US); | |
72 speech = true; | |
73 } | |
74 } | |
75 | |
76 @Override | |
77 public void onCreate() { | |
78 WifiManager wMgr = (WifiManager) getSystemService(Context.WIFI_SERVICE); | |
79 wifiLock = wMgr.createWifiLock(WifiManager.WIFI_MODE_FULL, "MyWifiLock"); | |
80 wifiLock.acquire(); | |
81 | |
82 PowerManager pMgr = (PowerManager) getSystemService(Context.POWER_SERVICE); | |
83 wakeLock = pMgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakeLock"); | |
84 wakeLock.acquire(); | |
85 | |
86 talker = new TextToSpeech(this, this); | |
87 this.serverThread = new Thread(new ServerThread()); | |
88 this.serverThread.start(); | |
89 } | |
90 | |
91 @Override | |
92 public IBinder onBind(Intent intent) { | |
93 startService(new Intent(this, MonitorService.class)); | |
94 return binder; | |
95 } | |
96 | |
97 public void printer(String msg) { | |
98 if (handler != null) handler.sendMessage(handler.obtainMessage(MonitorActivity.MESSAGE_CODE_PRINT, msg)); | |
99 } | |
100 | |
101 @Override | |
102 public int onStartCommand(Intent intent, int flags, int startId) { | |
103 Log.i(TAG, "service onStartCommand()"); | |
104 return START_STICKY; | |
105 } | |
106 | |
107 @Override | |
108 public void onDestroy() { | |
109 try { | |
110 Log.i(TAG, "service onDestroy()"); | |
111 talker.stop(); | |
112 talker.shutdown(); | |
113 wifiLock.release(); | |
114 wakeLock.release(); | |
115 serverSocket.close(); | |
116 } catch (IOException e) { | |
117 Log.e(TAG, "exception in onDestroy()", e); | |
118 } | |
119 super.onDestroy(); | |
120 } | |
121 | |
122 class ServerThread extends Thread { | |
123 public void run() { | |
124 Socket socket = null; | |
125 int connection = 0; | |
126 try { | |
127 serverSocket = new ServerSocket(MONITORPORT); | |
128 } catch (IOException e) { | |
129 Log.e(TAG, "exception in ServerThread.run(), cannot create listening socket", e); | |
130 return; | |
131 } | |
132 while (true) { | |
133 try{ | |
134 socket = serverSocket.accept(); | |
135 connection = connection + 1; | |
136 CommunicationThread commThread = new CommunicationThread(connection, socket); | |
137 clients.put(connection, commThread); | |
138 commThread.start(); | |
139 } catch (IOException e) { | |
140 Log.e(TAG, "exception in ServerThread.run(), listening socket closed", e); | |
141 break; | |
142 } | |
143 } | |
144 } | |
145 } | |
146 | |
147 class triple { | |
148 private int l, c; | |
149 private char[] b; | |
150 public triple(int l, int c, char[] b) { | |
151 this.l = l; | |
152 this.c = c; | |
153 this.b = b; | |
154 } | |
155 } | |
156 | |
157 class CommunicationThread extends Thread { | |
158 public int connection; | |
159 private Socket client_socket; | |
160 private InputStream client_in; | |
161 private OutputStream client_out; | |
162 private boolean is_closing = false; | |
163 private BlockingQueue<triple> queue = new ArrayBlockingQueue<triple>(1); | |
164 | |
165 public CommunicationThread(int handle, Socket socket) { | |
166 connection = handle; | |
167 client_socket = socket; | |
168 try { | |
169 client_in = client_socket.getInputStream(); | |
170 client_out = client_socket.getOutputStream(); | |
171 } catch (IOException e) { | |
172 Log.e(TAG, "exception in CommunicationThread() constructor, cannot get socket streams", e); | |
173 } | |
174 } | |
175 | |
4 | 176 public void speak(byte [] msg, boolean flush, boolean synchronous) { |
177 if (speech) { | |
178 String smsg = bytesToString(msg); | |
179 if (synchronous) { | |
180 HashMap<String, String> myHashParms = new HashMap(); | |
181 myHashParms.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID, String.format("connection %d", connection)); | |
182 talker.speak(smsg, (flush) ? TextToSpeech.QUEUE_FLUSH : TextToSpeech.QUEUE_ADD, myHashParms); | |
183 try { | |
184 String x = talkerQueue.take(); // wait for completion | |
185 } catch (InterruptedException e) { | |
186 Log.e(TAG, "exception in cm.speak()", e); | |
187 } | |
188 } | |
189 else { | |
190 talker.speak(smsg, (flush) ? TextToSpeech.QUEUE_FLUSH : TextToSpeech.QUEUE_ADD, null); | |
191 } | |
192 } | |
193 } | |
194 | |
195 public String bytesToString(byte[] b) { | |
196 char[] c = new char[b.length]; | |
197 int bp = 0; | |
198 for(int i = 0; i < c.length; i++) { | |
199 byte b1 = 0; | |
200 byte b2 = b[bp++]; | |
201 c[i] = (char) (((b1 & 0x00FF) << 8) + (b2 & 0x00FF)); | |
202 } | |
203 return new String(c); | |
204 } | |
205 | |
0 | 206 public char[] bytesToChars(byte[] b, int len) { |
207 char[] c = new char[len >> 1]; | |
208 int bp = 0; | |
209 for(int i = 0; i < c.length; i++) { | |
210 byte b1 = b[bp++]; | |
211 byte b2 = b[bp++]; | |
212 c[i] = (char) (((b1 & 0x00FF) << 8) + (b2 & 0x00FF)); | |
213 } | |
214 return c; | |
215 } | |
216 | |
217 public byte[] charsToBytes(char[] c) { | |
218 byte[] b = new byte[c.length << 1]; | |
219 int bp = 0; | |
220 for (int i=0; i<c.length; i++) { | |
221 b[bp++] = (byte) ((c[i] & 0xff00) >> 8); | |
222 b[bp++] = (byte) (c[i] & 0x00ff); | |
223 } | |
224 return b; | |
225 } | |
226 | |
227 public synchronized void clientWrite(char cmd, char[] c) { | |
228 try { | |
229 if (client_out != null) { | |
230 c[0] = (char)(c.length - 1); // number of chars following | |
231 c[1] = cmd; | |
232 Log.i(TAG, String.format("sending %d command", (int)cmd)); | |
233 client_out.write(charsToBytes(c)); | |
234 } | |
235 } | |
236 catch (IOException e) { | |
237 Log.e(TAG, "exception in monitorWrite()", e); | |
238 try { | |
239 client_out.close(); | |
240 } | |
241 catch (IOException ee) { | |
242 Log.e(TAG, "exception in monitorWrite() closing socket", ee); | |
243 } | |
244 client_out = null; | |
245 } | |
246 }; | |
247 | |
248 private char[] forceRead(int len) throws IOException { | |
249 int len2 = len*2; | |
250 int off = 0; | |
251 byte[] b = new byte[len2]; | |
252 while (off < len2) { | |
253 int l = client_in.read(b, off, len2-off); | |
254 if (l < 0) { | |
255 is_closing = true; | |
256 throw new IOException("eof"); | |
257 } | |
258 off += l; | |
259 } | |
260 return bytesToChars(b, len2); | |
261 } | |
262 | |
263 public void run() { | |
264 Log.i(TAG, String.format("CommunicationThread.run() client %d connected", connection)); | |
265 while (true) { | |
266 try { | |
267 char[] len = forceRead(1); | |
268 char[] packet = forceRead(len[0]); | |
269 char[] buf; | |
270 char cmd = packet[0]; | |
271 int plen = packet.length; | |
272 //Log.i(TAG, String.format("received %d command length %d", (int)cmd, plen)); | |
273 switch (cmd) { | |
274 case MONITOR_CMD_INIT: | |
275 buf = new char[plen-1]; | |
276 System.arraycopy(packet, 1, buf, 0, plen-1); | |
277 abandonGetField(connection); | |
278 teInit(connection, buf); | |
279 break; | |
280 case MONITOR_CMD_ACTIVATE: | |
281 abandonGetField(connection); | |
282 buf = new char[plen-3]; | |
283 System.arraycopy(packet, 3, buf, 0, plen-3); | |
284 teActivate(connection, packet[1], packet[2], buf); | |
285 break; | |
2 | 286 case MONITOR_CMD_KEYSTATE: |
287 teKeyState(connection, (packet[1] == 1)); | |
0 | 288 break; |
289 case MONITOR_CMD_CURSORMOVE: | |
290 teCursorMove(connection, packet[1], packet[2]); | |
291 break; | |
292 case MONITOR_CMD_SCREENCHANGE: | |
293 buf = new char[plen-3]; | |
294 System.arraycopy(packet, 3, buf, 0, plen-3); | |
295 teScreenChange(connection, packet[1], packet[2], buf); | |
296 break; | |
297 case MONITOR_CMD_FIELDVALUE: | |
298 buf = new char[plen-3]; | |
299 System.arraycopy(packet, 3, buf, 0, plen-3); | |
300 Log.i(TAG, String.format("teFieldValue %d line %d column %d b.len %d", connection, packet[1], packet[2], buf.length)); | |
301 queue.put(new triple(packet[1], packet[2], buf)); | |
302 break; | |
303 default: | |
304 break; | |
305 } | |
306 } catch (IOException e) { | |
307 if (!is_closing) Log.e(TAG, "exception in CommunicationThread.run()", e); | |
308 break; | |
309 } catch (InterruptedException e) { | |
310 Log.e(TAG, "exception in CommunicationThread.run()", e); | |
311 break; | |
312 } | |
313 } | |
314 Log.i(TAG, String.format("shutting down connection %d", connection)); | |
315 try { | |
316 if (client_in != null) client_in.close(); | |
317 if (client_out != null) client_out.close(); | |
318 if (client_socket != null) client_socket.close(); | |
319 } catch (IOException e) { | |
320 Log.e(TAG, "exception in CommunicationThread.run() closing sockets", e); | |
321 } | |
322 client_in = null; | |
323 client_out = null; | |
324 client_socket = null; | |
325 } | |
326 } | |
327 | |
328 private void abandonGetField(int except) { | |
329 for (CommunicationThread cm : clients.values()) { | |
330 if (cm.connection != except) { | |
331 cm.queue.offer(new triple(0, 0, new char[0])); | |
332 } | |
333 } | |
334 } | |
335 | |
336 | |
337 //////////////////////////////////////// | |
338 //// these functions run on the reader thread here and call your monitoring code | |
339 | |
340 public void teInit(int connection, char[] buf) { | |
341 String fn = new String(buf); | |
342 Log.i(TAG, String.format("teInit %d file %s", connection, fn)); | |
2 | 343 //printer(String.format("init %d %s", connection, fn)); |
0 | 344 } |
345 | |
346 public void teActivate(int connection, int lines, int columns, char[] buf) { | |
347 Log.i(TAG, String.format("teActivate %d", connection)); | |
2 | 348 //printer(String.format("activate %d lines %d columns %d b.len %d", connection, lines, columns, buf.length)); |
0 | 349 } |
350 | |
2 | 351 public void teKeyState(int connection, boolean down) { |
352 String d = (down) ? "yes" : "no"; | |
353 Log.i(TAG, String.format("teKeyState %d isdown %s", connection, d)); | |
354 //printer(String.format("keystate %d isdown %s", connection, d)); | |
0 | 355 } |
356 | |
357 public void teCursorMove(int connection, int l, int c) { | |
358 //Log.i(TAG, String.format("teCursorMove %d line %d column %d", connection, l, c)); | |
359 } | |
360 | |
361 public void teScreenChange(int connection, int lines, int columns, char[] buf) { | |
362 Log.i(TAG, String.format("teScreenChange %d lines %d columns %d b.len %d", connection, lines, columns, buf.length)); | |
363 } | |
364 | |
365 | |
366 //////////////////////////////////////// | |
367 //// these functions are called from your monitoring code thread | |
368 | |
3
2be5bca648ab
switch to static functions
Carl Byington <carl@five-ten-sg.com>
parents:
2
diff
changeset
|
369 public static void teSetField(int connection, int l, int c, char[] buf) { |
0 | 370 int len = buf.length; |
371 Log.i(TAG, String.format("teSetField %d request line %d column %d len %d", connection, l, c, len)); | |
372 CommunicationThread cm = clients.get(connection); | |
373 if (cm != null) { | |
374 char[] arg2 = new char[4 + len]; | |
375 arg2[2] = (char) (l & 0x0000ffff); | |
376 arg2[3] = (char) (c & 0x0000ffff); | |
377 int base = 4; | |
378 System.arraycopy(buf, 0, arg2, base, len); | |
379 cm.clientWrite(MONITOR_CMD_SETFIELD, arg2); | |
380 } | |
381 } | |
382 | |
3
2be5bca648ab
switch to static functions
Carl Byington <carl@five-ten-sg.com>
parents:
2
diff
changeset
|
383 public static char[] teGetField(int connection, int l, int c, int len) { |
0 | 384 Log.i(TAG, String.format("teGetField %d request line %d column %d len %d", connection, l, c, len)); |
385 CommunicationThread cm = clients.get(connection); | |
386 if (cm != null) { | |
387 char[] arg = new char[5]; | |
388 arg[2] = (char) (l & 0x0000ffff); | |
389 arg[3] = (char) (c & 0x0000ffff); | |
390 arg[4] = (char) (len & 0x0000ffff); | |
391 cm.queue.clear(); // we never have more than one outstanding getfield request on the connection | |
392 cm.clientWrite(MONITOR_CMD_GETFIELD, arg); | |
393 try { | |
394 triple t = cm.queue.take(); // wait for response | |
395 Log.i(TAG, String.format("teGetField %d response line %d column %d len %d", connection, t.l, t.c, t.b.length)); | |
396 return t.b; | |
397 } catch (InterruptedException e) { | |
398 Log.e(TAG, "exception in teGetField(), return empty string", e); | |
399 } | |
400 } | |
401 return new char[0]; | |
402 } | |
403 | |
3
2be5bca648ab
switch to static functions
Carl Byington <carl@five-ten-sg.com>
parents:
2
diff
changeset
|
404 public static void teScreenWatch(int connection, int l, int c, int len) { |
0 | 405 Log.i(TAG, String.format("teScreenWatch %d request line %d column %d len %d", connection, l, c, len)); |
406 CommunicationThread cm = clients.get(connection); | |
407 if (cm != null) { | |
408 char[] arg = new char[5]; | |
409 arg[2] = (char) (l & 0x0000ffff); | |
410 arg[3] = (char) (c & 0x0000ffff); | |
411 arg[4] = (char) (len & 0x0000ffff); | |
412 cm.clientWrite(MONITOR_CMD_GETFIELD, arg); | |
413 } | |
414 } | |
415 | |
7 | 416 public static void teSpeak(int connection, byte [] msg, boolean flush, boolean synchronous) { |
4 | 417 CommunicationThread cm = clients.get(connection); |
418 if (cm != null) cm.speak(msg, flush, synchronous); | |
0 | 419 } |
2 | 420 |
421 public static void teDepress(int connection, int vk_key) { | |
422 // http://msdn.microsoft.com/en-us/library/windows/desktop/dd375731 | |
423 Log.i(TAG, String.format("teDepress %d, %d", connection, vk_key)); | |
424 CommunicationThread cm = clients.get(connection); | |
425 if (cm != null) { | |
426 char[] arg = new char[3]; | |
427 arg[2] = (char) (vk_key & 0x0000ffff); | |
428 cm.clientWrite(MONITOR_CMD_DEPRESS, arg); | |
429 } | |
430 } | |
13 | 431 |
432 public static void teShowUrl(int connection, char [] url) { | |
433 int len = url.length; | |
434 CommunicationThread cm = clients.get(connection); | |
435 if (cm != null) { | |
436 char[] arg2 = new char[2 + len]; | |
437 int base = 2; | |
438 System.arraycopy(url, 0, arg2, base, len); | |
439 cm.clientWrite(MONITOR_CMD_SHOWURL, arg2); | |
440 } | |
441 } | |
442 | |
0 | 443 } |